In the following steps, we will add the axios interceptor to the HTTP method functions. Follow these steps to do it correctly:
- Open the baseFetch.js file in the src/http folder.
- We need to import the three interceptors we just created:
import {
errorInterceptor,
requestInterceptor,
responseInterceptor,
} from './interceptors';
- After the creation of the localApi instance, we declare the use of the request and response interceptor:
localApi.interceptors
.request.use(requestInterceptor, errorInterceptor);
localApi.interceptors
.response.use(responseInterceptor, errorInterceptor);
- After the creation of the jsonPlaceholderApi instance, we declare the use of the request and response interceptor:
jsonPlaceholderApi.interceptors
.request.use(requestInterceptor, errorInterceptor);
jsonPlaceholderApi.interceptors
.response.use(responseInterceptor, errorInterceptor);