From 95b565c1dea39b2097ce7ddf3f81d23c0671e03e Mon Sep 17 00:00:00 2001 From: Bartosz Jarocki Date: Fri, 2 Feb 2024 20:01:30 +0100 Subject: [PATCH 1/6] feat: allow to provide custom fetch method to AbstractRestfulClient --- src/lib/constants.ts | 5 +++-- src/lib/types/DeepgramClientOptions.ts | 8 ++++++-- src/packages/AbstractRestfulClient.ts | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index ed3a846f..9347521b 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, + fetchOptions: DEFAULT_FETCH_OPTIONS, }; diff --git a/src/lib/types/DeepgramClientOptions.ts b/src/lib/types/DeepgramClientOptions.ts index 8ba12c5a..82fea1ea 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?: { @@ -12,8 +12,12 @@ export interface DeepgramClientOptions { */ url?: string; }; - fetch?: FetchOptions; + fetchOptions?: FetchOptions; restProxy?: { url: null | string; }; } + +export interface DeepgramClientOptionsWithFetchOverride extends DeepgramClientOptions { + fetch: Fetch; +} diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index 6ac59984..8c8ba444 100644 --- a/src/packages/AbstractRestfulClient.ts +++ b/src/packages/AbstractRestfulClient.ts @@ -5,11 +5,15 @@ import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fet import { AbstractClient } from "./AbstractClient"; import { DeepgramClientOptions } from "../lib/types"; import { isBrowser } from "../lib/helpers"; +import { DeepgramClientOptionsWithFetchOverride } from "../lib/types/DeepgramClientOptions"; export abstract class AbstractRestfulClient extends AbstractClient { protected fetch: Fetch; - constructor(protected key: string, protected options: DeepgramClientOptions) { + constructor( + protected key: string, + protected options: DeepgramClientOptions | DeepgramClientOptionsWithFetchOverride + ) { super(key, options); if (isBrowser() && !this._willProxy()) { @@ -18,7 +22,11 @@ export abstract class AbstractRestfulClient extends AbstractClient { ); } - this.fetch = fetchWithAuth(this.key); + if ("fetch" in options) { + this.fetch = options.fetch; + } else { + this.fetch = fetchWithAuth(this.key); + } } protected _getErrorMessage(err: any): string { @@ -49,9 +57,9 @@ export abstract class AbstractRestfulClient extends AbstractClient { body?: string | Buffer | Readable ) { const params: { [k: string]: any } = { - ...this.options?.fetch, + ...this.options?.fetchOptions, method, - headers: { ...this.options?.fetch?.headers, ...headers } || {}, + headers: { ...this.options?.fetchOptions?.headers, ...headers } || {}, }; if (method === "GET") { From 141cd81b754a8ebd1f63481343052310bf83d037 Mon Sep 17 00:00:00 2001 From: Bartosz Jarocki Date: Fri, 2 Feb 2024 20:18:18 +0100 Subject: [PATCH 2/6] fix: allow to provide both fetch and fetch options --- src/lib/types/DeepgramClientOptions.ts | 5 +---- src/packages/AbstractRestfulClient.ts | 12 ++---------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/lib/types/DeepgramClientOptions.ts b/src/lib/types/DeepgramClientOptions.ts index 82fea1ea..f4c79a80 100644 --- a/src/lib/types/DeepgramClientOptions.ts +++ b/src/lib/types/DeepgramClientOptions.ts @@ -13,11 +13,8 @@ export interface DeepgramClientOptions { url?: string; }; fetchOptions?: FetchOptions; + fetch?: Fetch; restProxy?: { url: null | string; }; } - -export interface DeepgramClientOptionsWithFetchOverride extends DeepgramClientOptions { - fetch: Fetch; -} diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index 8c8ba444..40c1c968 100644 --- a/src/packages/AbstractRestfulClient.ts +++ b/src/packages/AbstractRestfulClient.ts @@ -5,15 +5,11 @@ import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fet import { AbstractClient } from "./AbstractClient"; import { DeepgramClientOptions } from "../lib/types"; import { isBrowser } from "../lib/helpers"; -import { DeepgramClientOptionsWithFetchOverride } from "../lib/types/DeepgramClientOptions"; export abstract class AbstractRestfulClient extends AbstractClient { protected fetch: Fetch; - constructor( - protected key: string, - protected options: DeepgramClientOptions | DeepgramClientOptionsWithFetchOverride - ) { + constructor(protected key: string, protected options: DeepgramClientOptions) { super(key, options); if (isBrowser() && !this._willProxy()) { @@ -22,11 +18,7 @@ export abstract class AbstractRestfulClient extends AbstractClient { ); } - if ("fetch" in options) { - this.fetch = options.fetch; - } else { - this.fetch = fetchWithAuth(this.key); - } + this.fetch = options.fetch ? options.fetch : fetchWithAuth(this.key); } protected _getErrorMessage(err: any): string { From 22c4efe932e427d444bf19871b9ada0ea6106a71 Mon Sep 17 00:00:00 2001 From: Bartosz Jarocki Date: Tue, 6 Feb 2024 11:15:26 +0100 Subject: [PATCH 3/6] refactor: init custom fetch in resolveFetch --- src/lib/constants.ts | 2 +- src/lib/fetch.ts | 10 ++++++---- src/lib/types/DeepgramClientOptions.ts | 4 ++-- src/packages/AbstractRestfulClient.ts | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 9347521b..f3d29335 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -21,5 +21,5 @@ export const DEFAULT_FETCH_OPTIONS: FetchOptions = { export const DEFAULT_OPTIONS: DeepgramClientOptions = { global: DEFAULT_GLOBAL_OPTIONS, - fetchOptions: DEFAULT_FETCH_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 f4c79a80..8bffca93 100644 --- a/src/lib/types/DeepgramClientOptions.ts +++ b/src/lib/types/DeepgramClientOptions.ts @@ -12,8 +12,8 @@ export interface DeepgramClientOptions { */ url?: string; }; - fetchOptions?: FetchOptions; - fetch?: Fetch; + fetch?: FetchOptions; + customFetch?: Fetch; restProxy?: { url: null | string; }; diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index 40c1c968..bc974483 100644 --- a/src/packages/AbstractRestfulClient.ts +++ b/src/packages/AbstractRestfulClient.ts @@ -18,7 +18,7 @@ export abstract class AbstractRestfulClient extends AbstractClient { ); } - this.fetch = options.fetch ? options.fetch : fetchWithAuth(this.key); + this.fetch = fetchWithAuth(this.key, options.customFetch); } protected _getErrorMessage(err: any): string { @@ -49,9 +49,9 @@ export abstract class AbstractRestfulClient extends AbstractClient { body?: string | Buffer | Readable ) { const params: { [k: string]: any } = { - ...this.options?.fetchOptions, + ...this.options?.fetch, method, - headers: { ...this.options?.fetchOptions?.headers, ...headers } || {}, + headers: { ...this.options?.fetch?.headers, ...headers } || {}, }; if (method === "GET") { From f68b1e3892753ad6631507e9944cc1eb7084a323 Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Thu, 25 Apr 2024 09:30:08 +0100 Subject: [PATCH 4/6] rename `customFetch` to `_experimentalCustomFetch` i want to avoid taking any new namespaces while i think about the best way to do this --- src/lib/types/DeepgramClientOptions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/types/DeepgramClientOptions.ts b/src/lib/types/DeepgramClientOptions.ts index 8bffca93..4b4d40ce 100644 --- a/src/lib/types/DeepgramClientOptions.ts +++ b/src/lib/types/DeepgramClientOptions.ts @@ -13,7 +13,7 @@ export interface DeepgramClientOptions { url?: string; }; fetch?: FetchOptions; - customFetch?: Fetch; + _experimentalCustomFetch?: Fetch; restProxy?: { url: null | string; }; From 1639db237cb23eef2352065b97f23cf10c98010b Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Thu, 25 Apr 2024 09:30:43 +0100 Subject: [PATCH 5/6] use `_experimentalCustomFetch` instead of `customFetch` for fetch override --- src/packages/AbstractRestfulClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index bc974483..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, options.customFetch); + this.fetch = fetchWithAuth(this.key, options._experimentalCustomFetch); } protected _getErrorMessage(err: any): string { From 84a4e2725dc68bca353a165cd58c4c69678e1cbe Mon Sep 17 00:00:00 2001 From: Bartosz Jarocki Date: Thu, 25 Apr 2024 19:05:12 +0200 Subject: [PATCH 6/6] test: added custom fetch test --- test/client.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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"]); + }); });