diff --git a/__tests__/utils/fetch.js b/__tests__/utils/fetch.js new file mode 100644 index 0000000..b55a89c --- /dev/null +++ b/__tests__/utils/fetch.js @@ -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'); + }); +}); diff --git a/src/adapters/vfs/system.js b/src/adapters/vfs/system.js index ceee251..16dcfd9 100644 --- a/src/adapters/vfs/system.js +++ b/src/adapters/vfs/system.js @@ -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) => diff --git a/src/utils/fetch.js b/src/utils/fetch.js index 46508e9..aaa6bfc 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -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, @@ -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; }