diff --git a/src/lib/constants.ts b/src/lib/constants.ts index ed3a846f..f3d29335 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -1,4 +1,5 @@ import { isBrowser } from "./helpers"; +import { DeepgramClientOptions } from "./types/DeepgramClientOptions"; import { FetchOptions } from "./types/Fetch"; import { version } from "./version"; @@ -18,7 +19,7 @@ export const DEFAULT_FETCH_OPTIONS: FetchOptions = { headers: DEFAULT_HEADERS, }; -export const DEFAULT_OPTIONS = { +export const DEFAULT_OPTIONS: DeepgramClientOptions = { global: DEFAULT_GLOBAL_OPTIONS, fetch: DEFAULT_FETCH_OPTIONS, }; diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index 0d65b632..37f7e594 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -2,9 +2,11 @@ import crossFetch from "cross-fetch"; import { resolveHeadersConstructor } from "./helpers"; import type { Fetch } from "./types/Fetch"; -export const resolveFetch = (): Fetch => { +export const resolveFetch = (customFetch?: Fetch): Fetch => { let _fetch: Fetch; - if (typeof fetch === "undefined") { + if (customFetch) { + _fetch = customFetch; + } else if (typeof fetch === "undefined") { _fetch = crossFetch as unknown as Fetch; } else { _fetch = fetch; @@ -12,8 +14,8 @@ export const resolveFetch = (): Fetch => { return (...args) => _fetch(...args); }; -export const fetchWithAuth = (apiKey: string): Fetch => { - const fetch = resolveFetch(); +export const fetchWithAuth = (apiKey: string, customFetch?: Fetch): Fetch => { + const fetch = resolveFetch(customFetch); const HeadersConstructor = resolveHeadersConstructor(); return async (input, init) => { diff --git a/src/lib/types/DeepgramClientOptions.ts b/src/lib/types/DeepgramClientOptions.ts index 8ba12c5a..4b4d40ce 100644 --- a/src/lib/types/DeepgramClientOptions.ts +++ b/src/lib/types/DeepgramClientOptions.ts @@ -1,4 +1,4 @@ -import { FetchOptions } from "./Fetch"; +import { Fetch, FetchOptions } from "./Fetch"; export interface DeepgramClientOptions { global?: { @@ -13,6 +13,7 @@ export interface DeepgramClientOptions { url?: string; }; fetch?: FetchOptions; + _experimentalCustomFetch?: Fetch; restProxy?: { url: null | string; }; diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index 6ac59984..48fa65ae 100644 --- a/src/packages/AbstractRestfulClient.ts +++ b/src/packages/AbstractRestfulClient.ts @@ -18,7 +18,7 @@ export abstract class AbstractRestfulClient extends AbstractClient { ); } - this.fetch = fetchWithAuth(this.key); + this.fetch = fetchWithAuth(this.key, options._experimentalCustomFetch); } protected _getErrorMessage(err: any): string { diff --git a/test/client.test.ts b/test/client.test.ts index 04c9b300..426edf8c 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -1,6 +1,6 @@ import { createClient } from "../src"; import { DEFAULT_URL } from "../src/lib/constants"; -import { expect } from "chai"; +import { expect, assert } from "chai"; import { faker } from "@faker-js/faker"; import { stripTrailingSlash } from "../src/lib/helpers"; import DeepgramClient from "../src/DeepgramClient"; @@ -82,4 +82,21 @@ describe("testing creation of a deepgram client object", () => { expect(client).is.instanceOf(DeepgramClient); }); + + it("should use custom fetch when provided", async () => { + const fetch = async () => { + return new Response(JSON.stringify({ customFetch: true })); + }; + + const client = createClient(faker.string.alphanumeric(40), { + global: { url: "https://api.mock.deepgram.com" }, + _experimentalCustomFetch: fetch, + }); + + const { result, error } = await client.manage.getProjectBalances(faker.string.uuid()); + + assert.isNull(error); + assert.isNotNull(result); + assert.containsAllDeepKeys(result, ["customFetch"]); + }); });