index.d.ts
2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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>>;
}