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

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.
Files changed (82) hide show
  1. package/dist/cjs/bot/factory/_service.js +20 -0
  2. package/dist/cjs/bot/factory/client-config.js +29 -0
  3. package/dist/cjs/bot/factory/make-bot.js +20 -0
  4. package/dist/cjs/bot/message-handler/_service.js +12 -0
  5. package/dist/cjs/bot/message-handler/types.js +5 -0
  6. package/dist/cjs/bot/message-handler/utils.js +19 -0
  7. package/dist/cjs/bot/run.js +10 -0
  8. package/dist/cjs/bot/update-poller/_service.js +39 -0
  9. package/dist/cjs/bot/update-poller/errors.js +5 -0
  10. package/dist/cjs/bot/update-poller/fetch-updates.js +70 -0
  11. package/dist/cjs/bot/update-poller/poll-and-handle.js +50 -0
  12. package/dist/cjs/bot/update-poller/settings.js +23 -0
  13. package/dist/cjs/client/_client.js +17 -14
  14. package/dist/cjs/client/config.js +17 -0
  15. package/dist/cjs/client/errors.js +8 -9
  16. package/dist/cjs/client/execute-request/_service.js +20 -0
  17. package/dist/cjs/client/execute-request/execute.js +59 -0
  18. package/dist/cjs/client/{request.js → execute-request/payload.js} +2 -2
  19. package/dist/cjs/client/file/_service.js +22 -0
  20. package/dist/cjs/client/file/get-file.js +38 -0
  21. package/dist/cjs/client/guards.js +12 -0
  22. package/dist/cjs/{client/const.js → const.js} +2 -1
  23. package/dist/cjs/index.js +19 -8
  24. package/dist/dts/bot/factory/_service.d.ts +37 -0
  25. package/dist/dts/bot/factory/client-config.d.ts +3 -0
  26. package/dist/dts/bot/factory/make-bot.d.ts +7 -0
  27. package/dist/dts/bot/message-handler/_service.d.ts +6 -0
  28. package/dist/dts/bot/message-handler/types.d.ts +16 -0
  29. package/dist/dts/bot/message-handler/utils.d.ts +6 -0
  30. package/dist/dts/bot/run.d.ts +7 -0
  31. package/dist/dts/bot/update-poller/_service.d.ts +21 -0
  32. package/dist/dts/bot/update-poller/errors.d.ts +1 -0
  33. package/dist/dts/bot/update-poller/fetch-updates.d.ts +17 -0
  34. package/dist/dts/bot/update-poller/poll-and-handle.d.ts +16 -0
  35. package/dist/dts/bot/update-poller/settings.d.ts +6 -0
  36. package/dist/dts/client/_client.d.ts +6 -17
  37. package/dist/dts/client/config.d.ts +8 -0
  38. package/dist/dts/client/errors.d.ts +6 -4
  39. package/dist/dts/client/execute-request/_service.d.ts +15 -0
  40. package/dist/dts/client/execute-request/execute.d.ts +5 -0
  41. package/dist/dts/client/file/_service.d.ts +16 -0
  42. package/dist/dts/client/file/get-file.d.ts +5 -0
  43. package/dist/dts/client/guards.d.ts +17 -0
  44. package/dist/dts/{client/const.d.ts → const.d.ts} +1 -0
  45. package/dist/dts/index.d.ts +2 -1
  46. package/dist/dts/specification/api.d.ts +786 -2776
  47. package/dist/dts/specification/types.d.ts +1387 -3547
  48. package/dist/esm/bot/factory/_service.js +12 -0
  49. package/dist/esm/bot/factory/client-config.js +20 -0
  50. package/dist/esm/bot/factory/make-bot.js +9 -0
  51. package/dist/esm/bot/message-handler/_service.js +4 -0
  52. package/dist/esm/bot/message-handler/types.js +1 -0
  53. package/dist/esm/bot/message-handler/utils.js +12 -0
  54. package/dist/esm/bot/run.js +6 -0
  55. package/dist/esm/bot/update-poller/_service.js +31 -0
  56. package/dist/esm/bot/update-poller/errors.js +1 -0
  57. package/dist/esm/bot/update-poller/fetch-updates.js +49 -0
  58. package/dist/esm/bot/update-poller/poll-and-handle.js +37 -0
  59. package/dist/esm/bot/update-poller/settings.js +15 -0
  60. package/dist/esm/client/_client.js +15 -14
  61. package/dist/esm/client/config.js +8 -0
  62. package/dist/esm/client/errors.js +6 -9
  63. package/dist/esm/client/execute-request/_service.js +12 -0
  64. package/dist/esm/client/execute-request/execute.js +37 -0
  65. package/dist/esm/client/{request.js → execute-request/payload.js} +2 -4
  66. package/dist/esm/client/file/_service.js +14 -0
  67. package/dist/esm/client/file/get-file.js +24 -0
  68. package/dist/esm/client/guards.js +7 -0
  69. package/dist/esm/{client/const.js → const.js} +1 -0
  70. package/dist/esm/index.js +2 -1
  71. package/package.json +12 -4
  72. package/readme.md +83 -24
  73. package/dist/cjs/client/download-file.js +0 -44
  74. package/dist/cjs/client/execute-request.js +0 -69
  75. package/dist/cjs/client/response.js +0 -22
  76. package/dist/dts/client/download-file.d.ts +0 -13
  77. package/dist/dts/client/execute-request.d.ts +0 -13
  78. package/dist/dts/client/response.d.ts +0 -7
  79. package/dist/esm/client/download-file.js +0 -36
  80. package/dist/esm/client/execute-request.js +0 -62
  81. package/dist/esm/client/response.js +0 -15
  82. /package/dist/dts/client/{request.d.ts → execute-request/payload.d.ts} +0 -0
