axiosTransform.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Data processing class, can be configured according to the project
  3. */
  4. import type { AxiosRequestConfig, AxiosResponse } from 'axios';
  5. import type { RequestOptions, Result } from '/#/axios';
  6. export interface CreateAxiosOptions extends AxiosRequestConfig {
  7. authenticationScheme?: string;
  8. transform?: AxiosTransform;
  9. requestOptions?: RequestOptions;
  10. }
  11. export abstract class AxiosTransform {
  12. /**
  13. * @description: Process configuration before request
  14. * @description: Process configuration before request
  15. */
  16. beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;
  17. /**
  18. * @description: 处理响应数据
  19. */
  20. transformResponseHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
  21. /**
  22. * @description: 请求失败处理
  23. */
  24. requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;
  25. /**
  26. * @description: 请求之前的拦截器
  27. */
  28. requestInterceptors?: (
  29. config: AxiosRequestConfig,
  30. options: CreateAxiosOptions,
  31. ) => AxiosRequestConfig;
  32. /**
  33. * @description: 请求之后的拦截器
  34. */
  35. responseInterceptors?: (res: AxiosResponse<any>) => AxiosResponse<any>;
  36. /**
  37. * @description: 请求之前的拦截器错误处理
  38. */
  39. requestInterceptorsCatch?: (error: Error) => void;
  40. /**
  41. * @description: 请求之后的拦截器错误处理
  42. */
  43. responseInterceptorsCatch?: (axiosInstance: AxiosResponse, error: Error) => void;
  44. }