From b368ab9da07aac3f172aa22afbec175cf3fde030 Mon Sep 17 00:00:00 2001 From: Danil Valov Date: Sat, 27 Dec 2025 14:32:21 +0300 Subject: [PATCH] feat: Add support for Fetch Implementation --- api-report/genai-node.api.md | 1 + api-report/genai-web.api.md | 1 + api-report/genai.api.md | 1 + src/_api_client.ts | 12 +++++++++++- src/client.ts | 8 ++++++++ src/node/node_client.ts | 1 + src/web/web_client.ts | 1 + 7 files changed, 24 insertions(+), 1 deletion(-) diff --git a/api-report/genai-node.api.md b/api-report/genai-node.api.md index bac11aebb..5f21bb54d 100644 --- a/api-report/genai-node.api.md +++ b/api-report/genai-node.api.md @@ -1624,6 +1624,7 @@ export class GoogleGenAI { export interface GoogleGenAIOptions { apiKey?: string; apiVersion?: string; + fetchImplementation?: typeof fetch; googleAuthOptions?: GoogleAuthOptions; httpOptions?: HttpOptions; location?: string; diff --git a/api-report/genai-web.api.md b/api-report/genai-web.api.md index bac11aebb..5f21bb54d 100644 --- a/api-report/genai-web.api.md +++ b/api-report/genai-web.api.md @@ -1624,6 +1624,7 @@ export class GoogleGenAI { export interface GoogleGenAIOptions { apiKey?: string; apiVersion?: string; + fetchImplementation?: typeof fetch; googleAuthOptions?: GoogleAuthOptions; httpOptions?: HttpOptions; location?: string; diff --git a/api-report/genai.api.md b/api-report/genai.api.md index bac11aebb..5f21bb54d 100644 --- a/api-report/genai.api.md +++ b/api-report/genai.api.md @@ -1624,6 +1624,7 @@ export class GoogleGenAI { export interface GoogleGenAIOptions { apiKey?: string; apiVersion?: string; + fetchImplementation?: typeof fetch; googleAuthOptions?: GoogleAuthOptions; httpOptions?: HttpOptions; location?: string; diff --git a/src/_api_client.ts b/src/_api_client.ts index fe4b2cbe1..bec294ad8 100644 --- a/src/_api_client.ts +++ b/src/_api_client.ts @@ -78,6 +78,13 @@ export interface ApiClientInitOptions { * This can be used to e.g specify the runtime and its version. */ userAgentExtra?: string; + /** + * Optional. A custom fetch implementation to use for all HTTP requests made by the SDK. + * + * @remarks + * If unset, the SDK will use the default `fetch` implementation available in the runtime environment. + */ + fetchImplementation?: typeof fetch; } /** @@ -630,7 +637,10 @@ export class ApiClient implements GeminiNextGenAPIClientAdapter { url: string, requestInit: RequestInit, ): Promise { - return fetch(url, requestInit).catch((e) => { + return (this.clientOptions.fetchImplementation || fetch)( + url, + requestInit, + ).catch((e) => { throw new Error(`exception ${e} sending request`); }); } diff --git a/src/client.ts b/src/client.ts index c3987cdeb..d550b9c13 100644 --- a/src/client.ts +++ b/src/client.ts @@ -89,6 +89,13 @@ export interface GoogleGenAIOptions { * Optional. A set of customizable configuration for HTTP requests. */ httpOptions?: HttpOptions; + /** + * Optional. A custom fetch implementation to use for all HTTP requests made by the SDK. + * + * @remarks + * If unset, the SDK will use the default `fetch` implementation available in the runtime environment. + */ + fetchImplementation?: typeof fetch; } /** @@ -197,6 +204,7 @@ export class GoogleGenAI { userAgentExtra: LANGUAGE_LABEL_PREFIX + 'cross', uploader: new CrossUploader(), downloader: new CrossDownloader(), + fetchImplementation: options.fetchImplementation, }); this.models = new Models(this.apiClient); this.live = new Live(this.apiClient, auth, new CrossWebSocketFactory()); diff --git a/src/node/node_client.ts b/src/node/node_client.ts index f03218244..903b24fbe 100644 --- a/src/node/node_client.ts +++ b/src/node/node_client.ts @@ -210,6 +210,7 @@ export class GoogleGenAI { userAgentExtra: LANGUAGE_LABEL_PREFIX + process.version, uploader: new NodeUploader(), downloader: new NodeDownloader(), + fetchImplementation: options.fetchImplementation, }); this.models = new Models(this.apiClient); this.live = new Live(this.apiClient, auth, new NodeWebSocketFactory()); diff --git a/src/web/web_client.ts b/src/web/web_client.ts index b3ac1a914..63547625f 100644 --- a/src/web/web_client.ts +++ b/src/web/web_client.ts @@ -151,6 +151,7 @@ export class GoogleGenAI { userAgentExtra: LANGUAGE_LABEL_PREFIX + 'web', uploader: new BrowserUploader(), downloader: new BrowserDownloader(), + fetchImplementation: options.fetchImplementation, }); this.models = new Models(this.apiClient); this.live = new Live(this.apiClient, auth, new BrowserWebSocketFactory());