Skip to content

Commit

Permalink
build: drop node-fetch and custom fetch function
Browse files Browse the repository at this point in the history
BREAKING: it is not longer possible to specify a custom fetch function, the global fetch function will always be used

Closes #682
  • Loading branch information
KnorpelSenf committed Dec 16, 2024
1 parent 56ba431 commit 58663d5
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 20 deletions.
19 changes: 1 addition & 18 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ export interface ApiClientOptions {
"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
Expand All @@ -230,8 +225,6 @@ export interface ApiClientOptions {
class ApiClient<R extends RawApi> {
private readonly options: Required<ApiClientOptions>;

private readonly fetch: typeof fetch;

private hasUsedWebhookReply = false;

readonly installedTransformers: Transformer<R>[] = [];
Expand All @@ -244,12 +237,6 @@ 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 @@ -261,11 +248,7 @@ class ApiClient<R extends RawApi> {
},
canUseWebhookReply: options.canUseWebhookReply ?? (() => false),
sensitiveLogs: options.sensitiveLogs ?? false,
fetch:
((...args: Parameters<typeof fetch>) =>
fetchFn(...args)) as typeof fetch,
};
this.fetch = this.options.fetch;
if (this.options.apiRoot.endsWith("/")) {
throw new Error(
`Remove the trailing '/' from the 'apiRoot' option (use '${
Expand Down Expand Up @@ -318,7 +301,7 @@ class ApiClient<R extends RawApi> {
const sig = controller.signal;
const options = { ...opts.baseFetchConfig, signal: sig, ...config };
// Perform fetch call, and handle networking errors
const successPromise = this.fetch(url, options)
const successPromise = fetch(url, options)
.catch(toHttpError(method, opts.sensitiveLogs));
// Those are the three possible outcomes of the fetch call:
const operations = [successPromise, streamErr.promise, timeout.promise];
Expand Down
2 changes: 0 additions & 2 deletions src/shim.node.ts

This file was deleted.

1 comment on commit 58663d5

@KnorpelSenf
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also fixed #598

Please sign in to comment.