@effect-ak/tg-bot-client 0.0.4 → 0.0.7

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.
@@ -15,7 +15,7 @@ const makeTgBotClient = inputConfig => {
15
15
  const execute = (0, _executeRequest.makeExecute)(config);
16
16
  const file = (0, _downloadFile.makeDownloadFile)(config, execute);
17
17
  return {
18
- execute,
18
+ ...execute,
19
19
  ...file
20
20
  };
21
21
  };
@@ -4,19 +4,38 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.makeDownloadFile = void 0;
7
+ var _errors = require("./errors.js");
7
8
  const makeDownloadFile = (config, execute) => {
8
9
  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
- });
10
+ const response = await execute.execute("get_file", input);
11
+ if (response.error) return {
12
+ error: response.error
13
+ };
14
+ const file_path = response.success?.file_path;
15
+ if (!file_path || file_path.length == 0) {
16
+ return {
17
+ error: new _errors.TgBotClientError({
18
+ type: "UnableToGetFile",
19
+ cause: "File path not defined"
20
+ })
21
+ };
22
+ }
15
23
  const file_name = file_path.replaceAll("/", "-");
16
24
  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;
25
+ try {
26
+ const fileContent = await fetch(url).then(_ => _.arrayBuffer());
27
+ const file = new File([new Uint8Array(fileContent)], file_name);
28
+ return {
29
+ success: file
30
+ };
31
+ } catch (cause) {
32
+ return {
33
+ error: new _errors.TgBotClientError({
34
+ type: "UnableToGetFile",
35
+ cause
36
+ })
37
+ };
38
+ }
20
39
  };
21
40
  return {
22
41
  getFile
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TgBotClientError = void 0;
7
+ class TgBotClientError extends Error {
8
+ reason;
9
+ _tag = "TgBotClientError";
10
+ constructor(reason) {
11
+ super();
12
+ this.reason = reason;
13
+ }
14
+ static missingSuccess = new TgBotClientError({
15
+ type: "ClientInternalError",
16
+ cause: "Expected 'success' to be defined"
17
+ });
18
+ }
19
+ exports.TgBotClientError = TgBotClientError;
@@ -6,21 +6,58 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.makeExecute = void 0;
7
7
  var _request = require("./request.js");
8
8
  var _response = require("./response.js");
9
+ var _errors = require("./errors.js");
9
10
  const makeExecute = config => {
10
11
  const execute = async (method, input) => {
11
- const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
12
- body: (0, _request.makePayload)(input) ?? null,
13
- method: "POST"
14
- }).then(_ => _.json());
15
- if (!(0, _response.isTgBotApiResponse)(httpResponse)) throw new Error("Not valid response", {
16
- cause: httpResponse
17
- });
18
- if (httpResponse.ok == false) {
19
- console.warn(httpResponse);
12
+ try {
13
+ const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
14
+ body: (0, _request.makePayload)(input) ?? null,
15
+ method: "POST"
16
+ });
17
+ const response = await httpResponse.json();
18
+ if (!(0, _response.isTgBotApiResponse)(response)) {
19
+ return {
20
+ error: new _errors.TgBotClientError({
21
+ type: "UnexpectedResponse",
22
+ response
23
+ })
24
+ };
25
+ }
26
+ if (!httpResponse.ok) {
27
+ return {
28
+ error: new _errors.TgBotClientError({
29
+ type: "NotOkResponse",
30
+ ...(response.error_code ? {
31
+ errorCode: response.error_code
32
+ } : undefined),
33
+ ...(response.description ? {
34
+ details: response.description
35
+ } : undefined)
36
+ })
37
+ };
38
+ }
39
+ return {
40
+ success: response.result
41
+ };
42
+ } catch (cause) {
43
+ return {
44
+ error: new _errors.TgBotClientError({
45
+ type: "ClientInternalError",
46
+ cause
47
+ })
48
+ };
20
49
  }
21
- return httpResponse;
22
50
  };
23
- return execute;
51
+ const unsafeExecute = async (method, input) => {
52
+ const result = await execute(method, input);
53
+ if (result.error) throw result.error;
54
+ if (!("success" in result)) throw _errors.TgBotClientError.missingSuccess;
55
+ return result.success;
56
+ };
57
+ return {
58
+ execute,
59
+ unsafeExecute
60
+ };
24
61
  };
25
62
  exports.makeExecute = makeExecute;
