axiosTransform.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Data processing class, can be configured according to the project
  3. */
  4. import type {
  5. AxiosInstance,
  6. AxiosRequestConfig,
  7. AxiosResponse,
  8. InternalAxiosRequestConfig,
  9. } from 'axios';
  10. import type { RequestOptions, Result } from '#/axios';
  11. export interface CreateAxiosOptions extends AxiosRequestConfig {
  12. authenticationScheme?: string;
  13. transform?: AxiosTransform;
  14. requestOptions?: RequestOptions;
  15. }
  16. export abstract class AxiosTransform {
  17. /**
  18. * A function that is called before a request is sent. It can modify the request configuration as needed.
  19. * 在发送请求之前调用的函数。它可以根据需要修改请求配置。
  20. */
  21. beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;
  22. /**
  23. * @description: 处理响应数据
  24. */
  25. transformResponseHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
  26. /**
  27. * @description: 请求失败处理
  28. */
  29. requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
  30. /**
  31. * @description: 请求之前的拦截器
  32. */
  33. requestInterceptors?: (
  34. config: InternalAxiosRequestConfig,
  35. options: CreateAxiosOptions,
  36. ) => InternalAxiosRequestConfig;
  37. /**
  38. * @description: 请求之后的拦截器
  39. */
  40. responseInterceptors?: (res: AxiosResponse<any>) => AxiosResponse<any>;
  41. /**
  42. * @description: 请求之前的拦截器错误处理
  43. */
  44. requestInterceptorsCatch?: (error: Error) => void;
  45. /**
  46. * @description: 请求之后的拦截器错误处理
  47. */
  48. responseInterceptorsCatch?: (axiosInstance: AxiosInstance, error: Error) => void;
  49. }