@@ -0,0 +1,37 @@
1
+ import * as Micro from "effect/Micro";
2
+ import * as Context from "effect/Context";
3
+ import type { TgBotClientSettingsInput } from "../../client/guards.js";
4
+ import type { BotMessageHandlerSettings } from "../../bot/message-handler/types.js";
5
+ import { TgBotClientConfig } from "../../client/config.js";
6
+ declare const BotFactoryService_base: Context.TagClass<BotFactoryService, "BotFactoryService", {
7
+ makeBot: (messageHandler: BotMessageHandlerSettings) => Micro.Micro<Micro.MicroFiber<{
8
+ updates: import("../../index.js").Update[];
9
+ lastSuccessId: number | undefined;
10
+ hasError: boolean;
11
+ }, import("../../client/errors.js").TgBotClientError>, string, TgBotClientConfig>;
12
+ runBot: (input: RunBotInput) => Micro.Micro<Micro.MicroFiber<{
13
+ updates: import("../../index.js").Update[];
14
+ lastSuccessId: number | undefined;
15
+ hasError: boolean;
16
+ }, import("../../client/errors.js").TgBotClientError>, string, never>;
17
+ }>;
18
+ export declare class BotFactoryService extends BotFactoryService_base {
19
+ }
20
+ export type RunBotInput = ({
21
+ type: "fromJsonFile";
22
+ } | {
23
+ type: "config";
24
+ } & TgBotClientSettingsInput) & BotMessageHandlerSettings;
25
+ export declare const BotFactoryServiceDefault: {
26
+ makeBot: (messageHandler: BotMessageHandlerSettings) => Micro.Micro<Micro.MicroFiber<{
27
+ updates: import("../../index.js").Update[];
28
+ lastSuccessId: number | undefined;
29
+ hasError: boolean;
30
+ }, import("../../client/errors.js").TgBotClientError>, string, TgBotClientConfig>;
31
+ runBot: (input: RunBotInput) => Micro.Micro<Micro.MicroFiber<{
32
+ updates: import("../../index.js").Update[];
33
+ lastSuccessId: number | undefined;
34
+ hasError: boolean;
35
+ }, import("../../client/errors.js").TgBotClientError>, string, never>;
36
+ };
37
+ export {};
@@ -0,0 +1,3 @@
1
+ import * as Micro from "effect/Micro";
2
+ import type { RunBotInput } from "./_service.js";
3
+ export declare const makeClientConfigFrom: (input: RunBotInput) => Micro.Micro<Required<import("../../client/guards.js").TgBotClientSettingsInput>, string, never>;
@@ -0,0 +1,7 @@
1
+ import * as Micro from "effect/Micro";
2
+ import type { BotMessageHandlerSettings } from "../../bot/message-handler/types.js";
3
+ export declare const makeBot: (messageHandler: BotMessageHandlerSettings) => Micro.Micro<Micro.MicroFiber<{
4
+ updates: import("../../index.js").Update[];
5
+ lastSuccessId: number | undefined;
6
+ hasError: boolean;
7
+ }, import("../../client/errors.js").TgBotClientError>, string, import("../../client/config.js").TgBotClientConfig>;
@@ -0,0 +1,6 @@
1
+ import * as Context from "effect/Context";
2
+ import { BotMessageHandlerSettings } from "./types.js";
3
+ declare const BotMessageHandler_base: Context.TagClass<BotMessageHandler, "BotMessageHandler", BotMessageHandlerSettings>;
4
+ export declare class BotMessageHandler extends BotMessageHandler_base {
5
+ }
6
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { Api } from "../../specification/api.js";
2
+ import type { Update } from "../../specification/types.js";
3
+ export type AvailableUpdateTypes = Exclude<keyof Update, 'update_id'>;
4
+ export type BotResponse = {
5
+ [K in keyof Api]: K extends `send_${infer R}` ? {
6
+ type: Lowercase<R>;
7
+ } & Omit<Parameters<Api[K]>[0], 'chat_id'> : never;
8
+ }[keyof Api];
9
+ export type BotMessageHandlers = {
10
+ [K in AvailableUpdateTypes as `on_${K}`]?: (update: NonNullable<Update[K]>) => BotResponse;
11
+ };
12
+ export type BotMessageHandlerSettings = {
13
+ readonly batch_size?: number;
14
+ readonly timeout?: number;
15
+ readonly max_empty_responses?: number;
16
+ } & BotMessageHandlers;
@@ -0,0 +1,6 @@
1
+ import type { Update } from "../../specification/types.js";
2
+ import { AvailableUpdateTypes } from "./types.js";
3
+ export type ExtractedUpdate<K extends AvailableUpdateTypes> = {
4
+ type: K;
5
+ } & Update[K];
6
+ export declare const extractUpdate: <U extends AvailableUpdateTypes>(input: Update) => ExtractedUpdate<U> | undefined;
@@ -0,0 +1,7 @@
1
+ import { Micro } from "effect";
2
+ import { BotFactoryServiceDefault } from "./factory/_service.js";
3
+ export declare const runTgChatBot: (input: Parameters<typeof BotFactoryServiceDefault.runBot>[0]) => Promise<Micro.MicroFiber<{
4
+ updates: import("../index.js").Update[];
5
+ lastSuccessId: number | undefined;
6
+ hasError: boolean;
7
+ }, import("../client/errors.js").TgBotClientError>>;
@@ -0,0 +1,21 @@
1
+ import * as Micro from "effect/Micro";
2
+ import * as Context from "effect/Context";
3
+ import { BotMessageHandlerSettings } from "../../bot/message-handler/types.js";
4
+ export type BotUpdatePollerServiceInterface = Micro.Micro.Success<typeof BotUpdatesPollerServiceDefault>;
5
+ declare const BotUpdatePollerService_base: Context.TagClass<BotUpdatePollerService, "BotUpdatePollerService", {
6
+ readonly runBot: (messageHandler: BotMessageHandlerSettings) => Micro.Micro<Micro.MicroFiber<{
7
+ updates: import("../../index.js").Update[];
8
+ lastSuccessId: number | undefined;
9
+ hasError: boolean;
10
+ }, import("../../client/errors.js").TgBotClientError>, string, never>;
11
+ }>;
12
+ export declare class BotUpdatePollerService extends BotUpdatePollerService_base {
13
+ }
14
+ export declare const BotUpdatesPollerServiceDefault: Micro.Micro<{
15
+ readonly runBot: (messageHandler: BotMessageHandlerSettings) => Micro.Micro<Micro.MicroFiber<{
16
+ updates: import("../../index.js").Update[];
17
+ lastSuccessId: number | undefined;
18
+ hasError: boolean;
19
+ }, import("../../client/errors.js").TgBotClientError>, string, never>;
20
+ }, never, import("../../client/config.js").TgBotClientConfig>;
21
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import * as Micro from "effect/Micro";
2
+ import type { State } from "./poll-and-handle.js";
3
+ import type { SafeSettings } from "./settings.js";
4
+ import type { BotMessageHandlers } from "../message-handler/types.js";
5
+ import type { ClientExecuteRequestServiceInterface } from "../../client/execute-request/_service.js";
6
+ type Context = {
7
+ state: State;
8
+ settings: SafeSettings;
9
+ handlers: BotMessageHandlers;
10
+ execute: ClientExecuteRequestServiceInterface["execute"];
11
+ };
12
+ export declare const fetchUpdates: ({ state, settings, execute, handlers }: Context) => Micro.Micro<{
13
+ updates: import("../../index.js").Update[];
14
+ lastSuccessId: number | undefined;
15
+ hasError: boolean;
16
+ }, import("../../client/errors.js").TgBotClientError, never>;
17
+ export {};
@@ -0,0 +1,16 @@
1
+ import * as Micro from "effect/Micro";
2
+ import type { ClientExecuteRequestServiceInterface } from "../../client/execute-request/_service.js";
3
+ import type { BotMessageHandlerSettings } from "../../bot/message-handler/types.js";
4
+ export type State = {
5
+ lastUpdateId: number | undefined;
6
+ emptyResponses: number;
7
+ };
8
+ export type PollAndHandleInput = {
9
+ settings: BotMessageHandlerSettings;
10
+ execute: ClientExecuteRequestServiceInterface["execute"];
11
+ };
12
+ export declare const pollAndHandle: (input: PollAndHandleInput) => Micro.Micro<{
13
+ updates: import("../../index.js").Update[];
14
+ lastSuccessId: number | undefined;
15
+ hasError: boolean;
16
+ }, import("../../client/errors.js").TgBotClientError, never>;
@@ -0,0 +1,6 @@
1
+ import type { BotMessageHandlerSettings } from "../../bot/message-handler/types.js";
2
+ export type SafeSettings = ReturnType<typeof makeSettingsFrom>;
3
+ export declare const makeSettingsFrom: (input: BotMessageHandlerSettings) => {
4
+ readonly limit: number;
5
+ readonly timeout: number;
6
+ };
@@ -1,19 +1,8 @@
1
- import type { SetOptional } from "type-fest";
2
- export type BotConfig = {
3
- token: string;
4
- baseUrl: string;
5
- };
1
+ import { ClientFileServiceInterface } from "./file/_service.js";
2
+ import { Api } from "../specification/api.js";
3
+ import type { TgBotClientSettingsInput } from "./guards.js";
6
4
  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<{
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]>>;
5
+ export declare const makeTgBotClient: (input: TgBotClientSettingsInput) => {
6
+ execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<ReturnType<Api[M]>>;
7
+ getFile: (input: Parameters<ClientFileServiceInterface["getFile"]>[0]) => Promise<File>;
19
8
  };
