8000 Better logic for returning the hook object from method call by daffl · Pull Request #706 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
11 changes: 8 additions & 3 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {

mixin[method] = function () {
const service = this;
const args = arguments;
const args = Array.from(arguments);
// If the last argument is `true` we want to return
// the actual hook object instead of the result
const returnHook = args[args.length - 1] === true
? args.pop() : false;

// We have to try/catch this so that argument validation
// returns a rejected promise
Expand All @@ -44,6 +48,7 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
// Create the hook object that gets passed through
const hookObject = createHookObject(method, args, {
type: 'before', // initial hook object type
returnHook,
service,
app
});
Expand Down Expand Up @@ -85,7 +90,7 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
.then(hookObject =>
// Finally, return the result
// Or the hook object if the `__returnHook` flag is set
hookObject.params.__returnHook ? hookObject : hookObject.result
hookObject.returnHook ? hookObject : hookObject.result
)
// Handle errors
.catch(error => {
Expand All @@ -105,7 +110,7 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
return processHooks
.call(service, hookChain, errorHookObject)
.then(hook => {
if (errorHookObject.params.__returnHook) {
if (errorHookObject.returnHook) {
// Return either the complete hook if the `__returnHook` flag is set
return Promise.reject(hook);
} else if (hook.result) {
Expand Down
14 changes: 4 additions & 10 deletions test/hooks/hooks.test.js
8000
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,21 @@ describe('hooks basics', () => {
});
});

describe('returns the hook object with __returnHook', () => {
describe('returns the hook object when passing true as last parameter', () => {
it('on normal method call', () => {
const app = feathers().use('/dummy', {
get (id, params) {
return Promise.resolve({ id, params });
}
});
const params = {
__returnHook: true
};

return app.service('dummy').get(10, params).then(context => {
return app.service('dummy').get(10, {}, true).then(context => {
assert.equal(context.service, app.service('dummy'));
assert.equal(context.type, 'after');
assert.equal(context.path, 'dummy');
assert.deepEqual(context.result, {
id: 10,
params
params: {}
});
});
});
Expand All @@ -176,11 +173,8 @@ describe('hooks basics', () => {
return Promise.reject(new Error('Something went wrong'));
}
});
const params = {
__returnHook: true
};

return app.service('dummy').get(10, params).catch(context => {
return app.service('dummy').get(10, {}, true).catch(context => {
assert.equal(context.service, app.service('dummy'));
assert.equal(context.type, 'error');
assert.equal(context.path, 'dummy');
Expand Down
0