index.d.ts
5.07 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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>>;
}