@@ -71,52 +71,79 @@ var todoService = app.lookup('todos');
71
71
72
72
### ` service.before(beforeHooks) `
73
73
74
- ` before ` hooks allow to pre-process service call parameters. They will be called with the original service parameters
75
- and a callback which should be called with the same (potentially modified) parameters and an error (for example, when
76
- a user is not authorized, ` null ` for no error).
74
+ ` before ` hooks allow to pre-process service call parameters. They will be called with the hook object
75
+ and a callback which should be called with any errors or no arguments or ` null ` and the modified hook object.
76
+ The hook object contains information about the intercepted method and for ` before ` hooks can have the following properties:
77
+
78
+ - __ method__ - The method name
79
+ - __ type__ - The hook type (` before ` or ` after ` )
80
+ - __ callback__ - The original callback (can be replaced but shouldn't be called in your hook)
81
+ - __ params__ - The service method parameters
82
+ - __ data__ - The request data (for ` create ` , ` update ` and ` patch ` )
83
+ - __ id__ - The id (for ` get ` , ` remove ` , ` update ` and ` patch ` )
84
+
85
+ All properties of the hook object can be modified and the modified data will be used for the actual service method
86
+ call. This is very helpful for pre-processing parameters and massaging data when creating or updating.
87
+
77
88
The following example checks if a user has been passed to the services ` find ` method and returns an error if not
78
89
and also adds a ` createdAt ` property to a newly created todo:
79
90
80
91
``` js
81
92
todoService .before ({
82
- find : function (params , callback ) {
83
- if (! params .user ) {
93
+ find : function (hook , next ) {
94
+ if (! hook . params .user ) {
84
95
return callback (new Error (' You are not logged in' ));
85
96
}
86
- callback (null , params);
97
+
98
+ next ();
87
99
},
88
100
89
- create : function (data , params , callback ) {
90
- data .createdAt = new Date ();
101
+ create : function (hook , next ) {
102
+ hook . data .createdAt = new Date ();
91
103
92
- callback (null , data, params);
104
+ next ();
105
+ // Or
106
+ next (null , hook);
93
107
}
94
108
});
95
109
```
96
110
97
111
### ` service.after(afterHooks) `
98
112
99
- ` after ` hooks will be called with the result of the service call, the original parameters and a callback. The following example
100
- filters the data returned by a ` find ` service call based on a users company id and checks if the current user is allowed
101
- to retrieve the data returned by ` get ` (that is, they have the same company id):
113
+ ` after ` hooks will be called with a similar hook object than ` before ` hooks but additionally contain a ` result `
114
+ property with the service call results:
115
+
116
+ - __ method__ - The method name
117
+ - __ type__ - The hook type (` before ` or ` after ` )
118
+ - __ result__ - The service call result data
119
+ - __ callback__ - The original callback (can be replaced but shouldn't be called in your hook)
120
+ - __ params__ - The service method parameters
121
+ - __ data__ - The request data (for ` create ` , ` update ` and ` patch ` )
122
+ - __ id__ - The id (for ` get ` , ` remove ` , ` update ` and ` patch ` )
123
+
124
+ In any ` after ` hook, only modifications to the ` result ` object will have any effect. This is a good place to filter or
125
+ post-process the data retrieved by a service and also add some additional authorization that needs the actual data.
126
+
127
+ The following example filters the data returned by a ` find ` service call based on a users company id
128
+ and checks if the current user is allowed to retrieve the data returned by ` get ` (that is, they have the same company id):
102
129
103
130
``` js
104
131
todoService .after ({
105
- find : function (data , params , callback ) {
132
+ find : function (hook , next ) {
106
133
// Manually filter the find results
107
- var filtered = _ .filter (data , function (current ) {
134
+ hook . result = _ .filter (hook . result , function (current ) {
108
135
return current .companyId === params .user .companyId ;
109
136
});
110
137
111
- callback ( null , filtered );
138
+ next ( );
112
139
},
113
140
114
- get : function (data , id , params , callback ) {
115
- if (data . companyId !== params .user .companyId ) {
141
+ get : function (hook , next ) {
142
+ if (hook . result . companyId !== hook . params .user .companyId ) {
116
143
return callback (new Error (' You are not authorized to access this information' ));
117
144
}
118
145
119
- callback ( null , data );
146
+ next ( );
120
147
}
121
148
});
122
149
```
@@ -129,37 +156,39 @@ You can also add `before` and `after` hooks to your initial service object right
129
156
``` js
130
157
var TodoService = {
131
158
before: {
132
- find : function (params , callback ) {
133
- if (! params .user ) {
159
+ find : function (hook , next ) {
160
+ if (! hook . params .user ) {
134
161
return callback (new Error (' You are not logged in' ));
135
162
}
136
- callback (null , params);
163
+
164
+ next ();
137
165
},
138
-
139
- get : function (id , params , callback ) {
140
- // Pre-process the params passed to the actual service
141
- params .something = ' test' ;
142
-
143
- callback (null , id, params);
166
+
167
+ create : function (hook , next ) {
168
+ hook .data .createdAt = new Date ();
169
+
170
+ next ();
171
+ // Or
172
+ next (null , hook);
144
173
}
145
174
},
146
175
147
176
after: {
148
- find : function (data , params , callback ) {
177
+ find : function (hook , next ) {
149
178
// Manually filter the find results
150
- var filtered = _ .filter (data , function (current ) {
179
+ hook . result = _ .filter (hook . result , function (current ) {
151
180
return current .companyId === params .user .companyId ;
152
181
});
153
-
154
- callback ( null , filtered );
182
+
183
+ next ( );
155
184
},
156
-
157
- get : function (data , id , params , callback ) {
158
- if (data . companyId !== params .user .companyId ) {
185
+
186
+ get : function (hook , next ) {
187
+ if (hook . result . companyId !== hook . params .user .companyId ) {
159
188
return callback (new Error (' You are not authorized to access this information' ));
160
189
}
161
-
162
- callback ( null , data );
190
+
191
+ next ( );
163
192
}
164
193
}
165
194
}
0 commit comments