Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/build-status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [14.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -21,4 +21,4 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm i
- run: npm run build-types
- run: npm test
- run: npm test
4 changes: 2 additions & 2 deletions .github/workflows/coverage-status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Comment thread
Alejandro-Gonzalez marked this conversation as resolved.
const API = require('./api');
const APIError = require('./error');
Expand Down Expand Up @@ -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 || {};
Expand Down
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ const omitRecursive = (object, exclude) => {
return omit(object, exclude);
};

const trimObjectValues = value => {

Comment thread
Alejandro-Gonzalez marked this conversation as resolved.
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;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esta recursiva esta mal, porque el return {} adultera datos, ademas en el medio se pierden datos y por ultimo entra en casos donde no es necesario, por ejemplo el el value del k es true entra a la recursiva y eso no esta ok.


module.exports = {
isObject,
omitRecursive
omitRecursive,
trimObjectValues
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
24 changes: 23 additions & 1 deletion tests/dispatcher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.'
}
});

Expand Down Expand Up @@ -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({
Comment thread
Alejandro-Gonzalez marked this conversation as resolved.
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',
Expand Down