Skip to content

Commit 23b3c57

Browse files
author
mahsa shadi
committed
Support unserialization of VFS URL query parameters
1 parent 948873c commit 23b3c57

File tree

2 files changed

+20
-91
lines changed

2 files changed

+20
-91
lines changed

__tests__/utils/vfs.js

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('VFS Utils', () => {
203203
const parser = utils.parseFields();
204204

205205
return expect(parser({
206-
url: '/foo/?bar.s=baz&jazz.s=bass',
206+
url: '/foo/?bar=baz&jazz=bass',
207207
method: 'get'
208208
}))
209209
.resolves
@@ -246,65 +246,22 @@ describe('VFS Utils', () => {
246246

247247
test('assembleQueryData', () => {
248248
const result1 = utils.assembleQueryData({
249-
'a.s': 'b',
250-
'b.a.s': 'foo',
251-
'b.b.a.s': 'foo',
252-
'b.b.b.s': 'foo',
253-
'b.b.c.s': 'foo',
254-
'b.b.d.s': 'foo',
255-
'b.b.e.s': 'foo',
256-
'c.n': 'null',
257-
'd.b': 'true',
258-
'e.i': '1',
259-
'f.u': 'undefined'
260-
});
261-
262-
const result2 = utils.assembleQueryData({
263-
'a.0.s': 'foo',
264-
'a.1.s': 'foo',
265-
'b.0.s': 'foo',
266-
'b.1.s': 'foo',
267-
'b.a.s': 'foo',
268-
'c.a.s': 'foo',
269-
'c.b.0.s': 'foo',
270-
'c.b.1.s': 'foo',
271-
'c.c.0.s': 'foo',
272-
'c.c.1.s': 'foo',
273-
'c.c.a.s': 'foo',
249+
'a': 'b',
250+
'b': '{"a":"foo"}',
251+
'c': '{"a":false,"c":null,"d":1,"e":{"a":"foo"}}'
274252
});
275253

276254
expect(result1).toEqual({
277255
a: 'b',
278256
b: {
279-
a: 'foo',
280-
b: {
281-
a: 'foo',
282-
b: 'foo',
283-
c: 'foo',
284-
d: 'foo',
285-
e: 'foo'
286-
}
287-
},
288-
c: null,
289-
d: true,
290-
e: 1,
291-
f: undefined
292-
});
293-
294-
expect(result2).toEqual({
295-
a: ['foo', 'foo'],
296-
b:{
297-
'0': 'foo',
298-
'1': 'foo',
299-
'a': 'foo',
257+
a: 'foo'
300258
},
301259
c: {
302-
a: 'foo',
303-
b: ['foo', 'foo'],
304-
c: {
305-
'0': 'foo',
306-
'1': 'foo',
307-
'a': 'foo'
260+
a: false,
261+
c: null,
262+
d: 1,
263+
e: {
264+
a: 'foo'
308265
}
309266
}
310267
});

src/utils/vfs.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ const errorCodes = {
4242
EACCES: 401
4343
};
4444

45-
/**
46-
* A map of data types
47-
*/
48-
const typeMap = {
49-
i: str => parseInt(str, 10),
50-
s: str => str,
51-
b: str => str === 'true',
52-
u: str => undefined,
53-
n: str => null
54-
};
55-
5645
/**
5746
* Gets prefix of a VFS path
5847
*/
@@ -185,35 +174,18 @@ const mountpointResolver = core => async (path) => {
185174
/*
186175
* Assembles a given object query
187176
*/
188-
const assembleQueryData = (object) => {
189-
const assembled = {};
190-
const keys = Object.keys(object);
191-
192-
for (let i = 0; i < keys.length; i++) {
193-
const key = keys[i];
194-
const dots = key.split('.');
195-
const type = dots[dots.length - 1];
196-
dots.pop();
197-
198-
let last = assembled;
199-
let parent = null;
200-
for (let j = 0; j < dots.length; j++) {
201-
const dot = dots[j];
202-
if (j >= dots.length - 1) {
203-
if (!/^\d+$/.test(dot) && Array.isArray(last)) {
204-
last = Object.fromEntries(last.map((value, i) => [i, value]));
205-
parent[dots[j - 1]] = last;
206-
}
207-
last[dot] = typeMap[type](object[key]);
208-
} else {
209-
last[dot] = last[dot] || (/^\d+$/.test(dots[j + 1]) ? [] : {});
177+
const assembleQueryData = (data) => {
178+
const entries = Object
179+
.entries(data)
180+
.map(([k, v]) => {
181+
try {
182+
return [k, JSON.parse(v)];
183+
} catch (e) {
184+
return [k, v];
210185
}
186+
});
211187

212-
parent = last;
213-
last = last[dot];
214-
}
215-
}
216-
return assembled;
188+
return Object.fromEntries(entries);
217189
};
218190

219191
/*

0 commit comments

Comments
 (0)