From c325a3cf81f5d4e49f790e52002286aefd46bbcf Mon Sep 17 00:00:00 2001 From: David Luecke Date: Sun, 19 Nov 2017 17:13:28 -0800 Subject: [PATCH] Add a toJSON method to the hook context (#63) --- packages/commons/lib/hooks.js | 9 ++++++++- packages/commons/lib/utils.js | 4 +++- packages/commons/test/hooks.test.js | 18 ++++++++++++++++++ packages/commons/test/module.test.js | 2 +- packages/commons/test/utils.test.js | 11 +++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/commons/lib/hooks.js b/packages/commons/lib/hooks.js index a77bcc182..a0a44db13 100644 --- a/packages/commons/lib/hooks.js +++ b/packages/commons/lib/hooks.js @@ -1,4 +1,4 @@ -const { each } = require('./utils')._; +const { each, pick } = require('./utils')._; function convertGetOrRemove (args) { const [ id, params = {} ] = args; @@ -35,6 +35,13 @@ exports.converters = { exports.createHookObject = function createHookObject (method, args, data = {}) { const hook = exports.converters[method](args); + Object.defineProperty(hook, 'toJSON', { + value () { + return pick(this, 'type', 'method', 'path', + 'params', 'id', 'data', 'result', 'error'); + } + }); + return Object.assign(hook, data, { method, // A dynamic getter that returns the path of the service diff --git a/packages/commons/lib/utils.js b/packages/commons/lib/utils.js index 29c906e16..ea1799806 100644 --- a/packages/commons/lib/utils.js +++ b/packages/commons/lib/utils.js @@ -58,7 +58,9 @@ const _ = exports._ = { pick (source, ...keys) { const result = {}; keys.forEach(key => { - result[key] = source[key]; + if (source[key] !== undefined) { + result[key] = source[key]; + } }); return result; }, diff --git a/packages/commons/test/hooks.test.js b/packages/commons/test/hooks.test.js index ce6437b6a..339e6d72b 100644 --- a/packages/commons/test/hooks.test.js +++ b/packages/commons/test/hooks.test.js @@ -134,6 +134,24 @@ describe('hook utilities', () => { }; const hookData = { app, service }; + it('.toJSON', () => { + let hookObject = utils.createHookObject('find', [ + { some: 'thing' } + ], hookData); + + expect(hookObject.toJSON()).to.deep.equal({ + params: { some: 'thing' }, + method: 'find', + path: 'testing' + }); + + expect(JSON.stringify(hookObject)).to.equal(JSON.stringify({ + method: 'find', + path: 'testing', + params: { some: 'thing' } + })); + }); + it('for find', () => { let hookObject = utils.createHookObject('find', [ { some: 'thing' } diff --git a/packages/commons/test/module.test.js b/packages/commons/test/module.test.js index 07fa47ac4..9bca88da9 100644 --- a/packages/commons/test/module.test.js +++ b/packages/commons/test/module.test.js @@ -4,7 +4,7 @@ const { _ } = require('../lib/commons'); describe('module', () => { it('is commonjs compatible', () => { let commons = require('../lib/commons'); - console.log(commons); + expect(typeof commons).to.equal('object'); expect(typeof commons.stripSlashes).to.equal('function'); expect(typeof commons.sorter).to.equal('function'); diff --git a/packages/commons/test/utils.test.js b/packages/commons/test/utils.test.js index 45f36aac0..737d673ff 100644 --- a/packages/commons/test/utils.test.js +++ b/packages/commons/test/utils.test.js @@ -46,6 +46,10 @@ describe('@feathersjs/commons utils', () => { expect(key).to.equal(0); expect(value).to.equal('hi'); }); + + _.each('moo', value => expect(false) + .to.equal(true, 'Should never get here') + ); }); it('some', () => { @@ -113,6 +117,13 @@ describe('@feathersjs/commons utils', () => { first: 1, second: 2 }); + + expect(_.pick({ + name: 'David', + first: 1 + }, 'first', 'second')).to.deep.equal({ + first: 1 + }); }); it('merge', () => {