Blame view

src/utils/http/axios/axiosTransform.ts 1.59 KB
陈文彬 authored
1
/**
Vben authored
2
 * Data processing class, can be configured according to the project
陈文彬 authored
3
 */
Kirk Lin authored
4
5
6
7
import type {
  AxiosInstance,
  AxiosRequestConfig,
  AxiosResponse,
8
  InternalAxiosRequestConfig,
Kirk Lin authored
9
} from 'axios';
10
import type { RequestOptions, Result } from '/#/axios';
陈文彬 authored
11
12
export interface CreateAxiosOptions extends AxiosRequestConfig {
13
  authenticationScheme?: string;
14
15
16
17
  transform?: AxiosTransform;
  requestOptions?: RequestOptions;
}
陈文彬 authored
18
19
export abstract class AxiosTransform {
  /**
Kirk Lin authored
20
21
   * A function that is called before a request is sent. It can modify the request configuration as needed.
   * 在发送请求之前调用的函数。它可以根据需要修改请求配置。
陈文彬 authored
22
23
24
25
   */
  beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;

  /**
26
   * @description: 处理响应数据
陈文彬 authored
27
   */
28
  transformResponseHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
陈文彬 authored
29
30
31
32

  /**
   * @description: 请求失败处理
   */
33
  requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
陈文彬 authored
34
35
36
37

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

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

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

  /**
   * @description: 请求之后的拦截器错误处理
   */
Kirk Lin authored
56
  responseInterceptorsCatch?: (axiosInstance: AxiosInstance, error: Error) => void;
陈文彬 authored
57
}