Skip to content

Commit c9db604

Browse files
committed
ignore query params with undefined value
1 parent 1178b3e commit c9db604

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/tools/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export function serializeQueryParams(params: { [key: string]: any }): string {
152152
const result: Array<string> = [];
153153

154154
for (const key in params) {
155-
if (params[key] === null) {
156-
// skip null query params
155+
if (params[key] === null || typeof params[key] === "undefined") {
156+
// skip null or undefined query params
157157
continue;
158158
}
159159

tests/Client.spec.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,42 +228,42 @@ describe("Client", function () {
228228

229229
fetchMock.on({
230230
method: "GET",
231-
url: "test_base_url/123",
231+
url: "test_base_url/123?queryA=456",
232232
replyCode: 200,
233233
replyBody: "successGet",
234234
});
235235

236236
fetchMock.on({
237237
method: "POST",
238-
url: "test_base_url/123",
238+
url: "test_base_url/123?queryA=456",
239239
replyCode: 200,
240240
replyBody: "successPost",
241241
});
242242

243243
fetchMock.on({
244244
method: "PUT",
245-
url: "test_base_url/123",
245+
url: "test_base_url/123?queryA=456",
246246
replyCode: 200,
247247
replyBody: "successPut",
248248
});
249249

250250
fetchMock.on({
251251
method: "PATCH",
252-
url: "test_base_url/123",
252+
url: "test_base_url/123?queryA=456",
253253
replyCode: 200,
254254
replyBody: "successPatch",
255255
});
256256

257257
fetchMock.on({
258258
method: "DELETE",
259-
url: "test_base_url/123",
259+
url: "test_base_url/123?queryA=456",
260260
replyCode: 200,
261261
replyBody: "successDelete",
262262
});
263263

264264
fetchMock.on({
265265
method: "GET",
266-
url: "test_base_url/multipart",
266+
url: "test_base_url/multipart?queryA=456",
267267
additionalMatcher: (_, config: any): boolean => {
268268
// multipart/form-data requests shouldn't have explicitly set Content-Type
269269
return !config?.headers?.["Content-Type"];
@@ -274,7 +274,7 @@ describe("Client", function () {
274274

275275
fetchMock.on({
276276
method: "GET",
277-
url: "test_base_url/multipartAuto",
277+
url: "test_base_url/multipartAuto?queryA=456",
278278
additionalMatcher: (_, config: any): boolean => {
279279
if (
280280
// multipart/form-data requests shouldn't have explicitly set Content-Type
@@ -301,26 +301,28 @@ describe("Client", function () {
301301
replyBody: "successMultipartAuto",
302302
});
303303

304+
const testQueryParams = { queryA: 456, queryB: null, queryC: undefined }
305+
304306
const testCases = [
305-
[client.send("/123", { method: "GET" }), "successGet"],
306-
[client.send("/123", { method: "POST" }), "successPost"],
307-
[client.send("/123", { method: "PUT" }), "successPut"],
308-
[client.send("/123", { method: "PATCH" }), "successPatch"],
309-
[client.send("/123", { method: "DELETE" }), "successDelete"],
307+
[client.send("/123", Object.assign({ method: "GET" }, testQueryParams)), "successGet"],
308+
[client.send("/123", Object.assign({ method: "POST" }, testQueryParams)), "successPost"],
309+
[client.send("/123", Object.assign({ method: "PUT" }, testQueryParams)), "successPut"],
310+
[client.send("/123", Object.assign({ method: "PATCH" }, testQueryParams)), "successPatch"],
311+
[client.send("/123", Object.assign({ method: "DELETE" }, testQueryParams)), "successDelete"],
310312
[
311-
client.send("/multipart", { method: "GET", body: new FormData() }),
313+
client.send("/multipart", Object.assign({ method: "GET", body: new FormData() }, testQueryParams)),
312314
"successMultipart",
313315
],
314316
[
315-
client.send("/multipartAuto", {
317+
client.send("/multipartAuto", Object.assign({
316318
method: "GET",
317319
body: {
318320
title: "test",
319321
roles: ["a", "b"],
320322
json: null,
321323
files: [new Blob(["11"]), new Blob(["2"])],
322324
},
323-
}),
325+
}, testQueryParams)),
324326
"successMultipartAuto",
325327
],
326328
];

0 commit comments

Comments
 (0)