@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,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeTgBotClient = void 0;
7
+ var _executeRequest = require("./execute-request.js");
8
+ var _downloadFile = require("./download-file.js");
9
+ const defaultBaseUrl = "https://api.telegram.org";
10
+ const makeTgBotClient = inputConfig => {
11
+ const config = {
12
+ ...inputConfig,
13
+ baseUrl: inputConfig.baseUrl ?? defaultBaseUrl
14
+ };
15
+ const execute = (0, _executeRequest.makeExecute)(config);
16
+ const file = (0, _downloadFile.makeDownloadFile)(config, execute);
17
+ return {
18
+ execute,
19
+ ...file
20
+ };
21
+ };
22
+ exports.makeTgBotClient = makeTgBotClient;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeDownloadFile = void 0;
7
+ const makeDownloadFile = (config, execute) => {
8
+ const getFile = async input => {
9
+ const response = await execute("get_file", input);
10
+ response.result?.file_path;
11
+ const file_path = response.result?.file_path;
12
+ if (!file_path || file_path.length == 0) throw new Error("NoFilePath", {
13
+ cause: response
14
+ });
15
+ const file_name = file_path.replaceAll("/", "-");
16
+ const url = `${config.baseUrl}/file/bot${config.token}/${file_path}`;
17
+ const fileContent = await fetch(url).then(_ => _.arrayBuffer());
18
+ const file = new File([new Uint8Array(fileContent)], file_name);
19
+ return file;
20
+ };
21
+ return {
22
+ getFile
23
+ };
24
+ };
25
+ exports.makeDownloadFile = makeDownloadFile;
@@ -3,14 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.makeTgBotClient = void 0;
6
+ exports.makeExecute = void 0;
7
7
  var _request = require("./request.js");
8
8
  var _response = require("./response.js");
9
- const defaultBaseUrl = "https://api.telegram.org";
10
- const makeTgBotClient = options => {
11
- const baseUrl = options.baseUrl ?? defaultBaseUrl;
9
+ const makeExecute = config => {
12
10
  const execute = async (method, input) => {
13
- const httpResponse = await fetch(`${baseUrl}/bot${options.token}/${(0, _request.methodPath)(method)}`, {
11
+ const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
14
12
  body: (0, _request.makePayload)(input) ?? null,
15
13
  method: "POST"
16
14
  }).then(_ => _.json());
@@ -22,8 +20,13 @@ const makeTgBotClient = options => {
22
20
  }
23
21
  return httpResponse;
24
22
  };
25
- return {
26
- execute
27
- };
23
+ return execute;
28
24
  };
29
- exports.makeTgBotClient = makeTgBotClient;
25
+ exports.makeExecute = makeExecute;
26
+ const snakeToCamel = methodName => methodName.split("_").reduce((result, word, step) => {
27
+ if (step == 0) {
28
+ return word;
29
+ } else {
30
+ return result + word.at(0)?.toUpperCase() + word.slice(1);
31
+ }
32
+ }, "");
@@ -3,15 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.methodPath = exports.makePayload = void 0;
7
- const methodPath = methodName => methodName.split("_").reduce((result, word, step) => {
8
- if (step == 0) {
9
- return word;
10
- } else {
11
- return result + word.at(0)?.toUpperCase() + word.slice(1);
12
- }
13
- }, "");
14
- exports.methodPath = methodPath;
6
+ exports.makePayload = void 0;
15
7
  const hasFileContent = input => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name === "string" && input.file_name.length > 0;
16
8
  const makePayload = body => {
17
9
  const entries = Object.entries(body);
package/dist/cjs/index.js CHANGED
@@ -14,14 +14,14 @@ Object.keys(_const).forEach(function (key) {
14
14
  }
15
15
  });
16
16
  });
17
- var _factory = require("./client/factory.js");
18
- Object.keys(_factory).forEach(function (key) {
17
+ var _client = require("./client/_client.js");
18
+ Object.keys(_client).forEach(function (key) {
19
19
  if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _factory[key]) return;
20
+ if (key in exports && exports[key] === _client[key]) return;
21
21
  Object.defineProperty(exports, key, {
22
22
  enumerable: true,
23
23
  get: function () {
24
- return _factory[key];
24
+ return _client[key];
25
25
  }
26
26
  });
27
27
  });
@@ -0,0 +1,12 @@
1
+ import type { SetOptional } from "type-fest";
2
+ export type BotConfig = {
3
+ token: string;
4
+ baseUrl: string;
5
+ };
6
+ export type TgBotClient = ReturnType<typeof makeTgBotClient>;
7
+ export declare const makeTgBotClient: (inputConfig: SetOptional<BotConfig, "baseUrl">) => {
8
+ readonly getFile: (input: {
9
+ file_id: string;
10
+ }) => Promise<import("buffer").File>;
11
+ readonly execute: <M extends keyof import("../index.js").Api>(method: M, input: Parameters<import("../index.js").Api[M]>[0]) => Promise<import("./response.js").TgBotApiResponse<ReturnType<import("../index.js").Api[M]>>>;
12
+ };
@@ -0,0 +1,7 @@
1
+ import type { BotConfig } from "./_client.js";
2
+ import type { ExecuteRequest } from "./execute-request.js";
3
+ export declare const makeDownloadFile: (config: BotConfig, execute: ExecuteRequest) => {
4
+ readonly getFile: (input: {
5
+ file_id: string;
6
+ }) => Promise<import("buffer").File>;
7
+ };
@@ -0,0 +1,5 @@
1
+ import type { BotConfig } from "./_client.js";
2
+ import type { Api } from "../specification/api.js";
3
+ import { TgBotApiResponse } from "./response.js";
4
+ export type ExecuteRequest = ReturnType<typeof makeExecute>;
5
+ export declare const makeExecute: (config: BotConfig) => <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<TgBotApiResponse<ReturnType<Api[M]>>>;
@@ -1,2 +1 @@
1
- export declare const methodPath: (methodName: string) => string;
2
1
  export declare const makePayload: (body: Record<string, unknown>) => FormData | undefined;
@@ -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";