Blame view

src/utils/http/axios/axiosTransform.ts 1.44 KB
陈文彬 authored
1
/**
Vben authored
2
 * Data processing class, can be configured according to the project
陈文彬 authored
3
4
 */
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
5
import type { RequestOptions, Result } from '/#/axios';
陈文彬 authored
6
7
export interface CreateAxiosOptions extends AxiosRequestConfig {
8
  authenticationScheme?: string;
9
  urlPrefix?: string;
10
11
12
13
  transform?: AxiosTransform;
  requestOptions?: RequestOptions;
}
陈文彬 authored
14
15
export abstract class AxiosTransform {
  /**
Vben authored
16
   * @description: Process configuration before request
陈文彬 authored
17
18
19
20
21
   * @description: Process configuration before request
   */
  beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;

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

  /**
   * @description: 请求失败处理
   */
29
  requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
陈文彬 authored
30
31
32
33

  /**
   * @description: 请求之前的拦截器
   */
34
35
36
37
  requestInterceptors?: (
    config: AxiosRequestConfig,
    options: CreateAxiosOptions
  ) => AxiosRequestConfig;
陈文彬 authored
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

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

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

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