axiosTransform.ts 1.4 KB
Newer Older
陈文彬 已提交
1
/**
V
Vben 已提交
2
 * Data processing class, can be configured according to the project
陈文彬 已提交
3 4 5 6
 */
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import type { RequestOptions, Result } from './types';

7
export interface CreateAxiosOptions extends AxiosRequestConfig {
V
Vben 已提交
8
  urlPrefix?: string;
9 10 11 12
  transform?: AxiosTransform;
  requestOptions?: RequestOptions;
}

陈文彬 已提交
13 14
export abstract class AxiosTransform {
  /**
V
Vben 已提交
15
   * @description: Process configuration before request
陈文彬 已提交
16 17 18 19 20
   * @description: Process configuration before request
   */
  beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;

  /**
V
Vben 已提交
21
   * @description: Request successfully processed
陈文彬 已提交
22
   */
V
Vben 已提交
23
  transformRequestHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
陈文彬 已提交
24 25 26 27

  /**
   * @description: 请求失败处理
   */
28
  requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
陈文彬 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  /**
   * @description: 请求之前的拦截器
   */
  requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig;

  /**
   * @description: 请求之后的拦截器
   */
  responseInterceptors?: (res: AxiosResponse<any>) => AxiosResponse<any>;

  /**
   * @description: 请求之前的拦截器错误处理
   */
  requestInterceptorsCatch?: (error: Error) => void;

  /**
   * @description: 请求之后的拦截器错误处理
   */
  responseInterceptorsCatch?: (error: Error) => void;
}