Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __tests__/utils/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as fetch from '../../src/utils/fetch';

describe('utils.fetch#encodeQueryData', () => {
test('should create valid query string', () => {
const result1 = fetch.encodeQueryData({
a: 1,
b: true,
c: null,
d: 'foo',
e: undefined
});

const result2 = fetch.encodeQueryData({
a: 1,
b: {
c: {
d: 'foo'
}
}
});

expect(result1).toEqual('a=1&b=true&d=foo');
expect(result2).toEqual('a=1&b=%7B%22c%22%3A%7B%22d%22%3A%22foo%22%7D%7D');
});
});
2 changes: 1 addition & 1 deletion src/adapters/vfs/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const methods = (core, request) => {
return {
readdir: ({path}, options) => request('readdir', {
path,
options: {}
options,
}, 'json').then(({body}) => body),

readfile: ({path}, type, options) =>
Expand Down
19 changes: 14 additions & 5 deletions src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@
* @license Simplified BSD License
*/


/*
* Creates URL request path
*/
const encodeQueryData = data => Object.keys(data)
.filter(k => typeof data[k] !== 'object')
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(data[k]))
.join('&');
export const encodeQueryData = (data) => {
const pairs = Object.entries(data)
.filter(([, val]) => val !== null && val !== undefined)
.map(([key, val]) => {
const value = typeof val === 'object' ? JSON.stringify(val) : val;
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
});

return pairs.join('&');
};

const bodyTypes = [
window.ArrayBuffer,
Expand Down Expand Up @@ -65,7 +72,9 @@ const createFetchOptions = (url, options, type) => {
}

if (fetchOptions.body && fetchOptions.method.toLowerCase() === 'get') {
url += '?' + encodeQueryData(fetchOptions.body);
if(encodeQueryData(fetchOptions.body) !== '') {
url += '?' + encodeQueryData(fetchOptions.body);
}
delete fetchOptions.body;
}

Expand Down