From 7a71911f69cd1059c471b9962be18fcc3ad75b23 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 30 Mar 2021 10:31:42 -0700 Subject: [PATCH 1/2] fix(feathers): Always enable hooks on default service methods even when not passed in options --- packages/feathers/src/hooks/index.ts | 8 ++++++-- packages/feathers/test/hooks/hooks.test.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/feathers/src/hooks/index.ts b/packages/feathers/src/hooks/index.ts index b55e8db1be..5f82686b93 100644 --- a/packages/feathers/src/hooks/index.ts +++ b/packages/feathers/src/hooks/index.ts @@ -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, defaultServiceMethods } from '../service'; import { collectLegacyHooks, enableLegacyHooks, @@ -59,7 +59,11 @@ export function hookMixin ( } const app = this; - const serviceMethodHooks = options.methods.reduce((res, method) => { + const { methods } = options; + const serviceMethods = defaultServiceMethods.filter(m => + typeof (service as any)[m] === 'function' && !methods.includes(m) + ).concat(methods); + const serviceMethodHooks = serviceMethods.reduce((res, method) => { const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ]; res[method] = new FeathersHookManager(app, method) diff --git a/packages/feathers/test/hooks/hooks.test.ts b/packages/feathers/test/hooks/hooks.test.ts index dc57174cea..a6ecd6fb9b 100644 --- a/packages/feathers/test/hooks/hooks.test.ts +++ b/packages/feathers/test/hooks/hooks.test.ts @@ -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; } @@ -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({ From ed8617bc8d62d8598251062da113defb8b36c970 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 30 Mar 2021 20:18:59 -0700 Subject: [PATCH 2/2] Refactoring --- packages/feathers/src/hooks/index.ts | 8 ++------ packages/feathers/src/service.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/feathers/src/hooks/index.ts b/packages/feathers/src/hooks/index.ts index 5f82686b93..d8be284c86 100644 --- a/packages/feathers/src/hooks/index.ts +++ b/packages/feathers/src/hooks/index.ts @@ -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, defaultServiceMethods } from '../service'; +import { defaultServiceArguments, getHookMethods } from '../service'; import { collectLegacyHooks, enableLegacyHooks, @@ -59,11 +59,7 @@ export function hookMixin ( } const app = this; - const { methods } = options; - const serviceMethods = defaultServiceMethods.filter(m => - typeof (service as any)[m] === 'function' && !methods.includes(m) - ).concat(methods); - const serviceMethodHooks = serviceMethods.reduce((res, method) => { + const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => { const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ]; res[method] = new FeathersHookManager(app, method) diff --git a/packages/feathers/src/service.ts b/packages/feathers/src/service.ts index d026edfcdd..f6ab028c2d 100644 --- a/packages/feathers/src/service.ts +++ b/packages/feathers/src/service.ts @@ -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 {