import axios from "axios"; import type { CreateAxiosDefaults, AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse, } from "axios"; class WrappedAxios { private instance?: AxiosInstance; constructor(options: CreateAxiosDefaults = {}) { this.instance = axios.create({ timeout: 10000, ...options, }); } beforeRequest( onFulfilled?: | (( value: InternalAxiosRequestConfig ) => | InternalAxiosRequestConfig | Promise>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions ) { this.instance.interceptors.request.use(onFulfilled, onRejected, options); return this; } afterResponse( onFulfilled?: | (( value: AxiosResponse ) => AxiosResponse | Promise>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions ) { this.instance.interceptors.response.use(onFulfilled, onRejected, options); return this; } get axiosInstance() { return this.instance; } get( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.instance.get(url, { params: data, ...config, }) as Promise; } post( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.instance.post(url, data, config) as Promise; } put( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.instance.put(url, data, config) as Promise; } delete( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.instance.delete(url, { params: data, ...config, }) as Promise; } private createCancelObj( config: AxiosRequestConfig = {}, send: () => Promise ) { const controller = new AbortController(); config.signal = controller.signal; return { abort() { controller.abort(); }, send, }; } createGetWithCancel( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.createCancelObj(config, () => { return this.get(url, data, config); }); } createPostWithCancel( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.createCancelObj(config, () => { return this.post(url, data, config); }); } createPutWithCancel( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.createCancelObj(config, () => { return this.put(url, data, config); }); } createDeleteWithCancel( url: string, data: Record, config: AxiosRequestConfig = {} ) { return this.createCancelObj(config, () => { return this.delete(url, data, config); }); } } export { WrappedAxios };