Skip to content

Commit c09810e

Browse files
iammminzzyHantingZhang2jack-edmonds-dd
authored
Allow custom fetch function to be provided (#1646)
* Allow custom `fetch` function to be provided * Switch type to any. * Fix test. * Fix test. --------- Co-authored-by: Hanting ZHANG <[email protected]> Co-authored-by: Jack Edmonds <[email protected]>
1 parent 18d6812 commit c09810e

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

.generator/src/generator/templates/configuration.j2

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export interface ConfigurationParameters {
106106
* Default index of a server to use for an operation from the predefined server operation map
107107
*/
108108
operationServerIndices?: { [ name: string ]: number };
109+
/**
110+
* Custom `fetch` function
111+
*/
112+
fetch?: any;
109113
/**
110114
* HTTP library to use e.g. IsomorphicFetch
111115
*/
@@ -180,7 +184,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
180184
conf.baseServer,
181185
conf.serverIndex || 0,
182186
conf.operationServerIndices || {},
183-
conf.httpApi || new DefaultHttpLibrary(),
187+
conf.httpApi || new DefaultHttpLibrary({ fetch: conf.fetch }),
184188
configureAuthMethods(authMethods),
185189
conf.httpConfig || {},
186190
conf.debug,

.generator/src/generator/templates/http/isomorphic-fetch.j2

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1212
public maxRetries!: number ;
1313
public backoffBase!: number ;
1414
public backoffMultiplier!: number;
15+
#fetch: any;
16+
17+
constructor({ fetch: customFetch }: { fetch?: any }) {
18+
this.#fetch =
19+
customFetch ||
20+
// On non-node environments, use native fetch if available.
21+
// `cross-fetch` incorrectly assumes all browsers have XHR available.
22+
// See https://github.com/lquixada/cross-fetch/issues/78
23+
// TODO: Remove once once above issue is resolved.
24+
(!isNode && typeof fetch === "function" ? fetch : crossFetch);
25+
}
1526

1627
public send(request: RequestContext): Promise<ResponseContext> {
1728
if (this.debug) {
@@ -61,19 +72,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
6172
currentAttempt: number,
6273
headers: {[key: string]: string}
6374
): Promise<ResponseContext> {
64-
// On non-node environments, use native fetch if available.
65-
// `cross-fetch` incorrectly assumes all browsers have XHR available.
66-
// See https://github.com/lquixada/cross-fetch/issues/78
67-
// TODO: Remove once once above issue is resolved.
68-
const fetchFunction =!isNode && typeof fetch === "function" ? fetch : crossFetch;
6975
const fetchOptions = {
7076
method: request.getHttpMethod().toString(),
7177
body: body,
7278
headers: headers,
7379
signal: request.getHttpConfig().signal,
7480
}
7581
try {
76-
const resp = await fetchFunction(request.getUrl(),fetchOptions);
82+
const resp = await this.#fetch(request.getUrl(),fetchOptions);
7783
const responseHeaders: { [name: string]: string } = {};
7884
resp.headers.forEach((value: string, name: string) => {
7985
responseHeaders[name] = value;

packages/datadog-api-client-common/configuration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export interface ConfigurationParameters {
120120
* Default index of a server to use for an operation from the predefined server operation map
121121
*/
122122
operationServerIndices?: { [name: string]: number };
123+
/**
124+
* Custom `fetch` function
125+
*/
126+
fetch?: any;
123127
/**
124128
* HTTP library to use e.g. IsomorphicFetch
125129
*/
@@ -205,7 +209,7 @@ export function createConfiguration(
205209
conf.baseServer,
206210
conf.serverIndex || 0,
207211
conf.operationServerIndices || {},
208-
conf.httpApi || new DefaultHttpLibrary(),
212+
conf.httpApi || new DefaultHttpLibrary({ fetch: conf.fetch }),
209213
configureAuthMethods(authMethods),
210214
conf.httpConfig || {},
211215
conf.debug,

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1717
public maxRetries!: number;
1818
public backoffBase!: number;
1919
public backoffMultiplier!: number;
20+
#fetch: any;
21+
22+
constructor({ fetch: customFetch }: { fetch?: any }) {
23+
this.#fetch =
24+
customFetch ||
25+
// On non-node environments, use native fetch if available.
26+
// `cross-fetch` incorrectly assumes all browsers have XHR available.
27+
// See https://github.com/lquixada/cross-fetch/issues/78
28+
// TODO: Remove once once above issue is resolved.
29+
(!isNode && typeof fetch === "function" ? fetch : crossFetch);
30+
}
2031

2132
public send(request: RequestContext): Promise<ResponseContext> {
2233
if (this.debug) {
@@ -66,20 +77,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
6677
currentAttempt: number,
6778
headers: { [key: string]: string }
6879
): Promise<ResponseContext> {
69-
// On non-node environments, use native fetch if available.
70-
// `cross-fetch` incorrectly assumes all browsers have XHR available.
71-
// See https://github.com/lquixada/cross-fetch/issues/78
72-
// TODO: Remove once once above issue is resolved.
73-
const fetchFunction =
74-
!isNode && typeof fetch === "function" ? fetch : crossFetch;
7580
const fetchOptions = {
7681
method: request.getHttpMethod().toString(),
7782
body: body,
7883
headers: headers,
7984
signal: request.getHttpConfig().signal,
8085
};
8186
try {
82-
const resp = await fetchFunction(request.getUrl(), fetchOptions);
87+
const resp = await this.#fetch(request.getUrl(), fetchOptions);
8388
const responseHeaders: { [name: string]: string } = {};
8489
resp.headers.forEach((value: string, name: string) => {
8590
responseHeaders[name] = value;

tests/api/retry.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
describe("IsomorphicFetchHttpLibrary Retry Test", () => {
1010

1111
const fakeRequestContext = new RequestContext("https://retry.test.com",HttpMethod.GET);
12-
const httpLibrary = new IsomorphicFetchHttpLibrary();
12+
const httpLibrary = new IsomorphicFetchHttpLibrary({fetch: null});
1313
httpLibrary['sleep'] = jest.fn(() => Promise.resolve());
1414
httpLibrary.enableRetry = true;
1515
httpLibrary.maxRetries = 3;

0 commit comments

Comments
 (0)