How axios interceptor gets triggered

735 views Asked by At

I am creating a response interceptor to get the status type and set the flag based on the status type so that an ant d modal popup can be loaded based the flag value. How this Interceptor will be triggered for every http calls and how to set the flag in response interceptor so that i can read it in another typescript class to show modal popup

import axios, { AxiosInstance } from 'axios'; import React, { useState } from 'react';

axios.interceptors.response.use(function(response){
console.log("New Interceptor response");
console.log(response.status);
console.log(response.statusText);
return response;
},
function (error){
return Promise.reject(console.error);
});
1

There are 1 answers

2
P.Zdravkov On BEST ANSWER

Wrap the axios in a hook like useReqest or sth. set the interceptor and it will be triggered every time you use this hook. Axios like RegExp has some state in it so it remembers some settings.

Essentially you must use the axios instance which has been configured with an interceptor and will be called by default. Same thing like intercepors in Spring/NestJS/Express if you are familiar with backend dev.