diff --git a/src/core/client.ts b/src/core/client.ts index 197f8c30..f707a593 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -205,6 +205,12 @@ export interface ApiClientOptions { NonNullable[1]>, "method" | "headers" | "body" >; + + /** + * `fetch` function to use for making HTTP requests. Default: `node-fetch` in Node.js, `fetch` in Deno. + */ + fetch?: typeof fetch; + /** * When the network connection is unreliable and some API requests fail * because of that, grammY will throw errors that tell you exactly which @@ -222,6 +228,8 @@ export interface ApiClientOptions { class ApiClient { private readonly options: Required; + private readonly fetch: typeof fetch; + private hasUsedWebhookReply = false; readonly installedTransformers: Transformer[] = []; @@ -244,7 +252,9 @@ class ApiClient { }, canUseWebhookReply: options.canUseWebhookReply ?? (() => false), sensitiveLogs: options.sensitiveLogs ?? false, + fetch: options.fetch ?? fetch, }; + this.fetch = this.options.fetch; if (this.options.apiRoot.endsWith("/")) { throw new Error( `Remove the trailing '/' from the 'apiRoot' option (use '${ @@ -297,7 +307,7 @@ class ApiClient { const sig = controller.signal; const options = { ...opts.baseFetchConfig, signal: sig, ...config }; // Perform fetch call, and handle networking errors - const successPromise = fetch( + const successPromise = this.fetch( url instanceof URL ? url.href : url, options, ).catch(toHttpError(method, opts.sensitiveLogs));