Skip to content

Commit 0346d4c

Browse files
committed
fix: skip falsy params value
To prevent ?null= or ?false= in a URL's query string.
1 parent 63cd03c commit 0346d4c

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default class Client {
5252
*/
5353
buildUrl(path, params = {}, withLocale = true) {
5454
const url = new URL((this.locale && withLocale ? `/${this.locale}` : '') + path, this.baseUrl);
55-
url.search = new URLSearchParams(params);
55+
56+
if(params) {
57+
url.search = new URLSearchParams(params);
58+
}
5659

5760
return url;
5861
}

tests/client.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ test.describe('Client', () => {
1919
});
2020

2121
// Feature tests
22-
test('should build a correct URL', async () => {
22+
test('should build a correct URL without params', async () => {
23+
const url = client.buildUrl('/test');
24+
expect(url.toString()).toBe(`${client.baseUrl}/test`);
25+
});
26+
27+
test('should build a correct URL with params', async () => {
2328
const url = client.buildUrl('/test', {
2429
foo: 'bar',
2530
baz: 'qux',
2631
});
27-
2832
expect(url.toString()).toBe(`${client.baseUrl}/test?foo=bar&baz=qux`);
2933
});
3034

35+
test('should build a correct URL with falsy params', async () => {
36+
const url = client.buildUrl('/test', false);
37+
expect(url.toString()).toBe(`${client.baseUrl}/test`);
38+
});
39+
3140
test('should transform response', async () => {
3241
const originalOnResponse = client.onResponse;
3342
const transformedResponse = {

0 commit comments

Comments
 (0)