Skip to content

Commit e98b9e6

Browse files
committed
Lazily import cross-fetch
1 parent ae7bd48 commit e98b9e6

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

packages/datadog-api-client-common/http/isomorphic-fetch.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
ResponseContext,
55
ZstdCompressorCallback,
66
} from "./http";
7-
import { fetch as crossFetch } from "cross-fetch";
87
import pako from "pako";
98
import bufferFrom from "buffer-from";
109
import { isBrowser, isNode } from "../util";
1110
import { logger } from "../../../logger";
1211

12+
let cachedCrossFetch: any;
13+
1314
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1415
public debug = false;
1516
public zstdCompressorCallback: ZstdCompressorCallback | undefined;
@@ -61,6 +62,10 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
6162
return this.executeRequest(request, body, 0, headers);
6263
}
6364

65+
private async importCrossFetch(): Promise<any> {
66+
return cachedCrossFetch ?? (cachedCrossFetch = (await import("cross-fetch")).fetch);
67+
}
68+
6469
private async executeRequest(
6570
request: RequestContext,
6671
body: any,
@@ -80,7 +85,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
8085
// `cross-fetch` incorrectly assumes all browsers have XHR available.
8186
// See https://github.com/lquixada/cross-fetch/issues/78
8287
// TODO: Remove once once above issue is resolved.
83-
(!isNode && typeof fetch === "function" ? fetch : crossFetch);
88+
(!isNode && typeof fetch === "function" ? fetch : this.importCrossFetch());
8489

8590
const resp = await fetchFunction(request.getUrl(), fetchOptions);
8691
const responseHeaders: { [name: string]: string } = {};

0 commit comments

Comments
 (0)