You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And it should be importable with webpack out of the box
20
20
21
21
# Usage
22
+
22
23
## Set up InterceptorService
24
+
23
25
Interceptors are registered when the service is created (to avoid any race-condition). To do so, you have to provide the instance of the service by yourself. So on your module declaration, you should put a provider like:
24
26
25
27
```ts
@@ -53,7 +55,9 @@ export function interceptorFactory(xhrBackend: XHRBackend, requestOptions: Reque
53
55
```
54
56
55
57
## Using InterceptorService
58
+
56
59
Once we have it set up, we can use it in our Controllers as if we were using the default Angular `Http` service:
We can also "cheat" the Injector so that every time we ask for the `Http` we get the `InterceptorService` instead. All we have to do is replace `InterceptorService` on the provider definition for `Http`, and then we can get our service when we use `private http: Http`:
86
+
82
87
```ts
83
88
{
84
89
provide: Http,
@@ -88,13 +93,14 @@ We can also "cheat" the Injector so that every time we ask for the `Http` we get
88
93
```
89
94
90
95
## Creating your own Interceptor
96
+
91
97
Basically, an interceptor has the option to selectively implement one more of the following methods depending on what part of the flow it wants to intercept. i.e modify flow/take action.
92
98
93
99
Here is the interceptor interface that details which method is invoked in what part of the flow
One that will get the request that's about to be sent to the server, and another that will get the response that the server just sent. For that, we just need to create a new class that implements Interceptor:
// Do whatever with request, such as chaing request by adding additional headers such as `Content-Type` (or) `Authorization`
192
-
// refer to jsdoc of each of 'InterceptorRequest' to know the significance of return values & additional features such as short circuiting the whole flow
193
-
let modifiedOptions:RequestOptionsArgs=addHeaders(request.options);
// Do whatever with responseWrapper: get/edit response
201
-
// refer to jsdoc of each of 'InterceptorResponseWrapper' to know the significance of return values & additional features such as short circuiting the whole flow
// Do whatever with request, such as chaing request by adding additional headers such as `Content-Type` (or) `Authorization`
217
+
// refer to jsdoc of each of 'InterceptorRequest' to know the significance of return values & additional features such as short circuiting the whole flow
218
+
let modifiedOptions:RequestOptionsArgs=addHeaders(request.options);
// Do whatever with responseWrapper: get/edit response
232
+
// refer to jsdoc of each of 'InterceptorResponseWrapper' to know the significance of return values & additional features such as short circuiting the whole flow
// can chose to seletively implement any of the four hooks mentioned in the Interceptor interface
239
+
// can chose to seletively implement any of the four hooks mentioned in the Interceptor interface
209
240
}
210
241
```
211
242
@@ -234,23 +265,27 @@ class InterceptedResponseWrapper {
234
265
// a lot more properties; refer to jsdoc
235
266
}
236
267
```
268
+
237
269
`sharedData` on `InterceptorRequest` is guaranteed to be the same of that one of `InterceptedResponseWrapper` for the same call: The stuff you put in `interceptorOptions` while in `interceptBefore` will be available when you get `onResponse(..)` called.
238
270
239
271
## Creating one Injectable Interceptor
272
+
240
273
Interceptors are usually pure classes with pure functions: Given a call, they return a modified one, but sometimes we need these Interceptors to be actual Services to be used all around our application.
241
274
242
275
For instance, an interceptor that shows a loading spinner every time we have a call has -in some way- to comunicate with the `LoadingComponent` to make the spinner appear/disappear from the screen.
243
276
244
277
To do that you have to do some steps in the module/factory declaration file:
245
-
1. Create a Service (`@Injectable()` annotation) that implements `Interceptor` and the interceptor methods.
246
-
2. Define his provider before `InterceptorService`
247
-
3. Add it as a parameter to the factory function
248
-
4. Add it to the `deps` array. Note that the order of the elements have to match the one on the factory function.
249
-
5. Add it to the pipeline
278
+
279
+
1. Create a Service (`@Injectable()` annotation) that implements `Interceptor` and the interceptor methods.
280
+
2. Define his provider before `InterceptorService`
281
+
3. Add it as a parameter to the factory function
282
+
4. Add it to the `deps` array. Note that the order of the elements have to match the one on the factory function.
283
+
5. Add it to the pipeline
250
284
251
285
If you are using the `provideInterceptorService` option (without AoT Compiler support), then you can skip steps 2-4.
252
286
253
287
If our `ServerURLInterceptor` were a Service, we would have a module declaration like:
0 commit comments