Skip to content

Commit

Permalink
fix(feathers): Always enable hooks on default service methods (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Mar 31, 2021
1 parent 879bd6b commit 827cc9b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/feathers/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getManager, HookContextData, HookManager, HookMap, HOOKS, hooks, Middleware } from '@feathersjs/hooks';
import { Service, ServiceOptions, HookContext, FeathersService, Application } from '../declarations';
import { defaultServiceArguments } from '../service';
import { defaultServiceArguments, getHookMethods } from '../service';
import {
collectLegacyHooks,
enableLegacyHooks,
Expand Down Expand Up @@ -59,7 +59,7 @@ export function hookMixin<A> (
}

const app = this;
const serviceMethodHooks = options.methods.reduce((res, method) => {
const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => {
const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];

res[method] = new FeathersHookManager<A>(app, method)
Expand Down
8 changes: 8 additions & 0 deletions packages/feathers/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export const defaultEventMap = {
remove: 'removed'
}

export function getHookMethods (service: any, options: ServiceOptions) {
const { methods } = options;

return defaultServiceMethods.filter(m =>
typeof service[m] === 'function' && !methods.includes(m)
).concat(methods);
}

export function getServiceOptions (
service: any, options: ServiceOptions = {}
): ServiceOptions {
Expand Down
11 changes: 8 additions & 3 deletions packages/feathers/test/hooks/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,16 @@ describe('hooks basics', () => {
});
});

it('can register hooks on a custom method', async () => {
it('can register hooks on a custom method, still adds hooks to default methods', async () => {
class Dummy {
async get (id: Id) {
return { id };
}

async create (data: any) {
return data;
}

async custom (data: any) {
return data;
}
Expand All @@ -322,9 +326,10 @@ describe('hooks basics', () => {

app.service('dummy').hooks({
custom: [async (context, next) => {
(context.data as any).fromHook = true;
context.data.fromHook = true;
await next();
}]
}],
create: [async (_context, next) => next()]
});

assert.deepStrictEqual(await app.service('dummy').custom({
Expand Down

0 comments on commit 827cc9b

Please # to comment.