3
3
* @date 2017-06-27
4
4
* @author xialeistudio<[email protected] >
5
5
*/
6
- import { Callback } from 'urllib' ;
6
+ import { Callback , RequestOptions } from 'urllib' ;
7
+ import { Agent as HttpAgent , IncomingMessage } from 'http' ;
8
+ import { Agent as HttpsAgent } from 'https' ;
9
+ import { Readable } from "stream" ;
7
10
8
11
export declare type callback = ( e ?: Error , respBody ?: any , respInfo ?: any ) => void ;
9
12
@@ -417,6 +420,150 @@ export declare namespace util {
417
420
function isQiniuCallback ( mac : auth . digest . Mac , requestURI : string , reqBody : string | null , callbackAuth : string ) : boolean ;
418
421
}
419
422
423
+ export declare namespace httpc {
424
+ interface ReqOpts < T = any > {
425
+ agent ?: HttpAgent ;
426
+ httpsAgent ?: HttpsAgent ;
427
+ url : string ;
428
+ middlewares : middleware . Middleware [ ] ;
429
+ callback ?: Callback < T > ;
430
+ urllibOptions : RequestOptions ;
431
+ }
432
+
433
+ interface RespWrapperOptions < T = any > {
434
+ data : T ;
435
+ resp : IncomingMessage ;
436
+ }
437
+
438
+ class RespWrapper < T = any > {
439
+ data : T ;
440
+ resp : IncomingMessage ;
441
+ constructor ( options : RespWrapperOptions ) ;
442
+ ok ( ) : boolean ;
443
+ needRetry ( ) : boolean ;
444
+ }
445
+
446
+ namespace middleware {
447
+ interface Middleware {
448
+ send < T > (
449
+ request : ReqOpts < T > ,
450
+ next : ( reqOpts : ReqOpts < T > ) => Promise < RespWrapper < T > >
451
+ ) : Promise < RespWrapper < T > > ;
452
+ }
453
+
454
+ /**
455
+ * 组合中间件为一个调用函数
456
+ * @param middlewares 中间件列表
457
+ * @param handler 请求函数
458
+ */
459
+ function composeMiddlewares < T > (
460
+ middlewares : Middleware [ ] ,
461
+ handler : ( reqOpts : ReqOpts < T > ) => Promise < RespWrapper < T > >
462
+ ) ;
463
+
464
+ /**
465
+ * 设置 User-Agent 请求头中间件
466
+ */
467
+ class UserAgentMiddleware implements Middleware {
468
+ constructor ( sdkVersion : string ) ;
469
+ send < T > (
470
+ request : httpc . ReqOpts < T > ,
471
+ next : ( reqOpts : httpc . ReqOpts < T > ) => Promise < httpc . RespWrapper < T > >
472
+ ) : Promise < httpc . RespWrapper < T > > ;
473
+ }
474
+
475
+ interface RetryDomainsMiddlewareOptions {
476
+ backupDomains : string [ ] ;
477
+ maxRetryTimes : number ;
478
+ retryCondition : ( ) => boolean ;
479
+ }
480
+
481
+ class RetryDomainsMiddleware implements Middleware {
482
+ /**
483
+ * 备用域名
484
+ */
485
+ backupDomains : string [ ] ;
486
+
487
+ /**
488
+ * 最大重试次数,包括首次请求
489
+ */
490
+ maxRetryTimes : number ;
491
+
492
+ /**
493
+ * 是否可以重试,可以通过该函数配置更详细的重试规则
494
+ */
495
+ retryCondition : ( ) => boolean ;
496
+
497
+ /**
498
+ * 已经重试的次数
499
+ * @private
500
+ */
501
+ private _retriedTimes : number ;
502
+
503
+ /**
504
+ * 实例化重试域名中间件
505
+ * @param retryDomainsOptions
506
+ */
507
+ constructor ( retryDomainsOptions : RetryDomainsMiddlewareOptions )
508
+
509
+ /**
510
+ * 重试域名中间件逻辑
511
+ * @param request
512
+ * @param next
513
+ */
514
+ send < T > (
515
+ request : httpc . ReqOpts < T > ,
516
+ next : ( reqOpts : httpc . ReqOpts < T > ) => Promise < httpc . RespWrapper < T > >
517
+ ) : Promise < httpc . RespWrapper < T > > ;
518
+
519
+ /**
520
+ * 控制重试逻辑,主要为 {@link retryCondition} 服务。若没有设置 retryCondition,默认 2xx 才会终止重试
521
+ * @param err
522
+ * @param respWrapper
523
+ * @param reqOpts
524
+ * @private
525
+ */
526
+ private _shouldRetry < T > (
527
+ err : Error | null ,
528
+ respWrapper : RespWrapper < T > ,
529
+ reqOpts : ReqOpts < T >
530
+ ) : boolean ;
531
+ }
532
+ }
533
+
534
+ interface HttpClientOptions {
535
+ httpAgent ?: HttpAgent ;
536
+ httpsAgent ?: HttpsAgent ;
537
+ middlewares ?: middleware . Middleware [ ] ;
538
+ }
539
+
540
+ interface GetOptions < T = any > extends ReqOpts < T > {
541
+ params : Record < string , string > ;
542
+ headers : Record < string , string > ;
543
+ }
544
+
545
+ interface PostOptions < T = any > extends ReqOpts < T > {
546
+ data : string | Buffer | Readable ;
547
+ headers : Record < string , string > ;
548
+ }
549
+
550
+ interface PutOptions < T = any > extends ReqOpts < T > {
551
+ data : string | Buffer | Readable ;
552
+ headers : Record < string , string >
553
+ }
554
+
555
+ class HttpClient {
556
+ httpAgent : HttpAgent ;
557
+ httpsAgent : HttpsAgent ;
558
+ middlewares : middleware . Middleware [ ] ;
559
+ constructor ( options : HttpClientOptions )
560
+ sendRequest ( requestOptions : ReqOpts ) : Promise < RespWrapper >
561
+ get ( getOptions : GetOptions ) : Promise < RespWrapper >
562
+ post ( postOptions : PostOptions ) : Promise < RespWrapper >
563
+ put ( putOptions : PutOptions ) : Promise < RespWrapper >
564
+ }
565
+ }
566
+
420
567
export declare namespace rpc {
421
568
type Headers = Record < string , string > & {
422
569
'User-Agent' ?: string ;
@@ -428,6 +575,8 @@ export declare namespace rpc {
428
575
mac : auth . digest . Mac ;
429
576
}
430
577
578
+ const qnHttpClient : httpc . HttpClient ;
579
+
431
580
/**
432
581
*
433
582
* @param requestUrl 请求地址
0 commit comments