-
Notifications
You must be signed in to change notification settings - Fork 20
Concept
The idea behind interceptors is to execute some code every time an HTTP request is done. This is mainly needed for Authorization and Authentication methods (like setting the Auth header for an API), but they can be used for anything: Append the server URL on each call, log all request for analytics, provide mock data, or even show a loading spinner when there's some request going on.
The way this implementation is made makes it easy to understand all the interceptors like they are in a pipeline: They will get called in order, first getting the request that's about to be sent (and modifiying the request if needed), and then getting the response (again, modifying it if needed):
Also, each Interceptor is capable of cancel the request, preventing it from reaching the server, and giving back a fake server response. When one interceptor cancels the request, he's inmediately called with a cancelled response and he is able to edit that response to the fake one. All the interceptors next to the one that cancelled the request won't ever see that request, while the interceptors previous will get the fake request back.
This makes it easy to think of each Interceptor of a black box, where each one only has one job. You can then reorder them in the way that makes more sense for your app.