@@ -9,35 +9,36 @@ import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
9
9
import { ILogger , LogLevel } from "./ILogger" ;
10
10
import { Platform } from "./Utils" ;
11
11
12
- let abortControllerType : { prototype : AbortController , new ( ) : AbortController } ;
13
- let fetchType : ( input : RequestInfo , init ?: RequestInit ) => Promise < Response > ;
14
- let jar : tough . CookieJar ;
15
- if ( typeof fetch === "undefined" ) {
16
- // In order to ignore the dynamic require in webpack builds we need to do this magic
17
- // @ts -ignore: TS doesn't know about these names
18
- const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
19
-
20
- // Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
21
- jar = new ( requireFunc ( "tough-cookie" ) ) . CookieJar ( ) ;
22
- fetchType = requireFunc ( "node-fetch" ) ;
23
-
24
- // node-fetch doesn't have a nice API for getting and setting cookies
25
- // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
26
- fetchType = requireFunc ( "fetch-cookie" ) ( fetchType , jar ) ;
27
-
28
- // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
29
- abortControllerType = requireFunc ( "abort-controller" ) ;
30
- } else {
31
- fetchType = fetch ;
32
- abortControllerType = AbortController ;
33
- }
34
-
35
12
export class FetchHttpClient extends HttpClient {
13
+ private readonly abortControllerType : { prototype : AbortController , new ( ) : AbortController } ;
14
+ private readonly fetchType : ( input : RequestInfo , init ?: RequestInit ) => Promise < Response > ;
15
+ private readonly jar ?: tough . CookieJar ;
16
+
36
17
private readonly logger : ILogger ;
37
18
38
19
public constructor ( logger : ILogger ) {
39
20
super ( ) ;
40
21
this . logger = logger ;
22
+
23
+ if ( typeof fetch === "undefined" ) {
24
+ // In order to ignore the dynamic require in webpack builds we need to do this magic
25
+ // @ts -ignore: TS doesn't know about these names
26
+ const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
27
+
28
+ // Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
29
+ this . jar = new ( requireFunc ( "tough-cookie" ) ) . CookieJar ( ) ;
30
+ this . fetchType = requireFunc ( "node-fetch" ) ;
31
+
32
+ // node-fetch doesn't have a nice API for getting and setting cookies
33
+ // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
34
+ this . fetchType = requireFunc ( "fetch-cookie" ) ( this . fetchType , this . jar ) ;
35
+
36
+ // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
37
+ this . abortControllerType = requireFunc ( "abort-controller" ) ;
38
+ } else {
39
+ this . fetchType = fetch . bind ( self ) ;
40
+ this . abortControllerType = AbortController ;
41
+ }
41
42
}
42
43
43
44
/** @inheritDoc */
@@ -54,7 +55,7 @@ export class FetchHttpClient extends HttpClient {
54
55
throw new Error ( "No url defined." ) ;
55
56
}
56
57
57
- const abortController = new abortControllerType ( ) ;
58
+ const abortController = new this . abortControllerType ( ) ;
58
59
59
60
let error : any ;
60
61
// Hook our abortSignal into the abort controller
@@ -79,7 +80,7 @@ export class FetchHttpClient extends HttpClient {
79
80
80
81
let response : Response ;
81
82
try {
82
- response = await fetchType ( request . url ! , {
83
+ response = await this . fetchType ( request . url ! , {
83
84
body : request . content ! ,
84
85
cache : "no-cache" ,
85
86
credentials : request . withCredentials === true ? "include" : "same-origin" ,
@@ -127,9 +128,9 @@ export class FetchHttpClient extends HttpClient {
127
128
128
129
public getCookieString ( url : string ) : string {
129
130
let cookies : string = "" ;
130
- if ( Platform . isNode ) {
131
+ if ( Platform . isNode && this . jar ) {
131
132
// @ts -ignore: unused variable
132
- jar . getCookies ( url , ( e , c ) => cookies = c . join ( "; " ) ) ;
133
+ this . jar . getCookies ( url , ( e , c ) => cookies = c . join ( "; " ) ) ;
133
134
}
134
135
return cookies ;
135
136
}
0 commit comments