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.
This commit is contained in:
Adrien de Peretti
2022-06-30 11:17:57 +02:00
committed by GitHub
parent 55978e9e81
commit fc1cbe72c7
2 changed files with 25 additions and 1 deletions

View File

@@ -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}`
}

View File

@@ -0,0 +1,23 @@
export function stringifyNullProperties<T extends object>(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)
}