@@ -9,13 +9,15 @@ import {
99 APIResponseSuccess ,
1010 HttpMethod ,
1111 IClient ,
12+ IConfig ,
1213 IPaginator ,
1314 Response ,
1415 SuperAgentError ,
1516} from '../definitions' ;
1617
1718import { isAPIResponseError , isAPIResponseSuccess } from '../guards' ;
18- import { createRequest } from './utils/http' ;
19+ import { getGlobalProxy } from './utils/http' ;
20+ import { fsReadFile } from './utils/fs' ;
1921import { FatalException } from './errors' ;
2022
2123const FORMAT_ERROR_BODY_MAX_LENGTH = 1000 ;
@@ -24,13 +26,74 @@ const CONTENT_TYPE_JSON = 'application/json';
2426export const ERROR_UNKNOWN_CONTENT_TYPE = 'UNKNOWN_CONTENT_TYPE' ;
2527export const ERROR_UNKNOWN_RESPONSE_FORMAT = 'UNKNOWN_RESPONSE_FORMAT' ;
2628
29+ let CAS : string [ ] | undefined ;
30+ let CERTS : string [ ] | undefined ;
31+ let KEYS : string [ ] | undefined ;
32+
33+ export async function createRequest ( config : IConfig , method : string , url : string ) : Promise < { req : superagentType . SuperAgentRequest ; } > {
34+ const superagent = await import ( 'superagent' ) ;
35+ const c = await config . load ( ) ;
36+ const [ proxy , ] = getGlobalProxy ( ) ;
37+
38+ let req = superagent ( method , url ) ;
39+
40+ if ( proxy && req . proxy ) {
41+ req = req . proxy ( proxy ) ;
42+ }
43+
44+ if ( c . ssl ) {
45+ const conform = ( p ?: string | string [ ] ) : string [ ] => {
46+ if ( ! p ) {
47+ return [ ] ;
48+ }
49+
50+ if ( typeof p === 'string' ) {
51+ return [ p ] ;
52+ }
53+
54+ return p ;
55+ } ;
56+
57+ if ( ! CAS ) {
58+ CAS = await Promise . all ( conform ( c . ssl . cafile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
59+ }
60+
61+ if ( ! CERTS ) {
62+ CERTS = await Promise . all ( conform ( c . ssl . certfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
63+ }
64+
65+ if ( ! KEYS ) {
66+ KEYS = await Promise . all ( conform ( c . ssl . keyfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
67+ }
68+
69+ if ( CAS . length > 0 ) {
70+ req = req . ca ( CAS ) ;
71+ }
72+
73+ if ( CERTS . length > 0 ) {
74+ req = req . cert ( CERTS ) ;
75+ }
76+
77+ if ( KEYS . length > 0 ) {
78+ req = req . key ( KEYS ) ;
79+ }
80+ }
81+
82+ return { req } ;
83+ }
84+
2785export class Client implements IClient {
28- constructor ( public host : string ) { }
86+ constructor ( public config : IConfig ) { }
2987
30- make ( method : HttpMethod , path : string ) : superagentType . SuperAgentRequest {
31- return createRequest ( method , `${ this . host } ${ path } ` )
88+ async make ( method : HttpMethod , path : string ) : Promise < { req : superagentType . SuperAgentRequest ; } > {
89+ const config = await this . config . load ( ) ;
90+ const url = path . startsWith ( 'http://' ) || path . startsWith ( 'https://' ) ? path : `${ config . urls . api } ${ path } ` ;
91+ let { req } = await createRequest ( this . config , method , url ) ;
92+ req = req
3293 . set ( 'Content-Type' , CONTENT_TYPE_JSON )
3394 . set ( 'Accept' , CONTENT_TYPE_JSON ) ;
95+
96+ return { req } ;
3497 }
3598
3699 async do ( req : superagentType . SuperAgentRequest ) : Promise < APIResponseSuccess > {
@@ -45,7 +108,7 @@ export class Client implements IClient {
45108 return r ;
46109 }
47110
48- paginate < T extends Response < Object [ ] > > ( reqgen : ( ) => superagentType . SuperAgentRequest , guard : ( res : APIResponseSuccess ) => res is T ) : Paginator < T > {
111+ async paginate < T extends Response < Object [ ] > > ( reqgen : ( ) => Promise < { req : superagentType . SuperAgentRequest ; } > , guard : ( res : APIResponseSuccess ) => res is T ) : Promise < Paginator < T > > {
49112 return new Paginator < T > ( this , reqgen , guard ) ;
50113 }
51114}
@@ -56,7 +119,7 @@ export class Paginator<T extends Response<Object[]>> implements IPaginator<T> {
56119
57120 constructor (
58121 protected client : IClient ,
59- protected reqgen : ( ) => superagentType . SuperAgentRequest ,
122+ protected reqgen : ( ) => Promise < { req : superagentType . SuperAgentRequest ; } > ,
60123 protected guard : ( res : APIResponseSuccess ) => res is T ,
61124 ) { }
62125
@@ -68,7 +131,7 @@ export class Paginator<T extends Response<Object[]>> implements IPaginator<T> {
68131 return {
69132 done : false ,
70133 value : ( async ( ) => {
71- const req = this . reqgen ( ) ;
134+ const { req } = await this . reqgen ( ) ;
72135
73136 if ( ! this . previousReq ) {
74137 this . previousReq = req ;
0 commit comments