From 37fd98af40d0f6d98d5748e9f30f3e5d208adb64 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Mon, 3 Oct 2022 18:53:19 -0300 Subject: [PATCH 01/14] Added new functionality to trim the request data. Fix pristine Data --- lib/dispatcher.js | 10 +++++++--- lib/utils.js | 26 +++++++++++++++++++++++++- package.json | 1 + 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index c432ac0..e8972a0 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -2,11 +2,12 @@ const { struct } = require('@janiscommerce/superstruct'); const { ApiSession } = require('@janiscommerce/api-session'); +const cloneDeep = require('lodash.clonedeep'); const Events = require('@janiscommerce/events'); const Fetcher = require('./fetcher'); -const { isObject } = require('./utils'); +const { isObject, trimRecursive, cloneObj } = require('./utils'); const API = require('./api'); const APIError = require('./error'); @@ -37,10 +38,13 @@ module.exports = class Dispatcher { constructor(request) { this._validateRequest(request); + const trimData = trimRecursive(request.data); + const pristineData = cloneObj(request.data); + this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); - this.data = request.data || {}; - this.pristineData = request.data && { ...request.data }; + this.data = trimData || {}; + this.pristineData = request.data && pristineData; Object.freeze(this.pristineData); this.rawData = request.rawData; this.headers = request.headers || {}; diff --git a/lib/utils.js b/lib/utils.js index 62ce001..6ea742b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,7 @@ 'use strict'; const omit = require('lodash.omit'); +const cloneObj = require('lodash.clonedeep'); /** * Determines if the passed value is an object. @@ -29,7 +30,30 @@ const omitRecursive = (object, exclude) => { return omit(object, exclude); }; +const trimRecursive = obj => { + + if(Array.isArray(obj)) { + return obj.map(object => trimObjValues(object)); + } + + return trimObjValues(obj); +} + +const trimObjValues = obj => { + + Object.keys(obj).forEach(k => { + if((typeof obj[k] === "string")) + obj[k] = obj[k].trim(); + else + trimObjValues(obj[k]); + }); + + return obj; +} + module.exports = { isObject, - omitRecursive + omitRecursive, + trimRecursive, + cloneObj }; diff --git a/package.json b/package.json index 277ab2a..c0aff04 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@janiscommerce/events": "^0.1.0", "@janiscommerce/log": "^3.5.1", "@janiscommerce/superstruct": "^1.2.0", + "lodash.clonedeep": "^4.5.0", "lodash.omit": "4.5.0", "uuid": "^8.3.2" } From e84c2e2887ec30cda6c6263184fa8a2c92c0cda8 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 4 Oct 2022 15:00:45 -0300 Subject: [PATCH 02/14] Change formatting req.data --- lib/dispatcher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index e8972a0..80ba159 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -38,8 +38,8 @@ module.exports = class Dispatcher { constructor(request) { this._validateRequest(request); - const trimData = trimRecursive(request.data); - const pristineData = cloneObj(request.data); + const trimData = trimRecursive({ ...request.data }); + const pristineData = cloneObj({ ...request.data }); this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); From 2eb86ea2a7c10eba99edf56a0fa0560240eb17d2 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 4 Oct 2022 15:01:56 -0300 Subject: [PATCH 03/14] Change formatting req.data --- lib/dispatcher.js | 1 - lib/utils.js | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 80ba159..ddb9912 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -2,7 +2,6 @@ const { struct } = require('@janiscommerce/superstruct'); const { ApiSession } = require('@janiscommerce/api-session'); -const cloneDeep = require('lodash.clonedeep'); const Events = require('@janiscommerce/events'); diff --git a/lib/utils.js b/lib/utils.js index 6ea742b..ff2dbc4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,19 +30,10 @@ const omitRecursive = (object, exclude) => { return omit(object, exclude); }; -const trimRecursive = obj => { - - if(Array.isArray(obj)) { - return obj.map(object => trimObjValues(object)); - } - - return trimObjValues(obj); -} - const trimObjValues = obj => { Object.keys(obj).forEach(k => { - if((typeof obj[k] === "string")) + if((typeof obj[k] === 'string')) obj[k] = obj[k].trim(); else trimObjValues(obj[k]); @@ -51,6 +42,16 @@ const trimObjValues = obj => { return obj; } +const trimRecursive = obj => { + + if(Array.isArray(obj)) { + return obj.map(object => trimObjValues(object)); + } + + return trimObjValues(obj); +} + + module.exports = { isObject, omitRecursive, From 86fb1a8855f7abbd3cf0f96cdfbb8a9aff60a2f1 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 4 Oct 2022 15:02:42 -0300 Subject: [PATCH 04/14] Fix eslint lines --- lib/utils.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index ff2dbc4..6ecf5bb 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -40,16 +40,15 @@ const trimObjValues = obj => { }); return obj; -} +}; const trimRecursive = obj => { - if(Array.isArray(obj)) { + if(Array.isArray(obj)) return obj.map(object => trimObjValues(object)); - } return trimObjValues(obj); -} +}; module.exports = { From f0613bc04b1d819748e9351784ed3a39abdb2e9c Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 4 Oct 2022 15:40:42 -0300 Subject: [PATCH 05/14] Refactor code --- lib/dispatcher.js | 5 ++--- lib/utils.js | 11 +---------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index ddb9912..d5e5c3f 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -6,7 +6,7 @@ const { ApiSession } = require('@janiscommerce/api-session'); const Events = require('@janiscommerce/events'); const Fetcher = require('./fetcher'); -const { isObject, trimRecursive, cloneObj } = require('./utils'); +const { isObject, trimObjValues, cloneObj } = require('./utils'); const API = require('./api'); const APIError = require('./error'); @@ -37,9 +37,8 @@ module.exports = class Dispatcher { constructor(request) { this._validateRequest(request); - const trimData = trimRecursive({ ...request.data }); + const trimData = trimObjValues({ ...request.data }); const pristineData = cloneObj({ ...request.data }); - this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); this.data = trimData || {}; diff --git a/lib/utils.js b/lib/utils.js index 6ecf5bb..c7c09aa 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -42,18 +42,9 @@ const trimObjValues = obj => { return obj; }; -const trimRecursive = obj => { - - if(Array.isArray(obj)) - return obj.map(object => trimObjValues(object)); - - return trimObjValues(obj); -}; - - module.exports = { isObject, omitRecursive, - trimRecursive, + trimObjValues, cloneObj }; From a01b130013b70304c10dffa486b020dc44f15da8 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 4 Oct 2022 15:41:38 -0300 Subject: [PATCH 06/14] Modify data in dispatcher --- lib/dispatcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index d5e5c3f..62da891 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -41,7 +41,7 @@ module.exports = class Dispatcher { const pristineData = cloneObj({ ...request.data }); this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); - this.data = trimData || {}; + this.data = trimData; this.pristineData = request.data && pristineData; Object.freeze(this.pristineData); this.rawData = request.rawData; From 9a2c11c70a50f178d2ec4c5dd239c6ccbe2c10af Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 11 Oct 2022 12:54:37 -0300 Subject: [PATCH 07/14] Changes seen in pr --- lib/dispatcher.js | 5 ++--- lib/utils.js | 17 ++++++++++------- tests/dispatcher-test.js | 7 +++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 62da891..480e3d6 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -6,7 +6,7 @@ const { ApiSession } = require('@janiscommerce/api-session'); const Events = require('@janiscommerce/events'); const Fetcher = require('./fetcher'); -const { isObject, trimObjValues, cloneObj } = require('./utils'); +const { isObject, trimObjectValues, cloneObj } = require('./utils'); const API = require('./api'); const APIError = require('./error'); @@ -36,8 +36,7 @@ module.exports = class Dispatcher { */ constructor(request) { this._validateRequest(request); - - const trimData = trimObjValues({ ...request.data }); + const trimData = trimObjectValues({ ...request.data }); const pristineData = cloneObj({ ...request.data }); this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); diff --git a/lib/utils.js b/lib/utils.js index c7c09aa..d69e104 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,21 +30,24 @@ const omitRecursive = (object, exclude) => { return omit(object, exclude); }; -const trimObjValues = obj => { +const trimObjectValues = object => { - Object.keys(obj).forEach(k => { - if((typeof obj[k] === 'string')) - obj[k] = obj[k].trim(); + if(typeof object !== 'object' || object === null) + return {}; + + Object.keys(object).forEach(k => { + if((typeof object[k] === 'string')) + object[k] = object[k].trim(); else - trimObjValues(obj[k]); + trimObjectValues(object[k]); }); - return obj; + return object; }; module.exports = { isObject, omitRecursive, - trimObjValues, + trimObjectValues, cloneObj }; diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index 015ddea..3c55157 100644 --- a/tests/dispatcher-test.js +++ b/tests/dispatcher-test.js @@ -491,6 +491,13 @@ describe('Dispatcher', function() { }, 200); }); + it('should return code 200 when api validates correctly the struct & apply trim to data', async function() { + await test({ + endpoint: 'api/struct-endpoint', + data: { foo: ' bar ' } + }, 200); + }); + it('should return code 200 when api has no validate method', async function() { await test({ endpoint: 'api/valid-endpoint' From 2a5b86c5712e23fae0e5d8be02e90280cfd64766 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Thu, 13 Oct 2022 10:27:31 -0300 Subject: [PATCH 08/14] Update dispatcher test for trim data --- tests/dispatcher-test.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index 3c55157..f064fa0 100644 --- a/tests/dispatcher-test.js +++ b/tests/dispatcher-test.js @@ -484,17 +484,24 @@ describe('Dispatcher', function() { }, 200); }); - it('should return code 200 when api validates correctly the struct', async function() { + it('should return code 200 when api validates correctly & apply trim to data', async function() { await test({ - endpoint: 'api/struct-endpoint', - data: { foo: 'bar' } + endpoint: 'api/validate-correctly-endpoint', + data: { + foo: { + a: ' bar ', + b: { + c: ' test ' + } + } + } }, 200); }); - it('should return code 200 when api validates correctly the struct & apply trim to data', async function() { + it('should return code 200 when api validates correctly the struct', async function() { await test({ endpoint: 'api/struct-endpoint', - data: { foo: ' bar ' } + data: { foo: 'bar' } }, 200); }); From 70038ad210ed16651f3cfe0a5f99174a23df41e9 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Thu, 13 Oct 2022 12:27:48 -0300 Subject: [PATCH 09/14] Update unit test dispatcher --- tests/dispatcher-test.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index f064fa0..8f6153a 100644 --- a/tests/dispatcher-test.js +++ b/tests/dispatcher-test.js @@ -484,16 +484,33 @@ describe('Dispatcher', function() { }, 200); }); + extraProcess = api => { + assert.deepStrictEqual(api.data, { + foo: 'foo', + bar: ['bar'], + baz: [{ test: 'test', numField: 123 }], + nullField: null + }); + }; + it('should return code 200 when api validates correctly & apply trim to data', async function() { + + extraProcess = api => { + assert.deepStrictEqual(api.data, { + foo: 'foo', + bar: ['bar'], + baz: [{ test: 'test', numField: 123 }], + nullField: null + }); + }; + await test({ - endpoint: 'api/validate-correctly-endpoint', + endpoint: 'api/valid-endpoint', data: { - foo: { - a: ' bar ', - b: { - c: ' test ' - } - } + foo: ' foo ', + bar: [' bar '], + baz: [{ test: 'test ', numField: 123 }], + nullField: null } }, 200); }); From 567a594aad2bf0f1ac7be2d872edab01125f55ba Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Wed, 26 Oct 2022 11:58:47 -0300 Subject: [PATCH 10/14] change priotineData --- lib/dispatcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 480e3d6..83d7062 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -37,7 +37,7 @@ module.exports = class Dispatcher { constructor(request) { this._validateRequest(request); const trimData = trimObjectValues({ ...request.data }); - const pristineData = cloneObj({ ...request.data }); + const pristineData = request.data && cloneObj(request.data); this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); this.data = trimData; From 224634a4d7a90d52f7c60b87f794e61dc6b13fc3 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Fri, 4 Nov 2022 13:13:18 -0300 Subject: [PATCH 11/14] Changes seen in PR --- lib/dispatcher.js | 6 ++---- lib/utils.js | 20 +++++++++++--------- tests/dispatcher-test.js | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 83d7062..c7721b4 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -36,12 +36,10 @@ module.exports = class Dispatcher { */ constructor(request) { this._validateRequest(request); - const trimData = trimObjectValues({ ...request.data }); - const pristineData = request.data && cloneObj(request.data); this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); - this.data = trimData; - this.pristineData = request.data && pristineData; + this.data = trimObjectValues(request.data && cloneObj(request.data)); + this.pristineData = request.data && cloneObj(request.data); Object.freeze(this.pristineData); this.rawData = request.rawData; this.headers = request.headers || {}; diff --git a/lib/utils.js b/lib/utils.js index d69e104..a2abf17 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,19 +30,21 @@ const omitRecursive = (object, exclude) => { return omit(object, exclude); }; -const trimObjectValues = object => { +const trimObjectValues = value => { - if(typeof object !== 'object' || object === null) - return {}; + if(typeof value !== 'object' || !value) + return value; - Object.keys(object).forEach(k => { - if((typeof object[k] === 'string')) - object[k] = object[k].trim(); - else - trimObjectValues(object[k]); + Object.keys(value).forEach(k => { + + if((typeof value[k] === 'string')) + value[k] = value[k].trim(); + + if(typeof value[k] === 'object' && value[k] !== null) + value[k] = trimObjectValues(value[k]); }); - return object; + return value; }; module.exports = { diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index 8f6153a..85dbb8b 100644 --- a/tests/dispatcher-test.js +++ b/tests/dispatcher-test.js @@ -433,7 +433,7 @@ describe('Dispatcher', function() { }), { statusCode: 400, body: { - message: 'Expected a value of type `string` for `foo` but received `undefined`.' + message: 'Expected a value of type `{foo}` but received `undefined`.' } }); From da8ae1c56921645774043988c6f45da3d619dae2 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Mon, 7 Nov 2022 14:52:11 -0300 Subject: [PATCH 12/14] Update github actions --- .github/workflows/build-status.yml | 4 ++-- .github/workflows/coverage-status.yml | 4 ++-- .github/workflows/npm-publish.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-status.yml b/.github/workflows/build-status.yml index f5926dd..412f806 100644 --- a/.github/workflows/build-status.yml +++ b/.github/workflows/build-status.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: - node-version: [12.x, 14.x] + node-version: [14.x] steps: - uses: actions/checkout@v2 @@ -21,4 +21,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: npm i - run: npm run build-types - - run: npm test + - run: npm test \ No newline at end of file diff --git a/.github/workflows/coverage-status.yml b/.github/workflows/coverage-status.yml index b961b9a..592c264 100644 --- a/.github/workflows/coverage-status.yml +++ b/.github/workflows/coverage-status.yml @@ -11,10 +11,10 @@ jobs: - uses: actions/checkout@v1 - - name: Use Node.js 12.x + - name: Use Node.js 14.x uses: actions/setup-node@v1 with: - node-version: 12.x + node-version: 14.x - name: npm install, make test-coverage run: | diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 7ddcd2b..043d5c3 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: - node-version: 12 + node-version: 14 - run: npm i - run: npm test @@ -22,7 +22,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: - node-version: 12 + node-version: 14 registry-url: https://registry.npmjs.org/ - run: npm i - run: npm run build-types From 5c3bcb922d07bbb7ea93fef7d6fef0b7b2c829e3 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Tue, 15 Nov 2022 16:53:20 -0300 Subject: [PATCH 13/14] Update utils and change pristine data --- lib/dispatcher.js | 5 +++-- lib/utils.js | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index c7721b4..ebff0a5 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -5,8 +5,9 @@ const { ApiSession } = require('@janiscommerce/api-session'); const Events = require('@janiscommerce/events'); +const cloneObj = require('lodash.clonedeep'); const Fetcher = require('./fetcher'); -const { isObject, trimObjectValues, cloneObj } = require('./utils'); +const { isObject, trimObjectValues } = require('./utils'); const API = require('./api'); const APIError = require('./error'); @@ -39,7 +40,7 @@ module.exports = class Dispatcher { this.endpoint = request.endpoint.replace(/^\/?(api\/)?/i, ''); this.method = (request.method || 'get').toLowerCase(); this.data = trimObjectValues(request.data && cloneObj(request.data)); - this.pristineData = request.data && cloneObj(request.data); + this.pristineData = request.data; Object.freeze(this.pristineData); this.rawData = request.rawData; this.headers = request.headers || {}; diff --git a/lib/utils.js b/lib/utils.js index a2abf17..5047ed5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,7 +1,6 @@ 'use strict'; const omit = require('lodash.omit'); -const cloneObj = require('lodash.clonedeep'); /** * Determines if the passed value is an object. @@ -50,6 +49,5 @@ const trimObjectValues = value => { module.exports = { isObject, omitRecursive, - trimObjectValues, - cloneObj + trimObjectValues }; From 3209d50e6b0a287447d0b90c1a9423088feaa0b6 Mon Sep 17 00:00:00 2001 From: Juan Musto Date: Fri, 18 Nov 2022 15:02:36 -0300 Subject: [PATCH 14/14] Remove duplicate function 'extraProcess' in dispatcher-test --- tests/dispatcher-test.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index 85dbb8b..5fe1320 100644 --- a/tests/dispatcher-test.js +++ b/tests/dispatcher-test.js @@ -484,15 +484,6 @@ describe('Dispatcher', function() { }, 200); }); - extraProcess = api => { - assert.deepStrictEqual(api.data, { - foo: 'foo', - bar: ['bar'], - baz: [{ test: 'test', numField: 123 }], - nullField: null - }); - }; - it('should return code 200 when api validates correctly & apply trim to data', async function() { extraProcess = api => {