@yaebal/commands 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 neverlane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @yaebal/commands
2
+
3
+ define a command: its name, menu description, and handler(s).
4
+
5
+ ## install
6
+
7
+ ```sh
8
+ pnpm add @yaebal/commands
9
+ ```
10
+
11
+ ---
12
+
13
+ part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
package/lib/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { Context, Middleware, Plugin } from "@yaebal/core";
2
+ export interface BotCommand {
3
+ command: string;
4
+ description: string;
5
+ }
6
+ interface CommandApi {
7
+ call<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
8
+ }
9
+ export interface CommandsRegistry {
10
+ /** Define a command: its name, menu description, and handler(s). */
11
+ add(name: string, description: string, ...handlers: Middleware<Context>[]): CommandsRegistry;
12
+ /** The `{ command, description }[]` for `setMyCommands`. */
13
+ list(): BotCommand[];
14
+ /** A plugin that registers every command's handler on the bot. */
15
+ plugin(): Plugin<Context, Record<never, never>>;
16
+ /** Push the command list to Telegram (so they show in the `/` menu). */
17
+ register(api: CommandApi, options?: {
18
+ languageCode?: string;
19
+ }): Promise<unknown>;
20
+ }
21
+ /**
22
+ * A single source of truth for commands: name + menu description + handler.
23
+ * `bot.install(cmd.plugin())` wires the handlers; `cmd.register(bot.api)` pushes
24
+ * the menu to Telegram.
25
+ */
26
+ export declare function commands(): CommandsRegistry;
27
+ export {};
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEhE,MAAM,WAAW,UAAU;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,UAAU;IACnB,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAChF;AAED,MAAM,WAAW,gBAAgB;IAChC,oEAAoE;IACpE,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,GAAG,gBAAgB,CAAC;IAC7F,4DAA4D;IAC5D,IAAI,IAAI,UAAU,EAAE,CAAC;IACrB,kEAAkE;IAClE,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAChD,wEAAwE;IACxE,QAAQ,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE;QAAE,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACjF;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,IAAI,gBAAgB,CA6B3C"}
package/lib/index.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A single source of truth for commands: name + menu description + handler.
3
+ * `bot.install(cmd.plugin())` wires the handlers; `cmd.register(bot.api)` pushes
4
+ * the menu to Telegram.
5
+ */
6
+ export function commands() {
7
+ const defs = [];
8
+ const registry = {
9
+ add(name, description, ...handlers) {
10
+ defs.push({ name, description, handlers });
11
+ return registry;
12
+ },
13
+ list() {
14
+ return defs.map((d) => ({ command: d.name, description: d.description }));
15
+ },
16
+ plugin() {
17
+ return (composer) => {
18
+ for (const d of defs)
19
+ composer.command(d.name, ...d.handlers);
20
+ return composer;
21
+ };
22
+ },
23
+ register(api, options) {
24
+ return api.call("setMyCommands", {
25
+ commands: registry.list(),
26
+ ...(options?.languageCode ? { language_code: options.languageCode } : {}),
27
+ });
28
+ },
29
+ };
30
+ return registry;
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA;;;;GAIG;AACH,MAAM,UAAU,QAAQ;IACvB,MAAM,IAAI,GAAkF,EAAE,CAAC;IAE/F,MAAM,QAAQ,GAAqB;QAClC,GAAG,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,QAAQ;YACjC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC3C,OAAO,QAAQ,CAAC;QACjB,CAAC;QAED,IAAI;YACH,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;QAC3E,CAAC;QAED,MAAM;YACL,OAAO,CAAC,QAAQ,EAAE,EAAE;gBACnB,KAAK,MAAM,CAAC,IAAI,IAAI;oBAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;gBAC9D,OAAO,QAAQ,CAAC;YACjB,CAAC,CAAC;QACH,CAAC;QAED,QAAQ,CAAC,GAAG,EAAE,OAAO;YACpB,OAAO,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE;gBAChC,QAAQ,EAAE,QAAQ,CAAC,IAAI,EAAE;gBACzB,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACzE,CAAC,CAAC;QACJ,CAAC;KACD,CAAC;IAEF,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,57 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { Composer, Context } from "@yaebal/core";
4
+ import { commands } from "./index.js";
5
+ const noop = async () => { };
6
+ const entry = (c) => c.toMiddleware();
7
+ const api = {};
8
+ const cmdCtx = (text) => new Context({
9
+ api,
10
+ update: {
11
+ update_id: 1,
12
+ message: {
13
+ message_id: 1,
14
+ date: 0,
15
+ chat: { id: 1, type: "private" },
16
+ from: { id: 1, is_bot: false, first_name: "u" },
17
+ text,
18
+ },
19
+ },
20
+ updateType: "message",
21
+ });
22
+ test("list() yields the {command, description} menu", () => {
23
+ const cmd = commands()
24
+ .add("start", "start the bot", () => { })
25
+ .add("help", "show help", () => { });
26
+ assert.deepEqual(cmd.list(), [
27
+ { command: "start", description: "start the bot" },
28
+ { command: "help", description: "show help" },
29
+ ]);
30
+ });
31
+ test("plugin() registers the handlers", async () => {
32
+ let started = false;
33
+ const cmd = commands().add("start", "start", () => {
34
+ started = true;
35
+ });
36
+ const mw = entry(new Composer().install(cmd.plugin()));
37
+ await mw(cmdCtx("/start"), noop);
38
+ assert.equal(started, true);
39
+ });
40
+ test("register() pushes the list via setMyCommands", async () => {
41
+ const calls = [];
42
+ const fakeApi = {
43
+ call: (method, params) => {
44
+ calls.push({ method, params });
45
+ return Promise.resolve(true);
46
+ },
47
+ };
48
+ const cmd = commands().add("start", "go", () => { });
49
+ await cmd.register(fakeApi, { languageCode: "en" });
50
+ assert.deepEqual(calls, [
51
+ {
52
+ method: "setMyCommands",
53
+ params: { commands: [{ command: "start", description: "go" }], language_code: "en" },
54
+ },
55
+ ]);
56
+ });
57
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAmB,MAAM,cAAc,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;AAC5B,MAAM,KAAK,GAAG,CAAoB,CAAc,EAAE,EAAE,CACnD,CAAC,CAAC,YAAY,EAAoC,CAAC;AAEpD,MAAM,GAAG,GAAG,EAAW,CAAC;AACxB,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE,CAC/B,IAAI,OAAO,CAAC;IACX,GAAG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AM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,8CAA8C,EAAE,KAAK,IAAI,EAAE;IAC/D,MAAM,KAAK,GAA+C,EAAE,CAAC;IAC7D,MAAM,OAAO,GAAG;QACf,IAAI,EAAE,CAAC,MAAc,EAAE,MAAe,EAAE,EAAE;YACzC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;YAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC9B,CAAC;KACD,CAAC;IAEF,MAAM,GAAG,GAAG,QAAQ,EAAE,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACpD,MAAM,GAAG,CAAC,QAAQ,CAAC,OAAgB,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;IAE7D,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE;QACvB;YACC,MAAM,EAAE,eAAe;YACvB,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE;SACpF;KACD,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@yaebal/commands",
3
+ "version": "0.0.1",
4
+ "description": "yaebal commands — one registry for command handlers + the telegram command menu.",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "lib",
16
+ "src"
17
+ ],
18
+ "dependencies": {
19
+ "@yaebal/core": "0.0.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "latest"
23
+ },
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "keywords": [
28
+ "telegram",
29
+ "telegram-bot",
30
+ "yaebal",
31
+ "commands",
32
+ "setmycommands"
33
+ ],
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/neverlane/yaebal",
38
+ "directory": "packages/commands"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.json",
45
+ "typecheck": "tsc -p tsconfig.json --noEmit",
46
+ "test": "node --test lib"
47
+ }
48
+ }
@@ -0,0 +1,71 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { Composer, Context, type Middleware } from "@yaebal/core";
4
+ import { commands } from "./index.js";
5
+
6
+ const noop = async () => {};
7
+ const entry = <C extends Context>(c: Composer<C>) =>
8
+ c.toMiddleware() as unknown as Middleware<Context>;
9
+
10
+ const api = {} as never;
11
+ const cmdCtx = (text: string) =>
12
+ new Context({
13
+ api,
14
+
15
+ update: {
16
+ update_id: 1,
17
+ message: {
18
+ message_id: 1,
19
+ date: 0,
20
+ chat: { id: 1, type: "private" },
21
+ from: { id: 1, is_bot: false, first_name: "u" },
22
+ text,
23
+ },
24
+ } as never,
25
+
26
+ updateType: "message",
27
+ });
28
+
29
+ test("list() yields the {command, description} menu", () => {
30
+ const cmd = commands()
31
+ .add("start", "start the bot", () => {})
32
+ .add("help", "show help", () => {});
33
+
34
+ assert.deepEqual(cmd.list(), [
35
+ { command: "start", description: "start the bot" },
36
+ { command: "help", description: "show help" },
37
+ ]);
38
+ });
39
+
40
+ test("plugin() registers the handlers", async () => {
41
+ let started = false;
42
+
43
+ const cmd = commands().add("start", "start", () => {
44
+ started = true;
45
+ });
46
+
47
+ const mw = entry(new Composer<Context>().install(cmd.plugin()));
48
+ await mw(cmdCtx("/start"), noop);
49
+
50
+ assert.equal(started, true);
51
+ });
52
+
53
+ test("register() pushes the list via setMyCommands", async () => {
54
+ const calls: Array<{ method: string; params: unknown }> = [];
55
+ const fakeApi = {
56
+ call: (method: string, params: unknown) => {
57
+ calls.push({ method, params });
58
+ return Promise.resolve(true);
59
+ },
60
+ };
61
+
62
+ const cmd = commands().add("start", "go", () => {});
63
+ await cmd.register(fakeApi as never, { languageCode: "en" });
64
+
65
+ assert.deepEqual(calls, [
66
+ {
67
+ method: "setMyCommands",
68
+ params: { commands: [{ command: "start", description: "go" }], language_code: "en" },
69
+ },
70
+ ]);
71
+ });
package/src/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { Context, Middleware, Plugin } from "@yaebal/core";
2
+
3
+ export interface BotCommand {
4
+ command: string;
5
+ description: string;
6
+ }
7
+
8
+ interface CommandApi {
9
+ call<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
10
+ }
11
+
12
+ export interface CommandsRegistry {
13
+ /** Define a command: its name, menu description, and handler(s). */
14
+ add(name: string, description: string, ...handlers: Middleware<Context>[]): CommandsRegistry;
15
+ /** The `{ command, description }[]` for `setMyCommands`. */
16
+ list(): BotCommand[];
17
+ /** A plugin that registers every command's handler on the bot. */
18
+ plugin(): Plugin<Context, Record<never, never>>;
19
+ /** Push the command list to Telegram (so they show in the `/` menu). */
20
+ register(api: CommandApi, options?: { languageCode?: string }): Promise<unknown>;
21
+ }
22
+
23
+ /**
24
+ * A single source of truth for commands: name + menu description + handler.
25
+ * `bot.install(cmd.plugin())` wires the handlers; `cmd.register(bot.api)` pushes
26
+ * the menu to Telegram.
27
+ */
28
+ export function commands(): CommandsRegistry {
29
+ const defs: Array<{ name: string; description: string; handlers: Middleware<Context>[] }> = [];
30
+
31
+ const registry: CommandsRegistry = {
32
+ add(name, description, ...handlers) {
33
+ defs.push({ name, description, handlers });
34
+ return registry;
35
+ },
36
+
37
+ list() {
38
+ return defs.map((d) => ({ command: d.name, description: d.description }));
39
+ },
40
+
41
+ plugin() {
42
+ return (composer) => {
43
+ for (const d of defs) composer.command(d.name, ...d.handlers);
44
+ return composer;
45
+ };
46
+ },
47
+
48
+ register(api, options) {
49
+ return api.call("setMyCommands", {
50
+ commands: registry.list(),
51
+ ...(options?.languageCode ? { language_code: options.languageCode } : {}),
52
+ });
53
+ },
54
+ };
55
+
56
+ return registry;
57
+ }