Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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&c=null&d=foo&e=undefined');
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
27 changes: 19 additions & 8 deletions src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicate comment characters :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! sorry for that.

export const encodeQueryData = (data) => {
const replacer = (k, v)=>(v === undefined ? null : v);
const pairs = Object.entries(data).map(([key, val]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to just filter out any null-ish values before doing the encoding. The backend doesn't really take null or undefined values anyway.

This way you can just do the following:

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)}`;
  });

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,
Expand Down Expand Up @@ -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;
}

Expand Down