Skip to content

feat(marshalling): add any marshalling #2054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/client/src/helpers/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,68 @@
objT === 'object'
)
}

/**
* Camelizes a string.
*
* @param str - The string to camelize
* @returns The camelized string
*
* @internal
*/
export const camelize = (str: string): string => {
const strLength = str.length
if (strLength <= 0) {
return str
}
let out = ''
for (let capNext = false, index = 0; index < strLength; index += 1) {
const char = str.charAt(index)
if (char >= 'a' && char <= 'z') {
if (capNext) {
out += char.toUpperCase()
} else {
out += char
}
} else if (char >= 'A' && char <= 'Z') {
out += char
} else if (char >= '0' && char <= '9') {
out += char
}
capNext = char === '_' || char === ' ' || char === '-' || char === '.'
}

return out.charAt(0).toLowerCase() + out.substring(1)
}

/**
* Camelizes keys of an object (deeply).
*
* @param obj - The object
* @param ignoreKeys - The keys to ignore
* @returns The object with camelized keys
*
* @internal
*/
export const camelizeKeys = <T>(
obj: object | unknown[] | unknown,

Check warning on line 89 in packages/client/src/helpers/json.ts

View workflow job for this annotation

GitHub Actions / lint

'unknown' overrides all other types in this union type
ignoreKeys: string[] = [],
): T => {
if (Array.isArray(obj)) {
return obj.map(v => camelizeKeys(v, ignoreKeys)) as unknown as T
}

if (obj && typeof obj === 'object' && !(obj instanceof Date)) {
return Object.entries(obj).reduce(
(acc, [key, value]) => ({
...acc,
[camelize(key)]: ignoreKeys.includes(key)
? (value as unknown)
: camelizeKeys(value, ignoreKeys),
}),
{},
) as T
}

return obj as T
}
60 changes: 59 additions & 1 deletion packages/client/src/scw/custom-marshalling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isJSONObject } from '../helpers/json'
import { camelizeKeys, isJSONObject } from '../helpers/json'
import { unmarshalArrayOfObject, unmarshalDate } from '../helpers/marshalling'
import { fromByteArray } from '../vendor/base64'
import type {
Expand Down Expand Up @@ -207,3 +207,61 @@ export const marshalTimeSeries = (
export const marshalDecimal = (obj: Decimal): { value: string } => ({
value: obj.toString(),
})

/**
* Unmarshals record to convert iso dates from string to Dates.
*
* @param obj - The input
* @param keys - The keys requiring a conversion
* @returns The updated input
*
* @internal
*/
export const unmarshalDates = <T>(obj: unknown, keys: string[]): T => {
if (Array.isArray(obj)) {
return obj.map(v => unmarshalDates(v, keys)) as unknown as T
}

if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce(
(acc, [key, value]) => ({
...acc,
[key]:
typeof value === 'string' && keys.includes(key)
? new Date(value)
: unmarshalDates(value, keys),
}),
{},
) as T
}

return obj as T
}

/**
* Unmarshals input to a record with camilized keys and instanciated Date.
*
* @param obj - The input
* @param ignoreKeys - The keys which should be not be transformed
* @param dateKeys - The keys which should be transformed to Date
* @returns The record
*
* @throws TypeError
* Thrown if the input isn't {@link JSONObject}.
*
* @internal
*/
export const unmarshalAnyRes = <T>(
obj: unknown,
ignoreKeys: string[] = [],
dateKeys?: string[],
): T => {
if (!isJSONObject(obj)) {
throw new TypeError(`Data isn't a dictionary.`)
}

return camelizeKeys(
dateKeys && dateKeys.length > 0 ? unmarshalDates(obj, dateKeys) : obj,
ignoreKeys,
)
}
Loading