Skip to content

Commit

Permalink
fix: implement custom fetch handling in ApiClient for Cloudflare comp…
Browse files Browse the repository at this point in the history
…atibility (#660)
  • Loading branch information
IKatsuba authored Nov 12, 2024
1 parent ec08af5 commit 9062048
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ class ApiClient<R extends RawApi> {
) {
const apiRoot = options.apiRoot ?? "https://api.telegram.org";
const environment = options.environment ?? "prod";

// In an ideal world, `fetch` is independent of the context being called,
// but in a Cloudflare worker, any context other than global throws an error.
// That is why we need to call custom fetch or fetch without context.
const { fetch: customFetch } = options;
const fetchFn = customFetch ?? fetch;

this.options = {
apiRoot,
environment,
Expand All @@ -252,7 +259,9 @@ class ApiClient<R extends RawApi> {
},
canUseWebhookReply: options.canUseWebhookReply ?? (() => false),
sensitiveLogs: options.sensitiveLogs ?? false,
fetch: options.fetch ?? fetch,
fetch:
((...args: Parameters<typeof fetch>) =>
fetchFn(...args)) as typeof fetch,
};
this.fetch = this.options.fetch;
if (this.options.apiRoot.endsWith("/")) {
Expand Down

0 comments on commit 9062048

Please sign in to comment.