26
63
  const snakeToCamel = methodName => methodName.split("_").reduce((result, word, step) => {
@@ -5,6 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.isTgBotApiResponse = void 0;
7
7
  const isTgBotApiResponse = input => {
8
- return "ok" in input && typeof input.ok == "boolean" && (typeof input.error_code === 'undefined' || typeof input.error_code === 'number') && (typeof input.description === 'undefined' || typeof input.description === 'string') && (typeof input.result === 'undefined' || typeof input.result === 'object');
8
+ if (typeof input !== "object" || input == null) {
9
+ return false;
10
+ }
11
+ if (!("ok" in input && typeof input.ok == "boolean")) {
12
+ return false;
13
+ }
14
+ if ("error_code" in input && typeof input.error_code != "number") {
15
+ return false;
16
+ }
17
+ if ("description" in input && typeof input.description != "string") {
18
+ return false;
19
+ }
20
+ return true;
9
21
  };
10
22
  exports.isTgBotApiResponse = isTgBotApiResponse;
@@ -7,6 +7,13 @@ export type TgBotClient = ReturnType<typeof makeTgBotClient>;
7
7
  export declare const makeTgBotClient: (inputConfig: SetOptional<BotConfig, "baseUrl">) => {
8
8
  readonly getFile: (input: {
9
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]>>>;
10
+ }) => Promise<{
11
+ success?: File;
12
+ error?: import("./errors.js").TgBotClientError;
13
+ }>;
14
+ readonly execute: <M extends keyof import("../index.js").Api>(method: M, input: Parameters<import("../index.js").Api[M]>[0]) => Promise<{
15
+ success?: ReturnType<import("../index.js").Api[M]>;
16
+ error?: import("./errors.js").TgBotClientError;
17
+ }>;
18
+ readonly unsafeExecute: <M extends keyof import("../index.js").Api>(method: M, input: Parameters<import("../index.js").Api[M]>[0]) => Promise<ReturnType<import("../index.js").Api[M]>>;
12
19
  };
@@ -1,7 +1,13 @@
1
1
  import type { BotConfig } from "./_client.js";
2
+ import { TgBotClientError } from "./errors.js";
2
3
  import type { ExecuteRequest } from "./execute-request.js";
4
+ type GetFileResult = {
5
+ success?: File;
6
+ error?: TgBotClientError;
7
+ };
3
8
  export declare const makeDownloadFile: (config: BotConfig, execute: ExecuteRequest) => {
4
9
  readonly getFile: (input: {
5
10
  file_id: string;
6
- }) => Promise<import("buffer").File>;
11
+ }) => Promise<GetFileResult>;
7
12
  };
13
+ export {};
@@ -0,0 +1,21 @@
1
+ type ErrorReason = {
2
+ readonly type: "NotOkResponse";
3
+ readonly errorCode?: number;
4
+ readonly details?: string;
5
+ } | {
6
+ readonly type: "UnexpectedResponse";
7
+ readonly response: unknown;
8
+ } | {
9
+ readonly type: "ClientInternalError";
10
+ readonly cause: unknown;
11
+ } | {
12
+ readonly type: "UnableToGetFile";
13
+ readonly cause: unknown;
14
+ };
15
+ export declare class TgBotClientError extends Error {
16
+ readonly reason: ErrorReason;
17
+ readonly _tag = "TgBotClientError";
18
+ constructor(reason: ErrorReason);
19
+ static readonly missingSuccess: TgBotClientError;
20
+ }
21
+ export {};
@@ -1,5 +1,13 @@
1
1
  import type { BotConfig } from "./_client.js";
2
2
  import type { Api } from "../specification/api.js";
3
- import { TgBotApiResponse } from "./response.js";
3
+ import { TgBotClientError } from "./errors.js";
4
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]>>>;
5
+ type SafeExecuteResult<M extends keyof Api> = {
6
+ success?: ReturnType<Api[M]>;
7
+ error?: TgBotClientError;
8
+ };
9
+ export declare const makeExecute: (config: BotConfig) => {
10
+ readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<SafeExecuteResult<M>>;
11
+ readonly unsafeExecute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<ReturnType<Api[M]>>;
12
+ };
13
+ export {};
@@ -1 +1 @@
1
- export declare const makePayload: (body: Record<string, unknown>) => FormData | undefined;
1
+ export declare const makePayload: (body: object) => FormData | undefined;
@@ -4,4 +4,4 @@ export type TgBotApiResponse<O> = {
4
4
  description?: string;
5
5
  result?: O;
6
6
  };
7
- export declare const isTgBotApiResponse: <O>(input: Record<string, unknown>) => input is TgBotApiResponse<O>;
7
+ export declare const isTgBotApiResponse: <O>(input: unknown) => input is TgBotApiResponse<O>;