@@ -58,59 +58,61 @@ async function getNodeRequest(options: IRequestOptions): TPromise<IRawRequestFun
5858export function request ( options : IRequestOptions ) : TPromise < IRequestContext > {
5959 let req : http . ClientRequest ;
6060
61- return new TPromise < IRequestContext > ( async ( c , e ) => {
62- const endpoint = parseUrl ( options . url ) ;
63- const rawRequest = options . getRawRequest
64- ? options . getRawRequest ( options )
65- : await getNodeRequest ( options ) ;
66-
67- const opts : https . RequestOptions = {
68- hostname : endpoint . hostname ,
69- port : endpoint . port ? parseInt ( endpoint . port ) : ( endpoint . protocol === 'https:' ? 443 : 80 ) ,
70- protocol : endpoint . protocol ,
71- path : endpoint . path ,
72- method : options . type || 'GET' ,
73- headers : options . headers ,
74- agent : options . agent ,
75- rejectUnauthorized : isBoolean ( options . strictSSL ) ? options . strictSSL : true
76- } ;
77-
78- if ( options . user && options . password ) {
79- opts . auth = options . user + ':' + options . password ;
80- }
61+ const rawRequestPromise = options . getRawRequest
62+ ? TPromise . as ( options . getRawRequest ( options ) )
63+ : getNodeRequest ( options ) ;
64+
65+ return rawRequestPromise . then ( rawRequest => {
66+ return new TPromise < IRequestContext > ( ( c , e ) => {
67+ const endpoint = parseUrl ( options . url ) ;
68+
69+ const opts : https . RequestOptions = {
70+ hostname : endpoint . hostname ,
71+ port : endpoint . port ? parseInt ( endpoint . port ) : ( endpoint . protocol === 'https:' ? 443 : 80 ) ,
72+ protocol : endpoint . protocol ,
73+ path : endpoint . path ,
74+ method : options . type || 'GET' ,
75+ headers : options . headers ,
76+ agent : options . agent ,
77+ rejectUnauthorized : isBoolean ( options . strictSSL ) ? options . strictSSL : true
78+ } ;
79+
80+ if ( options . user && options . password ) {
81+ opts . auth = options . user + ':' + options . password ;
82+ }
8183
82- req = rawRequest ( opts , ( res : http . ClientResponse ) => {
83- const followRedirects = isNumber ( options . followRedirects ) ? options . followRedirects : 3 ;
84+ req = rawRequest ( opts , ( res : http . ClientResponse ) => {
85+ const followRedirects = isNumber ( options . followRedirects ) ? options . followRedirects : 3 ;
8486
85- if ( res . statusCode >= 300 && res . statusCode < 400 && followRedirects > 0 && res . headers [ 'location' ] ) {
86- request ( assign ( { } , options , {
87- url : res . headers [ 'location' ] ,
88- followRedirects : followRedirects - 1
89- } ) ) . done ( c , e ) ;
90- } else {
91- let stream : Stream = res ;
87+ if ( res . statusCode >= 300 && res . statusCode < 400 && followRedirects > 0 && res . headers [ 'location' ] ) {
88+ request ( assign ( { } , options , {
89+ url : res . headers [ 'location' ] ,
90+ followRedirects : followRedirects - 1
91+ } ) ) . done ( c , e ) ;
92+ } else {
93+ let stream : Stream = res ;
9294
93- if ( res . headers [ 'content-encoding' ] === 'gzip' ) {
94- stream = stream . pipe ( createGunzip ( ) ) ;
95- }
95+ if ( res . headers [ 'content-encoding' ] === 'gzip' ) {
96+ stream = stream . pipe ( createGunzip ( ) ) ;
97+ }
9698
97- c ( { res, stream } ) ;
98- }
99- } ) ;
99+ c ( { res, stream } ) ;
100+ }
101+ } ) ;
100102
101- req . on ( 'error' , e ) ;
103+ req . on ( 'error' , e ) ;
102104
103- if ( options . timeout ) {
104- req . setTimeout ( options . timeout ) ;
105- }
105+ if ( options . timeout ) {
106+ req . setTimeout ( options . timeout ) ;
107+ }
106108
107- if ( options . data ) {
108- req . write ( options . data ) ;
109- }
109+ if ( options . data ) {
110+ req . write ( options . data ) ;
111+ }
110112
111- req . end ( ) ;
112- } ,
113- ( ) => req && req . abort ( ) ) ;
113+ req . end ( ) ;
114+ } , ( ) => req && req . abort ( ) ) ;
115+ } ) ;
114116}
115117
116118function isSuccess ( context : IRequestContext ) : boolean {
0 commit comments