conf.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import axios, {
  2. AxiosInstance,
  3. AxiosRequestConfig,
  4. AxiosResponse,
  5. AxiosError,
  6. InternalAxiosRequestConfig
  7. } from 'axios';
  8. // API 基础配置
  9. export const API_CONFIG = {
  10. BASE_URL: process.env.EXPO_PUBLIC_API_URL,
  11. // 超时时间
  12. TIMEOUT: 15000,
  13. // 重试配置
  14. RETRY: {
  15. maxRetries: 3,
  16. retryDelay: 1000,
  17. },
  18. // 错误消息映射
  19. ERROR_MESSAGES: {
  20. NETWORK_ERROR: '网络连接失败,请检查网络设置',
  21. TIMEOUT_ERROR: '请求超时,请重试',
  22. SERVER_ERROR: '服务器错误,请稍后重试',
  23. UNAUTHORIZED: '登录已过期,请重新登录',
  24. } as const,
  25. } as const;
  26. // 基础响应结构
  27. export interface BaseResponse<T = any> {
  28. code: number;
  29. message: string;
  30. data: T;
  31. success: boolean;
  32. }
  33. // 错误响应结构
  34. export interface ErrorResponse {
  35. code: number;
  36. message: string;
  37. timestamp?: string;
  38. path?: string;
  39. }
  40. // 请求配置扩展
  41. export interface RequestConfig {
  42. needAuth?: boolean; // 是否需要认证
  43. token?: string | null;
  44. showError?: boolean; // 是否显示错误提示
  45. retryCount?: number; // 重试次数
  46. }
  47. // 扩展的 Axios 请求配置
  48. export interface ExtendedRequestConfig extends AxiosRequestConfig {
  49. customConfig?: RequestConfig;
  50. }