Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify using Test DC #509

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,35 @@
* https://api.telegram.org
*/
apiRoot?: string;
/**
* 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
* 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.
*/
environment?: "prod" | "test";
/**
* URL builder function for API calls. Can be used to modify which API
* server should be called.
*
* @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 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) => string | URL;
buildUrl?: (
root: string,
token: string,
method: string,
env: "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
Expand Down Expand Up @@ -213,10 +232,11 @@
private readonly webhookReplyEnvelope: WebhookReplyEnvelope = {},
) {
const apiRoot = options.apiRoot ?? "https://api.telegram.org";
const environment = options.environment ?? "prod";
this.options = {
apiRoot,
buildUrl: options.buildUrl ??
((root, token, method) => `${root}/bot${token}/${method}`),
environment,
buildUrl: options.buildUrl ?? defaultBuildUrl,
timeoutSeconds: options.timeoutSeconds ?? 500,
baseFetchConfig: {
...baseFetchConfig(apiRoot),
Expand Down Expand Up @@ -265,7 +285,12 @@
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.environment,
);
const config = formDataRequired
? createFormDataPayload(payload, (err) => streamErr.catch(err))
: createJsonPayload(payload);
Expand Down Expand Up @@ -346,6 +371,16 @@
return api;
}

const defaultBuildUrl: NonNullable<ApiClientOptions["buildUrl"]> = (
root,
token,
method,
env,

Check warning on line 378 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L374-L378

Added lines #L374 - L378 were not covered by tests
) => {
const prefix = env === "test" ? "test/" : "";
return `${root}/bot${token}/${prefix}${method}`;
};

Check warning on line 382 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L380-L382

Added lines #L380 - L382 were not covered by tests

const proxyMethods = {
set() {
return false;
Expand Down
Loading