Response Interceptor
E.g., deal with error here and there's no need to deal with error everywhere
// response intercept: deal with error and message
instance.interceptors.response.use((res) => {
const resData = (res.data || {}) as ResType;
const { errno, data, msg } = resData;
if (errno !== 0) {
// error tip
if (msg) {
message.error(msg);
}
throw new Error(msg);
}
return data as any;
});
export default instance;
Request Interceptor
E.g., add token every time when sending request
// request intercept: add token
instance.interceptors.request.use(
(config) => {
config.headers["Authorization"] = `Bearer ${getToken()}`;
return config;
},
(error) => Promise.reject(error)
);
Comments
Post a Comment