@djvlc/openapi-client-core 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +996 -146
- package/dist/index.d.mts +2526 -804
- package/dist/index.d.ts +2526 -804
- package/dist/index.js +17950 -1181
- package/dist/index.mjs +17887 -1139
- package/package.json +5 -5
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,996 +1,2718 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
export { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 认证配置类型定义
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* 认证类型
|
|
11
|
+
*/
|
|
12
|
+
type AuthType = 'bearer' | 'api-key' | 'basic' | 'custom' | 'none';
|
|
13
|
+
/**
|
|
14
|
+
* Bearer Token 认证配置
|
|
15
|
+
*/
|
|
16
|
+
interface BearerAuthConfig {
|
|
17
|
+
readonly type: 'bearer';
|
|
18
|
+
/**
|
|
19
|
+
* 获取 Token 的函数
|
|
20
|
+
* 可以是同步或异步函数
|
|
21
|
+
*/
|
|
22
|
+
readonly getToken: () => string | null | Promise<string | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Token 头部名称
|
|
25
|
+
* @default 'Authorization'
|
|
26
|
+
*/
|
|
27
|
+
readonly headerName?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Token 前缀
|
|
30
|
+
* @default 'Bearer'
|
|
31
|
+
*/
|
|
32
|
+
readonly prefix?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* API Key 认证配置
|
|
36
|
+
*/
|
|
37
|
+
interface ApiKeyAuthConfig {
|
|
38
|
+
readonly type: 'api-key';
|
|
39
|
+
/**
|
|
40
|
+
* API Key 值
|
|
41
|
+
*/
|
|
42
|
+
readonly apiKey: string;
|
|
43
|
+
/**
|
|
44
|
+
* API Key 头部名称
|
|
45
|
+
* @default 'X-API-Key'
|
|
46
|
+
*/
|
|
47
|
+
readonly headerName?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Basic Auth 认证配置
|
|
51
|
+
*/
|
|
52
|
+
interface BasicAuthConfig {
|
|
53
|
+
readonly type: 'basic';
|
|
54
|
+
/**
|
|
55
|
+
* 用户名
|
|
56
|
+
*/
|
|
57
|
+
readonly username: string;
|
|
58
|
+
/**
|
|
59
|
+
* 密码
|
|
60
|
+
*/
|
|
61
|
+
readonly password: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 自定义认证配置
|
|
65
|
+
*/
|
|
66
|
+
interface CustomAuthConfig {
|
|
67
|
+
readonly type: 'custom';
|
|
68
|
+
/**
|
|
69
|
+
* 自定义认证函数
|
|
70
|
+
* 接收请求头对象,修改后返回
|
|
71
|
+
*/
|
|
72
|
+
readonly authenticate: (headers: Record<string, string>) => void | Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 无认证配置
|
|
76
|
+
*/
|
|
77
|
+
interface NoAuthConfig {
|
|
78
|
+
readonly type: 'none';
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 认证配置联合类型
|
|
82
|
+
*/
|
|
83
|
+
type AuthConfig = BearerAuthConfig | ApiKeyAuthConfig | BasicAuthConfig | CustomAuthConfig | NoAuthConfig;
|
|
84
|
+
/**
|
|
85
|
+
* Token 刷新配置
|
|
86
|
+
*/
|
|
87
|
+
interface TokenRefreshConfig {
|
|
88
|
+
/**
|
|
89
|
+
* 刷新 Token 的函数
|
|
90
|
+
* 返回新的 Token,如果刷新失败则抛出异常
|
|
91
|
+
*/
|
|
92
|
+
readonly refreshToken: () => Promise<string>;
|
|
93
|
+
/**
|
|
94
|
+
* 触发刷新的 HTTP 状态码
|
|
95
|
+
* @default [401]
|
|
96
|
+
*/
|
|
97
|
+
readonly triggerStatusCodes?: readonly number[];
|
|
98
|
+
/**
|
|
99
|
+
* 最大刷新重试次数
|
|
100
|
+
* @default 1
|
|
101
|
+
*/
|
|
102
|
+
readonly maxRetries?: number;
|
|
103
|
+
/**
|
|
104
|
+
* 刷新失败时的回调
|
|
105
|
+
*/
|
|
106
|
+
readonly onRefreshFailed?: (error: Error) => void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 重试配置类型定义
|
|
111
|
+
*
|
|
112
|
+
* @packageDocumentation
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* 退避策略类型
|
|
116
|
+
*/
|
|
117
|
+
type BackoffStrategy = 'fixed' | 'linear' | 'exponential';
|
|
118
|
+
/**
|
|
119
|
+
* 重试配置
|
|
120
|
+
*/
|
|
121
|
+
interface RetryConfig {
|
|
122
|
+
/**
|
|
123
|
+
* 最大重试次数
|
|
124
|
+
* @default 3
|
|
125
|
+
*/
|
|
126
|
+
readonly maxRetries: number;
|
|
127
|
+
/**
|
|
128
|
+
* 初始延迟(毫秒)
|
|
129
|
+
* @default 1000
|
|
130
|
+
*/
|
|
131
|
+
readonly initialDelayMs: number;
|
|
132
|
+
/**
|
|
133
|
+
* 最大延迟(毫秒)
|
|
134
|
+
* @default 30000
|
|
135
|
+
*/
|
|
136
|
+
readonly maxDelayMs: number;
|
|
137
|
+
/**
|
|
138
|
+
* 退避策略
|
|
139
|
+
* - fixed: 固定延迟
|
|
140
|
+
* - linear: 线性增长 (initialDelay * (attempt + 1))
|
|
141
|
+
* - exponential: 指数增长 (initialDelay * 2^attempt)
|
|
142
|
+
* @default 'exponential'
|
|
143
|
+
*/
|
|
144
|
+
readonly backoffStrategy: BackoffStrategy;
|
|
145
|
+
/**
|
|
146
|
+
* 可重试的 HTTP 状态码
|
|
147
|
+
* @default [429, 500, 502, 503, 504]
|
|
148
|
+
*/
|
|
149
|
+
readonly retryableStatusCodes?: readonly number[];
|
|
150
|
+
/**
|
|
151
|
+
* 是否重试网络错误
|
|
152
|
+
* @default true
|
|
153
|
+
*/
|
|
154
|
+
readonly retryOnNetworkError?: boolean;
|
|
155
|
+
/**
|
|
156
|
+
* 是否重试超时错误
|
|
157
|
+
* @default true
|
|
158
|
+
*/
|
|
159
|
+
readonly retryOnTimeout?: boolean;
|
|
160
|
+
/**
|
|
161
|
+
* 是否尊重 Retry-After 响应头
|
|
162
|
+
* @default true
|
|
163
|
+
*/
|
|
164
|
+
readonly respectRetryAfter?: boolean;
|
|
165
|
+
/**
|
|
166
|
+
* 重试时的回调函数
|
|
167
|
+
*/
|
|
168
|
+
readonly onRetry?: RetryCallback;
|
|
169
|
+
/**
|
|
170
|
+
* 自定义判断是否应该重试的函数
|
|
171
|
+
* 如果提供,将覆盖默认的重试逻辑
|
|
172
|
+
*/
|
|
173
|
+
readonly shouldRetry?: ShouldRetryFn;
|
|
174
|
+
/**
|
|
175
|
+
* 抖动系数(0-1 之间)
|
|
176
|
+
* 用于添加随机延迟,防止惊群效应
|
|
177
|
+
* @default 0.1
|
|
178
|
+
*/
|
|
179
|
+
readonly jitterFactor?: number;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 重试回调函数
|
|
183
|
+
*/
|
|
184
|
+
type RetryCallback = (info: RetryInfo) => void;
|
|
185
|
+
/**
|
|
186
|
+
* 重试信息
|
|
187
|
+
*/
|
|
188
|
+
interface RetryInfo {
|
|
189
|
+
/**
|
|
190
|
+
* 当前重试次数(从 1 开始)
|
|
191
|
+
*/
|
|
192
|
+
readonly attempt: number;
|
|
193
|
+
/**
|
|
194
|
+
* 最大重试次数
|
|
195
|
+
*/
|
|
196
|
+
readonly maxRetries: number;
|
|
197
|
+
/**
|
|
198
|
+
* 下次重试延迟(毫秒)
|
|
199
|
+
*/
|
|
200
|
+
readonly delayMs: number;
|
|
201
|
+
/**
|
|
202
|
+
* 导致重试的错误
|
|
203
|
+
*/
|
|
204
|
+
readonly error: Error;
|
|
205
|
+
/**
|
|
206
|
+
* 请求 URL
|
|
207
|
+
*/
|
|
208
|
+
readonly url: string;
|
|
209
|
+
/**
|
|
210
|
+
* 请求方法
|
|
211
|
+
*/
|
|
212
|
+
readonly method: string;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 自定义重试判断函数
|
|
216
|
+
*/
|
|
217
|
+
type ShouldRetryFn = (error: Error, attempt: number, context: RetryContext) => boolean;
|
|
218
|
+
/**
|
|
219
|
+
* 重试上下文
|
|
220
|
+
*/
|
|
221
|
+
interface RetryContext {
|
|
222
|
+
/**
|
|
223
|
+
* 请求 URL
|
|
224
|
+
*/
|
|
225
|
+
readonly url: string;
|
|
226
|
+
/**
|
|
227
|
+
* 请求方法
|
|
228
|
+
*/
|
|
229
|
+
readonly method: string;
|
|
230
|
+
/**
|
|
231
|
+
* HTTP 状态码(如果有响应)
|
|
232
|
+
*/
|
|
233
|
+
readonly statusCode?: number;
|
|
234
|
+
/**
|
|
235
|
+
* Retry-After 头的值(秒)
|
|
236
|
+
*/
|
|
237
|
+
readonly retryAfter?: number;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 默认重试配置
|
|
241
|
+
*/
|
|
242
|
+
declare const DEFAULT_RETRY_CONFIG: Readonly<RetryConfig>;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 拦截器类型定义
|
|
246
|
+
*
|
|
247
|
+
* @packageDocumentation
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 请求拦截器
|
|
252
|
+
*
|
|
253
|
+
* 在请求发送前执行,可以修改请求选项
|
|
254
|
+
*/
|
|
255
|
+
interface RequestInterceptor {
|
|
256
|
+
/**
|
|
257
|
+
* 拦截器名称(用于调试和日志)
|
|
258
|
+
*/
|
|
259
|
+
readonly name: string;
|
|
260
|
+
/**
|
|
261
|
+
* 执行顺序(数字越小越先执行)
|
|
262
|
+
* @default 0
|
|
263
|
+
*/
|
|
264
|
+
readonly order?: number;
|
|
265
|
+
/**
|
|
266
|
+
* 拦截函数
|
|
267
|
+
* @param context - 请求上下文
|
|
268
|
+
* @returns 修改后的请求选项,或 Promise
|
|
269
|
+
*/
|
|
270
|
+
intercept(context: RequestContext$1): MutableRequestOptions | Promise<MutableRequestOptions>;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 响应拦截器
|
|
274
|
+
*
|
|
275
|
+
* 在收到响应后执行,可以修改响应数据
|
|
276
|
+
*/
|
|
277
|
+
interface ResponseInterceptor {
|
|
278
|
+
/**
|
|
279
|
+
* 拦截器名称(用于调试和日志)
|
|
280
|
+
*/
|
|
281
|
+
readonly name: string;
|
|
282
|
+
/**
|
|
283
|
+
* 执行顺序(数字越小越先执行)
|
|
284
|
+
* @default 0
|
|
285
|
+
*/
|
|
286
|
+
readonly order?: number;
|
|
287
|
+
/**
|
|
288
|
+
* 拦截函数
|
|
289
|
+
* @param response - 响应数据
|
|
290
|
+
* @param context - 请求上下文
|
|
291
|
+
* @returns 修改后的响应数据,或 Promise
|
|
292
|
+
*/
|
|
293
|
+
intercept<T>(response: ResponseData<T>, context: RequestContext$1): ResponseData<T> | Promise<ResponseData<T>>;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 错误拦截器
|
|
297
|
+
*
|
|
298
|
+
* 在发生错误时执行,可以处理、转换或重新抛出错误
|
|
299
|
+
*/
|
|
300
|
+
interface ErrorInterceptor {
|
|
301
|
+
/**
|
|
302
|
+
* 拦截器名称(用于调试和日志)
|
|
303
|
+
*/
|
|
304
|
+
readonly name: string;
|
|
305
|
+
/**
|
|
306
|
+
* 执行顺序(数字越小越先执行)
|
|
307
|
+
* @default 0
|
|
308
|
+
*/
|
|
309
|
+
readonly order?: number;
|
|
310
|
+
/**
|
|
311
|
+
* 拦截函数
|
|
312
|
+
*
|
|
313
|
+
* @param error - 错误对象
|
|
314
|
+
* @param context - 请求上下文
|
|
315
|
+
* @returns 如果返回 void,错误将继续传播;如果返回 ResponseData,将作为成功响应返回
|
|
316
|
+
* @throws 可以抛出新的错误来替换原错误
|
|
317
|
+
*/
|
|
318
|
+
intercept<T>(error: Error, context: RequestContext$1): void | ResponseData<T> | Promise<void | ResponseData<T>>;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* 拦截器集合
|
|
322
|
+
*/
|
|
323
|
+
interface Interceptors {
|
|
324
|
+
/**
|
|
325
|
+
* 请求拦截器列表
|
|
326
|
+
*/
|
|
327
|
+
readonly request: readonly RequestInterceptor[];
|
|
328
|
+
/**
|
|
329
|
+
* 响应拦截器列表
|
|
330
|
+
*/
|
|
331
|
+
readonly response: readonly ResponseInterceptor[];
|
|
332
|
+
/**
|
|
333
|
+
* 错误拦截器列表
|
|
334
|
+
*/
|
|
335
|
+
readonly error: readonly ErrorInterceptor[];
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* 拦截器工厂函数类型
|
|
339
|
+
*/
|
|
340
|
+
type RequestInterceptorFactory<TConfig = void> = (config: TConfig) => RequestInterceptor;
|
|
341
|
+
type ResponseInterceptorFactory<TConfig = void> = (config: TConfig) => ResponseInterceptor;
|
|
342
|
+
type ErrorInterceptorFactory<TConfig = void> = (config: TConfig) => ErrorInterceptor;
|
|
343
|
+
/**
|
|
344
|
+
* 拦截器链执行结果
|
|
345
|
+
*/
|
|
346
|
+
interface InterceptorChainResult<T> {
|
|
347
|
+
/**
|
|
348
|
+
* 是否成功
|
|
349
|
+
*/
|
|
350
|
+
readonly success: boolean;
|
|
351
|
+
/**
|
|
352
|
+
* 响应数据(成功时)
|
|
353
|
+
*/
|
|
354
|
+
readonly data?: ResponseData<T>;
|
|
355
|
+
/**
|
|
356
|
+
* 错误(失败时)
|
|
357
|
+
*/
|
|
358
|
+
readonly error?: Error;
|
|
359
|
+
/**
|
|
360
|
+
* 是否被拦截器处理
|
|
361
|
+
*/
|
|
362
|
+
readonly handled: boolean;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 日志类型定义
|
|
367
|
+
*
|
|
368
|
+
* @packageDocumentation
|
|
369
|
+
*/
|
|
370
|
+
/**
|
|
371
|
+
* 日志级别
|
|
372
|
+
*/
|
|
373
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
374
|
+
/**
|
|
375
|
+
* 日志器接口
|
|
376
|
+
*/
|
|
377
|
+
interface Logger {
|
|
378
|
+
/**
|
|
379
|
+
* 调试日志
|
|
380
|
+
*/
|
|
381
|
+
debug(message: string, ...args: unknown[]): void;
|
|
382
|
+
/**
|
|
383
|
+
* 信息日志
|
|
384
|
+
*/
|
|
385
|
+
info(message: string, ...args: unknown[]): void;
|
|
386
|
+
/**
|
|
387
|
+
* 警告日志
|
|
388
|
+
*/
|
|
389
|
+
warn(message: string, ...args: unknown[]): void;
|
|
390
|
+
/**
|
|
391
|
+
* 错误日志
|
|
392
|
+
*/
|
|
393
|
+
error(message: string, ...args: unknown[]): void;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* 控制台日志器配置
|
|
397
|
+
*/
|
|
398
|
+
interface ConsoleLoggerConfig {
|
|
399
|
+
/**
|
|
400
|
+
* 日志前缀
|
|
401
|
+
* @default '[DJV-API]'
|
|
402
|
+
*/
|
|
403
|
+
readonly prefix?: string;
|
|
404
|
+
/**
|
|
405
|
+
* 最低日志级别
|
|
406
|
+
* @default 'info'
|
|
407
|
+
*/
|
|
408
|
+
readonly level?: LogLevel;
|
|
409
|
+
/**
|
|
410
|
+
* 是否显示时间戳
|
|
411
|
+
* @default true
|
|
412
|
+
*/
|
|
413
|
+
readonly timestamp?: boolean;
|
|
414
|
+
/**
|
|
415
|
+
* 是否使用颜色(仅在 Node.js 环境有效)
|
|
416
|
+
* @default true
|
|
417
|
+
*/
|
|
418
|
+
readonly colors?: boolean;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* 日志级别优先级映射
|
|
422
|
+
*/
|
|
423
|
+
declare const LOG_LEVEL_PRIORITY: Readonly<Record<LogLevel, number>>;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* 客户端配置类型定义
|
|
427
|
+
*
|
|
428
|
+
* @packageDocumentation
|
|
429
|
+
*/
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* 客户端基础配置
|
|
433
|
+
*/
|
|
434
|
+
interface ClientConfig {
|
|
435
|
+
/**
|
|
436
|
+
* 基础 URL
|
|
437
|
+
* @example 'https://api.example.com'
|
|
438
|
+
*/
|
|
439
|
+
readonly baseUrl: string;
|
|
440
|
+
/**
|
|
441
|
+
* 默认超时时间(毫秒)
|
|
442
|
+
* @default 30000
|
|
443
|
+
*/
|
|
444
|
+
readonly timeout?: number;
|
|
445
|
+
/**
|
|
446
|
+
* 默认请求头
|
|
447
|
+
*/
|
|
448
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
449
|
+
/**
|
|
450
|
+
* 认证配置
|
|
451
|
+
*/
|
|
452
|
+
readonly auth?: AuthConfig;
|
|
453
|
+
/**
|
|
454
|
+
* 重试配置
|
|
455
|
+
*/
|
|
456
|
+
readonly retry?: RetryConfig;
|
|
457
|
+
/**
|
|
458
|
+
* 请求拦截器列表
|
|
459
|
+
*/
|
|
460
|
+
readonly requestInterceptors?: readonly RequestInterceptor[];
|
|
461
|
+
/**
|
|
462
|
+
* 响应拦截器列表
|
|
463
|
+
*/
|
|
464
|
+
readonly responseInterceptors?: readonly ResponseInterceptor[];
|
|
465
|
+
/**
|
|
466
|
+
* 错误拦截器列表
|
|
467
|
+
*/
|
|
468
|
+
readonly errorInterceptors?: readonly ErrorInterceptor[];
|
|
469
|
+
/**
|
|
470
|
+
* 日志器
|
|
471
|
+
*/
|
|
472
|
+
readonly logger?: Logger;
|
|
473
|
+
/**
|
|
474
|
+
* 是否开启调试模式
|
|
475
|
+
* @default false
|
|
476
|
+
*/
|
|
477
|
+
readonly debug?: boolean;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* HTTP 方法类型
|
|
481
|
+
*/
|
|
482
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
483
|
+
/**
|
|
484
|
+
* 请求选项
|
|
485
|
+
*/
|
|
486
|
+
interface RequestOptions {
|
|
487
|
+
/**
|
|
488
|
+
* HTTP 方法
|
|
489
|
+
*/
|
|
490
|
+
readonly method: HttpMethod;
|
|
491
|
+
/**
|
|
492
|
+
* 请求路径
|
|
493
|
+
*/
|
|
494
|
+
readonly path: string;
|
|
495
|
+
/**
|
|
496
|
+
* 查询参数
|
|
497
|
+
*/
|
|
498
|
+
readonly query?: Readonly<Record<string, string | number | boolean | undefined | null>>;
|
|
499
|
+
/**
|
|
500
|
+
* 请求体
|
|
501
|
+
*/
|
|
502
|
+
readonly body?: unknown;
|
|
503
|
+
/**
|
|
504
|
+
* 请求头
|
|
505
|
+
*/
|
|
506
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
507
|
+
/**
|
|
508
|
+
* 超时时间(毫秒)
|
|
509
|
+
*/
|
|
510
|
+
readonly timeout?: number;
|
|
511
|
+
/**
|
|
512
|
+
* 是否跳过认证
|
|
513
|
+
* @default false
|
|
514
|
+
*/
|
|
515
|
+
readonly skipAuth?: boolean;
|
|
516
|
+
/**
|
|
517
|
+
* AbortSignal 用于取消请求
|
|
518
|
+
*/
|
|
519
|
+
readonly signal?: AbortSignal;
|
|
520
|
+
/**
|
|
521
|
+
* 请求元数据(用于拦截器间传递数据)
|
|
522
|
+
*/
|
|
523
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* 可变的请求选项(用于拦截器修改)
|
|
527
|
+
*/
|
|
528
|
+
interface MutableRequestOptions {
|
|
529
|
+
method: HttpMethod;
|
|
530
|
+
path: string;
|
|
531
|
+
query?: Record<string, string | number | boolean | undefined | null>;
|
|
532
|
+
body?: unknown;
|
|
533
|
+
headers: Record<string, string>;
|
|
534
|
+
timeout?: number;
|
|
535
|
+
skipAuth?: boolean;
|
|
536
|
+
signal?: AbortSignal;
|
|
537
|
+
metadata: Record<string, unknown>;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* 响应数据结构
|
|
541
|
+
*/
|
|
542
|
+
interface ResponseData<T = unknown> {
|
|
543
|
+
/**
|
|
544
|
+
* 响应数据
|
|
545
|
+
*/
|
|
546
|
+
readonly data: T;
|
|
547
|
+
/**
|
|
548
|
+
* HTTP 状态码
|
|
549
|
+
*/
|
|
550
|
+
readonly status: number;
|
|
551
|
+
/**
|
|
552
|
+
* HTTP 状态文本
|
|
553
|
+
*/
|
|
554
|
+
readonly statusText: string;
|
|
555
|
+
/**
|
|
556
|
+
* 响应头
|
|
557
|
+
*/
|
|
558
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
559
|
+
/**
|
|
560
|
+
* 请求 ID
|
|
561
|
+
*/
|
|
562
|
+
readonly requestId: string;
|
|
563
|
+
/**
|
|
564
|
+
* 追踪 ID
|
|
565
|
+
*/
|
|
566
|
+
readonly traceId?: string;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* 请求上下文(拦截器使用)
|
|
570
|
+
*/
|
|
571
|
+
interface RequestContext$1 {
|
|
572
|
+
/**
|
|
573
|
+
* 请求 ID
|
|
574
|
+
*/
|
|
575
|
+
readonly requestId: string;
|
|
576
|
+
/**
|
|
577
|
+
* 追踪 ID
|
|
578
|
+
*/
|
|
579
|
+
readonly traceId?: string;
|
|
580
|
+
/**
|
|
581
|
+
* 请求开始时间戳
|
|
582
|
+
*/
|
|
583
|
+
readonly startTime: number;
|
|
584
|
+
/**
|
|
585
|
+
* 重试次数
|
|
586
|
+
*/
|
|
587
|
+
retryCount: number;
|
|
588
|
+
/**
|
|
589
|
+
* 原始请求选项
|
|
590
|
+
*/
|
|
591
|
+
readonly originalOptions: Readonly<RequestOptions>;
|
|
592
|
+
/**
|
|
593
|
+
* 当前请求选项(可能被拦截器修改)
|
|
594
|
+
*/
|
|
595
|
+
options: MutableRequestOptions;
|
|
596
|
+
/**
|
|
597
|
+
* 完整 URL
|
|
598
|
+
*/
|
|
599
|
+
readonly url: string;
|
|
600
|
+
/**
|
|
601
|
+
* 元数据存储
|
|
602
|
+
*/
|
|
603
|
+
metadata: Record<string, unknown>;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* 指标类型定义
|
|
608
|
+
*
|
|
609
|
+
* @packageDocumentation
|
|
610
|
+
*/
|
|
611
|
+
/**
|
|
612
|
+
* 请求指标
|
|
613
|
+
*/
|
|
614
|
+
interface RequestMetrics {
|
|
615
|
+
/**
|
|
616
|
+
* 请求 ID
|
|
617
|
+
*/
|
|
618
|
+
readonly requestId: string;
|
|
619
|
+
/**
|
|
620
|
+
* 完整 URL
|
|
621
|
+
*/
|
|
622
|
+
readonly url: string;
|
|
623
|
+
/**
|
|
624
|
+
* 请求路径(不含查询参数)
|
|
625
|
+
*/
|
|
626
|
+
readonly path: string;
|
|
627
|
+
/**
|
|
628
|
+
* HTTP 方法
|
|
629
|
+
*/
|
|
630
|
+
readonly method: string;
|
|
631
|
+
/**
|
|
632
|
+
* 开始时间戳(毫秒)
|
|
633
|
+
*/
|
|
634
|
+
readonly startTime: number;
|
|
635
|
+
/**
|
|
636
|
+
* 结束时间戳(毫秒)
|
|
637
|
+
*/
|
|
638
|
+
readonly endTime: number;
|
|
639
|
+
/**
|
|
640
|
+
* 耗时(毫秒)
|
|
641
|
+
*/
|
|
642
|
+
readonly durationMs: number;
|
|
643
|
+
/**
|
|
644
|
+
* HTTP 状态码
|
|
645
|
+
*/
|
|
646
|
+
readonly status?: number;
|
|
647
|
+
/**
|
|
648
|
+
* 是否成功(2xx)
|
|
649
|
+
*/
|
|
650
|
+
readonly success: boolean;
|
|
651
|
+
/**
|
|
652
|
+
* 重试次数
|
|
653
|
+
*/
|
|
654
|
+
readonly retryCount: number;
|
|
655
|
+
/**
|
|
656
|
+
* 追踪 ID
|
|
657
|
+
*/
|
|
658
|
+
readonly traceId?: string;
|
|
659
|
+
/**
|
|
660
|
+
* 错误信息(如果失败)
|
|
661
|
+
*/
|
|
662
|
+
readonly error?: string;
|
|
663
|
+
/**
|
|
664
|
+
* 请求体大小(字节)
|
|
665
|
+
*/
|
|
666
|
+
readonly requestSize?: number;
|
|
667
|
+
/**
|
|
668
|
+
* 响应体大小(字节)
|
|
669
|
+
*/
|
|
670
|
+
readonly responseSize?: number;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* 指标摘要
|
|
674
|
+
*/
|
|
675
|
+
interface MetricsSummary {
|
|
676
|
+
/**
|
|
677
|
+
* 总请求数
|
|
678
|
+
*/
|
|
679
|
+
readonly totalRequests: number;
|
|
680
|
+
/**
|
|
681
|
+
* 成功请求数
|
|
682
|
+
*/
|
|
683
|
+
readonly successfulRequests: number;
|
|
684
|
+
/**
|
|
685
|
+
* 失败请求数
|
|
686
|
+
*/
|
|
687
|
+
readonly failedRequests: number;
|
|
688
|
+
/**
|
|
689
|
+
* 成功率(0-1)
|
|
690
|
+
*/
|
|
691
|
+
readonly successRate: number;
|
|
692
|
+
/**
|
|
693
|
+
* 平均耗时(毫秒)
|
|
694
|
+
*/
|
|
695
|
+
readonly avgDurationMs: number;
|
|
696
|
+
/**
|
|
697
|
+
* P50 耗时(毫秒)
|
|
698
|
+
*/
|
|
699
|
+
readonly p50Ms: number;
|
|
700
|
+
/**
|
|
701
|
+
* P90 耗时(毫秒)
|
|
702
|
+
*/
|
|
703
|
+
readonly p90Ms: number;
|
|
704
|
+
/**
|
|
705
|
+
* P95 耗时(毫秒)
|
|
706
|
+
*/
|
|
707
|
+
readonly p95Ms: number;
|
|
708
|
+
/**
|
|
709
|
+
* P99 耗时(毫秒)
|
|
710
|
+
*/
|
|
711
|
+
readonly p99Ms: number;
|
|
712
|
+
/**
|
|
713
|
+
* 最小耗时(毫秒)
|
|
714
|
+
*/
|
|
715
|
+
readonly minDurationMs: number;
|
|
716
|
+
/**
|
|
717
|
+
* 最大耗时(毫秒)
|
|
718
|
+
*/
|
|
719
|
+
readonly maxDurationMs: number;
|
|
720
|
+
/**
|
|
721
|
+
* 总重试次数
|
|
722
|
+
*/
|
|
723
|
+
readonly totalRetries: number;
|
|
724
|
+
/**
|
|
725
|
+
* 按状态码分布
|
|
726
|
+
*/
|
|
727
|
+
readonly statusCodeDistribution: Readonly<Record<number, number>>;
|
|
728
|
+
/**
|
|
729
|
+
* 按路径分布
|
|
730
|
+
*/
|
|
731
|
+
readonly pathDistribution: Readonly<Record<string, PathMetrics>>;
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* 路径指标
|
|
735
|
+
*/
|
|
736
|
+
interface PathMetrics {
|
|
737
|
+
/**
|
|
738
|
+
* 请求数
|
|
739
|
+
*/
|
|
740
|
+
readonly count: number;
|
|
741
|
+
/**
|
|
742
|
+
* 成功数
|
|
743
|
+
*/
|
|
744
|
+
readonly successCount: number;
|
|
745
|
+
/**
|
|
746
|
+
* 平均耗时(毫秒)
|
|
747
|
+
*/
|
|
748
|
+
readonly avgDurationMs: number;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* 指标收集器配置
|
|
752
|
+
*/
|
|
753
|
+
interface MetricsCollectorConfig {
|
|
754
|
+
/**
|
|
755
|
+
* 最大保留的指标数量
|
|
756
|
+
* @default 1000
|
|
757
|
+
*/
|
|
758
|
+
readonly maxMetrics?: number;
|
|
759
|
+
/**
|
|
760
|
+
* 指标过期时间(毫秒)
|
|
761
|
+
* @default 3600000 (1 hour)
|
|
762
|
+
*/
|
|
763
|
+
readonly ttlMs?: number;
|
|
764
|
+
/**
|
|
765
|
+
* 每次请求完成时的回调
|
|
766
|
+
*/
|
|
767
|
+
readonly onMetrics?: (metrics: RequestMetrics) => void;
|
|
768
|
+
/**
|
|
769
|
+
* 是否启用详细指标(包括请求/响应大小)
|
|
770
|
+
* @default false
|
|
771
|
+
*/
|
|
772
|
+
readonly detailed?: boolean;
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* 指标收集器接口
|
|
776
|
+
*/
|
|
777
|
+
interface MetricsCollector {
|
|
778
|
+
/**
|
|
779
|
+
* 记录请求指标
|
|
780
|
+
*/
|
|
781
|
+
record(metrics: RequestMetrics): void;
|
|
782
|
+
/**
|
|
783
|
+
* 获取指标摘要
|
|
784
|
+
*/
|
|
785
|
+
summary(): MetricsSummary;
|
|
786
|
+
/**
|
|
787
|
+
* 获取所有指标
|
|
788
|
+
*/
|
|
789
|
+
getAll(): readonly RequestMetrics[];
|
|
790
|
+
/**
|
|
791
|
+
* 按路径获取指标
|
|
792
|
+
*/
|
|
793
|
+
getByPath(path: string): readonly RequestMetrics[];
|
|
794
|
+
/**
|
|
795
|
+
* 清空所有指标并返回
|
|
796
|
+
*/
|
|
797
|
+
flush(): readonly RequestMetrics[];
|
|
798
|
+
/**
|
|
799
|
+
* 清空所有指标
|
|
800
|
+
*/
|
|
801
|
+
clear(): void;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* 基础错误类
|
|
806
|
+
*
|
|
807
|
+
* @packageDocumentation
|
|
808
|
+
*/
|
|
809
|
+
/**
|
|
810
|
+
* 客户端基础错误类
|
|
811
|
+
*
|
|
812
|
+
* 所有自定义错误的基类,提供统一的错误处理能力
|
|
813
|
+
*/
|
|
814
|
+
declare abstract class BaseClientError extends Error {
|
|
815
|
+
/**
|
|
816
|
+
* 错误类型标识符
|
|
817
|
+
*/
|
|
818
|
+
abstract readonly type: string;
|
|
819
|
+
/**
|
|
820
|
+
* 是否可重试
|
|
821
|
+
*/
|
|
822
|
+
abstract readonly retryable: boolean;
|
|
823
|
+
/**
|
|
824
|
+
* 错误发生时间
|
|
825
|
+
*/
|
|
826
|
+
readonly timestamp: Date;
|
|
827
|
+
constructor(message: string);
|
|
828
|
+
/**
|
|
829
|
+
* 转换为 JSON 对象(用于日志和序列化)
|
|
830
|
+
*/
|
|
831
|
+
toJSON(): Record<string, unknown>;
|
|
832
|
+
/**
|
|
833
|
+
* 转换为字符串
|
|
834
|
+
*/
|
|
835
|
+
toString(): string;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* API 错误类
|
|
840
|
+
*
|
|
841
|
+
* @packageDocumentation
|
|
842
|
+
*/
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* 错误详情
|
|
846
|
+
*/
|
|
847
|
+
interface ApiErrorDetail {
|
|
848
|
+
/**
|
|
849
|
+
* 字段名
|
|
850
|
+
*/
|
|
851
|
+
readonly field?: string;
|
|
852
|
+
/**
|
|
853
|
+
* 错误消息
|
|
854
|
+
*/
|
|
855
|
+
readonly message: string;
|
|
856
|
+
/**
|
|
857
|
+
* 错误码
|
|
858
|
+
*/
|
|
859
|
+
readonly code?: string;
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* API 错误配置
|
|
863
|
+
*/
|
|
864
|
+
interface ApiErrorConfig {
|
|
865
|
+
/**
|
|
866
|
+
* 错误消息
|
|
867
|
+
*/
|
|
868
|
+
readonly message: string;
|
|
869
|
+
/**
|
|
870
|
+
* 业务错误码
|
|
871
|
+
*/
|
|
872
|
+
readonly code: string;
|
|
873
|
+
/**
|
|
874
|
+
* HTTP 状态码
|
|
875
|
+
*/
|
|
876
|
+
readonly statusCode: number;
|
|
877
|
+
/**
|
|
878
|
+
* 错误详情
|
|
879
|
+
*/
|
|
880
|
+
readonly details?: readonly ApiErrorDetail[];
|
|
881
|
+
/**
|
|
882
|
+
* 请求 ID
|
|
883
|
+
*/
|
|
884
|
+
readonly requestId: string;
|
|
885
|
+
/**
|
|
886
|
+
* 追踪 ID
|
|
887
|
+
*/
|
|
888
|
+
readonly traceId?: string;
|
|
889
|
+
/**
|
|
890
|
+
* Retry-After 头的值(秒)
|
|
891
|
+
*/
|
|
892
|
+
readonly retryAfter?: number;
|
|
893
|
+
/**
|
|
894
|
+
* 原始响应数据
|
|
895
|
+
*/
|
|
896
|
+
readonly rawResponse?: unknown;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* API 业务错误
|
|
900
|
+
*
|
|
901
|
+
* 表示服务器返回的业务错误响应(HTTP 非 2xx)
|
|
902
|
+
*/
|
|
903
|
+
declare class ApiError extends BaseClientError {
|
|
904
|
+
readonly type: "API_ERROR";
|
|
905
|
+
/**
|
|
906
|
+
* 业务错误码
|
|
907
|
+
*/
|
|
908
|
+
readonly code: string;
|
|
909
|
+
/**
|
|
910
|
+
* HTTP 状态码
|
|
911
|
+
*/
|
|
912
|
+
readonly statusCode: number;
|
|
913
|
+
/**
|
|
914
|
+
* 错误详情
|
|
915
|
+
*/
|
|
916
|
+
readonly details?: readonly ApiErrorDetail[];
|
|
917
|
+
/**
|
|
918
|
+
* 请求 ID
|
|
919
|
+
*/
|
|
920
|
+
readonly requestId: string;
|
|
921
|
+
/**
|
|
922
|
+
* 追踪 ID
|
|
923
|
+
*/
|
|
924
|
+
readonly traceId?: string;
|
|
925
|
+
/**
|
|
926
|
+
* Retry-After 头的值(秒)
|
|
927
|
+
*/
|
|
928
|
+
readonly retryAfterSeconds?: number;
|
|
929
|
+
/**
|
|
930
|
+
* 原始响应数据
|
|
931
|
+
*/
|
|
932
|
+
readonly rawResponse?: unknown;
|
|
933
|
+
constructor(config: ApiErrorConfig);
|
|
934
|
+
/**
|
|
935
|
+
* 是否可重试
|
|
936
|
+
*
|
|
937
|
+
* 5xx 和 429 错误通常可重试
|
|
938
|
+
*/
|
|
939
|
+
get retryable(): boolean;
|
|
940
|
+
/**
|
|
941
|
+
* 是否为未授权错误 (401)
|
|
942
|
+
*/
|
|
943
|
+
isUnauthorized(): boolean;
|
|
944
|
+
/**
|
|
945
|
+
* 是否为禁止访问错误 (403)
|
|
946
|
+
*/
|
|
947
|
+
isForbidden(): boolean;
|
|
948
|
+
/**
|
|
949
|
+
* 是否为认证相关错误 (401/403)
|
|
950
|
+
*/
|
|
951
|
+
isAuthError(): boolean;
|
|
952
|
+
/**
|
|
953
|
+
* 是否为未找到错误 (404)
|
|
954
|
+
*/
|
|
955
|
+
isNotFound(): boolean;
|
|
956
|
+
/**
|
|
957
|
+
* 是否为冲突错误 (409)
|
|
958
|
+
*/
|
|
959
|
+
isConflict(): boolean;
|
|
960
|
+
/**
|
|
961
|
+
* 是否为验证错误 (400/422)
|
|
962
|
+
*/
|
|
963
|
+
isValidationError(): boolean;
|
|
964
|
+
/**
|
|
965
|
+
* 是否为限流错误 (429)
|
|
966
|
+
*/
|
|
967
|
+
isRateLimited(): boolean;
|
|
968
|
+
/**
|
|
969
|
+
* 是否为客户端错误 (4xx)
|
|
970
|
+
*/
|
|
971
|
+
isClientError(): boolean;
|
|
972
|
+
/**
|
|
973
|
+
* 是否为服务端错误 (5xx)
|
|
974
|
+
*/
|
|
975
|
+
isServerError(): boolean;
|
|
976
|
+
/**
|
|
977
|
+
* 获取 Retry-After 值(秒)
|
|
978
|
+
*/
|
|
979
|
+
getRetryAfter(): number | undefined;
|
|
980
|
+
/**
|
|
981
|
+
* 获取推荐的重试延迟(毫秒)
|
|
982
|
+
*
|
|
983
|
+
* @param defaultMs - 默认延迟(毫秒)
|
|
984
|
+
*/
|
|
985
|
+
getRetryDelayMs(defaultMs?: number): number;
|
|
986
|
+
toJSON(): Record<string, unknown>;
|
|
987
|
+
/**
|
|
988
|
+
* 类型守卫:判断是否为 ApiError
|
|
989
|
+
*/
|
|
990
|
+
static is(error: unknown): error is ApiError;
|
|
991
|
+
/**
|
|
992
|
+
* 从响应数据创建 ApiError
|
|
993
|
+
*/
|
|
994
|
+
static fromResponse(data: {
|
|
995
|
+
code?: string;
|
|
996
|
+
message?: string;
|
|
997
|
+
details?: ApiErrorDetail[];
|
|
998
|
+
requestId?: string;
|
|
999
|
+
traceId?: string;
|
|
1000
|
+
}, statusCode: number, retryAfter?: number): ApiError;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
* 网络错误类
|
|
1005
|
+
*
|
|
1006
|
+
* @packageDocumentation
|
|
1007
|
+
*/
|
|
1008
|
+
|
|
1009
|
+
/**
|
|
1010
|
+
* 网络错误
|
|
1011
|
+
*
|
|
1012
|
+
* 表示网络层面的错误,如 DNS 解析失败、连接被拒绝等
|
|
1013
|
+
*/
|
|
1014
|
+
declare class NetworkError extends BaseClientError {
|
|
1015
|
+
readonly type: "NETWORK_ERROR";
|
|
1016
|
+
readonly retryable = true;
|
|
1017
|
+
/**
|
|
1018
|
+
* 原始错误
|
|
1019
|
+
*/
|
|
1020
|
+
readonly cause?: Error;
|
|
1021
|
+
constructor(message: string, cause?: Error);
|
|
1022
|
+
toJSON(): Record<string, unknown>;
|
|
1023
|
+
/**
|
|
1024
|
+
* 类型守卫:判断是否为 NetworkError
|
|
1025
|
+
*/
|
|
1026
|
+
static is(error: unknown): error is NetworkError;
|
|
1027
|
+
/**
|
|
1028
|
+
* 从原生 fetch 错误创建 NetworkError
|
|
1029
|
+
*/
|
|
1030
|
+
static fromFetchError(error: Error): NetworkError;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* 超时错误类
|
|
1035
|
+
*
|
|
1036
|
+
* @packageDocumentation
|
|
1037
|
+
*/
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
* 超时错误
|
|
1041
|
+
*
|
|
1042
|
+
* 表示请求超时
|
|
1043
|
+
*/
|
|
1044
|
+
declare class TimeoutError extends BaseClientError {
|
|
1045
|
+
readonly type: "TIMEOUT_ERROR";
|
|
1046
|
+
readonly retryable = true;
|
|
1047
|
+
/**
|
|
1048
|
+
* 超时时间(毫秒)
|
|
1049
|
+
*/
|
|
1050
|
+
readonly timeoutMs: number;
|
|
1051
|
+
constructor(timeoutMs: number);
|
|
1052
|
+
toJSON(): Record<string, unknown>;
|
|
1053
|
+
/**
|
|
1054
|
+
* 类型守卫:判断是否为 TimeoutError
|
|
1055
|
+
*/
|
|
1056
|
+
static is(error: unknown): error is TimeoutError;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* 取消错误类
|
|
1061
|
+
*
|
|
1062
|
+
* @packageDocumentation
|
|
1063
|
+
*/
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* 取消错误
|
|
1067
|
+
*
|
|
1068
|
+
* 表示请求被主动取消
|
|
1069
|
+
*/
|
|
1070
|
+
declare class AbortError extends BaseClientError {
|
|
1071
|
+
readonly type: "ABORT_ERROR";
|
|
1072
|
+
readonly retryable = false;
|
|
1073
|
+
/**
|
|
1074
|
+
* 取消原因
|
|
1075
|
+
*/
|
|
1076
|
+
readonly reason?: string;
|
|
1077
|
+
constructor(reason?: string);
|
|
1078
|
+
toJSON(): Record<string, unknown>;
|
|
1079
|
+
/**
|
|
1080
|
+
* 类型守卫:判断是否为 AbortError
|
|
1081
|
+
*/
|
|
1082
|
+
static is(error: unknown): error is AbortError;
|
|
1083
|
+
/**
|
|
1084
|
+
* 从原生 AbortError 创建
|
|
1085
|
+
*/
|
|
1086
|
+
static fromNative(error: Error): AbortError;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* 错误模块
|
|
1091
|
+
*
|
|
1092
|
+
* @packageDocumentation
|
|
1093
|
+
*/
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* 判断错误是否可重试
|
|
1097
|
+
*/
|
|
1098
|
+
declare function isRetryableError(error: unknown): boolean;
|
|
1099
|
+
/**
|
|
1100
|
+
* 获取错误的推荐重试延迟(毫秒)
|
|
1101
|
+
*
|
|
1102
|
+
* @param error - 错误对象
|
|
1103
|
+
* @param defaultMs - 默认延迟
|
|
1104
|
+
*/
|
|
1105
|
+
declare function getRetryDelay(error: unknown, defaultMs?: number): number;
|
|
1106
|
+
/**
|
|
1107
|
+
* 判断是否为客户端错误类型
|
|
1108
|
+
*/
|
|
1109
|
+
declare function isClientError(error: unknown): error is ApiError | NetworkError | TimeoutError | AbortError;
|
|
1110
|
+
/**
|
|
1111
|
+
* 获取错误类型标识符
|
|
1112
|
+
*/
|
|
1113
|
+
declare function getErrorType(error: unknown): string;
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* 认证器接口
|
|
1117
|
+
*
|
|
1118
|
+
* @packageDocumentation
|
|
1119
|
+
*/
|
|
1120
|
+
/**
|
|
1121
|
+
* 认证器接口
|
|
1122
|
+
*
|
|
1123
|
+
* 定义了认证器的基本行为
|
|
1124
|
+
*/
|
|
1125
|
+
interface Authenticator {
|
|
1126
|
+
/**
|
|
1127
|
+
* 认证类型
|
|
1128
|
+
*/
|
|
1129
|
+
readonly type: string;
|
|
1130
|
+
/**
|
|
1131
|
+
* 向请求头添加认证信息
|
|
1132
|
+
*
|
|
1133
|
+
* @param headers - 请求头对象(可变)
|
|
1134
|
+
* @returns Promise<void> 或 void
|
|
1135
|
+
*/
|
|
1136
|
+
authenticate(headers: Record<string, string>): Promise<void> | void;
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* 认证器工厂函数类型
|
|
1140
|
+
*/
|
|
1141
|
+
type AuthenticatorFactory<TConfig> = (config: TConfig) => Authenticator;
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* Bearer Token 认证器
|
|
1145
|
+
*
|
|
1146
|
+
* @packageDocumentation
|
|
1147
|
+
*/
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* Bearer Token 认证器
|
|
1151
|
+
*
|
|
1152
|
+
* 将 Bearer Token 添加到 Authorization 头
|
|
1153
|
+
*/
|
|
1154
|
+
declare class BearerAuthenticator implements Authenticator {
|
|
1155
|
+
readonly type: "bearer";
|
|
1156
|
+
private readonly getToken;
|
|
1157
|
+
private readonly headerName;
|
|
1158
|
+
private readonly prefix;
|
|
1159
|
+
constructor(config: Omit<BearerAuthConfig, 'type'>);
|
|
1160
|
+
authenticate(headers: Record<string, string>): Promise<void>;
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* 创建 Bearer Token 认证器
|
|
1164
|
+
*/
|
|
1165
|
+
declare function createBearerAuthenticator(config: Omit<BearerAuthConfig, 'type'>): BearerAuthenticator;
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* API Key 认证器
|
|
1169
|
+
*
|
|
1170
|
+
* @packageDocumentation
|
|
1171
|
+
*/
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* API Key 认证器
|
|
1175
|
+
*
|
|
1176
|
+
* 将 API Key 添加到指定的请求头
|
|
1177
|
+
*/
|
|
1178
|
+
declare class ApiKeyAuthenticator implements Authenticator {
|
|
1179
|
+
readonly type: "api-key";
|
|
1180
|
+
private readonly apiKey;
|
|
1181
|
+
private readonly headerName;
|
|
1182
|
+
constructor(config: Omit<ApiKeyAuthConfig, 'type'>);
|
|
1183
|
+
authenticate(headers: Record<string, string>): void;
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* 创建 API Key 认证器
|
|
1187
|
+
*/
|
|
1188
|
+
declare function createApiKeyAuthenticator(config: Omit<ApiKeyAuthConfig, 'type'>): ApiKeyAuthenticator;
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* Basic Auth 认证器
|
|
1192
|
+
*
|
|
1193
|
+
* @packageDocumentation
|
|
1194
|
+
*/
|
|
1195
|
+
|
|
1196
|
+
/**
|
|
1197
|
+
* Basic Auth 认证器
|
|
1198
|
+
*
|
|
1199
|
+
* 将用户名和密码以 Base64 编码添加到 Authorization 头
|
|
1200
|
+
*/
|
|
1201
|
+
declare class BasicAuthenticator implements Authenticator {
|
|
1202
|
+
readonly type: "basic";
|
|
1203
|
+
private readonly encodedCredentials;
|
|
1204
|
+
constructor(config: Omit<BasicAuthConfig, 'type'>);
|
|
1205
|
+
authenticate(headers: Record<string, string>): void;
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* 创建 Basic Auth 认证器
|
|
1209
|
+
*/
|
|
1210
|
+
declare function createBasicAuthenticator(config: Omit<BasicAuthConfig, 'type'>): BasicAuthenticator;
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* 自定义认证器
|
|
1214
|
+
*
|
|
1215
|
+
* @packageDocumentation
|
|
1216
|
+
*/
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* 自定义认证器
|
|
1220
|
+
*
|
|
1221
|
+
* 允许用户自定义认证逻辑
|
|
1222
|
+
*/
|
|
1223
|
+
declare class CustomAuthenticator implements Authenticator {
|
|
1224
|
+
readonly type: "custom";
|
|
1225
|
+
private readonly authenticateFn;
|
|
1226
|
+
constructor(config: Omit<CustomAuthConfig, 'type'>);
|
|
1227
|
+
authenticate(headers: Record<string, string>): Promise<void>;
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* 创建自定义认证器
|
|
1231
|
+
*/
|
|
1232
|
+
declare function createCustomAuthenticator(config: Omit<CustomAuthConfig, 'type'>): CustomAuthenticator;
|
|
1233
|
+
|
|
1234
|
+
/**
|
|
1235
|
+
* 无认证器
|
|
1236
|
+
*
|
|
1237
|
+
* @packageDocumentation
|
|
1238
|
+
*/
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* 无认证器
|
|
1242
|
+
*
|
|
1243
|
+
* 不进行任何认证操作
|
|
1244
|
+
*/
|
|
1245
|
+
declare class NoAuthenticator implements Authenticator {
|
|
1246
|
+
readonly type: "none";
|
|
1247
|
+
authenticate(_headers: Record<string, string>): void;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* 创建无认证器
|
|
1251
|
+
*/
|
|
1252
|
+
declare function createNoAuthenticator(): NoAuthenticator;
|
|
1253
|
+
/**
|
|
1254
|
+
* 无认证器单例
|
|
1255
|
+
*/
|
|
1256
|
+
declare const noAuthenticator: NoAuthenticator;
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* 认证模块
|
|
1260
|
+
*
|
|
1261
|
+
* @packageDocumentation
|
|
1262
|
+
*/
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* 从配置创建认证器
|
|
1266
|
+
*
|
|
1267
|
+
* @param config - 认证配置
|
|
1268
|
+
* @returns 认证器实例
|
|
1269
|
+
*/
|
|
1270
|
+
declare function createAuthenticatorFromConfig(config: AuthConfig): Authenticator;
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* 拦截器管理器
|
|
1274
|
+
*
|
|
1275
|
+
* 管理拦截器的注册、排序和执行
|
|
1276
|
+
*
|
|
1277
|
+
* @packageDocumentation
|
|
1278
|
+
*/
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* 拦截器管理器
|
|
1282
|
+
*
|
|
1283
|
+
* 负责:
|
|
1284
|
+
* 1. 管理拦截器的注册和移除
|
|
1285
|
+
* 2. 按优先级排序拦截器
|
|
1286
|
+
* 3. 顺序执行拦截器链
|
|
1287
|
+
*/
|
|
1288
|
+
declare class InterceptorManager {
|
|
1289
|
+
private readonly requestInterceptors;
|
|
1290
|
+
private readonly responseInterceptors;
|
|
1291
|
+
private readonly errorInterceptors;
|
|
1292
|
+
/**
|
|
1293
|
+
* 获取所有拦截器
|
|
1294
|
+
*/
|
|
1295
|
+
get interceptors(): Interceptors;
|
|
1296
|
+
/**
|
|
1297
|
+
* 添加请求拦截器
|
|
1298
|
+
*
|
|
1299
|
+
* @param interceptor - 请求拦截器
|
|
1300
|
+
* @returns 返回自身,支持链式调用
|
|
1301
|
+
*/
|
|
1302
|
+
addRequestInterceptor(interceptor: RequestInterceptor): this;
|
|
1303
|
+
/**
|
|
1304
|
+
* 添加响应拦截器
|
|
1305
|
+
*
|
|
1306
|
+
* @param interceptor - 响应拦截器
|
|
1307
|
+
* @returns 返回自身,支持链式调用
|
|
1308
|
+
*/
|
|
1309
|
+
addResponseInterceptor(interceptor: ResponseInterceptor): this;
|
|
1310
|
+
/**
|
|
1311
|
+
* 添加错误拦截器
|
|
1312
|
+
*
|
|
1313
|
+
* @param interceptor - 错误拦截器
|
|
1314
|
+
* @returns 返回自身,支持链式调用
|
|
1315
|
+
*/
|
|
1316
|
+
addErrorInterceptor(interceptor: ErrorInterceptor): this;
|
|
1317
|
+
/**
|
|
1318
|
+
* 批量添加拦截器
|
|
1319
|
+
*
|
|
1320
|
+
* @param interceptors - 拦截器配置
|
|
1321
|
+
* @returns 返回自身,支持链式调用
|
|
1322
|
+
*/
|
|
1323
|
+
addInterceptors(interceptors: Partial<Interceptors>): this;
|
|
1324
|
+
/**
|
|
1325
|
+
* 移除请求拦截器
|
|
1326
|
+
*
|
|
1327
|
+
* @param name - 拦截器名称
|
|
1328
|
+
* @returns 是否移除成功
|
|
1329
|
+
*/
|
|
1330
|
+
removeRequestInterceptor(name: string): boolean;
|
|
1331
|
+
/**
|
|
1332
|
+
* 移除响应拦截器
|
|
1333
|
+
*
|
|
1334
|
+
* @param name - 拦截器名称
|
|
1335
|
+
* @returns 是否移除成功
|
|
1336
|
+
*/
|
|
1337
|
+
removeResponseInterceptor(name: string): boolean;
|
|
1338
|
+
/**
|
|
1339
|
+
* 移除错误拦截器
|
|
1340
|
+
*
|
|
1341
|
+
* @param name - 拦截器名称
|
|
1342
|
+
* @returns 是否移除成功
|
|
1343
|
+
*/
|
|
1344
|
+
removeErrorInterceptor(name: string): boolean;
|
|
1345
|
+
/**
|
|
1346
|
+
* 清除所有拦截器
|
|
1347
|
+
*/
|
|
1348
|
+
clear(): void;
|
|
1349
|
+
/**
|
|
1350
|
+
* 执行请求拦截器链
|
|
1351
|
+
*
|
|
1352
|
+
* @param context - 请求上下文
|
|
1353
|
+
* @returns 修改后的请求选项
|
|
1354
|
+
*/
|
|
1355
|
+
executeRequestInterceptors(context: RequestContext$1): Promise<MutableRequestOptions>;
|
|
1356
|
+
/**
|
|
1357
|
+
* 执行响应拦截器链
|
|
1358
|
+
*
|
|
1359
|
+
* @param response - 响应数据
|
|
1360
|
+
* @param context - 请求上下文
|
|
1361
|
+
* @returns 修改后的响应数据
|
|
1362
|
+
*/
|
|
1363
|
+
executeResponseInterceptors<T>(response: ResponseData<T>, context: RequestContext$1): Promise<ResponseData<T>>;
|
|
1364
|
+
/**
|
|
1365
|
+
* 执行错误拦截器链
|
|
1366
|
+
*
|
|
1367
|
+
* @param error - 错误对象
|
|
1368
|
+
* @param context - 请求上下文
|
|
1369
|
+
* @returns 如果返回 ResponseData,则表示错误被恢复;否则错误继续传播
|
|
1370
|
+
* @throws 如果拦截器抛出新错误,则使用新错误
|
|
1371
|
+
*/
|
|
1372
|
+
executeErrorInterceptors<T>(error: Error, context: RequestContext$1): Promise<ResponseData<T> | void>;
|
|
1373
|
+
/**
|
|
1374
|
+
* 按 order 排序拦截器
|
|
1375
|
+
*/
|
|
1376
|
+
private sortInterceptors;
|
|
1377
|
+
/**
|
|
1378
|
+
* 按名称移除拦截器
|
|
1379
|
+
*/
|
|
1380
|
+
private removeByName;
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* 创建拦截器管理器
|
|
1384
|
+
*/
|
|
1385
|
+
declare function createInterceptorManager(): InterceptorManager;
|
|
1386
|
+
|
|
1387
|
+
/**
|
|
1388
|
+
* 认证拦截器
|
|
1389
|
+
*
|
|
1390
|
+
* 自动向请求添加认证信息
|
|
1391
|
+
*
|
|
1392
|
+
* @packageDocumentation
|
|
1393
|
+
*/
|
|
1394
|
+
|
|
1395
|
+
/**
|
|
1396
|
+
* 认证拦截器配置
|
|
1397
|
+
*/
|
|
1398
|
+
interface AuthInterceptorConfig {
|
|
1399
|
+
/**
|
|
1400
|
+
* 认证器实例
|
|
1401
|
+
*/
|
|
1402
|
+
readonly authenticator: Authenticator;
|
|
1403
|
+
/**
|
|
1404
|
+
* 执行顺序
|
|
1405
|
+
* @default -100 (优先执行)
|
|
1406
|
+
*/
|
|
1407
|
+
readonly order?: number;
|
|
1408
|
+
}
|
|
1409
|
+
/**
|
|
1410
|
+
* 认证拦截器
|
|
1411
|
+
*
|
|
1412
|
+
* 在请求发送前添加认证信息
|
|
1413
|
+
*/
|
|
1414
|
+
declare class AuthInterceptor implements RequestInterceptor {
|
|
1415
|
+
readonly name = "auth";
|
|
1416
|
+
readonly order: number;
|
|
1417
|
+
private readonly authenticator;
|
|
1418
|
+
constructor(config: AuthInterceptorConfig);
|
|
1419
|
+
intercept(context: RequestContext$1): Promise<MutableRequestOptions>;
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* 创建认证拦截器
|
|
1423
|
+
*/
|
|
1424
|
+
declare function createAuthInterceptor(config: AuthInterceptorConfig): AuthInterceptor;
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* 请求 ID 拦截器
|
|
1428
|
+
*
|
|
1429
|
+
* 自动生成并添加请求 ID
|
|
1430
|
+
*
|
|
1431
|
+
* @packageDocumentation
|
|
1432
|
+
*/
|
|
1433
|
+
|
|
1434
|
+
/**
|
|
1435
|
+
* 请求 ID 拦截器配置
|
|
1436
|
+
*/
|
|
1437
|
+
interface RequestIdInterceptorConfig {
|
|
1438
|
+
/**
|
|
1439
|
+
* 请求 ID 头名称
|
|
1440
|
+
* @default 'X-Request-ID'
|
|
1441
|
+
*/
|
|
1442
|
+
readonly headerName?: string;
|
|
1443
|
+
/**
|
|
1444
|
+
* 自定义 ID 生成函数
|
|
1445
|
+
*/
|
|
1446
|
+
readonly generateId?: () => string;
|
|
1447
|
+
/**
|
|
1448
|
+
* 执行顺序
|
|
1449
|
+
* @default -90
|
|
1450
|
+
*/
|
|
1451
|
+
readonly order?: number;
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* 请求 ID 拦截器
|
|
1455
|
+
*
|
|
1456
|
+
* 为每个请求自动生成唯一的请求 ID
|
|
1457
|
+
*/
|
|
1458
|
+
declare class RequestIdInterceptor implements RequestInterceptor {
|
|
1459
|
+
readonly name = "request-id";
|
|
1460
|
+
readonly order: number;
|
|
1461
|
+
private readonly headerName;
|
|
1462
|
+
private readonly generateId;
|
|
1463
|
+
constructor(config?: RequestIdInterceptorConfig);
|
|
1464
|
+
intercept(context: RequestContext$1): MutableRequestOptions;
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* 创建请求 ID 拦截器
|
|
1468
|
+
*/
|
|
1469
|
+
declare function createRequestIdInterceptor(config?: RequestIdInterceptorConfig): RequestIdInterceptor;
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* 追踪上下文拦截器
|
|
1473
|
+
*
|
|
1474
|
+
* 自动添加追踪 ID 和 OpenTelemetry 上下文
|
|
1475
|
+
*
|
|
1476
|
+
* @packageDocumentation
|
|
1477
|
+
*/
|
|
1478
|
+
|
|
1479
|
+
/**
|
|
1480
|
+
* 追踪拦截器配置
|
|
1481
|
+
*/
|
|
1482
|
+
interface TraceInterceptorConfig {
|
|
1483
|
+
/**
|
|
1484
|
+
* 追踪 ID 头名称
|
|
1485
|
+
* @default 'X-Trace-ID'
|
|
1486
|
+
*/
|
|
1487
|
+
readonly traceIdHeader?: string;
|
|
1488
|
+
/**
|
|
1489
|
+
* 是否添加 traceparent 头(W3C Trace Context)
|
|
1490
|
+
* @default false
|
|
1491
|
+
*/
|
|
1492
|
+
readonly addTraceparent?: boolean;
|
|
1493
|
+
/**
|
|
1494
|
+
* 获取追踪 ID 的函数
|
|
1495
|
+
* 如果未提供,将自动生成
|
|
1496
|
+
*/
|
|
1497
|
+
readonly getTraceId?: () => string | undefined;
|
|
1498
|
+
/**
|
|
1499
|
+
* 获取 traceparent 的函数
|
|
1500
|
+
*/
|
|
1501
|
+
readonly getTraceparent?: () => string | undefined;
|
|
1502
|
+
/**
|
|
1503
|
+
* 执行顺序
|
|
1504
|
+
* @default -80
|
|
1505
|
+
*/
|
|
1506
|
+
readonly order?: number;
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
* 追踪上下文拦截器
|
|
1510
|
+
*
|
|
1511
|
+
* 为请求添加追踪信息,支持分布式追踪
|
|
1512
|
+
*/
|
|
1513
|
+
declare class TraceInterceptor implements RequestInterceptor {
|
|
1514
|
+
readonly name = "trace";
|
|
1515
|
+
readonly order: number;
|
|
1516
|
+
private readonly traceIdHeader;
|
|
1517
|
+
private readonly addTraceparent;
|
|
1518
|
+
private readonly getTraceId?;
|
|
1519
|
+
private readonly getTraceparent?;
|
|
1520
|
+
constructor(config?: TraceInterceptorConfig);
|
|
1521
|
+
intercept(context: RequestContext$1): MutableRequestOptions;
|
|
1522
|
+
}
|
|
1523
|
+
/**
|
|
1524
|
+
* 创建追踪拦截器
|
|
1525
|
+
*/
|
|
1526
|
+
declare function createTraceInterceptor(config?: TraceInterceptorConfig): TraceInterceptor;
|
|
1527
|
+
|
|
1528
|
+
/**
|
|
1529
|
+
* 超时拦截器
|
|
1530
|
+
*
|
|
1531
|
+
* 根据请求类型自动设置超时时间
|
|
1532
|
+
*
|
|
1533
|
+
* @packageDocumentation
|
|
1534
|
+
*/
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* 超时拦截器配置
|
|
1538
|
+
*/
|
|
1539
|
+
interface TimeoutInterceptorConfig {
|
|
1540
|
+
/**
|
|
1541
|
+
* 默认超时时间(毫秒)
|
|
1542
|
+
* @default 30000
|
|
1543
|
+
*/
|
|
1544
|
+
readonly defaultTimeout?: number;
|
|
1545
|
+
/**
|
|
1546
|
+
* 按方法设置超时时间
|
|
1547
|
+
* 例如: { GET: 10000, POST: 60000 }
|
|
1548
|
+
*/
|
|
1549
|
+
readonly methodTimeouts?: Partial<Record<HttpMethod, number>>;
|
|
1550
|
+
/**
|
|
1551
|
+
* 按路径模式设置超时时间
|
|
1552
|
+
* 使用正则表达式匹配路径
|
|
1553
|
+
*/
|
|
1554
|
+
readonly pathTimeouts?: ReadonlyArray<{
|
|
1555
|
+
pattern: RegExp;
|
|
1556
|
+
timeout: number;
|
|
1557
|
+
}>;
|
|
1558
|
+
/**
|
|
1559
|
+
* 执行顺序
|
|
1560
|
+
* @default -70
|
|
1561
|
+
*/
|
|
1562
|
+
readonly order?: number;
|
|
1563
|
+
}
|
|
1564
|
+
/**
|
|
1565
|
+
* 超时拦截器
|
|
1566
|
+
*
|
|
1567
|
+
* 智能设置请求超时时间
|
|
1568
|
+
*/
|
|
1569
|
+
declare class TimeoutInterceptor implements RequestInterceptor {
|
|
1570
|
+
readonly name = "timeout";
|
|
1571
|
+
readonly order: number;
|
|
1572
|
+
private readonly defaultTimeout;
|
|
1573
|
+
private readonly methodTimeouts;
|
|
1574
|
+
private readonly pathTimeouts;
|
|
1575
|
+
constructor(config?: TimeoutInterceptorConfig);
|
|
1576
|
+
intercept(context: RequestContext$1): MutableRequestOptions;
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1579
|
+
* 创建超时拦截器
|
|
1580
|
+
*/
|
|
1581
|
+
declare function createTimeoutInterceptor(config?: TimeoutInterceptorConfig): TimeoutInterceptor;
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* 重试拦截器
|
|
1585
|
+
*
|
|
1586
|
+
* 处理请求失败时的自动重试
|
|
1587
|
+
*
|
|
1588
|
+
* @packageDocumentation
|
|
1589
|
+
*/
|
|
1590
|
+
|
|
1591
|
+
/**
|
|
1592
|
+
* 重试拦截器配置
|
|
1593
|
+
*/
|
|
1594
|
+
interface RetryInterceptorConfig extends Partial<RetryConfig> {
|
|
1595
|
+
/**
|
|
1596
|
+
* 执行重试请求的函数
|
|
1597
|
+
* 由客户端注入
|
|
1598
|
+
*/
|
|
1599
|
+
readonly executeRequest: <T>(context: RequestContext$1) => Promise<ResponseData<T>>;
|
|
1600
|
+
/**
|
|
1601
|
+
* 执行顺序
|
|
1602
|
+
* @default 100 (最后执行)
|
|
1603
|
+
*/
|
|
1604
|
+
readonly order?: number;
|
|
1605
|
+
}
|
|
1606
|
+
/**
|
|
1607
|
+
* 重试拦截器
|
|
1608
|
+
*
|
|
1609
|
+
* 在发生可重试错误时自动重试请求
|
|
1610
|
+
*
|
|
1611
|
+
* 注意:这个拦截器应该注册为错误拦截器,而不是响应拦截器
|
|
1612
|
+
*/
|
|
1613
|
+
declare class RetryInterceptor implements ErrorInterceptor {
|
|
1614
|
+
readonly name = "retry";
|
|
1615
|
+
readonly order: number;
|
|
1616
|
+
private readonly config;
|
|
1617
|
+
private readonly executeRequest;
|
|
1618
|
+
constructor(config: RetryInterceptorConfig);
|
|
1619
|
+
intercept<T>(error: Error, context: RequestContext$1): Promise<ResponseData<T> | void>;
|
|
1620
|
+
/**
|
|
1621
|
+
* 判断是否应该重试
|
|
1622
|
+
*/
|
|
1623
|
+
private shouldRetry;
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
* 创建重试拦截器
|
|
1627
|
+
*/
|
|
1628
|
+
declare function createRetryInterceptor(config: RetryInterceptorConfig): RetryInterceptor;
|
|
1629
|
+
|
|
1630
|
+
/**
|
|
1631
|
+
* Token 刷新拦截器
|
|
1632
|
+
*
|
|
1633
|
+
* 在收到 401 响应时自动刷新 Token 并重试
|
|
1634
|
+
*
|
|
1635
|
+
* @packageDocumentation
|
|
1636
|
+
*/
|
|
1637
|
+
|
|
1638
|
+
/**
|
|
1639
|
+
* Token 刷新拦截器配置
|
|
1640
|
+
*/
|
|
1641
|
+
interface TokenRefreshInterceptorConfig extends TokenRefreshConfig {
|
|
1642
|
+
/**
|
|
1643
|
+
* 执行重试请求的函数
|
|
1644
|
+
* 由客户端注入
|
|
1645
|
+
*/
|
|
1646
|
+
readonly executeRequest: <T>(context: RequestContext$1) => Promise<ResponseData<T>>;
|
|
1647
|
+
/**
|
|
1648
|
+
* Token 刷新后的回调
|
|
1649
|
+
* 用于更新存储的 Token
|
|
1650
|
+
*/
|
|
1651
|
+
readonly onTokenRefreshed?: (newToken: string) => void;
|
|
1652
|
+
/**
|
|
1653
|
+
* 执行顺序
|
|
1654
|
+
* @default 50 (在重试拦截器之前)
|
|
1655
|
+
*/
|
|
1656
|
+
readonly order?: number;
|
|
1657
|
+
}
|
|
1
1658
|
/**
|
|
2
|
-
*
|
|
1659
|
+
* Token 刷新拦截器
|
|
3
1660
|
*
|
|
4
|
-
*
|
|
1661
|
+
* 功能:
|
|
1662
|
+
* 1. 在收到 401 响应时自动刷新 Token
|
|
1663
|
+
* 2. 合并并发的 Token 刷新请求,避免重复刷新
|
|
1664
|
+
* 3. 刷新成功后自动重试原请求
|
|
1665
|
+
* 4. 刷新失败时触发回调
|
|
1666
|
+
*/
|
|
1667
|
+
declare class TokenRefreshInterceptor implements ErrorInterceptor {
|
|
1668
|
+
readonly name = "token-refresh";
|
|
1669
|
+
readonly order: number;
|
|
1670
|
+
private readonly refreshToken;
|
|
1671
|
+
private readonly executeRequest;
|
|
1672
|
+
private readonly triggerStatusCodes;
|
|
1673
|
+
private readonly maxRetries;
|
|
1674
|
+
private readonly onTokenRefreshed?;
|
|
1675
|
+
private readonly onRefreshFailed?;
|
|
1676
|
+
private readonly state;
|
|
1677
|
+
constructor(config: TokenRefreshInterceptorConfig);
|
|
1678
|
+
intercept<T>(error: Error, context: RequestContext$1): Promise<ResponseData<T> | void>;
|
|
1679
|
+
/**
|
|
1680
|
+
* 判断是否应该刷新 Token
|
|
1681
|
+
*/
|
|
1682
|
+
private shouldRefresh;
|
|
1683
|
+
/**
|
|
1684
|
+
* 执行 Token 刷新
|
|
1685
|
+
*
|
|
1686
|
+
* 合并并发请求,确保同一时间只有一个刷新请求
|
|
1687
|
+
*/
|
|
1688
|
+
private doRefresh;
|
|
1689
|
+
/**
|
|
1690
|
+
* 重置状态(用于测试或手动重置)
|
|
1691
|
+
*/
|
|
1692
|
+
reset(): void;
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* 创建 Token 刷新拦截器
|
|
5
1696
|
*/
|
|
1697
|
+
declare function createTokenRefreshInterceptor(config: TokenRefreshInterceptorConfig): TokenRefreshInterceptor;
|
|
1698
|
+
|
|
6
1699
|
/**
|
|
7
|
-
*
|
|
1700
|
+
* 错误转换拦截器
|
|
1701
|
+
*
|
|
1702
|
+
* 将原始响应错误转换为结构化的错误对象
|
|
1703
|
+
*
|
|
1704
|
+
* @packageDocumentation
|
|
8
1705
|
*/
|
|
9
|
-
|
|
1706
|
+
|
|
10
1707
|
/**
|
|
11
|
-
*
|
|
1708
|
+
* 错误转换拦截器配置
|
|
12
1709
|
*/
|
|
13
|
-
|
|
1710
|
+
interface ErrorTransformInterceptorConfig {
|
|
1711
|
+
/**
|
|
1712
|
+
* 是否在响应中包含原始数据
|
|
1713
|
+
* @default false
|
|
1714
|
+
*/
|
|
1715
|
+
readonly includeRawResponse?: boolean;
|
|
1716
|
+
/**
|
|
1717
|
+
* 自定义错误消息提取函数
|
|
1718
|
+
*/
|
|
1719
|
+
readonly extractErrorMessage?: (data: unknown) => string | undefined;
|
|
1720
|
+
/**
|
|
1721
|
+
* 自定义错误码提取函数
|
|
1722
|
+
*/
|
|
1723
|
+
readonly extractErrorCode?: (data: unknown) => string | undefined;
|
|
1724
|
+
/**
|
|
1725
|
+
* 执行顺序
|
|
1726
|
+
* @default 0
|
|
1727
|
+
*/
|
|
1728
|
+
readonly order?: number;
|
|
1729
|
+
}
|
|
1730
|
+
/**
|
|
1731
|
+
* 错误转换拦截器
|
|
1732
|
+
*
|
|
1733
|
+
* 处理响应数据,提取错误信息
|
|
1734
|
+
* 注意:这个拦截器主要用于标准化响应格式
|
|
1735
|
+
*/
|
|
1736
|
+
declare class ErrorTransformInterceptor implements ResponseInterceptor {
|
|
1737
|
+
readonly name = "error-transform";
|
|
1738
|
+
readonly order: number;
|
|
1739
|
+
private readonly includeRawResponse;
|
|
1740
|
+
private readonly extractErrorMessage?;
|
|
1741
|
+
private readonly extractErrorCode?;
|
|
1742
|
+
constructor(config?: ErrorTransformInterceptorConfig);
|
|
1743
|
+
intercept<T>(response: ResponseData<T>, _context: RequestContext$1): ResponseData<T>;
|
|
1744
|
+
}
|
|
14
1745
|
/**
|
|
15
|
-
*
|
|
1746
|
+
* 创建错误转换拦截器
|
|
16
1747
|
*/
|
|
17
|
-
declare function
|
|
18
|
-
name: string;
|
|
19
|
-
version: string;
|
|
20
|
-
};
|
|
1748
|
+
declare function createErrorTransformInterceptor(config?: ErrorTransformInterceptorConfig): ErrorTransformInterceptor;
|
|
21
1749
|
|
|
22
1750
|
/**
|
|
23
|
-
*
|
|
1751
|
+
* 错误日志拦截器
|
|
24
1752
|
*
|
|
25
|
-
*
|
|
1753
|
+
* 记录所有请求错误
|
|
26
1754
|
*
|
|
27
|
-
*
|
|
28
|
-
* - DjvApiError: 业务层错误(服务器返回的错误响应)
|
|
29
|
-
* - NetworkError: 网络层错误(连接失败、DNS 解析失败等)
|
|
30
|
-
* - TimeoutError: 超时错误(继承自 NetworkError)
|
|
31
|
-
* - AbortError: 请求被取消(继承自 NetworkError)
|
|
1755
|
+
* @packageDocumentation
|
|
32
1756
|
*/
|
|
1757
|
+
|
|
33
1758
|
/**
|
|
34
|
-
*
|
|
1759
|
+
* 错误日志拦截器配置
|
|
1760
|
+
*/
|
|
1761
|
+
interface LoggingInterceptorConfig {
|
|
1762
|
+
/**
|
|
1763
|
+
* 日志器
|
|
1764
|
+
*/
|
|
1765
|
+
readonly logger: Logger;
|
|
1766
|
+
/**
|
|
1767
|
+
* 是否记录详细信息(包括请求体、响应体等)
|
|
1768
|
+
* @default false
|
|
1769
|
+
*/
|
|
1770
|
+
readonly verbose?: boolean;
|
|
1771
|
+
/**
|
|
1772
|
+
* 是否记录取消的请求
|
|
1773
|
+
* @default false
|
|
1774
|
+
*/
|
|
1775
|
+
readonly logAborted?: boolean;
|
|
1776
|
+
/**
|
|
1777
|
+
* 错误过滤器
|
|
1778
|
+
* 返回 true 表示记录该错误
|
|
1779
|
+
*/
|
|
1780
|
+
readonly filter?: (error: Error, context: RequestContext$1) => boolean;
|
|
1781
|
+
/**
|
|
1782
|
+
* 执行顺序
|
|
1783
|
+
* @default -100 (优先执行,确保先记录日志)
|
|
1784
|
+
*/
|
|
1785
|
+
readonly order?: number;
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* 错误日志拦截器
|
|
35
1789
|
*
|
|
36
|
-
*
|
|
1790
|
+
* 记录错误信息用于调试和监控
|
|
1791
|
+
*/
|
|
1792
|
+
declare class LoggingInterceptor implements ErrorInterceptor {
|
|
1793
|
+
readonly name = "logging";
|
|
1794
|
+
readonly order: number;
|
|
1795
|
+
private readonly logger;
|
|
1796
|
+
private readonly verbose;
|
|
1797
|
+
private readonly logAborted;
|
|
1798
|
+
private readonly filter?;
|
|
1799
|
+
constructor(config: LoggingInterceptorConfig);
|
|
1800
|
+
intercept<T>(error: Error, context: RequestContext$1): void | ResponseData<T>;
|
|
1801
|
+
/**
|
|
1802
|
+
* 构建日志信息
|
|
1803
|
+
*/
|
|
1804
|
+
private buildLogInfo;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* 创建错误日志拦截器
|
|
1808
|
+
*/
|
|
1809
|
+
declare function createLoggingInterceptor(config: LoggingInterceptorConfig): LoggingInterceptor;
|
|
1810
|
+
|
|
1811
|
+
/**
|
|
1812
|
+
* 错误上报拦截器
|
|
37
1813
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* await client.PageApi.resolvePage({ pageUid: 'xxx' });
|
|
42
|
-
* } catch (e) {
|
|
43
|
-
* if (DjvApiError.is(e)) {
|
|
44
|
-
* console.log('业务错误:', e.code, e.message);
|
|
45
|
-
* console.log('追踪ID:', e.traceId);
|
|
46
|
-
*
|
|
47
|
-
* // 使用增强方法
|
|
48
|
-
* if (e.isAuthError()) {
|
|
49
|
-
* redirectToLogin();
|
|
50
|
-
* } else if (e.isRateLimited()) {
|
|
51
|
-
* const retryAfter = e.getRetryAfter();
|
|
52
|
-
* await delay(retryAfter * 1000);
|
|
53
|
-
* }
|
|
54
|
-
* }
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
1814
|
+
* 将错误上报到监控系统(如 Sentry)
|
|
1815
|
+
*
|
|
1816
|
+
* @packageDocumentation
|
|
57
1817
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
readonly response?: Response | undefined;
|
|
69
|
-
/** 标识这是一个 API 错误(用于 instanceof 替代方案) */
|
|
70
|
-
readonly isApiError: true;
|
|
71
|
-
/** 错误名称 */
|
|
72
|
-
readonly name: "DjvApiError";
|
|
73
|
-
constructor(message: string,
|
|
74
|
-
/** 业务错误码(可能是字符串如 'INVALID_REQUEST' 或数字如 1001) */
|
|
75
|
-
code: string | number,
|
|
76
|
-
/** HTTP 状态码 */
|
|
77
|
-
status: number,
|
|
78
|
-
/** 追踪 ID(用于日志排查) */
|
|
79
|
-
traceId?: string | undefined,
|
|
80
|
-
/** 详细错误信息(字段级错误等) */
|
|
81
|
-
details?: unknown | undefined,
|
|
82
|
-
/** 原始响应对象 */
|
|
83
|
-
response?: Response | undefined);
|
|
84
|
-
/**
|
|
85
|
-
* 是否为认证错误 (401 Unauthorized)
|
|
1818
|
+
|
|
1819
|
+
/**
|
|
1820
|
+
* 错误上报器接口
|
|
1821
|
+
*/
|
|
1822
|
+
interface ErrorReporter {
|
|
1823
|
+
/**
|
|
1824
|
+
* 上报错误
|
|
1825
|
+
*
|
|
1826
|
+
* @param error - 错误对象
|
|
1827
|
+
* @param context - 错误上下文
|
|
86
1828
|
*/
|
|
87
|
-
|
|
1829
|
+
report(error: Error, context: Record<string, unknown>): void;
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* 错误严重程度
|
|
1833
|
+
*/
|
|
1834
|
+
type ErrorSeverity = 'fatal' | 'error' | 'warning' | 'info' | 'debug';
|
|
1835
|
+
/**
|
|
1836
|
+
* 错误上报拦截器配置
|
|
1837
|
+
*/
|
|
1838
|
+
interface ReportingInterceptorConfig {
|
|
88
1839
|
/**
|
|
89
|
-
*
|
|
1840
|
+
* 错误上报器
|
|
90
1841
|
*/
|
|
91
|
-
|
|
1842
|
+
readonly reporter: ErrorReporter;
|
|
92
1843
|
/**
|
|
93
|
-
*
|
|
1844
|
+
* 获取错误严重程度
|
|
1845
|
+
* @default 根据错误类型自动判断
|
|
94
1846
|
*/
|
|
95
|
-
|
|
1847
|
+
readonly getSeverity?: (error: Error) => ErrorSeverity;
|
|
96
1848
|
/**
|
|
97
|
-
*
|
|
1849
|
+
* 是否上报客户端错误(4xx)
|
|
1850
|
+
* @default false
|
|
98
1851
|
*/
|
|
99
|
-
|
|
1852
|
+
readonly reportClientErrors?: boolean;
|
|
100
1853
|
/**
|
|
101
|
-
*
|
|
1854
|
+
* 是否上报取消的请求
|
|
1855
|
+
* @default false
|
|
102
1856
|
*/
|
|
103
|
-
|
|
1857
|
+
readonly reportAborted?: boolean;
|
|
104
1858
|
/**
|
|
105
|
-
*
|
|
1859
|
+
* 是否上报网络错误
|
|
1860
|
+
* @default true
|
|
106
1861
|
*/
|
|
107
|
-
|
|
1862
|
+
readonly reportNetworkErrors?: boolean;
|
|
108
1863
|
/**
|
|
109
|
-
*
|
|
1864
|
+
* 采样率(0-1)
|
|
1865
|
+
* 用于减少上报量
|
|
1866
|
+
* @default 1 (100% 上报)
|
|
110
1867
|
*/
|
|
111
|
-
|
|
1868
|
+
readonly sampleRate?: number;
|
|
112
1869
|
/**
|
|
113
|
-
*
|
|
1870
|
+
* 自定义上下文提取器
|
|
114
1871
|
*/
|
|
115
|
-
|
|
1872
|
+
readonly extractContext?: (error: Error, context: RequestContext$1) => Record<string, unknown>;
|
|
116
1873
|
/**
|
|
117
|
-
*
|
|
1874
|
+
* 执行顺序
|
|
1875
|
+
* @default -50 (在日志之后)
|
|
118
1876
|
*/
|
|
119
|
-
|
|
1877
|
+
readonly order?: number;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* 错误上报拦截器
|
|
1881
|
+
*
|
|
1882
|
+
* 将错误上报到外部监控系统
|
|
1883
|
+
*/
|
|
1884
|
+
declare class ReportingInterceptor implements ErrorInterceptor {
|
|
1885
|
+
readonly name = "reporting";
|
|
1886
|
+
readonly order: number;
|
|
1887
|
+
private readonly reporter;
|
|
1888
|
+
private readonly getSeverity;
|
|
1889
|
+
private readonly reportClientErrors;
|
|
1890
|
+
private readonly reportAborted;
|
|
1891
|
+
private readonly reportNetworkErrors;
|
|
1892
|
+
private readonly sampleRate;
|
|
1893
|
+
private readonly extractContext?;
|
|
1894
|
+
constructor(config: ReportingInterceptorConfig);
|
|
1895
|
+
intercept<T>(error: Error, context: RequestContext$1): void | ResponseData<T>;
|
|
120
1896
|
/**
|
|
121
|
-
*
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
*
|
|
1897
|
+
* 判断是否应该上报
|
|
1898
|
+
*/
|
|
1899
|
+
private shouldReport;
|
|
1900
|
+
/**
|
|
1901
|
+
* 构建上报上下文
|
|
1902
|
+
*/
|
|
1903
|
+
private buildReportContext;
|
|
1904
|
+
/**
|
|
1905
|
+
* 默认的严重程度判断
|
|
1906
|
+
*/
|
|
1907
|
+
private defaultGetSeverity;
|
|
1908
|
+
}
|
|
1909
|
+
/**
|
|
1910
|
+
* 创建错误上报拦截器
|
|
1911
|
+
*/
|
|
1912
|
+
declare function createReportingInterceptor(config: ReportingInterceptorConfig): ReportingInterceptor;
|
|
1913
|
+
|
|
1914
|
+
/**
|
|
1915
|
+
* 请求去重插件
|
|
1916
|
+
*
|
|
1917
|
+
* 避免相同请求并发执行多次
|
|
1918
|
+
*
|
|
1919
|
+
* @packageDocumentation
|
|
1920
|
+
*/
|
|
1921
|
+
/**
|
|
1922
|
+
* 请求去重器配置
|
|
1923
|
+
*/
|
|
1924
|
+
interface DeduplicationConfig {
|
|
1925
|
+
/**
|
|
1926
|
+
* 是否只对 GET 请求去重
|
|
1927
|
+
* @default true
|
|
1928
|
+
*/
|
|
1929
|
+
readonly getOnly?: boolean;
|
|
1930
|
+
/**
|
|
1931
|
+
* 自定义请求键生成函数
|
|
1932
|
+
* 默认使用 method + url
|
|
1933
|
+
*/
|
|
1934
|
+
readonly generateKey?: (method: string, url: string) => string;
|
|
1935
|
+
/**
|
|
1936
|
+
* 最大缓存数量
|
|
1937
|
+
* @default 100
|
|
126
1938
|
*/
|
|
127
|
-
|
|
1939
|
+
readonly maxSize?: number;
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* 请求去重器
|
|
1943
|
+
*
|
|
1944
|
+
* 功能:
|
|
1945
|
+
* 1. 相同的并发请求只执行一次
|
|
1946
|
+
* 2. 结果共享给所有等待者
|
|
1947
|
+
* 3. 请求完成后自动清理
|
|
1948
|
+
*/
|
|
1949
|
+
declare class RequestDeduper {
|
|
1950
|
+
private readonly getOnly;
|
|
1951
|
+
private readonly generateKey;
|
|
1952
|
+
private readonly maxSize;
|
|
1953
|
+
private readonly inflightRequests;
|
|
1954
|
+
constructor(config?: DeduplicationConfig);
|
|
128
1955
|
/**
|
|
129
|
-
*
|
|
1956
|
+
* 执行请求(带去重)
|
|
130
1957
|
*
|
|
131
|
-
*
|
|
1958
|
+
* @param method - HTTP 方法
|
|
1959
|
+
* @param url - 请求 URL
|
|
1960
|
+
* @param execute - 实际执行请求的函数
|
|
1961
|
+
* @returns 请求结果
|
|
1962
|
+
*/
|
|
1963
|
+
execute<T>(method: string, url: string, execute: () => Promise<T>): Promise<T>;
|
|
1964
|
+
/**
|
|
1965
|
+
* 包装 fetch 函数
|
|
132
1966
|
*
|
|
133
|
-
* @param
|
|
1967
|
+
* @param fetch - 原始 fetch 函数
|
|
1968
|
+
* @returns 去重的 fetch 函数
|
|
1969
|
+
*/
|
|
1970
|
+
wrap(fetch: (url: string, init?: RequestInit) => Promise<Response>): (url: string, init?: RequestInit) => Promise<Response>;
|
|
1971
|
+
/**
|
|
1972
|
+
* 获取当前飞行中的请求数量
|
|
1973
|
+
*/
|
|
1974
|
+
get inflightCount(): number;
|
|
1975
|
+
/**
|
|
1976
|
+
* 清空所有飞行中的请求
|
|
1977
|
+
*/
|
|
1978
|
+
clear(): void;
|
|
1979
|
+
/**
|
|
1980
|
+
* 判断是否应该去重
|
|
1981
|
+
*/
|
|
1982
|
+
private shouldDeduplicate;
|
|
1983
|
+
/**
|
|
1984
|
+
* 默认的键生成函数
|
|
1985
|
+
*/
|
|
1986
|
+
private defaultGenerateKey;
|
|
1987
|
+
/**
|
|
1988
|
+
* 清理过期的请求
|
|
1989
|
+
*/
|
|
1990
|
+
private cleanup;
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* 创建请求去重器
|
|
1994
|
+
*/
|
|
1995
|
+
declare function createRequestDeduper(config?: DeduplicationConfig): RequestDeduper;
|
|
1996
|
+
|
|
1997
|
+
/**
|
|
1998
|
+
* 指标收集插件
|
|
1999
|
+
*
|
|
2000
|
+
* 收集请求级别的性能指标
|
|
2001
|
+
*
|
|
2002
|
+
* @packageDocumentation
|
|
2003
|
+
*/
|
|
2004
|
+
|
|
2005
|
+
/**
|
|
2006
|
+
* 默认指标收集器实现
|
|
2007
|
+
*/
|
|
2008
|
+
declare class DefaultMetricsCollector implements MetricsCollector {
|
|
2009
|
+
private metrics;
|
|
2010
|
+
private readonly maxMetrics;
|
|
2011
|
+
private readonly ttlMs;
|
|
2012
|
+
private readonly onMetrics?;
|
|
2013
|
+
constructor(config?: MetricsCollectorConfig);
|
|
2014
|
+
record(metrics: RequestMetrics): void;
|
|
2015
|
+
summary(): MetricsSummary;
|
|
2016
|
+
getAll(): readonly RequestMetrics[];
|
|
2017
|
+
getByPath(path: string): readonly RequestMetrics[];
|
|
2018
|
+
flush(): readonly RequestMetrics[];
|
|
2019
|
+
clear(): void;
|
|
2020
|
+
private cleanup;
|
|
2021
|
+
private emptySummary;
|
|
2022
|
+
}
|
|
2023
|
+
/**
|
|
2024
|
+
* 创建指标收集器
|
|
2025
|
+
*/
|
|
2026
|
+
declare function createMetricsCollector(config?: MetricsCollectorConfig): MetricsCollector;
|
|
2027
|
+
/**
|
|
2028
|
+
* 指标请求拦截器
|
|
2029
|
+
*
|
|
2030
|
+
* 在请求开始时记录时间戳
|
|
2031
|
+
*/
|
|
2032
|
+
declare class MetricsRequestInterceptor implements RequestInterceptor {
|
|
2033
|
+
readonly name = "metrics-request";
|
|
2034
|
+
readonly order = -1000;
|
|
2035
|
+
intercept(context: RequestContext$1): MutableRequestOptions;
|
|
2036
|
+
}
|
|
2037
|
+
/**
|
|
2038
|
+
* 指标响应拦截器
|
|
2039
|
+
*
|
|
2040
|
+
* 在请求成功时记录指标
|
|
2041
|
+
*/
|
|
2042
|
+
declare class MetricsResponseInterceptor implements ResponseInterceptor {
|
|
2043
|
+
private readonly collector;
|
|
2044
|
+
readonly name = "metrics-response";
|
|
2045
|
+
readonly order = 1000;
|
|
2046
|
+
constructor(collector: MetricsCollector);
|
|
2047
|
+
intercept<T>(response: ResponseData<T>, context: RequestContext$1): ResponseData<T>;
|
|
2048
|
+
}
|
|
2049
|
+
/**
|
|
2050
|
+
* 指标错误拦截器
|
|
2051
|
+
*
|
|
2052
|
+
* 在请求失败时记录指标
|
|
2053
|
+
*/
|
|
2054
|
+
declare class MetricsErrorInterceptor implements ErrorInterceptor {
|
|
2055
|
+
private readonly collector;
|
|
2056
|
+
readonly name = "metrics-error";
|
|
2057
|
+
readonly order = 1000;
|
|
2058
|
+
constructor(collector: MetricsCollector);
|
|
2059
|
+
intercept<T>(error: Error, context: RequestContext$1): void | ResponseData<T>;
|
|
2060
|
+
}
|
|
2061
|
+
/**
|
|
2062
|
+
* 创建指标拦截器集合
|
|
2063
|
+
*/
|
|
2064
|
+
declare function createMetricsInterceptors(collector: MetricsCollector): {
|
|
2065
|
+
request: MetricsRequestInterceptor;
|
|
2066
|
+
response: MetricsResponseInterceptor;
|
|
2067
|
+
error: MetricsErrorInterceptor;
|
|
2068
|
+
};
|
|
2069
|
+
|
|
2070
|
+
/**
|
|
2071
|
+
* 日志插件
|
|
2072
|
+
*
|
|
2073
|
+
* 提供可配置的日志器实现
|
|
2074
|
+
*
|
|
2075
|
+
* @packageDocumentation
|
|
2076
|
+
*/
|
|
2077
|
+
|
|
2078
|
+
/**
|
|
2079
|
+
* 控制台日志器
|
|
2080
|
+
*
|
|
2081
|
+
* 将日志输出到控制台
|
|
2082
|
+
*/
|
|
2083
|
+
declare class ConsoleLogger implements Logger {
|
|
2084
|
+
private readonly prefix;
|
|
2085
|
+
private readonly level;
|
|
2086
|
+
private readonly timestamp;
|
|
2087
|
+
private readonly levelPriority;
|
|
2088
|
+
constructor(config?: ConsoleLoggerConfig);
|
|
2089
|
+
debug(message: string, ...args: unknown[]): void;
|
|
2090
|
+
info(message: string, ...args: unknown[]): void;
|
|
2091
|
+
warn(message: string, ...args: unknown[]): void;
|
|
2092
|
+
error(message: string, ...args: unknown[]): void;
|
|
2093
|
+
private log;
|
|
2094
|
+
private formatMessage;
|
|
2095
|
+
}
|
|
2096
|
+
/**
|
|
2097
|
+
* 创建控制台日志器
|
|
2098
|
+
*/
|
|
2099
|
+
declare function createConsoleLogger(config?: ConsoleLoggerConfig): ConsoleLogger;
|
|
2100
|
+
/**
|
|
2101
|
+
* 静默日志器
|
|
2102
|
+
*
|
|
2103
|
+
* 不输出任何日志
|
|
2104
|
+
*/
|
|
2105
|
+
declare class SilentLogger implements Logger {
|
|
2106
|
+
debug(_message: string, ..._args: unknown[]): void;
|
|
2107
|
+
info(_message: string, ..._args: unknown[]): void;
|
|
2108
|
+
warn(_message: string, ..._args: unknown[]): void;
|
|
2109
|
+
error(_message: string, ..._args: unknown[]): void;
|
|
2110
|
+
}
|
|
2111
|
+
/**
|
|
2112
|
+
* 创建静默日志器
|
|
2113
|
+
*/
|
|
2114
|
+
declare function createSilentLogger(): SilentLogger;
|
|
2115
|
+
/**
|
|
2116
|
+
* 静默日志器单例
|
|
2117
|
+
*/
|
|
2118
|
+
declare const silentLogger: SilentLogger;
|
|
2119
|
+
/**
|
|
2120
|
+
* 缓冲日志器
|
|
2121
|
+
*
|
|
2122
|
+
* 将日志存储在内存中,用于测试
|
|
2123
|
+
*/
|
|
2124
|
+
declare class BufferLogger implements Logger {
|
|
2125
|
+
private readonly buffer;
|
|
2126
|
+
private readonly maxSize;
|
|
2127
|
+
constructor(maxSize?: number);
|
|
2128
|
+
debug(message: string, ...args: unknown[]): void;
|
|
2129
|
+
info(message: string, ...args: unknown[]): void;
|
|
2130
|
+
warn(message: string, ...args: unknown[]): void;
|
|
2131
|
+
error(message: string, ...args: unknown[]): void;
|
|
2132
|
+
/**
|
|
2133
|
+
* 获取所有日志
|
|
2134
|
+
*/
|
|
2135
|
+
getLogs(): ReadonlyArray<{
|
|
2136
|
+
level: LogLevel;
|
|
2137
|
+
message: string;
|
|
2138
|
+
args: unknown[];
|
|
2139
|
+
timestamp: Date;
|
|
2140
|
+
}>;
|
|
2141
|
+
/**
|
|
2142
|
+
* 获取指定级别的日志
|
|
2143
|
+
*/
|
|
2144
|
+
getLogsByLevel(level: LogLevel): ReadonlyArray<{
|
|
2145
|
+
level: LogLevel;
|
|
2146
|
+
message: string;
|
|
2147
|
+
args: unknown[];
|
|
2148
|
+
timestamp: Date;
|
|
2149
|
+
}>;
|
|
2150
|
+
/**
|
|
2151
|
+
* 清空日志
|
|
2152
|
+
*/
|
|
2153
|
+
clear(): void;
|
|
2154
|
+
/**
|
|
2155
|
+
* 获取日志数量
|
|
2156
|
+
*/
|
|
2157
|
+
get size(): number;
|
|
2158
|
+
private add;
|
|
2159
|
+
}
|
|
2160
|
+
/**
|
|
2161
|
+
* 创建缓冲日志器
|
|
2162
|
+
*/
|
|
2163
|
+
declare function createBufferLogger(maxSize?: number): BufferLogger;
|
|
2164
|
+
|
|
2165
|
+
/**
|
|
2166
|
+
* 基础客户端抽象类
|
|
2167
|
+
*
|
|
2168
|
+
* 定义 HTTP 客户端的基本接口和生命周期
|
|
2169
|
+
*
|
|
2170
|
+
* @packageDocumentation
|
|
2171
|
+
*/
|
|
2172
|
+
|
|
2173
|
+
/**
|
|
2174
|
+
* 基础客户端抽象类
|
|
2175
|
+
*
|
|
2176
|
+
* 提供:
|
|
2177
|
+
* 1. 配置管理
|
|
2178
|
+
* 2. 拦截器管理
|
|
2179
|
+
* 3. 认证管理
|
|
2180
|
+
* 4. 请求上下文创建
|
|
2181
|
+
*/
|
|
2182
|
+
declare abstract class BaseClient {
|
|
2183
|
+
protected readonly config: ClientConfig;
|
|
2184
|
+
protected readonly interceptorManager: InterceptorManager;
|
|
2185
|
+
protected readonly authenticator: Authenticator;
|
|
2186
|
+
constructor(config: ClientConfig);
|
|
2187
|
+
/**
|
|
2188
|
+
* 发起请求(子类实现)
|
|
2189
|
+
*/
|
|
2190
|
+
abstract request<T>(options: RequestOptions): Promise<T>;
|
|
2191
|
+
/**
|
|
2192
|
+
* GET 请求
|
|
2193
|
+
*/
|
|
2194
|
+
get<T>(path: string, query?: Record<string, string | number | boolean | undefined | null>): Promise<T>;
|
|
2195
|
+
/**
|
|
2196
|
+
* POST 请求
|
|
2197
|
+
*/
|
|
2198
|
+
post<T>(path: string, body?: unknown, query?: Record<string, string | number | boolean | undefined | null>): Promise<T>;
|
|
2199
|
+
/**
|
|
2200
|
+
* PUT 请求
|
|
2201
|
+
*/
|
|
2202
|
+
put<T>(path: string, body?: unknown): Promise<T>;
|
|
2203
|
+
/**
|
|
2204
|
+
* PATCH 请求
|
|
2205
|
+
*/
|
|
2206
|
+
patch<T>(path: string, body?: unknown): Promise<T>;
|
|
2207
|
+
/**
|
|
2208
|
+
* DELETE 请求
|
|
2209
|
+
*/
|
|
2210
|
+
delete<T>(path: string): Promise<T>;
|
|
2211
|
+
/**
|
|
2212
|
+
* 获取拦截器管理器
|
|
2213
|
+
*/
|
|
2214
|
+
get interceptors(): InterceptorManager;
|
|
2215
|
+
/**
|
|
2216
|
+
* 创建请求上下文
|
|
2217
|
+
*/
|
|
2218
|
+
protected createRequestContext(options: RequestOptions): RequestContext$1;
|
|
2219
|
+
/**
|
|
2220
|
+
* 执行请求拦截器
|
|
2221
|
+
*/
|
|
2222
|
+
protected runRequestInterceptors(context: RequestContext$1): Promise<MutableRequestOptions>;
|
|
2223
|
+
/**
|
|
2224
|
+
* 执行响应拦截器
|
|
2225
|
+
*/
|
|
2226
|
+
protected runResponseInterceptors<T>(response: ResponseData<T>, context: RequestContext$1): Promise<ResponseData<T>>;
|
|
2227
|
+
/**
|
|
2228
|
+
* 执行错误拦截器
|
|
2229
|
+
*/
|
|
2230
|
+
protected runErrorInterceptors<T>(error: Error, context: RequestContext$1): Promise<ResponseData<T> | void>;
|
|
2231
|
+
/**
|
|
2232
|
+
* 注册配置中的拦截器
|
|
2233
|
+
*/
|
|
2234
|
+
private registerConfigInterceptors;
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
/**
|
|
2238
|
+
* Fetch 客户端实现
|
|
2239
|
+
*
|
|
2240
|
+
* 基于原生 fetch API 的 HTTP 客户端
|
|
2241
|
+
*
|
|
2242
|
+
* @packageDocumentation
|
|
2243
|
+
*/
|
|
2244
|
+
|
|
2245
|
+
/**
|
|
2246
|
+
* Fetch 客户端配置
|
|
2247
|
+
*/
|
|
2248
|
+
interface FetchClientConfig extends ClientConfig {
|
|
2249
|
+
/**
|
|
2250
|
+
* 是否启用自动重试
|
|
2251
|
+
* @default true
|
|
2252
|
+
*/
|
|
2253
|
+
readonly enableRetry?: boolean;
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Fetch 客户端
|
|
2257
|
+
*
|
|
2258
|
+
* 特点:
|
|
2259
|
+
* 1. 基于原生 fetch API
|
|
2260
|
+
* 2. 支持完整的拦截器链
|
|
2261
|
+
* 3. 自动错误转换
|
|
2262
|
+
* 4. 超时和取消支持
|
|
2263
|
+
* 5. 自动重试
|
|
2264
|
+
* 6. 调试日志
|
|
2265
|
+
*/
|
|
2266
|
+
declare class FetchClient extends BaseClient {
|
|
2267
|
+
private readonly enableRetry;
|
|
2268
|
+
constructor(config: FetchClientConfig);
|
|
2269
|
+
/**
|
|
2270
|
+
* 发起请求
|
|
2271
|
+
*/
|
|
2272
|
+
request<T>(options: RequestOptions): Promise<T>;
|
|
2273
|
+
/**
|
|
2274
|
+
* 带重试的请求执行
|
|
2275
|
+
*/
|
|
2276
|
+
private executeWithRetry;
|
|
2277
|
+
/**
|
|
2278
|
+
* 判断是否应该重试
|
|
2279
|
+
*/
|
|
2280
|
+
private shouldRetry;
|
|
2281
|
+
/**
|
|
2282
|
+
* 执行单次请求
|
|
2283
|
+
*/
|
|
2284
|
+
private executeRequest;
|
|
2285
|
+
/**
|
|
2286
|
+
* 构建完整 URL
|
|
134
2287
|
*/
|
|
135
|
-
|
|
2288
|
+
private buildFullUrl;
|
|
136
2289
|
/**
|
|
137
|
-
*
|
|
2290
|
+
* 构建 fetch init 对象
|
|
138
2291
|
*/
|
|
139
|
-
|
|
2292
|
+
private buildFetchInit;
|
|
140
2293
|
/**
|
|
141
|
-
*
|
|
2294
|
+
* 创建超时信号
|
|
142
2295
|
*/
|
|
143
|
-
|
|
2296
|
+
private createTimeoutSignal;
|
|
144
2297
|
/**
|
|
145
|
-
*
|
|
2298
|
+
* 解析响应
|
|
146
2299
|
*/
|
|
147
|
-
|
|
148
|
-
name: "DjvApiError";
|
|
149
|
-
message: string;
|
|
150
|
-
code: string | number;
|
|
151
|
-
status: number;
|
|
152
|
-
traceId: string | undefined;
|
|
153
|
-
details: unknown;
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* 网络层错误
|
|
158
|
-
*
|
|
159
|
-
* 当请求无法到达服务器时抛出(连接失败、DNS 解析失败、CORS 等)。
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```typescript
|
|
163
|
-
* try {
|
|
164
|
-
* await client.PageApi.resolvePage({ pageUid: 'xxx' });
|
|
165
|
-
* } catch (e) {
|
|
166
|
-
* if (NetworkError.is(e)) {
|
|
167
|
-
* console.log('网络错误,请检查网络连接');
|
|
168
|
-
* }
|
|
169
|
-
* }
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
declare class NetworkError extends Error {
|
|
173
|
-
/** 标识这是一个网络错误 */
|
|
174
|
-
readonly isNetworkError: true;
|
|
175
|
-
/** 错误名称 */
|
|
176
|
-
readonly name: string;
|
|
177
|
-
/** 原始错误 */
|
|
178
|
-
readonly originalCause?: Error;
|
|
179
|
-
constructor(message: string,
|
|
180
|
-
/** 原始错误 */
|
|
181
|
-
cause?: Error);
|
|
2300
|
+
private parseResponse;
|
|
182
2301
|
/**
|
|
183
|
-
*
|
|
2302
|
+
* 转换错误
|
|
184
2303
|
*/
|
|
185
|
-
|
|
2304
|
+
private transformError;
|
|
186
2305
|
/**
|
|
187
|
-
*
|
|
2306
|
+
* 调试日志
|
|
188
2307
|
*/
|
|
189
|
-
|
|
2308
|
+
private logDebug;
|
|
190
2309
|
/**
|
|
191
|
-
*
|
|
2310
|
+
* 错误日志
|
|
192
2311
|
*/
|
|
193
|
-
|
|
194
|
-
toJSON(): Record<string, unknown>;
|
|
2312
|
+
private logError;
|
|
195
2313
|
}
|
|
196
2314
|
/**
|
|
197
|
-
*
|
|
2315
|
+
* 创建 Fetch 客户端
|
|
2316
|
+
*/
|
|
2317
|
+
declare function createFetchClient(config: FetchClientConfig): FetchClient;
|
|
2318
|
+
|
|
2319
|
+
/**
|
|
2320
|
+
* Axios 客户端实现
|
|
198
2321
|
*
|
|
199
|
-
*
|
|
2322
|
+
* 基于 Axios 的 HTTP 客户端,用于与 openapi-generator 生成的代码集成
|
|
200
2323
|
*
|
|
201
|
-
* @
|
|
202
|
-
* ```typescript
|
|
203
|
-
* try {
|
|
204
|
-
* await client.PageApi.resolvePage({ pageUid: 'xxx' });
|
|
205
|
-
* } catch (e) {
|
|
206
|
-
* if (TimeoutError.is(e)) {
|
|
207
|
-
* console.log(`请求超时 (${e.timeoutMs}ms),请稍后重试`);
|
|
208
|
-
* }
|
|
209
|
-
* }
|
|
210
|
-
* ```
|
|
2324
|
+
* @packageDocumentation
|
|
211
2325
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
2326
|
+
|
|
2327
|
+
declare module 'axios' {
|
|
2328
|
+
interface InternalAxiosRequestConfig {
|
|
2329
|
+
__retryCount?: number;
|
|
2330
|
+
__startTime?: number;
|
|
2331
|
+
__requestId?: string;
|
|
2332
|
+
__traceId?: string;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Axios 客户端配置
|
|
2337
|
+
*
|
|
2338
|
+
* 继承 ClientConfig 并添加 Axios 特有的选项
|
|
2339
|
+
*/
|
|
2340
|
+
interface AxiosClientConfig extends ClientConfig {
|
|
225
2341
|
/**
|
|
226
|
-
*
|
|
2342
|
+
* 是否启用自动重试
|
|
2343
|
+
* @default true
|
|
227
2344
|
*/
|
|
228
|
-
|
|
229
|
-
toJSON(): Record<string, unknown>;
|
|
2345
|
+
readonly enableRetry?: boolean;
|
|
230
2346
|
}
|
|
231
2347
|
/**
|
|
232
|
-
*
|
|
2348
|
+
* 创建配置好的 Axios 实例
|
|
233
2349
|
*
|
|
234
|
-
*
|
|
2350
|
+
* 用于注入到 openapi-generator 生成的 API 类中
|
|
235
2351
|
*
|
|
236
2352
|
* @example
|
|
237
2353
|
* ```typescript
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* }
|
|
2354
|
+
* import { createAxiosInstance, createConsoleLogger } from '@djvlc/openapi-client-core';
|
|
2355
|
+
* import { PagesApi, Configuration } from './generated';
|
|
2356
|
+
*
|
|
2357
|
+
* const axiosInstance = createAxiosInstance({
|
|
2358
|
+
* baseUrl: '/api/admin',
|
|
2359
|
+
* auth: { type: 'bearer', getToken: () => localStorage.getItem('token') },
|
|
2360
|
+
* retry: { maxRetries: 2, initialDelayMs: 1000, maxDelayMs: 10000, backoff: 'exponential' },
|
|
2361
|
+
* logger: createConsoleLogger({ prefix: '[Admin API]' }),
|
|
2362
|
+
* debug: true,
|
|
2363
|
+
* });
|
|
2364
|
+
*
|
|
2365
|
+
* const config = new Configuration();
|
|
2366
|
+
* const pagesApi = new PagesApi(config, undefined, axiosInstance);
|
|
248
2367
|
* ```
|
|
249
2368
|
*/
|
|
250
|
-
declare
|
|
251
|
-
|
|
252
|
-
readonly isAbort: true;
|
|
253
|
-
readonly name: string;
|
|
254
|
-
constructor(message?: string);
|
|
255
|
-
/**
|
|
256
|
-
* 类型保护:判断是否为 AbortError
|
|
257
|
-
*/
|
|
258
|
-
static is(error: unknown): error is AbortError;
|
|
259
|
-
}
|
|
2369
|
+
declare function createAxiosInstance(config: AxiosClientConfig): AxiosInstance;
|
|
2370
|
+
|
|
260
2371
|
/**
|
|
261
|
-
*
|
|
2372
|
+
* Middleware 工厂
|
|
262
2373
|
*
|
|
263
|
-
*
|
|
264
|
-
* - 超时错误
|
|
265
|
-
* - 网络错误(非取消)
|
|
266
|
-
* - 特定 HTTP 状态码:408, 429, 500, 502, 503, 504
|
|
267
|
-
*/
|
|
268
|
-
declare function isRetryableError(error: unknown): boolean;
|
|
269
|
-
/**
|
|
270
|
-
* 获取错误的推荐重试延迟(毫秒)
|
|
2374
|
+
* 将 openapi-client-core 的功能转换为 typescript-fetch 生成代码的 Middleware 格式
|
|
271
2375
|
*
|
|
272
|
-
*
|
|
2376
|
+
* 生成代码的 Middleware 接口:
|
|
2377
|
+
* - pre(context): 发请求前,可修改 url/init
|
|
2378
|
+
* - post(context): 收到响应后,可读/替换 Response
|
|
2379
|
+
* - onError(context): 网络错误时,可吞掉错误并返回兜底 Response
|
|
2380
|
+
*
|
|
2381
|
+
* @packageDocumentation
|
|
273
2382
|
*/
|
|
274
|
-
declare function getRetryDelay(error: unknown, defaultMs?: number): number;
|
|
275
2383
|
|
|
276
2384
|
/**
|
|
277
|
-
*
|
|
2385
|
+
* FetchAPI 类型(与生成代码兼容)
|
|
278
2386
|
*/
|
|
2387
|
+
type FetchAPI = WindowOrWorkerGlobalScope['fetch'];
|
|
279
2388
|
/**
|
|
280
|
-
*
|
|
2389
|
+
* 请求上下文(生成代码格式)
|
|
281
2390
|
*/
|
|
282
2391
|
interface RequestContext {
|
|
283
|
-
|
|
2392
|
+
fetch: FetchAPI;
|
|
284
2393
|
url: string;
|
|
285
|
-
/** 请求初始化配置 */
|
|
286
2394
|
init: RequestInit;
|
|
287
|
-
/** 额外元数据 */
|
|
288
|
-
meta?: Record<string, unknown>;
|
|
289
2395
|
}
|
|
290
2396
|
/**
|
|
291
|
-
*
|
|
2397
|
+
* 响应上下文(生成代码格式)
|
|
292
2398
|
*/
|
|
293
|
-
interface ResponseContext
|
|
294
|
-
|
|
2399
|
+
interface ResponseContext {
|
|
2400
|
+
fetch: FetchAPI;
|
|
2401
|
+
url: string;
|
|
2402
|
+
init: RequestInit;
|
|
295
2403
|
response: Response;
|
|
296
|
-
/** 请求耗时(毫秒) */
|
|
297
|
-
durationMs: number;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Middleware 定义
|
|
301
|
-
*/
|
|
302
|
-
interface Middleware {
|
|
303
|
-
/** 请求前钩子 */
|
|
304
|
-
pre?: (context: RequestContext) => Promise<RequestContext> | RequestContext;
|
|
305
|
-
/** 响应后钩子 */
|
|
306
|
-
post?: (context: ResponseContext) => Promise<void> | void;
|
|
307
2404
|
}
|
|
308
2405
|
/**
|
|
309
|
-
*
|
|
2406
|
+
* 错误上下文(生成代码格式)
|
|
310
2407
|
*/
|
|
311
|
-
interface
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
exponentialBackoff?: boolean;
|
|
318
|
-
/** 最大延迟时间(毫秒,默认 30000) */
|
|
319
|
-
maxDelayMs?: number;
|
|
320
|
-
/** 可重试的 HTTP 状态码(默认 [408, 429, 500, 502, 503, 504]) */
|
|
321
|
-
retryableStatuses?: number[];
|
|
322
|
-
/** 自定义重试判断函数 */
|
|
323
|
-
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
324
|
-
/** 重试前回调(用于日志等) */
|
|
325
|
-
onRetry?: (error: unknown, attempt: number, delayMs: number) => void;
|
|
326
|
-
/**
|
|
327
|
-
* 是否尊重 Retry-After 响应头
|
|
328
|
-
*
|
|
329
|
-
* 当服务器返回 429 或 503 时,优先使用 Retry-After 头中的延迟时间
|
|
330
|
-
*
|
|
331
|
-
* @default true
|
|
332
|
-
*/
|
|
333
|
-
respectRetryAfter?: boolean;
|
|
2408
|
+
interface ErrorContext {
|
|
2409
|
+
fetch: FetchAPI;
|
|
2410
|
+
url: string;
|
|
2411
|
+
init: RequestInit;
|
|
2412
|
+
error: unknown;
|
|
2413
|
+
response?: Response;
|
|
334
2414
|
}
|
|
335
2415
|
/**
|
|
336
|
-
*
|
|
2416
|
+
* Fetch 参数
|
|
337
2417
|
*/
|
|
338
|
-
interface
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
/** 响应日志 */
|
|
342
|
-
response?: (url: string, response: Response, durationMs: number) => void;
|
|
343
|
-
/** 错误日志 */
|
|
344
|
-
error?: (url: string, error: unknown) => void;
|
|
345
|
-
/** 重试日志 */
|
|
346
|
-
retry?: (url: string, attempt: number, error: unknown) => void;
|
|
2418
|
+
interface FetchParams {
|
|
2419
|
+
url: string;
|
|
2420
|
+
init: RequestInit;
|
|
347
2421
|
}
|
|
348
2422
|
/**
|
|
349
|
-
*
|
|
2423
|
+
* Middleware 接口(与生成代码兼容)
|
|
350
2424
|
*/
|
|
351
|
-
interface
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
response?: ResponseInterceptor[];
|
|
356
|
-
/** 错误拦截器 */
|
|
357
|
-
error?: ErrorInterceptor[];
|
|
2425
|
+
interface Middleware {
|
|
2426
|
+
pre?(context: RequestContext): Promise<FetchParams | void>;
|
|
2427
|
+
post?(context: ResponseContext): Promise<Response | void>;
|
|
2428
|
+
onError?(context: ErrorContext): Promise<Response | void>;
|
|
358
2429
|
}
|
|
359
2430
|
/**
|
|
360
|
-
*
|
|
361
|
-
*/
|
|
362
|
-
type RequestInterceptor = (context: RequestContext) => Promise<RequestContext> | RequestContext;
|
|
363
|
-
/**
|
|
364
|
-
* 响应拦截器
|
|
365
|
-
*/
|
|
366
|
-
type ResponseInterceptor = (response: Response, context: RequestContext) => Promise<Response> | Response;
|
|
367
|
-
/**
|
|
368
|
-
* 错误拦截器
|
|
2431
|
+
* Middleware 工厂配置
|
|
369
2432
|
*/
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
|
|
373
|
-
*/
|
|
374
|
-
interface BaseClientOptions {
|
|
375
|
-
/** 基础 URL */
|
|
376
|
-
baseUrl: string;
|
|
377
|
-
/** 获取认证 Token(Bearer) */
|
|
378
|
-
getAuthToken?: () => string | Promise<string>;
|
|
379
|
-
/** 获取 API Key */
|
|
380
|
-
getApiKey?: () => string | Promise<string>;
|
|
381
|
-
/** 获取追踪头(OpenTelemetry) */
|
|
382
|
-
getTraceHeaders?: () => Record<string, string>;
|
|
383
|
-
/** 默认请求头 */
|
|
384
|
-
defaultHeaders?: Record<string, string>;
|
|
385
|
-
/** 超时时间(毫秒,默认 30000) */
|
|
386
|
-
timeoutMs?: number;
|
|
387
|
-
/** 自定义 fetch 实现 */
|
|
388
|
-
fetchApi?: typeof fetch;
|
|
389
|
-
/** Cookie 策略(默认 'omit') */
|
|
390
|
-
credentials?: RequestCredentials;
|
|
2433
|
+
interface MiddlewareConfig {
|
|
2434
|
+
/** 认证配置 */
|
|
2435
|
+
auth?: AuthConfig;
|
|
391
2436
|
/** 重试配置 */
|
|
392
|
-
retry?:
|
|
393
|
-
/**
|
|
2437
|
+
retry?: RetryConfig;
|
|
2438
|
+
/** 是否启用重试 */
|
|
2439
|
+
enableRetry?: boolean;
|
|
2440
|
+
/** 日志器 */
|
|
394
2441
|
logger?: Logger;
|
|
395
2442
|
/** 是否开启调试模式 */
|
|
396
2443
|
debug?: boolean;
|
|
397
|
-
/**
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
interceptors?: Interceptors;
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* 请求额外配置(每个请求可覆盖)
|
|
404
|
-
*/
|
|
405
|
-
interface RequestOptions {
|
|
406
|
-
/** 取消信号 */
|
|
407
|
-
signal?: AbortSignal;
|
|
408
|
-
/** 超时时间(覆盖全局配置) */
|
|
409
|
-
timeoutMs?: number;
|
|
410
|
-
/** 是否跳过重试 */
|
|
411
|
-
skipRetry?: boolean;
|
|
412
|
-
/** 额外请求头 */
|
|
2444
|
+
/** 超时时间(毫秒) */
|
|
2445
|
+
timeout?: number;
|
|
2446
|
+
/** 额外的请求头 */
|
|
413
2447
|
headers?: Record<string, string>;
|
|
2448
|
+
/** 自定义 pre 中间件 */
|
|
2449
|
+
preMiddlewares?: Array<(context: RequestContext) => Promise<FetchParams | void>>;
|
|
2450
|
+
/** 自定义 post 中间件 */
|
|
2451
|
+
postMiddlewares?: Array<(context: ResponseContext) => Promise<Response | void>>;
|
|
2452
|
+
/** 自定义 onError 中间件 */
|
|
2453
|
+
errorMiddlewares?: Array<(context: ErrorContext) => Promise<Response | void>>;
|
|
414
2454
|
}
|
|
415
2455
|
/**
|
|
416
|
-
*
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
*
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
*
|
|
442
|
-
*/
|
|
443
|
-
declare function stripTrailingSlash(url: string): string;
|
|
444
|
-
/**
|
|
445
|
-
* 标准化 Headers
|
|
446
|
-
*/
|
|
447
|
-
declare function normalizeHeaders(initHeaders: HeadersInit | undefined): Headers;
|
|
448
|
-
/**
|
|
449
|
-
* 合并多个 Headers
|
|
450
|
-
*/
|
|
451
|
-
declare function mergeHeaders(...headersList: (HeadersInit | undefined)[]): Headers;
|
|
452
|
-
/**
|
|
453
|
-
* 延迟指定时间
|
|
454
|
-
*/
|
|
455
|
-
declare function delay(ms: number): Promise<void>;
|
|
456
|
-
/**
|
|
457
|
-
* 计算指数退避延迟
|
|
458
|
-
*/
|
|
459
|
-
declare function calculateBackoffDelay(attempt: number, baseDelayMs: number, maxDelayMs: number, exponential?: boolean): number;
|
|
460
|
-
/**
|
|
461
|
-
* 获取当前运行平台
|
|
462
|
-
*/
|
|
463
|
-
declare function getPlatform(): string;
|
|
464
|
-
/**
|
|
465
|
-
* 生成 SDK User-Agent 字符串
|
|
466
|
-
*/
|
|
467
|
-
declare function buildUserAgent(sdkName: string, sdkVersion: string): string;
|
|
468
|
-
/**
|
|
469
|
-
* 判断是否为浏览器环境
|
|
2456
|
+
* 创建 Middleware 数组
|
|
2457
|
+
*
|
|
2458
|
+
* 将 openapi-client-core 的功能转换为生成代码兼容的 Middleware 格式
|
|
2459
|
+
*
|
|
2460
|
+
* @param config - 配置选项
|
|
2461
|
+
* @returns Middleware 数组,可直接传给 Configuration
|
|
2462
|
+
*
|
|
2463
|
+
* @example
|
|
2464
|
+
* ```typescript
|
|
2465
|
+
* import { createMiddlewares, createConsoleLogger } from '@djvlc/openapi-client-core';
|
|
2466
|
+
* import { Configuration, PagesApi } from '@djvlc/openapi-admin-client';
|
|
2467
|
+
*
|
|
2468
|
+
* const middlewares = createMiddlewares({
|
|
2469
|
+
* auth: { type: 'bearer', getToken: () => token },
|
|
2470
|
+
* retry: { maxRetries: 3, backoffStrategy: 'exponential' },
|
|
2471
|
+
* logger: createConsoleLogger({ prefix: '[API]' }),
|
|
2472
|
+
* debug: true,
|
|
2473
|
+
* });
|
|
2474
|
+
*
|
|
2475
|
+
* const config = new Configuration({
|
|
2476
|
+
* basePath: '/api/admin',
|
|
2477
|
+
* middleware: middlewares,
|
|
2478
|
+
* });
|
|
2479
|
+
*
|
|
2480
|
+
* const api = new PagesApi(config);
|
|
2481
|
+
* ```
|
|
470
2482
|
*/
|
|
471
|
-
declare function
|
|
2483
|
+
declare function createMiddlewares(config?: MiddlewareConfig): Middleware[];
|
|
472
2484
|
/**
|
|
473
|
-
*
|
|
2485
|
+
* 创建认证中间件
|
|
474
2486
|
*/
|
|
475
|
-
declare function
|
|
2487
|
+
declare function createAuthMiddleware(authConfig: AuthConfig): Middleware;
|
|
476
2488
|
/**
|
|
477
|
-
*
|
|
2489
|
+
* 创建请求追踪中间件
|
|
478
2490
|
*/
|
|
479
|
-
declare function
|
|
2491
|
+
declare function createTrackingMiddleware(): Middleware;
|
|
480
2492
|
|
|
481
2493
|
/**
|
|
482
|
-
*
|
|
2494
|
+
* 请求 ID 生成工具
|
|
483
2495
|
*
|
|
484
|
-
*
|
|
485
|
-
*/
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* 创建带超时的 fetch
|
|
489
|
-
*/
|
|
490
|
-
declare function createFetchWithTimeout(fetchImpl: typeof fetch, timeoutMs: number): typeof fetch;
|
|
491
|
-
/**
|
|
492
|
-
* 创建带重试的 fetch
|
|
2496
|
+
* @packageDocumentation
|
|
493
2497
|
*/
|
|
494
|
-
declare function createFetchWithRetry(fetchImpl: typeof fetch, options?: RetryOptions, logger?: Logger): typeof fetch;
|
|
495
2498
|
/**
|
|
496
|
-
*
|
|
497
|
-
*/
|
|
498
|
-
declare function createEnhancedFetch(options: {
|
|
499
|
-
fetchApi?: typeof fetch;
|
|
500
|
-
timeoutMs?: number;
|
|
501
|
-
retry?: RetryOptions;
|
|
502
|
-
logger?: Logger;
|
|
503
|
-
}): typeof fetch;
|
|
504
|
-
/**
|
|
505
|
-
* 执行请求并处理错误
|
|
2499
|
+
* 生成请求 ID
|
|
506
2500
|
*
|
|
507
|
-
*
|
|
508
|
-
|
|
509
|
-
declare function executeRequest(fetchFn: typeof fetch, url: string, init: RequestInit, options?: RequestOptions): Promise<Response>;
|
|
510
|
-
|
|
511
|
-
/**
|
|
512
|
-
* @djvlc/openapi-client-core - Middleware 系统
|
|
2501
|
+
* 格式: req_{timestamp}_{random}
|
|
2502
|
+
* 例如: req_m5abc123_k7def456
|
|
513
2503
|
*
|
|
514
|
-
*
|
|
2504
|
+
* @returns 唯一的请求 ID
|
|
515
2505
|
*/
|
|
516
|
-
|
|
2506
|
+
declare function generateRequestId(): string;
|
|
517
2507
|
/**
|
|
518
|
-
*
|
|
2508
|
+
* 生成追踪 ID
|
|
519
2509
|
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
* - Trace Headers
|
|
525
|
-
* - SDK 版本
|
|
526
|
-
*/
|
|
527
|
-
declare function createHeadersMiddleware(opts: {
|
|
528
|
-
getAuthToken?: () => string | Promise<string>;
|
|
529
|
-
getApiKey?: () => string | Promise<string>;
|
|
530
|
-
getTraceHeaders?: () => Record<string, string>;
|
|
531
|
-
defaultHeaders?: Record<string, string>;
|
|
532
|
-
sdkInfo?: SdkInfo;
|
|
533
|
-
}): Middleware;
|
|
534
|
-
/**
|
|
535
|
-
* 创建日志 Middleware
|
|
536
|
-
*/
|
|
537
|
-
declare function createLoggingMiddleware(opts: {
|
|
538
|
-
onRequest?: (url: string, init: RequestInit) => void;
|
|
539
|
-
onResponse?: (url: string, response: Response, durationMs: number) => void;
|
|
540
|
-
onError?: (url: string, error: unknown) => void;
|
|
541
|
-
}): Middleware;
|
|
542
|
-
/**
|
|
543
|
-
* 应用 Middleware 链
|
|
2510
|
+
* 格式: trace_{timestamp}_{random}_{random}
|
|
2511
|
+
* 例如: trace_m5abc123_k7def456_x8ghi789
|
|
2512
|
+
*
|
|
2513
|
+
* @returns 唯一的追踪 ID
|
|
544
2514
|
*/
|
|
545
|
-
declare function
|
|
2515
|
+
declare function generateTraceId(): string;
|
|
546
2516
|
/**
|
|
547
|
-
*
|
|
2517
|
+
* 验证请求 ID 格式
|
|
2518
|
+
*
|
|
2519
|
+
* @param id - 待验证的 ID
|
|
2520
|
+
* @returns 是否为有效的请求 ID
|
|
548
2521
|
*/
|
|
549
|
-
declare function
|
|
2522
|
+
declare function isValidRequestId(id: string): boolean;
|
|
550
2523
|
/**
|
|
551
|
-
*
|
|
2524
|
+
* 验证追踪 ID 格式
|
|
2525
|
+
*
|
|
2526
|
+
* @param id - 待验证的 ID
|
|
2527
|
+
* @returns 是否为有效的追踪 ID
|
|
552
2528
|
*/
|
|
553
|
-
declare function
|
|
2529
|
+
declare function isValidTraceId(id: string): boolean;
|
|
554
2530
|
|
|
555
2531
|
/**
|
|
556
|
-
*
|
|
2532
|
+
* 重试延迟计算工具
|
|
557
2533
|
*
|
|
558
|
-
*
|
|
2534
|
+
* @packageDocumentation
|
|
559
2535
|
*/
|
|
560
2536
|
|
|
561
2537
|
/**
|
|
562
|
-
*
|
|
2538
|
+
* 计算重试延迟(毫秒)
|
|
2539
|
+
*
|
|
2540
|
+
* @param config - 重试配置
|
|
2541
|
+
* @param attempt - 当前重试次数(从 0 开始)
|
|
2542
|
+
* @returns 延迟时间(毫秒)
|
|
563
2543
|
*/
|
|
564
|
-
declare
|
|
565
|
-
private requestInterceptors;
|
|
566
|
-
private responseInterceptors;
|
|
567
|
-
private errorInterceptors;
|
|
568
|
-
constructor(interceptors?: Interceptors);
|
|
569
|
-
/**
|
|
570
|
-
* 添加请求拦截器
|
|
571
|
-
* @returns 移除拦截器的函数
|
|
572
|
-
*/
|
|
573
|
-
addRequestInterceptor(interceptor: RequestInterceptor): () => void;
|
|
574
|
-
/**
|
|
575
|
-
* 添加响应拦截器
|
|
576
|
-
* @returns 移除拦截器的函数
|
|
577
|
-
*/
|
|
578
|
-
addResponseInterceptor(interceptor: ResponseInterceptor): () => void;
|
|
579
|
-
/**
|
|
580
|
-
* 添加错误拦截器
|
|
581
|
-
* @returns 移除拦截器的函数
|
|
582
|
-
*/
|
|
583
|
-
addErrorInterceptor(interceptor: ErrorInterceptor): () => void;
|
|
584
|
-
/**
|
|
585
|
-
* 执行请求拦截器链
|
|
586
|
-
*/
|
|
587
|
-
runRequestInterceptors(context: RequestContext): Promise<RequestContext>;
|
|
588
|
-
/**
|
|
589
|
-
* 执行响应拦截器链
|
|
590
|
-
*/
|
|
591
|
-
runResponseInterceptors(response: Response, context: RequestContext): Promise<Response>;
|
|
592
|
-
/**
|
|
593
|
-
* 执行错误拦截器链
|
|
594
|
-
*/
|
|
595
|
-
runErrorInterceptors(error: unknown, context: RequestContext): Promise<unknown>;
|
|
596
|
-
/**
|
|
597
|
-
* 清除所有拦截器
|
|
598
|
-
*/
|
|
599
|
-
clear(): void;
|
|
600
|
-
}
|
|
2544
|
+
declare function calculateRetryDelay(config: RetryConfig, attempt: number): number;
|
|
601
2545
|
/**
|
|
602
|
-
*
|
|
2546
|
+
* 解析 Retry-After 响应头
|
|
2547
|
+
*
|
|
2548
|
+
* 支持两种格式:
|
|
2549
|
+
* 1. 秒数: "120"
|
|
2550
|
+
* 2. HTTP 日期: "Wed, 21 Oct 2015 07:28:00 GMT"
|
|
603
2551
|
*
|
|
604
|
-
*
|
|
2552
|
+
* @param retryAfter - Retry-After 头的值
|
|
2553
|
+
* @returns 延迟时间(秒),如果解析失败则返回 undefined
|
|
605
2554
|
*/
|
|
606
|
-
declare function
|
|
607
|
-
/** 刷新 token 的函数 */
|
|
608
|
-
refreshToken: () => Promise<string>;
|
|
609
|
-
/** 判断是否需要刷新 */
|
|
610
|
-
shouldRefresh?: (response: Response) => boolean;
|
|
611
|
-
/** 最大刷新重试次数 */
|
|
612
|
-
maxRetries?: number;
|
|
613
|
-
}): ResponseInterceptor;
|
|
2555
|
+
declare function parseRetryAfter(retryAfter: string | null | undefined): number | undefined;
|
|
614
2556
|
/**
|
|
615
|
-
*
|
|
2557
|
+
* 获取推荐的重试延迟
|
|
616
2558
|
*
|
|
617
|
-
*
|
|
2559
|
+
* 优先使用 Retry-After 头的值
|
|
2560
|
+
*
|
|
2561
|
+
* @param config - 重试配置
|
|
2562
|
+
* @param attempt - 当前重试次数
|
|
2563
|
+
* @param retryAfterSeconds - Retry-After 头的值(秒)
|
|
2564
|
+
* @returns 延迟时间(毫秒)
|
|
618
2565
|
*/
|
|
619
|
-
declare function
|
|
620
|
-
interceptor: RequestInterceptor;
|
|
621
|
-
clear: () => void;
|
|
622
|
-
};
|
|
623
|
-
/**
|
|
624
|
-
* 创建请求缓存拦截器
|
|
625
|
-
*
|
|
626
|
-
* 缓存 GET 请求的响应
|
|
627
|
-
*/
|
|
628
|
-
declare function createCacheInterceptor(options: {
|
|
629
|
-
/** 缓存 TTL(毫秒) */
|
|
630
|
-
ttlMs?: number;
|
|
631
|
-
/** 判断是否应该缓存 */
|
|
632
|
-
shouldCache?: (context: RequestContext) => boolean;
|
|
633
|
-
/** 最大缓存条目数 */
|
|
634
|
-
maxSize?: number;
|
|
635
|
-
}): {
|
|
636
|
-
requestInterceptor: RequestInterceptor;
|
|
637
|
-
responseInterceptor: ResponseInterceptor;
|
|
638
|
-
clear: () => void;
|
|
639
|
-
};
|
|
2566
|
+
declare function getRecommendedRetryDelay(config: RetryConfig, attempt: number, retryAfterSeconds?: number): number;
|
|
640
2567
|
|
|
641
2568
|
/**
|
|
642
|
-
*
|
|
2569
|
+
* 延迟执行工具
|
|
643
2570
|
*
|
|
644
|
-
*
|
|
645
|
-
*/
|
|
646
|
-
/**
|
|
647
|
-
* 请求去重器选项
|
|
2571
|
+
* @packageDocumentation
|
|
648
2572
|
*/
|
|
649
|
-
interface DedupeOptions {
|
|
650
|
-
/**
|
|
651
|
-
* 生成请求唯一标识的函数
|
|
652
|
-
*
|
|
653
|
-
* 默认使用 `${method}:${url}`
|
|
654
|
-
*/
|
|
655
|
-
keyGenerator?: (url: string, init?: RequestInit) => string;
|
|
656
|
-
/**
|
|
657
|
-
* 是否只对 GET 请求去重
|
|
658
|
-
*
|
|
659
|
-
* @default true
|
|
660
|
-
*/
|
|
661
|
-
getOnly?: boolean;
|
|
662
|
-
/**
|
|
663
|
-
* 调试日志
|
|
664
|
-
*/
|
|
665
|
-
onDedupe?: (key: string, pending: number) => void;
|
|
666
|
-
}
|
|
667
2573
|
/**
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
* @example
|
|
671
|
-
* ```typescript
|
|
672
|
-
* const deduper = createRequestDeduper();
|
|
2574
|
+
* 延迟执行
|
|
673
2575
|
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
* // 相同的请求只会执行一次
|
|
678
|
-
* const [r1, r2] = await Promise.all([
|
|
679
|
-
* dedupedFetch('/api/user'),
|
|
680
|
-
* dedupedFetch('/api/user'),
|
|
681
|
-
* ]);
|
|
682
|
-
* ```
|
|
2576
|
+
* @param ms - 延迟时间(毫秒)
|
|
2577
|
+
* @returns Promise,在指定时间后 resolve
|
|
683
2578
|
*/
|
|
684
|
-
declare function
|
|
685
|
-
/**
|
|
686
|
-
* 包装 fetch 函数,添加请求去重能力
|
|
687
|
-
*/
|
|
688
|
-
wrap: (fetchFn: typeof fetch) => typeof fetch;
|
|
689
|
-
/**
|
|
690
|
-
* 获取当前进行中的请求数量
|
|
691
|
-
*/
|
|
692
|
-
getPendingCount(): number;
|
|
693
|
-
/**
|
|
694
|
-
* 获取所有进行中的请求 key
|
|
695
|
-
*/
|
|
696
|
-
getPendingKeys(): string[];
|
|
697
|
-
/**
|
|
698
|
-
* 清除所有进行中的请求记录
|
|
699
|
-
*
|
|
700
|
-
* 注意:这不会取消请求,只是清除去重映射
|
|
701
|
-
*/
|
|
702
|
-
clear(): void;
|
|
703
|
-
};
|
|
2579
|
+
declare function sleep(ms: number): Promise<void>;
|
|
704
2580
|
/**
|
|
705
|
-
*
|
|
2581
|
+
* 可取消的延迟执行
|
|
2582
|
+
*
|
|
2583
|
+
* @param ms - 延迟时间(毫秒)
|
|
2584
|
+
* @param signal - AbortSignal,用于取消延迟
|
|
2585
|
+
* @returns Promise,在指定时间后 resolve,或在取消时 reject
|
|
706
2586
|
*/
|
|
707
|
-
|
|
2587
|
+
declare function sleepWithAbort(ms: number, signal?: AbortSignal): Promise<void>;
|
|
708
2588
|
|
|
709
2589
|
/**
|
|
710
|
-
*
|
|
2590
|
+
* URL 处理工具
|
|
711
2591
|
*
|
|
712
|
-
*
|
|
2592
|
+
* @packageDocumentation
|
|
713
2593
|
*/
|
|
714
|
-
|
|
715
2594
|
/**
|
|
716
|
-
*
|
|
2595
|
+
* 查询参数值类型
|
|
717
2596
|
*/
|
|
718
|
-
|
|
719
|
-
/** 请求 ID(唯一标识) */
|
|
720
|
-
requestId: string;
|
|
721
|
-
/** 请求 URL */
|
|
722
|
-
url: string;
|
|
723
|
-
/** 请求路径(不含 base URL 和 query) */
|
|
724
|
-
path: string;
|
|
725
|
-
/** HTTP 方法 */
|
|
726
|
-
method: string;
|
|
727
|
-
/** 请求开始时间戳(毫秒) */
|
|
728
|
-
startTime: number;
|
|
729
|
-
/** 请求结束时间戳(毫秒) */
|
|
730
|
-
endTime: number;
|
|
731
|
-
/** 请求耗时(毫秒) */
|
|
732
|
-
durationMs: number;
|
|
733
|
-
/** HTTP 状态码 */
|
|
734
|
-
status: number;
|
|
735
|
-
/** 是否成功(2xx) */
|
|
736
|
-
success: boolean;
|
|
737
|
-
/** 是否来自缓存 */
|
|
738
|
-
fromCache?: boolean;
|
|
739
|
-
/** 重试次数 */
|
|
740
|
-
retryCount?: number;
|
|
741
|
-
/** 请求体大小(字节) */
|
|
742
|
-
requestSize?: number;
|
|
743
|
-
/** 响应体大小(字节,如果可获取) */
|
|
744
|
-
responseSize?: number;
|
|
745
|
-
/** 错误信息(如果失败) */
|
|
746
|
-
error?: string;
|
|
747
|
-
/** 追踪 ID */
|
|
748
|
-
traceId?: string;
|
|
749
|
-
}
|
|
2597
|
+
type QueryParamValue = string | number | boolean | undefined | null;
|
|
750
2598
|
/**
|
|
751
|
-
*
|
|
752
|
-
*/
|
|
753
|
-
interface MetricsSummary {
|
|
754
|
-
/** 总请求数 */
|
|
755
|
-
totalRequests: number;
|
|
756
|
-
/** 成功请求数 */
|
|
757
|
-
successCount: number;
|
|
758
|
-
/** 失败请求数 */
|
|
759
|
-
failureCount: number;
|
|
760
|
-
/** 成功率 */
|
|
761
|
-
successRate: number;
|
|
762
|
-
/** 平均耗时(毫秒) */
|
|
763
|
-
avgDurationMs: number;
|
|
764
|
-
/** P50 耗时(毫秒) */
|
|
765
|
-
p50Ms: number;
|
|
766
|
-
/** P90 耗时(毫秒) */
|
|
767
|
-
p90Ms: number;
|
|
768
|
-
/** P95 耗时(毫秒) */
|
|
769
|
-
p95Ms: number;
|
|
770
|
-
/** P99 耗时(毫秒) */
|
|
771
|
-
p99Ms: number;
|
|
772
|
-
/** 最小耗时(毫秒) */
|
|
773
|
-
minMs: number;
|
|
774
|
-
/** 最大耗时(毫秒) */
|
|
775
|
-
maxMs: number;
|
|
776
|
-
/** 按状态码分组的计数 */
|
|
777
|
-
statusCodeCounts: Record<number, number>;
|
|
778
|
-
/** 按路径分组的计数 */
|
|
779
|
-
pathCounts: Record<string, number>;
|
|
780
|
-
/** 统计时间范围 */
|
|
781
|
-
timeRange: {
|
|
782
|
-
start: number;
|
|
783
|
-
end: number;
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
/**
|
|
787
|
-
* 指标收集器选项
|
|
788
|
-
*/
|
|
789
|
-
interface MetricsCollectorOptions {
|
|
790
|
-
/** 最大保留的指标数量(避免内存泄漏) */
|
|
791
|
-
maxMetrics?: number;
|
|
792
|
-
/** 指标 TTL(毫秒,超过此时间的指标会被清理) */
|
|
793
|
-
ttlMs?: number;
|
|
794
|
-
/** 是否自动清理过期指标 */
|
|
795
|
-
autoCleanup?: boolean;
|
|
796
|
-
/** 清理间隔(毫秒) */
|
|
797
|
-
cleanupIntervalMs?: number;
|
|
798
|
-
/** 指标收集回调(每次请求完成时调用) */
|
|
799
|
-
onMetrics?: (metrics: RequestMetrics) => void;
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* 指标收集器
|
|
2599
|
+
* 查询参数对象类型
|
|
803
2600
|
*/
|
|
804
|
-
|
|
805
|
-
/** 收集指标 */
|
|
806
|
-
collect(metrics: RequestMetrics): void;
|
|
807
|
-
/** 获取所有指标 */
|
|
808
|
-
getMetrics(): RequestMetrics[];
|
|
809
|
-
/** 获取并清空所有指标 */
|
|
810
|
-
flush(): RequestMetrics[];
|
|
811
|
-
/** 获取指标摘要 */
|
|
812
|
-
summary(): MetricsSummary;
|
|
813
|
-
/** 按路径获取指标 */
|
|
814
|
-
getByPath(path: string): RequestMetrics[];
|
|
815
|
-
/** 按时间范围获取指标 */
|
|
816
|
-
getByTimeRange(startTime: number, endTime: number): RequestMetrics[];
|
|
817
|
-
/** 清除所有指标 */
|
|
818
|
-
clear(): void;
|
|
819
|
-
/** 销毁收集器(清理定时器等) */
|
|
820
|
-
destroy(): void;
|
|
821
|
-
}
|
|
2601
|
+
type QueryParams = Record<string, QueryParamValue>;
|
|
822
2602
|
/**
|
|
823
|
-
*
|
|
2603
|
+
* 构建完整 URL
|
|
824
2604
|
*
|
|
825
|
-
* @
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
* onMetrics: (m) => console.log(`${m.method} ${m.path} - ${m.durationMs}ms`),
|
|
830
|
-
* });
|
|
831
|
-
*
|
|
832
|
-
* // 使用 middleware 收集指标
|
|
833
|
-
* const client = createClient({
|
|
834
|
-
* middleware: [createMetricsMiddleware(metrics)],
|
|
835
|
-
* });
|
|
836
|
-
*
|
|
837
|
-
* // 获取摘要
|
|
838
|
-
* const summary = metrics.summary();
|
|
839
|
-
* console.log(`成功率: ${(summary.successRate * 100).toFixed(1)}%`);
|
|
840
|
-
* console.log(`P95: ${summary.p95Ms}ms`);
|
|
841
|
-
* ```
|
|
2605
|
+
* @param baseUrl - 基础 URL
|
|
2606
|
+
* @param path - 路径
|
|
2607
|
+
* @param query - 查询参数
|
|
2608
|
+
* @returns 完整 URL
|
|
842
2609
|
*/
|
|
843
|
-
declare function
|
|
2610
|
+
declare function buildUrl(baseUrl: string, path: string, query?: QueryParams): string;
|
|
844
2611
|
/**
|
|
845
|
-
*
|
|
2612
|
+
* 从 URL 提取路径(不含查询参数)
|
|
846
2613
|
*
|
|
847
|
-
* @
|
|
848
|
-
*
|
|
849
|
-
|
|
850
|
-
|
|
2614
|
+
* @param url - 完整 URL
|
|
2615
|
+
* @returns 路径
|
|
2616
|
+
*/
|
|
2617
|
+
declare function extractPath(url: string): string;
|
|
2618
|
+
/**
|
|
2619
|
+
* 解析查询字符串
|
|
851
2620
|
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
* });
|
|
855
|
-
* ```
|
|
2621
|
+
* @param queryString - 查询字符串(可以包含或不包含 ?)
|
|
2622
|
+
* @returns 解析后的参数对象
|
|
856
2623
|
*/
|
|
857
|
-
declare function
|
|
858
|
-
|
|
2624
|
+
declare function parseQueryString(queryString: string): Record<string, string>;
|
|
859
2625
|
/**
|
|
860
|
-
*
|
|
2626
|
+
* 合并两个 URL 路径
|
|
2627
|
+
*
|
|
2628
|
+
* @param base - 基础路径
|
|
2629
|
+
* @param path - 要追加的路径
|
|
2630
|
+
* @returns 合并后的路径
|
|
861
2631
|
*/
|
|
2632
|
+
declare function joinPaths(base: string, path: string): string;
|
|
862
2633
|
|
|
863
2634
|
/**
|
|
864
|
-
*
|
|
2635
|
+
* 请求头处理工具
|
|
2636
|
+
*
|
|
2637
|
+
* @packageDocumentation
|
|
865
2638
|
*/
|
|
866
|
-
declare function createConsoleLogger(options?: {
|
|
867
|
-
/** 日志前缀 */
|
|
868
|
-
prefix?: string;
|
|
869
|
-
/** 是否启用 */
|
|
870
|
-
enabled?: boolean;
|
|
871
|
-
/** 日志级别:'debug' | 'info' | 'warn' | 'error' */
|
|
872
|
-
level?: 'debug' | 'info' | 'warn' | 'error';
|
|
873
|
-
/** 是否包含时间戳 */
|
|
874
|
-
timestamp?: boolean;
|
|
875
|
-
/** 是否包含请求体 */
|
|
876
|
-
includeBody?: boolean;
|
|
877
|
-
}): Logger;
|
|
878
2639
|
/**
|
|
879
|
-
*
|
|
2640
|
+
* 默认请求头
|
|
880
2641
|
*/
|
|
881
|
-
declare
|
|
2642
|
+
declare const DEFAULT_HEADERS: Readonly<Record<string, string>>;
|
|
882
2643
|
/**
|
|
883
|
-
*
|
|
2644
|
+
* 合并请求头
|
|
2645
|
+
*
|
|
2646
|
+
* 后面的头会覆盖前面的同名头(大小写不敏感)
|
|
884
2647
|
*
|
|
885
|
-
*
|
|
2648
|
+
* @param headersList - 要合并的请求头列表
|
|
2649
|
+
* @returns 合并后的请求头
|
|
886
2650
|
*/
|
|
887
|
-
declare function
|
|
888
|
-
onRequest?: (data: {
|
|
889
|
-
url: string;
|
|
890
|
-
method: string;
|
|
891
|
-
headers?: Record<string, string>;
|
|
892
|
-
timestamp: number;
|
|
893
|
-
}) => void;
|
|
894
|
-
onResponse?: (data: {
|
|
895
|
-
url: string;
|
|
896
|
-
status: number;
|
|
897
|
-
durationMs: number;
|
|
898
|
-
timestamp: number;
|
|
899
|
-
}) => void;
|
|
900
|
-
onError?: (data: {
|
|
901
|
-
url: string;
|
|
902
|
-
error: unknown;
|
|
903
|
-
timestamp: number;
|
|
904
|
-
}) => void;
|
|
905
|
-
onRetry?: (data: {
|
|
906
|
-
url: string;
|
|
907
|
-
attempt: number;
|
|
908
|
-
error: unknown;
|
|
909
|
-
timestamp: number;
|
|
910
|
-
}) => void;
|
|
911
|
-
}): Logger;
|
|
2651
|
+
declare function mergeHeaders(...headersList: (Record<string, string> | undefined)[]): Record<string, string>;
|
|
912
2652
|
/**
|
|
913
|
-
*
|
|
2653
|
+
* 获取请求头值(大小写不敏感)
|
|
2654
|
+
*
|
|
2655
|
+
* @param headers - 请求头对象
|
|
2656
|
+
* @param name - 头名称
|
|
2657
|
+
* @returns 头的值,不存在则返回 undefined
|
|
914
2658
|
*/
|
|
915
|
-
declare function
|
|
916
|
-
|
|
2659
|
+
declare function getHeader(headers: Record<string, string>, name: string): string | undefined;
|
|
917
2660
|
/**
|
|
918
|
-
*
|
|
2661
|
+
* 设置请求头(大小写不敏感,会替换同名头)
|
|
919
2662
|
*
|
|
920
|
-
*
|
|
2663
|
+
* @param headers - 请求头对象(会被修改)
|
|
2664
|
+
* @param name - 头名称
|
|
2665
|
+
* @param value - 头的值
|
|
921
2666
|
*/
|
|
922
|
-
|
|
2667
|
+
declare function setHeader(headers: Record<string, string>, name: string, value: string): void;
|
|
923
2668
|
/**
|
|
924
|
-
*
|
|
2669
|
+
* 删除请求头(大小写不敏感)
|
|
925
2670
|
*
|
|
926
|
-
*
|
|
2671
|
+
* @param headers - 请求头对象(会被修改)
|
|
2672
|
+
* @param name - 头名称
|
|
2673
|
+
* @returns 是否删除成功
|
|
927
2674
|
*/
|
|
928
|
-
declare
|
|
929
|
-
/** 基础路径 */
|
|
930
|
-
basePath: string;
|
|
931
|
-
/** fetch 实现 */
|
|
932
|
-
fetchApi: typeof fetch;
|
|
933
|
-
/** Middleware 列表 */
|
|
934
|
-
middleware: Middleware[];
|
|
935
|
-
/** Cookie 策略 */
|
|
936
|
-
credentials?: RequestCredentials;
|
|
937
|
-
/** 拦截器管理器 */
|
|
938
|
-
interceptors: InterceptorManager;
|
|
939
|
-
/** SDK 信息 */
|
|
940
|
-
sdkInfo?: SdkInfo;
|
|
941
|
-
constructor(params?: ConfigurationParameters);
|
|
942
|
-
/**
|
|
943
|
-
* 从 ClientOptions 创建 Configuration
|
|
944
|
-
*/
|
|
945
|
-
static fromClientOptions(opts: BaseClientOptions, sdkInfo?: SdkInfo): Configuration;
|
|
946
|
-
}
|
|
2675
|
+
declare function removeHeader(headers: Record<string, string>, name: string): boolean;
|
|
947
2676
|
/**
|
|
948
|
-
*
|
|
2677
|
+
* 从 Response 对象提取请求头
|
|
949
2678
|
*
|
|
950
|
-
*
|
|
2679
|
+
* @param response - Response 对象
|
|
2680
|
+
* @returns 请求头对象
|
|
951
2681
|
*/
|
|
952
|
-
declare function
|
|
953
|
-
|
|
2682
|
+
declare function extractResponseHeaders(response: Response): Record<string, string>;
|
|
954
2683
|
/**
|
|
955
|
-
*
|
|
2684
|
+
* 检查是否包含指定的 Content-Type
|
|
956
2685
|
*
|
|
957
|
-
*
|
|
2686
|
+
* @param headers - 请求头对象
|
|
2687
|
+
* @param contentType - 要检查的内容类型
|
|
2688
|
+
* @returns 是否匹配
|
|
958
2689
|
*/
|
|
2690
|
+
declare function hasContentType(headers: Record<string, string>, contentType: string): boolean;
|
|
959
2691
|
|
|
960
2692
|
/**
|
|
961
|
-
*
|
|
962
|
-
*/
|
|
963
|
-
type ApiConstructor<T> = new (configuration: Configuration) => T;
|
|
964
|
-
/**
|
|
965
|
-
* API 类映射
|
|
966
|
-
*/
|
|
967
|
-
type ApiMap = Record<string, ApiConstructor<unknown>>;
|
|
968
|
-
/**
|
|
969
|
-
* 从 API 类映射提取实例类型
|
|
970
|
-
*/
|
|
971
|
-
type ApiInstances<T extends ApiMap> = {
|
|
972
|
-
[K in keyof T]: InstanceType<T[K]>;
|
|
973
|
-
};
|
|
974
|
-
/**
|
|
975
|
-
* 创建客户端的通用工厂函数
|
|
2693
|
+
* @djvlc/openapi-client-core
|
|
976
2694
|
*
|
|
977
|
-
*
|
|
978
|
-
* ```typescript
|
|
979
|
-
* import * as apis from './gen/apis';
|
|
2695
|
+
* OpenAPI 客户端公共运行时
|
|
980
2696
|
*
|
|
981
|
-
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
984
|
-
*
|
|
985
|
-
*
|
|
2697
|
+
* 提供模块化的 HTTP 客户端能力:
|
|
2698
|
+
* - 类型定义(types/)
|
|
2699
|
+
* - 错误处理(errors/)
|
|
2700
|
+
* - 认证(auth/)
|
|
2701
|
+
* - 拦截器(interceptors/)
|
|
2702
|
+
* - 插件(plugins/)
|
|
2703
|
+
* - 客户端(clients/)
|
|
2704
|
+
* - 工具函数(utils/)
|
|
2705
|
+
*
|
|
2706
|
+
* @packageDocumentation
|
|
986
2707
|
*/
|
|
987
|
-
declare
|
|
988
|
-
|
|
989
|
-
apis: ApiInstances<T>;
|
|
990
|
-
} & ApiInstances<T>;
|
|
2708
|
+
declare const VERSION = "2.0.0";
|
|
2709
|
+
declare const SDK_NAME = "@djvlc/openapi-client-core";
|
|
991
2710
|
/**
|
|
992
|
-
*
|
|
2711
|
+
* 获取 SDK 信息
|
|
993
2712
|
*/
|
|
994
|
-
declare function
|
|
2713
|
+
declare function getSdkInfo(): {
|
|
2714
|
+
name: string;
|
|
2715
|
+
version: string;
|
|
2716
|
+
};
|
|
995
2717
|
|
|
996
|
-
export { AbortError, type
|
|
2718
|
+
export { AbortError, ApiError, type ApiErrorConfig, type ApiErrorDetail, type ApiKeyAuthConfig, ApiKeyAuthenticator, type AuthConfig, AuthInterceptor, type AuthInterceptorConfig, type AuthType, type Authenticator, type AuthenticatorFactory, type AxiosClientConfig, type BackoffStrategy, BaseClient, BaseClientError, type BasicAuthConfig, BasicAuthenticator, type BearerAuthConfig, BearerAuthenticator, BufferLogger, type ClientConfig, ConsoleLogger, type ConsoleLoggerConfig, type CustomAuthConfig, CustomAuthenticator, DEFAULT_HEADERS, DEFAULT_RETRY_CONFIG, type DeduplicationConfig, DefaultMetricsCollector, type ErrorInterceptor, type ErrorInterceptorFactory, type ErrorReporter, type ErrorSeverity, ErrorTransformInterceptor, type ErrorTransformInterceptorConfig, FetchClient, type FetchClientConfig, type HttpMethod, type InterceptorChainResult, InterceptorManager, type Interceptors, LOG_LEVEL_PRIORITY, type LogLevel, type Logger, LoggingInterceptor, type LoggingInterceptorConfig, type MetricsCollector, type MetricsCollectorConfig, MetricsErrorInterceptor, MetricsRequestInterceptor, MetricsResponseInterceptor, type MetricsSummary, type MiddlewareConfig, type MutableRequestOptions, NetworkError, type NoAuthConfig, NoAuthenticator, type PathMetrics, type QueryParamValue, type QueryParams, ReportingInterceptor, type ReportingInterceptorConfig, type RequestContext$1 as RequestContext, RequestDeduper, RequestIdInterceptor, type RequestIdInterceptorConfig, type RequestInterceptor, type RequestInterceptorFactory, type RequestMetrics, type RequestOptions, type ResponseData, type ResponseInterceptor, type ResponseInterceptorFactory, type RetryCallback, type RetryConfig, type RetryContext, type RetryInfo, RetryInterceptor, type RetryInterceptorConfig, SDK_NAME, type ShouldRetryFn, SilentLogger, TimeoutError, TimeoutInterceptor, type TimeoutInterceptorConfig, type TokenRefreshConfig, TokenRefreshInterceptor, type TokenRefreshInterceptorConfig, TraceInterceptor, type TraceInterceptorConfig, VERSION, buildUrl, calculateRetryDelay, createApiKeyAuthenticator, createAuthInterceptor, createAuthMiddleware, createAuthenticatorFromConfig, createAxiosInstance, createBasicAuthenticator, createBearerAuthenticator, createBufferLogger, createConsoleLogger, createCustomAuthenticator, createErrorTransformInterceptor, createFetchClient, createInterceptorManager, createLoggingInterceptor, createMetricsCollector, createMetricsInterceptors, createMiddlewares, createNoAuthenticator, createReportingInterceptor, createRequestDeduper, createRequestIdInterceptor, createRetryInterceptor, createSilentLogger, createTimeoutInterceptor, createTokenRefreshInterceptor, createTraceInterceptor, createTrackingMiddleware, extractPath, extractResponseHeaders, generateRequestId, generateTraceId, getErrorType, getHeader, getRecommendedRetryDelay, getRetryDelay, getSdkInfo, hasContentType, isClientError, isRetryableError, isValidRequestId, isValidTraceId, joinPaths, mergeHeaders, noAuthenticator, parseQueryString, parseRetryAfter, removeHeader, setHeader, silentLogger, sleep, sleepWithAbort };
|