index.d.ts 2.99 KB
export interface RequestHeaders {
  'Content-Type'?: string;
  'X-App-Id'?: string;
  'X-Nonce'?: string;
  'X-Timestamp'?: string;
  'X-Sign'?: string;
  [key: string]: string | undefined;
}

export interface RequestError extends Error {
  code: string;
  config: RequestConfig;
  request?: any;
  response?: ResponseData;
  isRequestError: boolean;
}

export interface RetryConfig {
  retries?: number;         // 最大重试次数
  retryDelay?: number;      // 重试延迟时间(ms)
  retryCondition?: (error: RequestError) => boolean;  // 重试条件
  shouldResetTimeout?: boolean;  // 重试时是否重置超时计时器
}

export interface RequestConfig extends RetryConfig {
  url?: string;
  method?: string;
  baseURL?: string;
  headers?: Record<string, string>;
  params?: Record<string, any>;
  data?: any;
  timeout?: number;
  appId?: string;
  appSecret?: string;
  signal?: AbortSignal;
  responseType?: 'json' | 'text' | 'blob' | 'arraybuffer'; // 响应数据类型
  signatureFunc?: (options: SignatureOptions) => SignatureResult; // 添加自定义签名函数
  events?: Record<string, Function>; // 事件订阅配置
  httpOptions?: any; // HTTP选项
  returnAll?: boolean; // 是否返回完整响应
  sentryDsn?: string; // Sentry DSN 配置(可选,推荐在初始化时设置)
  sentryOptions?: Record<string, any>; // Sentry 初始化选项
}

export interface SignatureOptions {
  appId: string;
  appSecret: string;
  params?: Record<string, any>;
  noSignAttr?: string[];
}

export interface SignatureResult {
  nonce: string;
  timestamp: string;
  sign: string;
  [key: string]: any;
}

export interface ResponseData<T = any> {
  data: T;
  status: number;
  statusText: string;
  headers: Record<string, string>;
  config: RequestConfig;
}

export interface InterceptorHandler<T = any> {
  fulfilled: (value: T) => T | Promise<T>;
  rejected: (error: RequestError) => any;
}

export interface InterceptorManagerInstance<T = any> {
  use(fulfilled: (value: T) => T | Promise<T>, rejected?: (error: RequestError) => any): number;
  eject(id: number): void;
  forEach(fn: (handler: InterceptorHandler<T>) => void): void;
}

export interface RequestInstance {
  defaults: RequestConfig;
  interceptors: {
    request: InterceptorManagerInstance<RequestConfig>;
    response: InterceptorManagerInstance<ResponseData>;
  };
  request<T = any>(config?: RequestConfig): Promise<ResponseData<T>> & { abort?: () => void };
  get<T = any>(url: string, config?: RequestConfig): Promise<ResponseData<T>>;
  delete<T = any>(url: string, config?: RequestConfig): Promise<ResponseData<T>>;
  head<T = any>(url: string, config?: RequestConfig): Promise<ResponseData<T>>;
  options<T = any>(url: string, config?: RequestConfig): Promise<ResponseData<T>>;
  post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<ResponseData<T>>;
  put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<ResponseData<T>>;
  patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<ResponseData<T>>;
}