From fc1cbe72c7b0cd2879a8b112a6f63fa94c728a19 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 30 Jun 2022 11:17:57 +0200 Subject: [PATCH] feat(medusa-js): Create utils to stringify null values and respect object types (#1748) ### What At the moment it is not possible to pass explicitly null on a property since the qs.stringify will transform it as `url?property=` which is then interpreted on the backend side as `""` value which is not what we expect. The actual way to pass null value is to set the property as `{ prop: "null" }` in order to get the stringify method to send it correctly. ### How I just created a small util that loop through the input object and transform all null values to "null" before being passed to the qs.stringify method. The new util is only applied on the list batch job since this is for now the only place that require that behaviour. --- .../src/resources/admin/batch-jobs.ts | 3 ++- packages/medusa-js/src/utils.ts | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 packages/medusa-js/src/utils.ts diff --git a/packages/medusa-js/src/resources/admin/batch-jobs.ts b/packages/medusa-js/src/resources/admin/batch-jobs.ts index e80dfc51ac..457541ccf3 100644 --- a/packages/medusa-js/src/resources/admin/batch-jobs.ts +++ b/packages/medusa-js/src/resources/admin/batch-jobs.ts @@ -7,6 +7,7 @@ import { import qs from "qs" import { ResponsePromise } from "../../typings" import BaseResource from "../base" +import { stringifyNullProperties } from "../../utils" class AdminBatchJobsResource extends BaseResource { create( @@ -24,7 +25,7 @@ class AdminBatchJobsResource extends BaseResource { let path = `/admin/batch-jobs` if (query) { - const queryString = qs.stringify(query) + const queryString = qs.stringify(stringifyNullProperties(query)) path = `/admin/batch-jobs?${queryString}` } diff --git a/packages/medusa-js/src/utils.ts b/packages/medusa-js/src/utils.ts new file mode 100644 index 0000000000..1dcf0936f1 --- /dev/null +++ b/packages/medusa-js/src/utils.ts @@ -0,0 +1,23 @@ +export function stringifyNullProperties(input: T): T { + const convertProperties = (obj: T) => { + const res = {} as T + + Object.keys(obj).reduce((acc: T, key: string) => { + if (typeof obj[key] === "object") { + acc[key] = convertProperties(obj[key]) + } + + if (obj[key] === null) { + acc[key] = "null" + } else { + acc[key] = obj[key] + } + + return acc + }, res) + + return res + } + + return convertProperties(input) +}