@@ -192,10 +192,9 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
192192 return service
193193 }
194194
195- const app = this
196195 const hookMethods = getHookMethods ( service , options )
197196
198- const createMethodHookManager = ( method : string ) => {
197+ const createMethodHookManager = ( app : A , method : string ) => {
199198 const params = ( defaultServiceArguments as any ) [ method ] || [ 'data' , 'params' ]
200199
201200 return new FeathersHookManager < A > ( app , method ) . params ( ...params ) . props ( {
@@ -216,7 +215,7 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
216215 }
217216
218217 const serviceMethodHooks = hookMethods . reduce ( ( res , method ) => {
219- res [ method ] = createMethodHookManager ( method )
218+ res [ method ] = createMethodHookManager ( this , method )
220219 return res
221220 } , { } as BaseHookMap )
222221
@@ -227,7 +226,18 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
227226
228227 hooks ( service , serviceMethodHooks )
229228
230- service . hooks = function ( this : any , hookOptions : any ) {
229+ service . hooks = createServiceHooksMethod ( this , service , registerHooks , createMethodHookManager )
230+
231+ return service
232+ }
233+
234+ function createServiceHooksMethod < A > (
235+ app : A ,
236+ service : FeathersService < A > ,
237+ registerHooks : ReturnType < typeof enableHooks > ,
238+ createMethodHookManager : ( app : A , method : string ) => FeathersHookManager < A >
239+ ) {
240+ return function ( this : any , hookOptions : any ) {
231241 if ( hookOptions . before || hookOptions . after || hookOptions . error || hookOptions . around ) {
232242 return registerHooks . call ( this , hookOptions )
233243 }
@@ -250,7 +260,7 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
250260 throw new Error ( `Method ${ method } does not exist on this service` )
251261 }
252262
253- const methodManager = createMethodHookManager ( method )
263+ const methodManager = createMethodHookManager ( app , method )
254264 hooks ( this , { [ method ] : methodManager } )
255265 manager = methodManager
256266 }
@@ -260,6 +270,4 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
260270
261271 return this
262272 }
263-
264- return service
265273}
0 commit comments