Skip to content

Commit 8cec864

Browse files
authored
Fix omit for nested objects (#46)
* Fix omit for nested objects We’re using the dedicated lodash.omit package from npm to reduce our bundle size. But it looks like the smaller npm modules are out of date: https://github.com/lodash/lodash/issues/4254 lodash/lodash#4193 lodash/lodash#4030 Can confirm this works when using the full [email protected] module, and not the [email protected] module. ``` const omit = require('lodash.omit'); omit({ a: { b: { c: 1 } } }, ['a.b.c']) { a: { b: { c: 1 } } } const omit = require('lodash/omit'); omit({ a: { b: { c: 1 } } }, ['a.b.c']) { a: { b: {} } } ``` * Remove individual lodash packages and include entire lodash library. * Add test for blacklisting/whitelisting nested properties in response
1 parent a8a51e7 commit 8cec864

6 files changed

+46
-18
lines changed

lib/process-request.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const url = require('url');
2-
const removeProperties = require('lodash.omit');
3-
const removeOtherProperties = require('lodash.pick');
2+
const removeProperties = require('lodash/omit');
3+
const removeOtherProperties = require('lodash/pick');
44

55
const objectToArray = require('./object-to-array');
66

lib/process-response.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const removeProperties = require('lodash.omit');
2-
const removeOtherProperties = require('lodash.pick');
1+
const removeProperties = require('lodash/omit');
2+
const removeOtherProperties = require('lodash/pick');
33

44
const objectToArray = require('./object-to-array');
55

package-lock.json

+1-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"dependencies": {
99
"configly": "^4.1.0",
1010
"jsonwebtoken": "^8.3.0",
11-
"lodash.omit": "^4.5.0",
12-
"lodash.pick": "^4.4.0",
11+
"lodash": "^4.17.15",
1312
"node-uuid": "^1.4.8",
1413
"r2": "^2.0.1"
1514
},

test/process-request.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ describe('processRequest()', () => {
3434
});
3535
});
3636

37+
it('should strip blacklisted nested properties', () => {
38+
const app = createApp({ blacklist: ['a.b.c'] });
39+
40+
return request(app)
41+
.post('/')
42+
.send({ a: { b: { c: 1 } } })
43+
.expect(({ body }) => {
44+
expect(body.postData.params).toStrictEqual([{ name: 'a', value: { b: {} } }]);
45+
});
46+
});
47+
3748
it('should only send whitelisted properties', () => {
3849
const app = createApp({ whitelist: ['password', 'apiKey'] });
3950

@@ -47,6 +58,17 @@ describe('processRequest()', () => {
4758
]);
4859
});
4960
});
61+
62+
it('should only send whitelisted nested properties', () => {
63+
const app = createApp({ whitelist: ['a.b.c'] });
64+
65+
return request(app)
66+
.post('/')
67+
.send({ a: { b: { c: 1 } }, d: 2 })
68+
.expect(({ body }) => {
69+
expect(body.postData.params).toStrictEqual([{ name: 'a', value: { b: { c: 1 } } }]);
70+
});
71+
});
5072
});
5173

5274
it('#method', () =>

test/process-response.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ describe('processResponse()', () => {
3333
}, JSON.stringify({ password: '123456', apiKey: 'abcdef', another: 'Hello world' }));
3434
});
3535

36+
it('should strip blacklisted nested properties', () => {
37+
expect.hasAssertions();
38+
return testResponse(res => {
39+
expect(processResponse(res, { blacklist: ['a.b.c'] }).content.text).toStrictEqual(
40+
JSON.stringify({ a: { b: {} } })
41+
);
42+
}, JSON.stringify({ a: { b: { c: 1 } } }));
43+
});
44+
3645
it('should only send whitelisted properties', () => {
3746
expect.hasAssertions();
3847
return testResponse(res => {
@@ -42,6 +51,15 @@ describe('processResponse()', () => {
4251
}, JSON.stringify({ password: '123456', apiKey: 'abcdef', another: 'Hello world' }));
4352
});
4453

54+
it('should only send whitelisted nested properties', () => {
55+
expect.hasAssertions();
56+
return testResponse(res => {
57+
expect(processResponse(res, { whitelist: ['a.b.c'] }).content.text).toStrictEqual(
58+
JSON.stringify({ a: { b: { c: 1 } } })
59+
);
60+
}, JSON.stringify({ a: { b: { c: 1 } }, d: 2 }));
61+
});
62+
4563
it('should not be applied for plain text bodies', () => {
4664
expect.hasAssertions();
4765
const body = 'hello world: dasdsas';

0 commit comments

Comments
 (0)