2024-08-05 21:12:22 +08:00
|
|
|
import type {
|
2024-06-02 20:50:51 +08:00
|
|
|
AxiosInstance,
|
|
|
|
AxiosResponse,
|
2024-08-05 21:12:22 +08:00
|
|
|
InternalAxiosRequestConfig,
|
2024-06-02 20:50:51 +08:00
|
|
|
} from 'axios';
|
|
|
|
|
2024-07-13 21:00:31 +08:00
|
|
|
const errorHandler = (res: Error) => Promise.reject(res);
|
|
|
|
|
2024-06-02 20:50:51 +08:00
|
|
|
class InterceptorManager {
|
|
|
|
private axiosInstance: AxiosInstance;
|
|
|
|
|
|
|
|
constructor(instance: AxiosInstance) {
|
|
|
|
this.axiosInstance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
addRequestInterceptor(
|
|
|
|
fulfilled: (
|
|
|
|
config: InternalAxiosRequestConfig,
|
|
|
|
) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>,
|
|
|
|
rejected?: (error: any) => any,
|
|
|
|
) {
|
2024-07-11 20:11:11 +08:00
|
|
|
this.axiosInstance.interceptors.request.use(
|
|
|
|
fulfilled,
|
2024-07-13 21:00:31 +08:00
|
|
|
rejected || errorHandler,
|
2024-07-11 20:11:11 +08:00
|
|
|
);
|
2024-06-02 20:50:51 +08:00
|
|
|
}
|
|
|
|
|
2024-07-11 20:11:11 +08:00
|
|
|
addResponseInterceptor<T = any>(
|
2024-06-02 20:50:51 +08:00
|
|
|
fulfilled: (
|
2024-07-11 20:11:11 +08:00
|
|
|
response: AxiosResponse<T>,
|
2024-06-02 20:50:51 +08:00
|
|
|
) => AxiosResponse | Promise<AxiosResponse>,
|
|
|
|
rejected?: (error: any) => any,
|
|
|
|
) {
|
2024-07-11 20:11:11 +08:00
|
|
|
this.axiosInstance.interceptors.response.use(
|
|
|
|
fulfilled,
|
2024-07-13 21:00:31 +08:00
|
|
|
rejected || errorHandler,
|
2024-07-11 20:11:11 +08:00
|
|
|
);
|
2024-06-02 20:50:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { InterceptorManager };
|