1- const { hooks, isPromise, _ } = require ( '@feathersjs/commons' ) ;
1+ const { hooks, isPromise } = require ( '@feathersjs/commons' ) ;
22const baseHooks = require ( './base' ) ;
33
44const {
99 ACTIVATE_HOOKS
1010} = hooks ;
1111
12- const makeArguments = ( service , method , hookObject ) => service . methods [ method ] . reduce ( ( result , value ) => ( [
13- ...result ,
14- hookObject [ value ]
15- ] ) , [ ] ) ;
16-
1712const withHooks = function withHooks ( {
1813 app,
1914 service,
@@ -33,8 +28,6 @@ const withHooks = function withHooks ({
3328 const returnHook = args [ args . length - 1 ] === true
3429 ? args . pop ( ) : false ;
3530
36- // A reference to the original method
37- const _super = original || service [ method ] . bind ( service ) ;
3831 // Create the hook object that gets passed through
3932 const hookObject = createHookObject ( method , {
4033 type : 'before' , // initial hook object type
@@ -43,70 +36,93 @@ const withHooks = function withHooks ({
4336 app
4437 } ) ;
4538
46- // Process all before hooks
47- return processHooks . call ( service , baseHooks . concat ( hooks . before ) , hookObject )
48- // Use the hook object to call the original method
39+ return Promise . resolve ( hookObject )
40+
41+ // Run `before` hooks
42+ . then ( hookObject => {
43+ return processHooks . call ( service , baseHooks . concat ( hooks . before ) , hookObject ) ;
44+ } )
45+
46+ // Run the original method
4947 . then ( hookObject => {
5048 // If `hookObject.result` is set, skip the original method
5149 if ( typeof hookObject . result !== 'undefined' ) {
5250 return hookObject ;
5351 }
5452
5553 // Otherwise, call it with arguments created from the hook object
56- const promise = _super ( ...makeArguments ( service , method , hookObject ) ) ;
57-
58- if ( ! isPromise ( promise ) ) {
59- throw new Error ( `Service method '${ hookObject . method } ' for '${ hookObject . path } ' service must return a promise` ) ;
60- }
54+ const promise = new Promise ( resolve => {
55+ const func = original || service [ method ] ;
56+ const args = service . methods [ method ] . map ( ( value ) => hookObject [ value ] ) ;
57+ const result = func . apply ( service , args ) ;
6158
62- return promise . then ( result => {
63- hookObject . result = result ;
59+ if ( ! isPromise ( result ) ) {
60+ throw new Error ( `Service method '${ hookObject . method } ' for '${ hookObject . path } ' service must return a promise` ) ;
61+ }
6462
65- return hookObject ;
63+ resolve ( result ) ;
6664 } ) ;
65+
66+ return promise
67+ . then ( result => {
68+ hookObject . result = result ;
69+ return hookObject ;
70+ } )
71+ . catch ( error => {
72+ error . hook = hookObject ;
73+ throw error ;
74+ } ) ;
6775 } )
68- // Make a (shallow) copy of hookObject from `before` hooks and update type
69- . then ( hookObject => Object . assign ( { } , hookObject , { type : 'after' } ) )
70- // Run through all `after` hooks
76+
77+ // Run `after` hooks
7178 . then ( hookObject => {
72- // Combine all app and service `after` and `finally` hooks and process
73- const hookChain = hooks . after . concat ( hooks . finally ) ;
79+ const afterHookObject = Object . assign ( { } , hookObject , {
80+ type : 'after'
81+ } ) ;
7482
75- return processHooks . call ( service , hookChain , hookObject ) ;
83+ return processHooks . call ( service , hooks . after , afterHookObject ) ;
7684 } )
77- . then ( hookObject =>
78- // Finally, return the result
79- // Or the hook object if the `returnHook` flag is set
80- returnHook ? hookObject : hookObject . result
81- )
82- // Handle errors
83- . catch ( error => {
84- // Combine all app and service `error` and `finally` hooks and process
85- const hookChain = hooks . error . concat ( hooks . finally ) ;
8685
87- // A shallow copy of the hook object
88- const errorHookObject = _ . omit ( Object . assign ( { } , error . hook , hookObject , {
86+ // Run `errors` hooks
87+ . catch ( error => {
88+ const errorHookObject = Object . assign ( { } , error . hook , {
8989 type : 'error' ,
9090 original : error . hook ,
91- error
92- } ) , 'result' ) ;
91+ error,
92+ result : undefined
93+ } ) ;
9394
94- return processHooks . call ( service , hookChain , errorHookObject )
95+ return processHooks . call ( service , hooks . error , errorHookObject )
9596 . catch ( error => {
96- errorHookObject . error = error ;
97+ const errorHookObject = Object . assign ( { } , error . hook , {
98+ error,
99+ result : undefined
100+ } ) ;
101+
102+ return errorHookObject ;
103+ } ) ;
104+ } )
105+
106+ // Run `finally` hooks
107+ . then ( hookObject => {
108+ return processHooks . call ( service , hooks . finally , hookObject )
109+ . catch ( error => {
110+ const errorHookObject = Object . assign ( { } , error . hook , {
111+ error,
112+ result : undefined
113+ } ) ;
97114
98115 return errorHookObject ;
99- } )
100- . then ( hook => {
101- if ( returnHook ) {
102- // Either resolve or reject with the hook object
103- return typeof hook . result !== 'undefined' ? hook : Promise . reject ( hook ) ;
104- }
105-
106- // Otherwise return either the result if set (to swallow errors)
107- // Or reject with the hook error
108- return typeof hook . result !== 'undefined' ? hook . result : Promise . reject ( hook . error ) ;
109116 } ) ;
117+ } )
118+
119+ // Resolve with a result or reject with an error
120+ . then ( hookObject => {
121+ if ( typeof hookObject . error !== 'undefined' && typeof hookObject . result === 'undefined' ) {
122+ return Promise . reject ( returnHook ? hookObject : hookObject . error ) ;
123+ } else {
124+ return returnHook ? hookObject : hookObject . result ;
125+ }
110126 } ) ;
111127 } ;
112128 } ;
0 commit comments