Skip to content

Commit cc0e5f7

Browse files
committed
Allow flag to return the hook object (#607)
1 parent b3c874d commit cc0e5f7

3 files changed

Lines changed: 74 additions & 24 deletions

File tree

src/hooks.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export function hookMixin (service) {
6767
});
6868
})
6969
// Make a copy of hookObject from `before` hooks and update type
70-
.then(hookObject => Object.assign({}, hookObject, { type: 'after' }))
70+
.then(hookObject =>
71+
Object.assign({}, hookObject, { type: 'after' })
72+
)
7173
// Run through all `after` hooks
7274
.then(hookObject => {
7375
const afterHooks = getHooks(app, service, 'after', method, true);
@@ -76,8 +78,10 @@ export function hookMixin (service) {
7678

7779
return processHooks.call(service, hookChain, hookObject);
7880
})
79-
// Finally, return the result
80-
.then(hookObject => hookObject.result)
81+
// Finally, return the result (or the hook object if a hidden flag is set)
82+
.then(hookObject =>
83+
hookObject.params.__returnHook ? hookObject : hookObject.result
84+
)
8185
// Handle errors
8286
.catch(error => {
8387
const errorHooks = getHooks(app, service, 'error', method, true);

test/application.test.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,6 @@ describe('Feathers application', () => {
6464
});
6565
});
6666

67-
it('providers are getting called with a service', () => {
68-
const app = feathers();
69-
let providerRan = false;
70-
71-
app.providers.push(function (service, location, options) {
72-
assert.ok(service.dummy);
73-
assert.equal(location, 'dummy');
74-
assert.deepEqual(options, {});
75-
providerRan = true;
76-
});
77-
78-
app.use('/dummy', {
79-
dummy: true,
80-
get () {}
81-
});
82-
83-
assert.ok(providerRan);
84-
85-
app.setup();
86-
});
87-
8867
describe('Services', () => {
8968
it('calling .use with a non service object throws', () => {
9069
const app = feathers();
@@ -291,4 +270,50 @@ describe('Feathers application', () => {
291270
assert.ok(_setup);
292271
});
293272
});
273+
274+
describe('providers', () => {
275+
it('are getting called with a service', () => {
276+
const app = feathers();
277+
let providerRan = false;
278+
279+
app.providers.push(function (service, location, options) {
280+
assert.ok(service.dummy);
281+
assert.equal(location, 'dummy');
282+
assert.deepEqual(options, {});
283+
providerRan = true;
284+
});
285+
286+
app.use('/dummy', {
287+
dummy: true,
288+
get () {}
289+
});
290+
291+
assert.ok(providerRan);
292+
293+
app.setup();
294+
});
295+
296+
it('are getting called with a service and options', () => {
297+
const app = feathers();
298+
const opts = { test: true };
299+
300+
let providerRan = false;
301+
302+
app.providers.push(function (service, location, options) {
303+
assert.ok(service.dummy);
304+
assert.equal(location, 'dummy');
305+
assert.deepEqual(options, opts);
306+
providerRan = true;
307+
});
308+
309+
app.use('/dummy', {
310+
dummy: true,
311+
get () {}
312+
}, opts);
313+
314+
assert.ok(providerRan);
315+
316+
app.setup();
317+
});
318+
});
294319
});

test/hooks/hooks.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,25 @@ describe('hooks basics', () => {
153153
assert.equal(e.message, `Service method 'get' for 'dummy' service must return a promise`);
154154
});
155155
});
156+
157+
it('allows to return the hook object', () => {
158+
const app = feathers().use('/dummy', {
159+
get (id, params) {
160+
return Promise.resolve({ id, params });
161+
}
162+
});
163+
const params = {
164+
__returnHook: true
165+
};
166+
167+
return app.service('dummy').get(10, params).then(context => {
168+
assert.equal(context.service, app.service('dummy'));
169+
assert.equal(context.type, 'after');
170+
assert.equal(context.path, 'dummy');
171+
assert.deepEqual(context.result, {
172+
id: 10,
173+
params
174+
});
175+
});
176+
});
156177
});

0 commit comments

Comments
 (0)