chore: Merge master to develop (#3653)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { omitDeep } from "../omit-deep"
|
||||
|
||||
describe("omitDeep", () => {
|
||||
it("should omit properties in a nested object", () => {
|
||||
const input = {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
address: {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
},
|
||||
variants: [
|
||||
20,
|
||||
{
|
||||
id: 22,
|
||||
title: "hello world",
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
variantOption: {
|
||||
id: 1,
|
||||
__typename: "123",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
test: null,
|
||||
__typename: "123",
|
||||
createdAt: "1020209",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const output = {
|
||||
id: 1,
|
||||
address: {
|
||||
id: 1,
|
||||
},
|
||||
variants: [
|
||||
20,
|
||||
{
|
||||
id: 22,
|
||||
title: "hello world",
|
||||
variantOption: {
|
||||
id: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
test: null,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
expect(
|
||||
omitDeep(input, ["createdAt", "updatedAt", "__typename"])
|
||||
).toEqual(output)
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,11 @@
|
||||
import { pick } from "lodash"
|
||||
import { omitDeep } from "./omit-deep"
|
||||
|
||||
// TODO: once the legacy totals decoration will be removed.
|
||||
// We will be able to only compute the totals if one of the total fields is present
|
||||
// and therefore avoid totals computation if the user don't want them to appear in the response
|
||||
// and therefore the below const will be removed
|
||||
const EXCLUDED_FIELDS = [
|
||||
const INCLUDED_FIELDS = [
|
||||
"shipping_total",
|
||||
"discount_total",
|
||||
"tax_total",
|
||||
@@ -22,6 +23,8 @@ const EXCLUDED_FIELDS = [
|
||||
"original_tax_total",
|
||||
]
|
||||
|
||||
const EXCLUDED_FIELDS = ["raw_discount_total"]
|
||||
|
||||
/**
|
||||
* Filter response data to contain props specified in the `allowedProperties`.
|
||||
* You can read more in the transformQuery middleware utility methods.
|
||||
@@ -33,17 +36,25 @@ function cleanResponseData<T extends unknown | unknown[]>(
|
||||
data: T,
|
||||
fields: string[]
|
||||
): T extends [] ? Partial<T>[] : Partial<T> {
|
||||
if (!fields.length) {
|
||||
return data as T extends [] ? Partial<T>[] : Partial<T>
|
||||
}
|
||||
fields = fields ?? []
|
||||
|
||||
const isDataArray = Array.isArray(data)
|
||||
const fieldsSet = new Set([...fields, ...EXCLUDED_FIELDS])
|
||||
let arrayData: Partial<T>[] = isDataArray ? data : [data]
|
||||
|
||||
if (!fields.length) {
|
||||
arrayData = arrayData.map((record) => omitDeep(record, EXCLUDED_FIELDS))
|
||||
return (isDataArray ? arrayData : arrayData[0]) as T extends []
|
||||
? Partial<T>[]
|
||||
: Partial<T>
|
||||
}
|
||||
|
||||
const fieldsSet = new Set([...fields, ...INCLUDED_FIELDS])
|
||||
|
||||
fields = [...fieldsSet]
|
||||
|
||||
let arrayData: Partial<T>[] = isDataArray ? data : [data]
|
||||
arrayData = arrayData.map((record) => pick(record, fields))
|
||||
arrayData = arrayData.map((record) =>
|
||||
pick(omitDeep(record, EXCLUDED_FIELDS), fields)
|
||||
)
|
||||
|
||||
return (isDataArray ? arrayData : arrayData[0]) as T extends []
|
||||
? Partial<T>[]
|
||||
|
||||
@@ -4,12 +4,12 @@ export * from "./csv-cell-content-formatter"
|
||||
export * from "./db-aware-column"
|
||||
export * from "./exception-formatter"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./has-changes"
|
||||
export * from "./is-date"
|
||||
export * from "./is-object"
|
||||
export * from "./is-string"
|
||||
export * from "./omit-deep"
|
||||
export * from "./product-category"
|
||||
export * from "./remove-undefined-properties"
|
||||
export * from "./set-metadata"
|
||||
export * from "./validate-id"
|
||||
export * from "./is-object"
|
||||
export * from "./has-changes"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export function isObject(obj: unknown): obj is object {
|
||||
return typeof obj === "object" && !!obj
|
||||
export function isObject(obj: any): obj is object {
|
||||
return obj != null && obj?.constructor?.name === "Object"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
export function omitDeep<T extends object = object>(
|
||||
input: object,
|
||||
excludes: Array<number | string>
|
||||
): T {
|
||||
if (!input) {
|
||||
return input
|
||||
}
|
||||
|
||||
return Object.entries(input).reduce((nextInput, [key, value]) => {
|
||||
const shouldExclude = excludes.includes(key)
|
||||
if (shouldExclude) {
|
||||
return nextInput
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
nextInput[key] = value.map((arrItem) => {
|
||||
if (isObject(arrItem)) {
|
||||
return omitDeep(arrItem, excludes)
|
||||
}
|
||||
return arrItem
|
||||
})
|
||||
return nextInput
|
||||
} else if (isObject(value)) {
|
||||
nextInput[key] = omitDeep(value, excludes)
|
||||
return nextInput
|
||||
}
|
||||
|
||||
nextInput[key] = value
|
||||
|
||||
return nextInput
|
||||
}, {} as T)
|
||||
}
|
||||
Reference in New Issue
Block a user