@@ -0,0 +1,8 @@
1
+ import * as Context from "effect/Context";
2
+ import { TgBotClientSettingsInput } from "./guards.js";
3
+ export type TgBotClientConfigObject = Required<TgBotClientSettingsInput>;
4
+ export declare const makeTgBotClientConfig: (input: TgBotClientSettingsInput) => Required<TgBotClientSettingsInput>;
5
+ declare const TgBotClientConfig_base: Context.TagClass<TgBotClientConfig, "TgBotClientConfig", Required<TgBotClientSettingsInput>>;
6
+ export declare class TgBotClientConfig extends TgBotClientConfig_base {
7
+ }
8
+ export {};
@@ -12,10 +12,12 @@ type ErrorReason = {
12
12
  readonly type: "UnableToGetFile";
13
13
  readonly cause: unknown;
14
14
  };
15
- export declare class TgBotClientError extends Error {
16
- readonly reason: ErrorReason;
17
- readonly _tag = "TgBotClientError";
18
- constructor(reason: ErrorReason);
15
+ declare const TgBotClientError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
16
+ readonly _tag: "TgBotClientError";
17
+ } & Readonly<A>;
18
+ export declare class TgBotClientError extends TgBotClientError_base<{
19
+ reason: ErrorReason;
20
+ }> {
19
21
  static readonly missingSuccess: TgBotClientError;
20
22
  }
