feat(medusa): Refactor undefined check into a single util (#2024)
This commit is contained in:
@@ -2,6 +2,7 @@ import { pick } from "lodash"
|
||||
import { FindConfig, QueryConfig, RequestQueryFields } from "../types/common"
|
||||
import { MedusaError } from "medusa-core-utils/dist"
|
||||
import { BaseEntity } from "../interfaces/models/base-entity"
|
||||
import { isDefined } from "."
|
||||
|
||||
export function pickByConfig<TModel extends BaseEntity>(
|
||||
obj: TModel | TModel[],
|
||||
@@ -26,14 +27,14 @@ export function getRetrieveConfig<TModel extends BaseEntity>(
|
||||
expand?: string[]
|
||||
): FindConfig<TModel> {
|
||||
let includeFields: (keyof TModel)[] = []
|
||||
if (typeof fields !== "undefined") {
|
||||
if (isDefined(fields)) {
|
||||
includeFields = Array.from(new Set([...fields, "id"])).map((field) =>
|
||||
typeof field === "string" ? field.trim() : field
|
||||
) as (keyof TModel)[]
|
||||
}
|
||||
|
||||
let expandFields: string[] = []
|
||||
if (typeof expand !== "undefined") {
|
||||
if (isDefined(expand)) {
|
||||
expandFields = expand.map((expandRelation) => expandRelation.trim())
|
||||
}
|
||||
|
||||
@@ -53,7 +54,7 @@ export function getListConfig<TModel extends BaseEntity>(
|
||||
order?: { [k: symbol]: "DESC" | "ASC" }
|
||||
): FindConfig<TModel> {
|
||||
let includeFields: (keyof TModel)[] = []
|
||||
if (typeof fields !== "undefined") {
|
||||
if (isDefined(fields)) {
|
||||
const fieldSet = new Set(fields)
|
||||
// Ensure created_at is included, since we are sorting on this
|
||||
fieldSet.add("created_at")
|
||||
@@ -62,7 +63,7 @@ export function getListConfig<TModel extends BaseEntity>(
|
||||
}
|
||||
|
||||
let expandFields: string[] = []
|
||||
if (typeof expand !== "undefined") {
|
||||
if (isDefined(expand)) {
|
||||
expandFields = expand
|
||||
}
|
||||
|
||||
@@ -96,7 +97,7 @@ export function prepareListQuery<
|
||||
}
|
||||
|
||||
let orderBy: { [k: symbol]: "DESC" | "ASC" } | undefined
|
||||
if (typeof order !== "undefined") {
|
||||
if (isDefined(order)) {
|
||||
let orderField = order
|
||||
if (order.startsWith("-")) {
|
||||
const [, field] = order.split("-")
|
||||
|
||||
@@ -3,3 +3,4 @@ export * from "./set-metadata"
|
||||
export * from "./validate-id"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./remove-undefined-properties"
|
||||
export * from "./is-defined"
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export function isDefined<T = undefined | unknown>(val: T): val is (T extends undefined ? never : T) {
|
||||
return typeof val !== "undefined"
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isDefined } from "./is-defined";
|
||||
|
||||
export function removeUndefinedProperties<T extends object>(inputObj: T): T {
|
||||
const removeProperties = (obj: T) => {
|
||||
const res = {} as T
|
||||
@@ -17,13 +19,13 @@ export function removeUndefinedProperties<T extends object>(inputObj: T): T {
|
||||
}
|
||||
|
||||
function removeUndefinedDeeply(input: unknown): any {
|
||||
if (typeof input !== "undefined") {
|
||||
if (isDefined(input)) {
|
||||
if (input === null || input === "null") {
|
||||
return null
|
||||
} else if (Array.isArray(input)) {
|
||||
return input.map((item) => {
|
||||
return removeUndefinedDeeply(item)
|
||||
}).filter(v => typeof v !== "undefined")
|
||||
}).filter(v => isDefined(v))
|
||||
} else if (Object.prototype.toString.call(input) === '[object Date]') {
|
||||
return input
|
||||
} else if (typeof input === "object") {
|
||||
|
||||
Reference in New Issue
Block a user