From f2f7b10e820ed8e6934f1c2f4b4e1afb97b574d3 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Sun, 24 Dec 2023 12:56:06 +0100 Subject: [PATCH 1/3] feat: simplify using Test DC --- src/core/client.ts | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/core/client.ts b/src/core/client.ts index bc06341c..60d6fb78 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -115,6 +115,19 @@ export interface ApiClientOptions { * https://api.telegram.org */ apiRoot?: string; + /** + * Specifies whether to use the [testing + * infrastructure](https://core.telegram.org/bots/webapps#using-bots-in-the-test-environment). + * Can be either `"prod"` (default) or `"test"`. + * + * The testing infrastructure is separate from the regular production + * infrastructure. No chats, accounts, or other data is shared between them. + * If you set this option to `"test"`, you will need to make your Telegram + * client connect to the testing data centers of Telegram, register your + * phone number again, open a new chat with @BotFather, and create a + * separate bot. + */ + dc?: "prod" | "test"; /** * URL builder function for API calls. Can be used to modify which API * server should be called. @@ -122,9 +135,15 @@ export interface ApiClientOptions { * @param root The URL that was passed in `apiRoot`, or its default value * @param token The bot's token that was passed when creating the bot * @param method The API method to be called, e.g. `getMe` + * @param dc The value that was passed in `dc`, or its default value * @returns The URL that will be fetched during the API call */ - buildUrl?: (root: string, token: string, method: string) => string | URL; + buildUrl?: ( + root: string, + token: string, + method: string, + dc: "prod" | "test", + ) => string | URL; /** * Maximum number of seconds that a request to the Bot API server may take. * If a request has not completed before this time has elapsed, grammY @@ -213,10 +232,11 @@ class ApiClient { private readonly webhookReplyEnvelope: WebhookReplyEnvelope = {}, ) { const apiRoot = options.apiRoot ?? "https://api.telegram.org"; + const dc = options.dc ?? "prod"; this.options = { apiRoot, - buildUrl: options.buildUrl ?? - ((root, token, method) => `${root}/bot${token}/${method}`), + dc, + buildUrl: options.buildUrl ?? defaultBuildUrl, timeoutSeconds: options.timeoutSeconds ?? 500, baseFetchConfig: { ...baseFetchConfig(apiRoot), @@ -346,6 +366,16 @@ export function createRawApi( return api; } +const defaultBuildUrl: NonNullable = ( + root, + token, + method, + dc, +) => { + const prefix = dc === "test" ? "test/" : ""; + return `${root}/bot${token}/${prefix}${method}`; +}; + const proxyMethods = { set() { return false; From 067f25fc51993a777a2fcac07336c368bf7e5ed8 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Sun, 24 Dec 2023 13:07:38 +0100 Subject: [PATCH 2/3] fix: actually pass dc option to buildUrl --- src/core/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/client.ts b/src/core/client.ts index 60d6fb78..1cd668b9 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -285,7 +285,7 @@ class ApiClient { const timeout = createTimeout(controller, opts.timeoutSeconds, method); const streamErr = createStreamError(controller); // Build request URL and config - const url = opts.buildUrl(opts.apiRoot, this.token, method); + const url = opts.buildUrl(opts.apiRoot, this.token, method, opts.dc); const config = formDataRequired ? createFormDataPayload(payload, (err) => streamErr.catch(err)) : createJsonPayload(payload); From ac9f31c15aed5e0db449039e2abda4dbfbacb72a Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Sun, 24 Dec 2023 13:15:44 +0100 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20rename=20dc=E2=86=92env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/client.ts | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/core/client.ts b/src/core/client.ts index 1cd668b9..cb680809 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -116,8 +116,8 @@ export interface ApiClientOptions { */ apiRoot?: string; /** - * Specifies whether to use the [testing - * infrastructure](https://core.telegram.org/bots/webapps#using-bots-in-the-test-environment). + * Specifies whether to use the [test + * environment](https://core.telegram.org/bots/webapps#using-bots-in-the-test-environment). * Can be either `"prod"` (default) or `"test"`. * * The testing infrastructure is separate from the regular production @@ -127,7 +127,7 @@ export interface ApiClientOptions { * phone number again, open a new chat with @BotFather, and create a * separate bot. */ - dc?: "prod" | "test"; + environment?: "prod" | "test"; /** * URL builder function for API calls. Can be used to modify which API * server should be called. @@ -135,14 +135,14 @@ export interface ApiClientOptions { * @param root The URL that was passed in `apiRoot`, or its default value * @param token The bot's token that was passed when creating the bot * @param method The API method to be called, e.g. `getMe` - * @param dc The value that was passed in `dc`, or its default value + * @param env The value that was passed in `environment`, or its default value * @returns The URL that will be fetched during the API call */ buildUrl?: ( root: string, token: string, method: string, - dc: "prod" | "test", + env: "prod" | "test", ) => string | URL; /** * Maximum number of seconds that a request to the Bot API server may take. @@ -232,10 +232,10 @@ class ApiClient { private readonly webhookReplyEnvelope: WebhookReplyEnvelope = {}, ) { const apiRoot = options.apiRoot ?? "https://api.telegram.org"; - const dc = options.dc ?? "prod"; + const environment = options.environment ?? "prod"; this.options = { apiRoot, - dc, + environment, buildUrl: options.buildUrl ?? defaultBuildUrl, timeoutSeconds: options.timeoutSeconds ?? 500, baseFetchConfig: { @@ -285,7 +285,12 @@ class ApiClient { const timeout = createTimeout(controller, opts.timeoutSeconds, method); const streamErr = createStreamError(controller); // Build request URL and config - const url = opts.buildUrl(opts.apiRoot, this.token, method, opts.dc); + const url = opts.buildUrl( + opts.apiRoot, + this.token, + method, + opts.environment, + ); const config = formDataRequired ? createFormDataPayload(payload, (err) => streamErr.catch(err)) : createJsonPayload(payload); @@ -370,9 +375,9 @@ const defaultBuildUrl: NonNullable = ( root, token, method, - dc, + env, ) => { - const prefix = dc === "test" ? "test/" : ""; + const prefix = env === "test" ? "test/" : ""; return `${root}/bot${token}/${prefix}${method}`; };