21
23
  export {};
@@ -0,0 +1,15 @@
1
+ import * as Micro from "effect/Micro";
2
+ import * as Context from "effect/Context";
3
+ import type { Api } from "../../specification/api.js";
4
+ import { TgBotClientError } from "../errors.js";
5
+ import { TgBotClientConfig } from "../config.js";
6
+ export type ClientExecuteRequestServiceInterface = Micro.Micro.Success<typeof ClientExecuteRequestServiceDefault>;
7
+ declare const ClientExecuteRequestService_base: Context.TagClass<ClientExecuteRequestService, "ClientExecuteRequestService", {
8
+ execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Micro.Micro<ReturnType<Api[M]>, TgBotClientError>;
9
+ }>;
10
+ export declare class ClientExecuteRequestService extends ClientExecuteRequestService_base {
11
+ }
12
+ export declare const ClientExecuteRequestServiceDefault: Micro.Micro<{
13
+ readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Micro.Micro<ReturnType<Api[M]>, TgBotClientError, never>;
14
+ }, never, TgBotClientConfig>;
15
+ export {};
@@ -0,0 +1,5 @@
1
+ import * as Micro from "effect/Micro";
2
+ import type { Api } from "../../specification/api.js";
3
+ import { TgBotClientError } from "../errors.js";
4
+ import { TgBotClientConfigObject } from "../config.js";
5
+ export declare const execute: <M extends keyof Api>(config: TgBotClientConfigObject, method: M, input: Parameters<Api[M]>[0]) => Micro.Micro<ReturnType<Api[M]>, TgBotClientError>;
@@ -0,0 +1,16 @@
1
+ import * as Micro from "effect/Micro";
2
+ import * as Context from "effect/Context";
3
+ import { TgBotClientConfig } from "../config.js";
4
+ import { getFile } from "./get-file.js";
5
+ export type ClientFileServiceInterface = Context.Tag.Service<typeof ClientFileService>;
6
+ declare const ClientFileService_base: Context.TagClass<ClientFileService, "ClientFileService", {
7
+ getFile: (input: {
8
+ file_id: string;
9
+ }) => ReturnType<typeof getFile>;
10
+ }>;
11
+ export declare class ClientFileService extends ClientFileService_base {
12
+ }
13
+ export declare const ClientFileServiceDefault: Micro.Micro<{
14
+ readonly getFile: (input: Parameters<ClientFileServiceInterface["getFile"]>[0]) => Micro.Micro<File, import("../errors.js").TgBotClientError, never>;
15
+ }, never, TgBotClientConfig>;
16
+ export {};
@@ -0,0 +1,5 @@
1
+ import * as Micro from "effect/Micro";
2
+ import { TgBotClientError } from "../errors.js";
3
+ import { TgBotClientConfigObject } from "../config.js";
4
+ import { ClientExecuteRequestServiceInterface } from "../execute-request/_service.js";
5
+ export declare const getFile: (fileId: string, config: TgBotClientConfigObject, execute: ClientExecuteRequestServiceInterface) => Micro.Micro<File, TgBotClientError>;
@@ -0,0 +1,17 @@
1
+ export type FileContent = {
2
+ file_content: Uint8Array;
3
+ file_name: string;
4
+ };
5
+ export declare const isFileContent: (input: unknown) => input is FileContent;
6
+ export type TgBotApiResponseSchema = {
7
+ ok: boolean;
8
+ error_code?: number;
9
+ description?: string;
10
+ result?: unknown;
11
+ };
12
+ export declare const isTgBotApiResponse: (input: unknown) => input is TgBotApiResponseSchema;
13
+ export type TgBotClientSettingsInput = {
14
+ ["bot-token"]: string;
15
+ ["base-url"]?: string;
16
+ };
17
+ export declare const isTgBotClientSettingsInput: (input: unknown) => input is TgBotClientSettingsInput;
@@ -1,3 +1,4 @@
1
+ export declare const defaultBaseUrl = "https://api.telegram.org";
1
2
  export declare const MESSAGE_EFFECTS: {
2
3
  readonly "\uD83D\uDD25": "5104841245755180586";
3
4
  readonly "\uD83D\uDC4D": "5107584321108051014";
@@ -1,4 +1,5 @@
1
- export * from "./client/const.js";
2
1
  export * from "./client/_client.js";
2
+ export * from "./bot/run.js";
3
3
  export * from "./specification/api.js";
4
4
  export * from "./specification/types.js";
5
+ export * from "./const.js";