Skip to content

Commit 52235f8

Browse files
mahsashadimahsa shadi
andauthored
Support serialization of url query parameters (#182)
Co-authored-by: mahsa shadi <mahsa.shadi@mail.um.ac.ir>
1 parent cd8add4 commit 52235f8

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

__tests__/utils/fetch.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as fetch from '../../src/utils/fetch';
2+
3+
describe('utils.fetch#encodeQueryData', () => {
4+
test('should create valid query string', () => {
5+
const result1 = fetch.encodeQueryData({
6+
a: 1,
7+
b: true,
8+
c: null,
9+
d: 'foo',
10+
e: undefined
11+
});
12+
13+
const result2 = fetch.encodeQueryData({
14+
a: 1,
15+
b: {
16+
c: {
17+
d: 'foo'
18+
}
19+
}
20+
});
21+
22+
expect(result1).toEqual('a=1&b=true&d=foo');
23+
expect(result2).toEqual('a=1&b=%7B%22c%22%3A%7B%22d%22%3A%22foo%22%7D%7D');
24+
});
25+
});

src/adapters/vfs/system.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const methods = (core, request) => {
5959
return {
6060
readdir: ({path}, options) => request('readdir', {
6161
path,
62-
options: {}
62+
options,
6363
}, 'json').then(({body}) => body),
6464

6565
readfile: ({path}, type, options) =>

src/utils/fetch.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@
2828
* @license Simplified BSD License
2929
*/
3030

31+
3132
/*
3233
* Creates URL request path
3334
*/
34-
const encodeQueryData = data => Object.keys(data)
35-
.filter(k => typeof data[k] !== 'object')
36-
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(data[k]))
37-
.join('&');
35+
export const encodeQueryData = (data) => {
36+
const pairs = Object.entries(data)
37+
.filter(([, val]) => val !== null && val !== undefined)
38+
.map(([key, val]) => {
39+
const value = typeof val === 'object' ? JSON.stringify(val) : val;
40+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
41+
});
42+
43+
return pairs.join('&');
44+
};
3845

3946
const bodyTypes = [
4047
window.ArrayBuffer,
@@ -65,7 +72,9 @@ const createFetchOptions = (url, options, type) => {
6572
}
6673

6774
if (fetchOptions.body && fetchOptions.method.toLowerCase() === 'get') {
68-
url += '?' + encodeQueryData(fetchOptions.body);
75+
if(encodeQueryData(fetchOptions.body) !== '') {
76+
url += '?' + encodeQueryData(fetchOptions.body);
77+
}
6978
delete fetchOptions.body;
7079
}
7180

0 commit comments

Comments
 (0)