admin-vben5/packages/effects/request/src/request-client/modules/interceptor.ts

42 lines
935 B
TypeScript
Raw Normal View History

import type {
2024-06-02 20:50:51 +08:00
AxiosInstance,
AxiosResponse,
InternalAxiosRequestConfig,
2024-06-02 20:50:51 +08:00
} from 'axios';
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,
) {
this.axiosInstance.interceptors.request.use(
fulfilled,
rejected || errorHandler,
);
2024-06-02 20:50:51 +08:00
}
addResponseInterceptor<T = any>(
2024-06-02 20:50:51 +08:00
fulfilled: (
response: AxiosResponse<T>,
2024-06-02 20:50:51 +08:00
) => AxiosResponse | Promise<AxiosResponse>,
rejected?: (error: any) => any,
) {
this.axiosInstance.interceptors.response.use(
fulfilled,
rejected || errorHandler,
);
2024-06-02 20:50:51 +08:00
}
}
export { InterceptorManager };