From 6f2c322adf7b8a377b387f78e9d10b90b09c3b75 Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Tue, 26 Jul 2022 18:11:15 +0430 Subject: [PATCH] Support unserialization of url query parameters --- __tests__/utils/vfs.js | 23 +++++++++++++++++++++++ src/utils/vfs.js | 24 +++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/__tests__/utils/vfs.js b/__tests__/utils/vfs.js index 85e968a..7dd3e17 100644 --- a/__tests__/utils/vfs.js +++ b/__tests__/utils/vfs.js @@ -243,4 +243,27 @@ describe('VFS Utils', () => { test('parseFields - POST w/Form', () => { // TODO }); + + test('assembleQueryData', () => { + const result1 = utils.assembleQueryData({ + 'a': 'b', + 'b': '{"a":"foo"}', + 'c': '{"a":false,"c":null,"d":1,"e":{"a":"foo"}}' + }); + + expect(result1).toEqual({ + a: 'b', + b: { + a: 'foo' + }, + c: { + a: false, + c: null, + d: 1, + e: { + a: 'foo' + } + } + }); + }); }); diff --git a/src/utils/vfs.js b/src/utils/vfs.js index e4dd10c..9c6d7fc 100644 --- a/src/utils/vfs.js +++ b/src/utils/vfs.js @@ -171,13 +171,30 @@ const mountpointResolver = core => async (path) => { return Object.freeze({mount, adapter}); }; +/* + * Assembles a given object query + */ +const assembleQueryData = (data) => { + const entries = Object + .entries(data) + .map(([k, v]) => { + try { + return [k, JSON.parse(v)]; + } catch (e) { + return [k, v]; + } + }); + + return Object.fromEntries(entries); +}; + /* * Parses URL Body */ const parseGet = req => { const {query} = url.parse(req.url, true); - - return Promise.resolve({fields: query, files: {}}); + const assembledQuery = assembleQueryData(query); + return Promise.resolve({fields: assembledQuery, files: {}}); }; /* @@ -254,5 +271,6 @@ module.exports = { getPrefix, parseFields, errorCodes, - methodArguments + methodArguments, + assembleQueryData };