breaking: move shared HTTP utils to the framework (#9402)
Fixes: FRMW-2728, FRMW-2729 After this PR gets merged the following middleware will be exported from the `@medusajs/framework/http` import path. - applyParamsAsFilters - clearFiltersByKey - applyDefaultFilters - setContext - getQueryConfig - httpCompression - maybeApplyLinkFilter - refetchEntities - unlessPath - validateBody - validateQuery Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
59
packages/core/utils/src/common/__tests__/omit-deep.spec.ts
Normal file
59
packages/core/utils/src/common/__tests__/omit-deep.spec.ts
Normal file
@@ -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
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,47 @@
|
||||
import { removeUndefinedProperties } from "../remove-undefined-properties"
|
||||
|
||||
describe("removeUndefinedProperties", () => {
|
||||
it("should remove all undefined properties from an input object", () => {
|
||||
const inputObj = {
|
||||
test: undefined,
|
||||
test1: "test1",
|
||||
test2: null,
|
||||
test3: {
|
||||
test3_1: undefined,
|
||||
test3_2: "test3_2",
|
||||
test3_3: null,
|
||||
},
|
||||
test4: [
|
||||
undefined,
|
||||
null,
|
||||
"null",
|
||||
[1, 2, undefined],
|
||||
{
|
||||
test4_1: undefined,
|
||||
test4_2: "test4_2",
|
||||
test4_3: null,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const cleanObject = removeUndefinedProperties(inputObj)
|
||||
|
||||
expect(cleanObject).toEqual({
|
||||
test1: "test1",
|
||||
test2: null,
|
||||
test3: {
|
||||
test3_2: "test3_2",
|
||||
test3_3: null,
|
||||
},
|
||||
test4: [
|
||||
null,
|
||||
null,
|
||||
[1, 2],
|
||||
{
|
||||
test4_2: "test4_2",
|
||||
test4_3: null,
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
export function getNodeVersion(): number {
|
||||
const [major] = process.versions.node.split('.').map(Number)
|
||||
const [major] = process.versions.node.split(".").map(Number)
|
||||
|
||||
return major
|
||||
}
|
||||
|
||||
export const MIN_SUPPORTED_NODE_VERSION = 20
|
||||
export const MIN_SUPPORTED_NODE_VERSION = 20
|
||||
|
||||
@@ -2,6 +2,7 @@ export * from "./alter-columns-helper"
|
||||
export * from "./array-difference"
|
||||
export * from "./array-intersection"
|
||||
export * from "./build-query"
|
||||
export * from "./remove-undefined-properties"
|
||||
export * from "./build-regexp-if-valid"
|
||||
export * from "./camel-to-snake-case"
|
||||
export * from "./container"
|
||||
@@ -73,3 +74,4 @@ export * from "./validate-handle"
|
||||
export * from "./wrap-handler"
|
||||
export * from "./resolve-exports"
|
||||
export * from "./dynamic-import"
|
||||
export * from "./omit-deep"
|
||||
|
||||
34
packages/core/utils/src/common/omit-deep.ts
Normal file
34
packages/core/utils/src/common/omit-deep.ts
Normal file
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { isDefined } from "./is-defined"
|
||||
|
||||
export function removeUndefinedProperties<T extends object>(inputObj: T): T {
|
||||
const removeProperties = (obj: T) => {
|
||||
const res = {} as T
|
||||
|
||||
Object.keys(obj).reduce((acc: T, key: string) => {
|
||||
if (typeof obj[key] === "undefined") {
|
||||
return acc
|
||||
}
|
||||
acc[key] = removeUndefinedDeeply(obj[key])
|
||||
return acc
|
||||
}, res)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
return removeProperties(inputObj)
|
||||
}
|
||||
|
||||
function removeUndefinedDeeply(input: unknown): any {
|
||||
if (isDefined(input)) {
|
||||
if (input === null || input === "null") {
|
||||
return null
|
||||
} else if (Array.isArray(input)) {
|
||||
return input
|
||||
.map((item) => {
|
||||
return removeUndefinedDeeply(item)
|
||||
})
|
||||
.filter((v) => isDefined(v))
|
||||
} else if (Object.prototype.toString.call(input) === "[object Date]") {
|
||||
return input
|
||||
} else if (typeof input === "object") {
|
||||
return Object.keys(input).reduce(
|
||||
(acc: Record<string, unknown>, key: string) => {
|
||||
if (typeof input[key] === "undefined") {
|
||||
return acc
|
||||
}
|
||||
acc[key] = removeUndefinedDeeply(input[key])
|
||||
return acc
|
||||
},
|
||||
{}
|
||||
)
|
||||
} else {
|
||||
return input
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user