From 22c4efe932e427d444bf19871b9ada0ea6106a71 Mon Sep 17 00:00:00 2001 From: Bartosz Jarocki Date: Tue, 6 Feb 2024 11:15:26 +0100 Subject: [PATCH] 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") {