@effect-ak/tg-bot-client 0.0.3 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ import { makeExecute } from "./execute-request.js";
2
+ import { makeDownloadFile } from "./download-file.js";
3
+ const defaultBaseUrl = "https://api.telegram.org";
4
+ export const makeTgBotClient = (inputConfig) => {
5
+ const config = {
6
+ ...inputConfig,
7
+ baseUrl: inputConfig.baseUrl ?? defaultBaseUrl
8
+ };
9
+ const execute = makeExecute(config);
10
+ const file = makeDownloadFile(config, execute);
11
+ return {
12
+ execute,
13
+ ...file
14
+ };
15
+ };
@@ -0,0 +1,17 @@
1
+ export const makeDownloadFile = (config, execute) => {
2
+ const getFile = async (input) => {
3
+ const response = await execute("get_file", input);
4
+ response.result?.file_path;
5
+ const file_path = response.result?.file_path;
6
+ if (!file_path || file_path.length == 0)
7
+ throw new Error("NoFilePath", { cause: response });
8
+ const file_name = file_path.replaceAll("/", "-");
9
+ const url = `${config.baseUrl}/file/bot${config.token}/${file_path}`;
10
+ const fileContent = await fetch(url).then(_ => _.arrayBuffer());
11
+ const file = new File([new Uint8Array(fileContent)], file_name);
12
+ return file;
13
+ };
14
+ return {
15
+ getFile
16
+ };
17
+ };
@@ -1,10 +1,8 @@
1
- import { makePayload, methodPath } from "./request.js";
1
+ import { makePayload } from "./request.js";
2
2
  import { isTgBotApiResponse } from "./response.js";
3
- const defaultBaseUrl = "https://api.telegram.org";
4
- export const makeTgBotClient = (options) => {
5
- const baseUrl = options.baseUrl ?? defaultBaseUrl;
3
+ export const makeExecute = (config) => {
6
4
  const execute = async (method, input) => {
7
- const httpResponse = await fetch(`${baseUrl}/bot${options.token}/${methodPath(method)}`, {
5
+ const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
8
6
  body: makePayload(input) ?? null,
9
7
  method: "POST"
10
8
  }).then(_ => _.json());
@@ -17,7 +15,15 @@ export const makeTgBotClient = (options) => {
17
15
  }
18
16
  return httpResponse;
19
17
  };
20
- return {
21
- execute
22
- };
18
+ return execute;
23
19
  };
20
+ const snakeToCamel = (methodName) => methodName
21
+ .split("_")
22
+ .reduce((result, word, step) => {
23
+ if (step == 0) {
24
+ return word;
25
+ }
26
+ else {
27
+ return result + word.at(0)?.toUpperCase() + word.slice(1);
28
+ }
29
+ }, "");
@@ -1,13 +1,3 @@
1
- export const methodPath = (methodName) => methodName
2
- .split("_")
3
- .reduce((result, word, step) => {
4
- if (step == 0) {
5
- return word;
6
- }
7
- else {
8
- return result + word.at(0)?.toUpperCase() + word.slice(1);
9
- }
10
- }, "");
11
1
  const hasFileContent = (input) => (typeof input == "object" && input != null) &&
12
2
  ("file_content" in input && input.file_content instanceof Uint8Array) &&
13
3
  ("file_name" in input && typeof input.file_name === "string" && input.file_name.length > 0);
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./client/const.js";
2
- export * from "./client/factory.js";
2
+ export * from "./client/_client.js";
3
3
  export * from "./specification/api.js";
4
4
  export * from "./specification/types.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect-ak/tg-bot-client",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Aleksandr Kondaurov",
@@ -34,11 +34,13 @@
34
34
  "node-html-parser": "^6.1.13",
35
35
  "ts-morph": "^24.0.0",
36
36
  "tsc-alias": "^1.8.10",
37
+ "type-fest": "^4.30.0",
37
38
  "typescript": "^5.7.2",
38
- "vitest": "^2.1.8",
39
- "vite-tsconfig-paths": "^5.1.4"
39
+ "vite-tsconfig-paths": "^5.1.4",
40
+ "vitest": "^2.1.8"
40
41
  },
41
42
  "scripts": {
43
+ "gen": "bun run codegen/main",
42
44
  "build": "pnpm build-esm && pnpm build-cjs",
43
45
  "build-esm": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
44
46
  "build-cjs": "babel dist/esm --out-dir dist/cjs"
package/readme.md CHANGED
@@ -67,6 +67,23 @@ await client.execute("send_document", {
67
67
  })
68
68
  ```
69
69
 
70
+
71
+ #### 4. Getting a file
72
+
73
+ In order to download file from Telegram server we need to send two http requests:
74
+ 1. execute `get_file` and get `remote_path`
75
+ 2. get file content via GET request with different url
76
+
77
+ `client.getFile` does exactly that. It returns [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File)
78
+
79
+ ```typescript
80
+ const file =
81
+ await client.getFile({
82
+ file_id: fileId
83
+ });
84
+ // file is File
85
+ ```
86
+
70
87
  ---
71
88
 
72
89
  ### Summary
@@ -1,9 +0,0 @@
1
- import type { Api } from "../specification/api.js";
2
- import { TgBotApiResponse } from "./response.js";
3
- export type TgBotClient = ReturnType<typeof makeTgBotClient>;
4
- export declare const makeTgBotClient: (options: {
5
- token: string;
6
- baseUrl?: string;
7
- }) => {
8
- readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<TgBotApiResponse<ReturnType<Api[M]>>>;
9
- };