From e8baf247b18a5d06958829292e6330e6cae3aec3 Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Tue, 26 Jul 2022 17:58:41 +0430 Subject: [PATCH 1/3] Support serialization of url query parameters --- __tests__/utils/fetch.js | 25 +++++++++++++++++++++++++ src/adapters/vfs/system.js | 2 +- src/utils/fetch.js | 27 +++++++++++++++++++-------- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 __tests__/utils/fetch.js diff --git a/__tests__/utils/fetch.js b/__tests__/utils/fetch.js new file mode 100644 index 0000000..964bd0c --- /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&c=null&d=foo&e=undefined'); + 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..6b23472 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -28,13 +28,22 @@ * @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('&'); + +// /* +// * Creates URL request path +// */ +export const encodeQueryData = (data) => { + const replacer = (k, v)=>(v === undefined ? null : v); + const pairs = Object.entries(data).map(([key, val]) => { + const isNull = val === null; + if (typeof val === 'object' && !isNull) { + return `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(val, replacer))}`; + } else { + return `${encodeURIComponent(key)}=${encodeURIComponent(val)}`; + } + }); + return pairs.join('&'); +}; const bodyTypes = [ window.ArrayBuffer, @@ -65,7 +74,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; } From c710a8ba95037990482ec9ac6e34352ab6c74dde Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Wed, 27 Jul 2022 10:50:39 +0430 Subject: [PATCH 2/3] Ignore null and undefined in url serialization --- __tests__/utils/fetch.js | 2 +- src/utils/fetch.js | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/__tests__/utils/fetch.js b/__tests__/utils/fetch.js index 964bd0c..b55a89c 100644 --- a/__tests__/utils/fetch.js +++ b/__tests__/utils/fetch.js @@ -19,7 +19,7 @@ describe('utils.fetch#encodeQueryData', () => { } }); - expect(result1).toEqual('a=1&b=true&c=null&d=foo&e=undefined'); + 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/utils/fetch.js b/src/utils/fetch.js index 6b23472..b520f65 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -29,19 +29,18 @@ */ -// /* -// * Creates URL request path -// */ +/* + * Creates URL request path + */ export const encodeQueryData = (data) => { - const replacer = (k, v)=>(v === undefined ? null : v); - const pairs = Object.entries(data).map(([key, val]) => { - const isNull = val === null; - if (typeof val === 'object' && !isNull) { - return `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(val, replacer))}`; - } else { - return `${encodeURIComponent(key)}=${encodeURIComponent(val)}`; - } - }); + + 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('&'); }; From a43cf99945b38594fef81c22d3cec7462fe244f5 Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Wed, 27 Jul 2022 10:53:10 +0430 Subject: [PATCH 3/3] Remove one blank line --- src/utils/fetch.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/fetch.js b/src/utils/fetch.js index b520f65..aaa6bfc 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -33,7 +33,6 @@ * Creates URL request path */ export const encodeQueryData = (data) => { - const pairs = Object.entries(data) .filter(([, val]) => val !== null && val !== undefined) .map(([key, val]) => {