Skip to content

Allow custom fetch function to be provided #1646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export interface ConfigurationParameters {
* Default index of a server to use for an operation from the predefined server operation map
*/
operationServerIndices?: { [ name: string ]: number };
/**
* Custom `fetch` function
*/
fetch?: any;
/**
* HTTP library to use e.g. IsomorphicFetch
*/
Expand Down Expand Up @@ -180,7 +184,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
conf.baseServer,
conf.serverIndex || 0,
conf.operationServerIndices || {},
conf.httpApi || new DefaultHttpLibrary(),
conf.httpApi || new DefaultHttpLibrary({ fetch: conf.fetch }),
configureAuthMethods(authMethods),
conf.httpConfig || {},
conf.debug,
Expand Down
18 changes: 12 additions & 6 deletions .generator/src/generator/templates/http/isomorphic-fetch.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public maxRetries!: number ;
public backoffBase!: number ;
public backoffMultiplier!: number;
#fetch: any;

constructor({ fetch: customFetch }: { fetch?: any }) {
this.#fetch =
customFetch ||
// On non-node environments, use native fetch if available.
// `cross-fetch` incorrectly assumes all browsers have XHR available.
// See https://github.com/lquixada/cross-fetch/issues/78
// TODO: Remove once once above issue is resolved.
(!isNode && typeof fetch === "function" ? fetch : crossFetch);
}

public send(request: RequestContext): Promise<ResponseContext> {
if (this.debug) {
Expand Down Expand Up @@ -61,19 +72,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
currentAttempt: number,
headers: {[key: string]: string}
): Promise<ResponseContext> {
// On non-node environments, use native fetch if available.
// `cross-fetch` incorrectly assumes all browsers have XHR available.
// See https://github.com/lquixada/cross-fetch/issues/78
// TODO: Remove once once above issue is resolved.
const fetchFunction =!isNode && typeof fetch === "function" ? fetch : crossFetch;
const fetchOptions = {
method: request.getHttpMethod().toString(),
body: body,
headers: headers,
signal: request.getHttpConfig().signal,
}
try {
const resp = await fetchFunction(request.getUrl(),fetchOptions);
const resp = await this.#fetch(request.getUrl(),fetchOptions);
const responseHeaders: { [name: string]: string } = {};
resp.headers.forEach((value: string, name: string) => {
responseHeaders[name] = value;
Expand Down
6 changes: 5 additions & 1 deletion packages/datadog-api-client-common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export interface ConfigurationParameters {
* Default index of a server to use for an operation from the predefined server operation map
*/
operationServerIndices?: { [name: string]: number };
/**
* Custom `fetch` function
*/
fetch?: any;
/**
* HTTP library to use e.g. IsomorphicFetch
*/
Expand Down Expand Up @@ -205,7 +209,7 @@ export function createConfiguration(
conf.baseServer,
conf.serverIndex || 0,
conf.operationServerIndices || {},
conf.httpApi || new DefaultHttpLibrary(),
conf.httpApi || new DefaultHttpLibrary({ fetch: conf.fetch }),
configureAuthMethods(authMethods),
conf.httpConfig || {},
conf.debug,
Expand Down
19 changes: 12 additions & 7 deletions packages/datadog-api-client-common/http/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public maxRetries!: number;
public backoffBase!: number;
public backoffMultiplier!: number;
#fetch: any;

constructor({ fetch: customFetch }: { fetch?: any }) {
this.#fetch =
customFetch ||
// On non-node environments, use native fetch if available.
// `cross-fetch` incorrectly assumes all browsers have XHR available.
// See https://github.com/lquixada/cross-fetch/issues/78
// TODO: Remove once once above issue is resolved.
(!isNode && typeof fetch === "function" ? fetch : crossFetch);
}

public send(request: RequestContext): Promise<ResponseContext> {
if (this.debug) {
Expand Down Expand Up @@ -66,20 +77,14 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
currentAttempt: number,
headers: { [key: string]: string }
): Promise<ResponseContext> {
// On non-node environments, use native fetch if available.
// `cross-fetch` incorrectly assumes all browsers have XHR available.
// See https://github.com/lquixada/cross-fetch/issues/78
// TODO: Remove once once above issue is resolved.
const fetchFunction =
!isNode && typeof fetch === "function" ? fetch : crossFetch;
const fetchOptions = {
method: request.getHttpMethod().toString(),
body: body,
headers: headers,
signal: request.getHttpConfig().signal,
};
try {
const resp = await fetchFunction(request.getUrl(), fetchOptions);
const resp = await this.#fetch(request.getUrl(), fetchOptions);
const responseHeaders: { [name: string]: string } = {};
resp.headers.forEach((value: string, name: string) => {
responseHeaders[name] = value;
Expand Down
2 changes: 1 addition & 1 deletion tests/api/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
describe("IsomorphicFetchHttpLibrary Retry Test", () => {

const fakeRequestContext = new RequestContext("https://retry.test.com",HttpMethod.GET);
const httpLibrary = new IsomorphicFetchHttpLibrary();
const httpLibrary = new IsomorphicFetchHttpLibrary({fetch: null});
httpLibrary['sleep'] = jest.fn(() => Promise.resolve());
httpLibrary.enableRetry = true;
httpLibrary.maxRetries = 3;
Expand Down
Loading