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 diff --git a/lib/dispatcher.js b/lib/dispatcher.js index c432ac0..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 } = require('./utils'); +const { isObject, trimObjectValues } = require('./utils'); const API = require('./api'); const APIError = require('./error'); @@ -36,11 +37,10 @@ module.exports = class Dispatcher { */ constructor(request) { this._validateRequest(request); - 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 = trimObjectValues(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 62ce001..5047ed5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -29,7 +29,25 @@ const omitRecursive = (object, exclude) => { return omit(object, exclude); }; +const trimObjectValues = value => { + + if(typeof value !== 'object' || !value) + return value; + + 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 value; +}; + module.exports = { isObject, - omitRecursive + omitRecursive, + trimObjectValues }; 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" } diff --git a/tests/dispatcher-test.js b/tests/dispatcher-test.js index 015ddea..5fe1320 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`.' } }); @@ -484,6 +484,28 @@ describe('Dispatcher', function() { }, 200); }); + 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/valid-endpoint', + data: { + foo: ' foo ', + bar: [' bar '], + baz: [{ test: 'test ', numField: 123 }], + nullField: null + } + }, 200); + }); + it('should return code 200 when api validates correctly the struct', async function() { await test({ endpoint: 'api/struct-endpoint',