feat(pricing,medusa,utils): added list + get endpoints for price lists (#6592)
what: - brings back price list GET endpoints to v2 endpoints
This commit is contained in:
@@ -31,8 +31,10 @@ export * from "./medusa-container"
|
||||
export * from "./object-from-string-path"
|
||||
export * from "./object-to-string-path"
|
||||
export * from "./optional-numeric-serializer"
|
||||
export * from "./pick-deep"
|
||||
export * from "./pick-value-from-object"
|
||||
export * from "./plurailze"
|
||||
export * from "./prefix-array-items"
|
||||
export * from "./promise-all"
|
||||
export * from "./remote-query-object-from-string"
|
||||
export * from "./remote-query-object-to-string"
|
||||
|
||||
63
packages/utils/src/common/pick-deep.ts
Normal file
63
packages/utils/src/common/pick-deep.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
export function pickDeep<T extends object = object>(
|
||||
input: object,
|
||||
fields: Array<number | string>,
|
||||
prefix: string = ""
|
||||
): T {
|
||||
if (!input) {
|
||||
return input
|
||||
}
|
||||
|
||||
return Object.entries(input).reduce((nextInput, [key, value]) => {
|
||||
const fieldKey = withPrefix(key, prefix)
|
||||
const fieldMatches = fields.includes(fieldKey)
|
||||
const partialKeyMatch =
|
||||
fields.filter((field) => field.toString().startsWith(`${fieldKey}.`))
|
||||
.length > 0
|
||||
|
||||
const valueIsObject = isObject(value)
|
||||
const valueIsArray = Array.isArray(value)
|
||||
|
||||
if (fieldMatches && (valueIsObject || valueIsArray)) {
|
||||
nextInput[key] = value
|
||||
|
||||
return nextInput
|
||||
}
|
||||
|
||||
if (!fieldMatches && !partialKeyMatch) {
|
||||
return nextInput
|
||||
}
|
||||
|
||||
if (valueIsArray) {
|
||||
nextInput[key] = value.map((arrItem) => {
|
||||
if (isObject(arrItem)) {
|
||||
return pickDeep(arrItem, fields, withPrefix(key, prefix))
|
||||
}
|
||||
return arrItem
|
||||
})
|
||||
|
||||
return nextInput
|
||||
} else if (valueIsObject) {
|
||||
if (Object.keys(value).length) {
|
||||
nextInput[key] = pickDeep(value, fields, withPrefix(key, prefix))
|
||||
}
|
||||
|
||||
return nextInput
|
||||
}
|
||||
|
||||
if (fieldMatches) {
|
||||
nextInput[key] = value
|
||||
}
|
||||
|
||||
return nextInput
|
||||
}, {} as T)
|
||||
}
|
||||
|
||||
function withPrefix(key: string, prefix: string): string {
|
||||
if (prefix.length) {
|
||||
return `${prefix}.${key}`
|
||||
} else {
|
||||
return key
|
||||
}
|
||||
}
|
||||
8
packages/utils/src/common/prefix-array-items.ts
Normal file
8
packages/utils/src/common/prefix-array-items.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Prefixes an array of strings with a specified string
|
||||
* @param array
|
||||
* @param prefix
|
||||
*/
|
||||
export function prefixArrayItems(array: string[], prefix: string): string[] {
|
||||
return array.map((arrEl) => `${prefix}${arrEl}`)
|
||||
}
|
||||
Reference in New Issue
Block a user