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

export interface SignatureHeaders {
  'X-App-Id': string;
  'X-Nonce': string;
  'X-Timestamp': string;
  'X-Sign': string;
  'X-SessionId'?:string;
}

export interface SecurityConfig {
  enable?: boolean;     // 是否启用白名单功能,默认为true
  address?: string;     // 获取白名单的地址,如果未提供则使用SDK内置默认地址
}

export interface SentryConfig {
  enable?: boolean;     // 是否启用Sentry错误上报,默认为true
  dsn?: string;         // Sentry DSN配置
  options?: Record<string, any>; // Sentry初始化选项
}

export interface ResponseConfig {
  code_key?: string | string[];           // 状态码字段名,可以是字符串或字符串数组
  code_success_value?: string | number;   // 成功状态码值
  data_key?: string;                      // 数据字段名
  errId?: string;                         // 错误ID字段名
  errId_logout_value?: string | number;   // 登出错误ID值
}

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;  // 重试时是否重置超时计时器
}

/**
 * 防重复提交配置
 */
/** 
 * 防重复提交配置类型
 * - true: 启用防重复提交,使用请求URL作为标识
 * - false: 禁用防重复提交
 */
export type DuplicatePreventionOption = boolean;

/**
 * Session 配置
 * - sessionId 支持 string | () => string | Promise<string>
 * - requiresSession 是否开启会话ID,默认 false
 * - sessionParamName 会话ID在请求参数中的字段名,默认 'sessionId'
 */
export interface SessionConfig {
  sessionId?: string | (() => string | Promise<string>) | Promise<string>;
  requiresSession?: boolean;
  sessionParamName?: string;
}

export interface RequestConfig {
  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: import('./security/signature').SignatureOptions) => import('./security/signature').SignatureResult; // 添加自定义签名函数
  events?: Record<string, Function>; // 事件订阅配置
  response?: ResponseConfig; // 响应处理配置(推荐使用)
  retry?: RetryConfig; // 重试配置
  returnAll?: boolean; // 是否返回完整响应
  sentry?: SentryConfig; // Sentry配置,包含错误上报开关、DSN和选项
  security?: SecurityConfig; // 安全配置,包含白名单功能开关和地址
  duplicatePrevention?: DuplicatePreventionOption; // 防重复提交配置(支持简化格式)
  session?: SessionConfig; // 会话配置
  noSignAttr?: string[]; // 不参与签名的参数名列表
}

export interface ISignatrueOptionsParams {
  params: Record<string, any>;
  data: Record<string, any>;
}

export interface SignatureOptions {
  appId: string;
  appSecret: string;
  sessionId?: string;
  params?: ISignatrueOptionsParams;
  noSignAttr?: string[];
  method?: string;
}

export interface SignatureResult {
  "X-App-Id": string;
  "X-Nonce": string;
  "X-Timestamp": string;
  "X-Sign": string;
  "X-SessionId"?: string;
}

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>>;
}