@@ -8,6 +8,7 @@ import { AbortError, HttpError, TimeoutError } from "./Errors";
88import { HttpClient , HttpRequest , HttpResponse } from "./HttpClient" ;
99import { ILogger , LogLevel } from "./ILogger" ;
1010import { Platform , getGlobalThis , isArrayBuffer } from "./Utils" ;
11+ import { configureAbortController , configureFetch } from "./DynamicImports" ;
1112
1213export class FetchHttpClient extends HttpClient {
1314 private readonly _abortControllerType : { prototype : AbortController , new ( ) : AbortController } ;
@@ -20,38 +21,19 @@ export class FetchHttpClient extends HttpClient {
2021 super ( ) ;
2122 this . _logger = logger ;
2223
23- // Node added a fetch implementation to the global scope starting in v18.
24- // We need to add a cookie jar in node to be able to share cookies with WebSocket
25- if ( typeof fetch === "undefined" || Platform . isNode ) {
26- // In order to ignore the dynamic require in webpack builds we need to do this magic
27- // @ts -ignore: TS doesn't know about these names
28- const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
29-
30- // Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
31- this . _jar = new ( requireFunc ( "tough-cookie" ) ) . CookieJar ( ) ;
32-
33- if ( typeof fetch === "undefined" ) {
34- this . _fetchType = requireFunc ( "node-fetch" ) ;
35- } else {
36- // Use fetch from Node if available
37- this . _fetchType = fetch ;
38- }
39-
40- // node-fetch doesn't have a nice API for getting and setting cookies
41- // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
42- this . _fetchType = requireFunc ( "fetch-cookie" ) ( this . _fetchType , this . _jar ) ;
24+ // This is how you do "reference" arguments
25+ const fetchObj = { _fetchType : undefined , _jar : undefined } ;
26+ if ( configureFetch ( fetchObj ) ) {
27+ this . _fetchType = fetchObj . _fetchType ! ;
28+ this . _jar = fetchObj . _jar ;
4329 } else {
4430 this . _fetchType = fetch . bind ( getGlobalThis ( ) ) ;
4531 }
46- if ( typeof AbortController === "undefined" ) {
47- // In order to ignore the dynamic require in webpack builds we need to do this magic
48- // @ts -ignore: TS doesn't know about these names
49- const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require ;
5032
51- // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
52- this . _abortControllerType = requireFunc ( "abort-controller" ) ;
53- } else {
54- this . _abortControllerType = AbortController ;
33+ this . _abortControllerType = AbortController ;
34+ const abortObj = { _abortControllerType : this . _abortControllerType } ;
35+ if ( configureAbortController ( abortObj ) ) {
36+ this . _abortControllerType = abortObj . _abortControllerType ;
5537 }
5638 }
5739
0 commit comments