FFFF Allow flag to return the hook object by daffl · Pull Request #607 · 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
10 changes: 7 additions & 3 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export function hookMixin (service) {
});
})
// Make a copy of hookObject from `before` hooks and update type
.then(hookObject => Object.assign({}, hookObject, { type: 'after' }))
.then(hookObject =>
Object.assign({}, hookObject, { type: 'after' })
)
// Run through all `after` hooks
.then(hookObject => {
const afterHooks = getHooks(app, service, 'after', method, true);
Expand All @@ -76,8 +78,10 @@ export function hookMixin (service) {

return processHooks.call(service, hookChain, hookObject);
})
// Finally, return the result
.then(hookObject => hookObject.result)
// Finally, return the result (or the hook object if a hidden flag is set)
.then(hookObject =>
hookObject.params.__returnHook ? hookObject : hookObject.result
)
// Handle errors
.catch(error => {
const errorHooks = getHooks(app, service, 'error', method, true);
Expand Down
67 changes: 46 additions & 21 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ describe('Feathers application', () => {
});
});

it('providers are getting called with a service', () => {
const app = feathers();
let providerRan = false;

app.providers.push(function (service, location, options) {
assert.ok(service.dummy);
assert.equal(location, 'dummy');
assert.deepEqual(options, {});
providerRan = true;
});

app.use('/dummy', {
dummy: true,
get () {}
});

assert.ok(providerRan);

app.setup();
});

describe('Services', () => {
it('calling .use with a non service object throws', () => {
const app = feathers();
Expand Down Expand Up @@ -285,4 +264,50 @@ describe('Feathers application', () => {
assert.ok(_setup);
});
});

describe('providers', () => {
it('are getting called with a service', () => {
const app = feathers();
let providerRan = false;

app.providers.push(function (service, location, options) {
assert.ok(service.dummy);
assert.equal(location, 'dummy');
assert.deepEqual(options, {});
providerRan = true;
});

app.use('/dummy', {
dummy: true,
get () {}
});

assert.ok(providerRan);

app.setup();
});

it('are getting called with a service and options', () => {
const app = feathers();
const opts = { test: true };

let providerRan = false;

app.providers.push(function (service, location, options) {
assert.ok(service.dummy);
assert.equal(location, 'dummy');
assert.deepEqual(options, opts);
8000 providerRan = true;
});

app.use('/dummy', {
dummy: true,
get () {}
}, opts);

assert.ok(providerRan);

app.setup();
});
});
});
21 changes: 21 additions & 0 deletions test/hooks/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,25 @@ describe('hooks basics', () => {
assert.equal(e.message, `Service method 'get' for 'dummy' service must return a promise`);
});
});

it('allows to return the hook object', () => {
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 => {
assert.equal(context.service, app.service('dummy'));
assert.equal(context.type, 'after');
assert.equal(context. 499C path, 'dummy');
assert.deepEqual(context.result, {
id: 10,
params
});
});
});
});
0