Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve serialization of VFS URL query parameters #165

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions __tests__/utils/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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'
});

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

expect(result1).toEqual('a=1&b=true&c=null&d=foo');
expect(result2).toEqual('a.a=1&a.b=true&a.c=null&a.d=foo&b.c.d=foo');
});
});
14 changes: 10 additions & 4 deletions src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@
/*
* 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, nesting = '') => {
const pairs = Object.entries(data).map(([key, val]) => {
if (typeof val === 'object' && val !== null) {
return encodeQueryData(val, nesting + `${key}.`);
} else {
return encodeURIComponent(nesting + key) + '=' + encodeURIComponent(val);
}
});
return pairs.join('&');
};

const bodyTypes = [
window.ArrayBuffer,
Expand Down