Skip to content

Commit

Permalink
refactor: rename dc→env
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf committed Dec 24, 2023
1 parent 067f25f commit ac9f31c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -127,22 +127,22 @@ 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.
*
* @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.
Expand Down Expand Up @@ -232,10 +232,10 @@ class ApiClient<R extends RawApi> {
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: {
Expand Down Expand Up @@ -285,7 +285,12 @@ class ApiClient<R extends RawApi> {
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);
Expand Down Expand Up @@ -370,9 +375,9 @@ const defaultBuildUrl: NonNullable<ApiClientOptions["buildUrl"]> = (
root,
token,
method,
dc,
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 = dc === "test" ? "test/" : "";
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

Expand Down

0 comments on commit ac9f31c

Please sign in to comment.