Skip to content

Commit 87e425a

Browse files
authored
🚑 Fix serialization of null & undefined (#43)
1 parent ebd2dda commit 87e425a

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

‎src/index.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,25 @@ const DEFAULTS: RequiredOptions<Payload> = {
145145
onJSON: (json) => json,
146146
}
147147

148-
function defaultSerialize(input: SearchParams): URLSearchParams {
148+
function serialize(input: SearchParams): URLSearchParams {
149149
const params = new URLSearchParams()
150150

151-
for (const key of Object.keys(input)) {
152-
if (Array.isArray(input[key])) {
153-
// @ts-expect-error checked the variable inside if statement
154-
input[key].forEach((item) => params.append(key, item))
155-
} else {
151+
for (const [key, value] of Object.entries(input)) {
152+
if (Array.isArray(value)) {
153+
value.forEach((item) => params.append(key, item))
154+
} else if (value != null) {
156155
params.append(key, input[key])
157156
}
158157
}
159158

160159
return params
161160
}
162161

163-
function mergeMaps<Init, Request extends URLSearchParams | Headers>(
162+
const mergeMaps = <Init, Request extends URLSearchParams | Headers>(
164163
M: new (init?: Init) => Request,
165164
left?: Init,
166165
right?: Init
167-
): Request {
166+
): Request => {
168167
const result = new M(left)
169168

170169
new M(right).forEach((value, key) => result.append(key, value))
@@ -173,12 +172,12 @@ function mergeMaps<Init, Request extends URLSearchParams | Headers>(
173172
}
174173

175174
const normalizeParams = (
176-
serialize: Serialize = defaultSerialize,
175+
transform: Serialize = serialize,
177176
params: SearchParams | URLSearchParams | string = ''
178177
) =>
179178
typeof params === 'string' || params instanceof URLSearchParams
180179
? params
181-
: serialize(params)
180+
: transform(params)
182181

183182
const mergeOptions = <A extends Options<Payload>, B extends Options<Payload>>(
184183
left: A,
@@ -255,9 +254,11 @@ function request<P extends Payload>(
255254
.then(options.onResponse)
256255
.then(options.onSuccess, options.onFailure) as ResponsePromise<P>
257256

258-
for (const key of Object.keys(CONTENT_TYPES) as Array<keyof BodyMethods>) {
257+
for (const [key, value] of Object.entries(CONTENT_TYPES) as Array<
258+
[keyof BodyMethods, string]
259+
>) {
259260
promise[key] = () => {
260-
options.headers.set('accept', CONTENT_TYPES[key])
261+
options.headers.set('accept', value)
261262
return promise
262263
.then((result) => (key === 'void' ? undefined : result.clone()[key]()))
263264
.then((parsed) => (key === 'json' ? options.onJSON(parsed) : parsed))
@@ -309,7 +310,7 @@ export {
309310
ResponseError,
310311
TimeoutError,
311312
Serialize,
312-
defaultSerialize as serialize,
313+
serialize,
313314
request,
314315
create,
315316
get,

‎tests/core.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ describe('Response', () => {
298298

299299
const result = await YF.get('http://localhost/blob').arrayBuffer()
300300

301-
// @ts-ignore
302301
expect(String.fromCharCode(...new Uint8Array(result))).toBe('test')
303302
expect(result.byteLength).toBe(4)
304303
expect(result).toBeInstanceOf(ArrayBuffer)
@@ -797,6 +796,8 @@ test('default serialize', () => {
797796
number: 0,
798797
string: 'text',
799798
array: [1, 'two', 3],
799+
null: null,
800+
undefined,
800801
}).toString()
801802

802803
expect(result).toBe('number=0&string=text&array=1&array=two&array=3')

0 commit comments

Comments
 (0)