Skip to content

Commit

Permalink
perf: 封装telegram 请求 uri 生成
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 29, 2024
1 parent 7469836 commit c810d52
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 38 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/telegram/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import { ENV } from '../../config/env';
class APIClientBase {
readonly token: string;
readonly baseURL: string = ENV.TELEGRAM_API_DOMAIN;

constructor(token: string, baseURL?: string) {
this.token = token;
if (baseURL) {
this.baseURL = baseURL;
}
while (this.baseURL.endsWith('/')) {
this.baseURL = this.baseURL.slice(0, -1);
}
}

private uri(method: Telegram.BotMethod): string {
return `${this.baseURL}/bot${this.token}/${method}`;
}

private jsonRequest<T>(method: Telegram.BotMethod, params: T): Promise<Response> {
return fetch(`${this.baseURL}/bot${this.token}/${method}`, {
return fetch(this.uri(method), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -35,7 +43,7 @@ class APIClientBase {
formData.append(key, JSON.stringify(value));
}
}
return fetch(`${this.baseURL}/bot${this.token}/${method}`, {
return fetch(this.uri(method), {
method: 'POST',
body: formData,
});
Expand Down

0 comments on commit c810d52

Please sign in to comment.