-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
549 lines (458 loc) · 11.7 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import { objectToFormData } from 'object-to-formdata'
declare type ResponseType =
| 'text'
| 'blob'
| 'json'
| 'document'
| 'arraybuffer'
| 'JSON'
| 'base64'
declare enum EngineName {
/** 微信小程序 api */
WX = 'wx',
/** 支付宝小程序 api */
MY = 'my',
/** xhr api */
XHR = 'xhr',
/** fetch api */
Fetch = 'fetch',
}
interface ProgressEv {
total: number
transmitted: number
progress: number
}
interface HttpHeaders {
[key: string]: string
}
declare type RequestData =
| {
[key in string | number]: any
}
| FormData
interface RequestSharedConfig {
method: string
/**
* 对应: responseType、dataType
* Default: json
* */
responseType: ResponseType
/**
* 微信 wx 和支付宝 my 中不支持,设置无效果
* Default: false
* */
withCredentials: boolean
/**
* Default: 30000
*
* fetch 不支持
* */
timeout: number
/**
* Default: { 'Content-Type': 'application/x-www-form-urlencoded' }
* */
headers: HttpHeaders
convertFormDataOptions?: Parameters<typeof objectToFormData>[1] & {
customConvertFn?(data: Exclude<RequestData, FormData>): FormData
}
/**
* Default: status => status === undefined || (200 <= status < 300)
* */
statusValidator?(status?: number): boolean
[key: string]: any
[key: number]: any
}
interface RequestEngineConfig extends RequestSharedConfig {
url: string
data?: RequestData | null
/** do not work in engine: wx, my, fetch */
onUploadProgress?(ev: ProgressEv): void
/** do not work in engine: wx, my, fetch */
onDownloadProgress?(ev: ProgressEv): void
}
interface RequestConfig extends RequestSharedConfig {
baseURL: string
}
interface RequestResponse<T> {
/** api url */
url: string
data: T
statusCode: number
headers: HttpHeaders
[key: string]: any
}
interface DownloadEngineConfig {
url: string
headers: HttpHeaders
/**
* 微信小程序配置,支付宝小程序、H5 中设置无效果
* */
timeout: RequestSharedConfig['timeout']
/**
* 微信小程序配置,H5 中设置表示需要生成临时 url,支付宝小程序中设置无效果
* */
filePath: string
withCredentials?: boolean
convertFormDataOptions?: RequestSharedConfig['convertFormDataOptions']
onDownloadProgress?(ev: ProgressEv): void
[key: string]: any
}
interface DownloadResponse {
/** api url */
url: string
/** base64 url */
tempFilePath: string
filePath: string
statusCode: number
/** filename that get from server */
filename: string
blob?: Blob
headers?: {
[k: string]: string
}
[key: string]: any
}
interface UploadEngineConfig {
url: string
timeout: RequestSharedConfig['timeout']
headers: HttpHeaders
/**
* 对应微信、支付宝小程序中的 filePath
* H5 中只能使用 blob | file 类型
* */
file: string | Blob | File
/**
* 文件对应的 key
* 对应微信小程序中的 name
* 对应支付宝小程序中的 fileName
* */
fileKey: string
/**
* 文件类型
* 支付宝小程序中需要
* */
fileType?: 'image' | 'video' | 'audio'
extraData: RequestData
withCredentials?: boolean
convertFormDataOptions?: RequestSharedConfig['convertFormDataOptions']
onUploadProgress?(ev: ProgressEv): void
[key: string]: any
}
interface RequestEngine<Config = RequestEngineConfig, Response = any> {
name: EngineName
config: Config
response: Response | RequestResponse<null>
requestInstance: any
requestTask: any
aborted: boolean
open(): Promise<Response>
abort(): void
}
declare type ConfigInterceptor = (config: any) => any
declare type ResponseInterceptor = (response: any) => any
declare type RequestError = Error & {
$request?: RequestEngine<any>
[key: string]: any
[key: number]: any
}
declare type ErrorHandler = (e: RequestError) => any
interface HttpInterceptors {
request: {
interceptors: ConfigInterceptor[]
use(resolve: ConfigInterceptor): void
}
response: {
interceptors: {
resolves: ResponseInterceptor[]
rejects: ErrorHandler[]
}
use(resolve?: ResponseInterceptor, reject?: ErrorHandler): void
}
}
declare class BaseEngine<Config, Response> {
config: Config
requestInstance: any
requestTask: any
response: Response | RequestResponse<null>
aborted: boolean
constructor(config: Config)
}
declare class FetchBase<Config, Response> extends BaseEngine<Config, Response> {
name: EngineName
constructor(config: Config)
abort(): void
getConfig(): any
}
declare class Fetch<T>
extends FetchBase<RequestEngineConfig, RequestResponse<T>>
implements RequestEngine<RequestEngineConfig, RequestResponse<T>> {
open(): Promise<RequestResponse<T>>
}
declare class FetchDownload
extends FetchBase<DownloadEngineConfig, DownloadResponse>
implements RequestEngine<DownloadEngineConfig, DownloadResponse> {
open(): Promise<DownloadResponse>
}
declare class FetchUpload<T>
extends FetchBase<UploadEngineConfig, RequestResponse<T>>
implements RequestEngine<UploadEngineConfig, RequestResponse<T>> {
open<T = any>(): Promise<RequestResponse<T>>
getConfig(): any
}
declare class MYBase<Config, Response> extends BaseEngine<Config, Response> {
name: EngineName
constructor(config: Config)
abort(): void
}
declare class MY<T> extends MYBase<RequestEngineConfig, RequestResponse<T>>
implements RequestEngine<RequestEngineConfig, RequestResponse<T>> {
getConfig(): {
url: string
method: string
data:
| {
[x: string]: any
[x: number]: any
}
| FormData
| null
| undefined
headers: HttpHeaders
header: HttpHeaders
timeout: number
dataType: ResponseType
}
open(): Promise<RequestResponse<T>>
}
declare class MYDownload extends MYBase<DownloadEngineConfig, DownloadResponse>
implements RequestEngine<DownloadEngineConfig, DownloadResponse> {
getConfig(): {
url: string
header: HttpHeaders
headers: HttpHeaders
}
open(): Promise<DownloadResponse>
}
declare class MYUpload<T> extends MYBase<UploadEngineConfig, RequestResponse<T>>
implements RequestEngine<UploadEngineConfig, RequestResponse<T>> {
getConfig(): {
url: string
filePath: string | Blob | File
fileName: string
fileType: 'image' | 'video' | 'audio'
header: HttpHeaders
headers: HttpHeaders
formData: RequestData
}
open(): Promise<RequestResponse<T>>
}
declare class WXBase<Config, Response> extends BaseEngine<Config, Response> {
name: EngineName
constructor(config: Config)
abort(): void
}
declare class WX<T> extends WXBase<RequestEngineConfig, RequestResponse<T>>
implements RequestEngine<RequestEngineConfig, RequestResponse<T>> {
getConfig(): {
url: string
data:
| {
[x: string]: any
[x: number]: any
}
| FormData
| null
| undefined
method: string
dataType: ResponseType
timeout: number
header: HttpHeaders
headers: HttpHeaders
}
open(): Promise<RequestResponse<T>>
}
declare class WXDownload extends WXBase<DownloadEngineConfig, DownloadResponse>
implements RequestEngine<DownloadEngineConfig, DownloadResponse> {
getConfig(): {
url: string
timeout: number
header: HttpHeaders
headers: HttpHeaders
filePath: string
}
open(): Promise<DownloadResponse>
}
declare class WXUpload<T> extends WXBase<UploadEngineConfig, RequestResponse<T>>
implements RequestEngine<UploadEngineConfig, RequestResponse<T>> {
getConfig(): {
url: string
timeout: number
header: HttpHeaders
headers: HttpHeaders
}
open(): Promise<RequestResponse<T>>
}
declare class XhrBase<Config, Response> extends BaseEngine<Config, Response> {
name: EngineName
constructor(config: Config)
abort(): void
}
declare class Xhr<T> extends XhrBase<RequestEngineConfig, RequestResponse<T>>
implements RequestEngine<RequestEngineConfig, RequestResponse<T>> {
open(): Promise<RequestResponse<T>>
}
declare class XhrDownload
extends XhrBase<DownloadEngineConfig, DownloadResponse>
implements RequestEngine<DownloadEngineConfig, DownloadResponse> {
open(): Promise<DownloadResponse>
}
declare class XhrUpload<T>
extends XhrBase<UploadEngineConfig, RequestResponse<T>>
implements RequestEngine<UploadEngineConfig, RequestResponse<T>> {
open(): Promise<RequestResponse<T>>
}
declare class RequestPromise<T extends any> {
promise: Promise<T>
abort: () => void
constructor(
cb: (
resolve: (value?: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
) => void,
)
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
| undefined
| null,
): RequestPromise<TResult1 | TResult2>
catch<TResult = never>(
onrejected?:
| ((reason: any) => TResult | PromiseLike<TResult>)
| undefined
| null,
): RequestPromise<T | TResult>
finally(cb: () => void): RequestPromise<T>
}
declare class Http {
engineName: EngineName
config: RequestConfig
interceptors: HttpInterceptors
constructor(engineName?: EngineName, config?: Partial<RequestConfig>)
calcConfig(
config?: any,
): {
config: Promise<any>
abort: () => void
}
getRequestInstance(
config: RequestEngineConfig,
): WX<unknown> | MY<unknown> | Fetch<unknown> | Xhr<unknown>
getDownloadInstance(
config: DownloadEngineConfig,
): FetchDownload | MYDownload | WXDownload | XhrDownload
getUploadInstance<T>(
config: UploadEngineConfig,
): WXUpload<T> | MYUpload<T> | FetchUpload<T> | XhrUpload<T>
static createError(
object: any,
request?: RequestEngine<any>,
msg?: string,
): RequestError
static dealResponse<T extends any>(
result: T,
request: any,
): T extends {
[k in string | number]: any
}
? T & {
$request: any
}
: T
checkStatus<
T extends {
statusCode: number
}
>(res: T): Promise<T>
request<T extends any = any>(
url: string,
data?: RequestData,
options?: Partial<RequestConfig>,
): RequestPromise<T>
/**
* 对应微信/支付宝小程序的 downloadFile
* */
downloadFile(
options: Partial<DownloadEngineConfig> & Pick<DownloadEngineConfig, 'url'>,
): RequestPromise<
DownloadResponse & {
$request: any
}
>
/**
* 对应微信/支付宝小程序的 uploadFile
* */
uploadFile<T extends any = any>(
options: Partial<UploadEngineConfig> &
Pick<UploadEngineConfig, 'url' | 'file' | 'fileKey'>,
): RequestPromise<
RequestResponse<T> & {
$request: any
}
>
get<T extends any = any>(
url: string,
data?: RequestData,
options?: Partial<
Pick<RequestConfig, Exclude<keyof RequestConfig, 'method'>>
>,
): RequestPromise<T>
post<T extends any = any>(
url: string,
data?: RequestData,
options?: Partial<
Pick<RequestConfig, Exclude<keyof RequestConfig, 'method'>>
>,
): RequestPromise<T>
put<T extends any = any>(
url: string,
data?: RequestData,
options?: Partial<
Pick<RequestConfig, Exclude<keyof RequestConfig, 'method'>>
>,
): RequestPromise<T>
delete<T extends any = any>(
url: string,
data?: RequestData,
options?: Partial<
Pick<RequestConfig, Exclude<keyof RequestConfig, 'method'>>
>,
): RequestPromise<T>
}
export default Http
export {
ConfigInterceptor,
DownloadEngineConfig,
DownloadResponse,
EngineName,
ErrorHandler,
Http,
HttpHeaders,
HttpInterceptors,
ProgressEv,
RequestConfig,
RequestData,
RequestEngine,
RequestEngineConfig,
RequestError,
RequestPromise,
RequestResponse,
RequestSharedConfig,
ResponseInterceptor,
ResponseType,
UploadEngineConfig,
}