@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.
- package/dist/cjs/client/_client.js +1 -1
- package/dist/cjs/client/download-file.js +28 -9
- package/dist/cjs/client/errors.js +19 -0
- package/dist/cjs/client/execute-request.js +48 -11
- package/dist/cjs/client/response.js +13 -1
- package/dist/dts/client/_client.d.ts +9 -2
- package/dist/dts/client/download-file.d.ts +7 -1
- package/dist/dts/client/errors.d.ts +21 -0
- package/dist/dts/client/execute-request.d.ts +10 -2
- package/dist/dts/client/request.d.ts +1 -1
- package/dist/dts/client/response.d.ts +1 -1
- package/dist/dts/specification/api.d.ts +131 -132
- package/dist/esm/client/_client.js +1 -1
- package/dist/esm/client/download-file.js +27 -8
- package/dist/esm/client/errors.js +12 -0
- package/dist/esm/client/execute-request.js +44 -11
- package/dist/esm/client/response.js +13 -4
- package/package.json +2 -1
- package/readme.md +11 -2
- package/dist/cjs/client/factory.js +0 -29
- package/dist/dts/client/factory.d.ts +0 -9
- package/dist/esm/client/factory.js +0 -23
|
@@ -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.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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<
|
|
11
|
-
|
|
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<
|
|
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 {
|
|
3
|
+
import { TgBotClientError } from "./errors.js";
|
|
4
4
|
export type ExecuteRequest = ReturnType<typeof makeExecute>;
|
|
5
|
-
|
|
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:
|
|
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:
|
|
7
|
+
export declare const isTgBotApiResponse: <O>(input: unknown) => input is TgBotApiResponse<O>;
|