I roughly figured out how to implement interceptors. But I cannot understand specifically in what cases to use it.
For example, as I understand it, interceptors are used during authorizations to update tokens in the local storage. Good.
But I cannot understand in what cases still they are used and in what cases they cannot be used. Or are they directly used in all requests?
If you are a consumer-side developer, you can use the Interceptor Framework for many different purposes. Some of the most common use cases for interceptors include:
- Handling Errors – You can use interceptors to handle errors returned from a producer. For instance, if a specific producer is not registered, you can trap the registration error and handle it as you wish. You may display an informative message to the user, or you may choose to automatically register the producer. An interceptor can also catch an I/O exception, which can occur if the producer is unavailable. In this case, you might choose to handle the error by displaying an informative message for the user, prevent future requests to the producer, or chose to redirect to another producer.
- Caching Markup – You can implement an interceptor to cache markup returned from a producer. This feature allows you to use any external caching system you choose. In addition, by caching markup on the consumer, you can, in some circumstances, reduce round-trip communication between the consumer and producer.
- Validating Data – You can use interceptors to filter user submitted data. If you detect the user’s data is invalid, you can display an informational message, or you can prevent the data from being sent to the producer.
- Replacing Markup – An interceptor can filter, replace, modify markup data sent from the producer. An interceptor can also modify the navigational state of a remote portlet. For information on navigational state, see Life Cycle of a Remote Portlet.
- Modifying HTTP Headers – Interceptors can add or remove some kinds of HTTP headers, and can also inspect response headers. Refer to the Javadoc for details on which kinds of HTTP headers can be modified by Interceptors.
example: https://bilot.group/articles/using-react-router-inside-axios-interceptors/