Skip to content

Commit

Permalink
Implement serialize in params-serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Dec 12, 2023
1 parent 31d56a8 commit 5aced6b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/lib/params-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ export const paramsSerializer: CustomParamsSerializer = (params) => {
)
}
for (const v of value) {
throwIfUnserializable(name, v)
searchParams.append(name, v)
searchParams.append(name, serialize(name, v))
}
continue
}

throwIfUnserializable(name, value)
searchParams.set(name, value)
searchParams.set(name, serialize(name, value))
}

searchParams.sort()
return searchParams.toString()
}

const throwIfUnserializable = (k: string, v: unknown): void => {
const serialize = (k: string, v: unknown): string => {
if (v == null) {
throw new UnserializableParamError(k, `is ${v} or contains ${v}`)
}
Expand All @@ -44,6 +42,17 @@ const throwIfUnserializable = (k: string, v: unknown): void => {
if (typeof v === 'object') {
throw new UnserializableParamError(k, 'is an object or contains an object')
}

if (typeof v === 'symbol') {
throw new UnserializableParamError(k, 'is a symbol')
}

if (typeof v === 'string') return v.toString()
if (typeof v === 'number') return v.toString()
if (typeof v === 'bigint') return v.toString()
if (typeof v === 'boolean') return v.toString()

throw new UnserializableParamError(k, `is a ${typeof v}`)
}

export class UnserializableParamError extends Error {
Expand Down

0 comments on commit 5aced6b

Please sign in to comment.