123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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<V = any>(
- onFulfilled?:
- | ((
- value: InternalAxiosRequestConfig<V>
- ) =>
- | InternalAxiosRequestConfig<V>
- | Promise<InternalAxiosRequestConfig<V>>)
- | null,
- onRejected?: ((error: any) => any) | null,
- options?: AxiosInterceptorOptions
- ) {
- this.instance.interceptors.request.use(onFulfilled, onRejected, options);
- return this;
- }
- afterResponse<V = any, D = any>(
- onFulfilled?:
- | ((
- value: AxiosResponse<V, D>
- ) => AxiosResponse<V, D> | Promise<AxiosResponse<V, D>>)
- | null,
- onRejected?: ((error: any) => any) | null,
- options?: AxiosInterceptorOptions
- ) {
- this.instance.interceptors.response.use(onFulfilled, onRejected, options);
- return this;
- }
- get axiosInstance() {
- return this.instance;
- }
- get<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.instance.get(url, {
- params: data,
- ...config,
- }) as Promise<R>;
- }
- post<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.instance.post(url, data, config) as Promise<R>;
- }
- put<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.instance.put(url, data, config) as Promise<R>;
- }
- delete<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.instance.delete(url, {
- params: data,
- ...config,
- }) as Promise<R>;
- }
- private createCancelObj<R>(
- config: AxiosRequestConfig = {},
- send: () => Promise<R>
- ) {
- const controller = new AbortController();
- config.signal = controller.signal;
- return {
- abort() {
- controller.abort();
- },
- send,
- };
- }
- createGetWithCancel<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.createCancelObj<R>(config, () => {
- return this.get<R>(url, data, config);
- });
- }
- createPostWithCancel<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.createCancelObj<R>(config, () => {
- return this.post<R>(url, data, config);
- });
- }
- createPutWithCancel<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.createCancelObj<R>(config, () => {
- return this.put<R>(url, data, config);
- });
- }
- createDeleteWithCancel<R = any>(
- url: string,
- data: Record<string, any>,
- config: AxiosRequestConfig = {}
- ) {
- return this.createCancelObj<R>(config, () => {
- return this.delete<R>(url, data, config);
- });
- }
- }
- export { WrappedAxios };
|