Interceptors
Interceptors are a powerful mechanism that can monitor and rewrite HTTP request before and after sending requests.
caution
Interceptors should only modify request/responce and return modified argument. It is consider rule. Please pay attantions while throwing errors inside interceptor it could lead to unpredictable bugs.
Intercept request
You can intercept request before fetch
In most cases there will not be onError interceptor while intercepting request. This situation could be only if you have some bugs in your own onSuccess interceptor.
The most popular usecase for request interceptor is to pass authorisation header.
Mostly if you are using Redux it is better to create middleware for setting up Authorization header.
Intercept responce
You can intercept request after sending Request
The most popular usecase for responce interceptor is to clear Rudux store on 401 responce status code. And the best place to do that is to create middleware
delete interceptor
Functions response.use and request.use returns functoin to delite this interceptor.