@gramio/composer 0.3.3 → 0.4.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.
package/dist/index.cjs CHANGED
@@ -81,6 +81,7 @@ class Composer {
81
81
  errorsDefinitions: {},
82
82
  tracer: void 0,
83
83
  macros: {},
84
+ commandsMeta: /* @__PURE__ */ new Map(),
84
85
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
85
86
  Out: void 0
86
87
  };
@@ -280,6 +281,11 @@ class Composer {
280
281
  }
281
282
  Object.assign(this["~"].errorsDefinitions, temp["~"].errorsDefinitions);
282
283
  this["~"].onErrors.push(...temp["~"].onErrors);
284
+ if (temp["~"].commandsMeta) {
285
+ for (const [cmd, meta] of temp["~"].commandsMeta) {
286
+ this["~"].commandsMeta.set(cmd, meta);
287
+ }
288
+ }
283
289
  for (const key of temp["~"].extended) {
284
290
  this["~"].extended.add(key);
285
291
  }
@@ -339,6 +345,11 @@ class Composer {
339
345
  Object.assign(this["~"].errorsDefinitions, other["~"].errorsDefinitions);
340
346
  Object.assign(this["~"].macros, other["~"].macros);
341
347
  this["~"].onErrors.push(...other["~"].onErrors);
348
+ if (other["~"].commandsMeta) {
349
+ for (const [cmd, meta] of other["~"].commandsMeta) {
350
+ this["~"].commandsMeta.set(cmd, meta);
351
+ }
352
+ }
342
353
  const pluginName = other["~"].name;
343
354
  const isNew = (m) => {
344
355
  if (!m.plugin) return true;
@@ -386,6 +397,31 @@ class Composer {
386
397
  return info;
387
398
  });
388
399
  }
400
+ /**
401
+ * Returns a Set of all event names registered via `.on()` and event-specific `.derive()`.
402
+ *
403
+ * Useful for introspecting which update types the middleware chain handles,
404
+ * e.g. to auto-derive `allowed_updates` for the Telegram Bot API.
405
+ *
406
+ * @example
407
+ * ```typescript
408
+ * composer.on("message", handler);
409
+ * composer.on(["callback_query", "inline_query"], handler);
410
+ * composer.registeredEvents(); // Set {"message", "callback_query", "inline_query"}
411
+ * ```
412
+ */
413
+ registeredEvents() {
414
+ const events = /* @__PURE__ */ new Set();
415
+ for (const mw of this["~"].middlewares) {
416
+ if ((mw.type === "on" || mw.type === "derive") && mw.name) {
417
+ for (const part of mw.name.split("|")) {
418
+ const eventPart = part.includes(":") ? part.split(":")[0] : part;
419
+ if (eventPart) events.add(eventPart);
420
+ }
421
+ }
422
+ }
423
+ return events;
424
+ }
389
425
  trace(handler) {
390
426
  this["~"].tracer = handler;
391
427
  this.invalidate();
package/dist/index.d.cts CHANGED
@@ -124,6 +124,22 @@ interface ComposerOptions {
124
124
  name?: string;
125
125
  seed?: unknown;
126
126
  }
127
+ /** Shorthand strings for common BotCommandScope types */
128
+ type ScopeShorthand = "default" | "all_private_chats" | "all_group_chats" | "all_chat_administrators";
129
+ /**
130
+ * Metadata for a bot command, used by `syncCommands()` to push
131
+ * descriptions, localized names, and visibility scopes to the Telegram API.
132
+ */
133
+ interface CommandMeta<TScope = any> {
134
+ /** Command description shown in the Telegram menu (1-256 chars) */
135
+ description: string;
136
+ /** Localized descriptions keyed by IETF language tag */
137
+ locales?: Record<string, string>;
138
+ /** Where this command is visible. Default: `["default"]` */
139
+ scopes?: (TScope | ScopeShorthand)[];
140
+ /** Exclude this command from `syncCommands()`. The handler still works. @default false */
141
+ hide?: boolean;
142
+ }
127
143
 
128
144
  /**
129
145
  * Compose an array of middleware functions into a single middleware.
@@ -154,6 +170,7 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
154
170
  }>;
155
171
  tracer: TraceHandler | undefined;
156
172
  macros: Record<string, MacroDef<any, any>>;
173
+ commandsMeta: Map<string, CommandMeta<any>>;
157
174
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
158
175
  Out: TOut;
159
176
  };
@@ -174,7 +191,8 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
174
191
  derive<D extends object>(handler: DeriveHandler<TOut, D>, options: {
175
192
  as: "scoped" | "global";
176
193
  }): Composer<TIn, TOut & D, TExposed & D>;
177
- guard<S extends TOut>(predicate: ((context: TOut) => context is S) | ((context: TOut) => boolean | Promise<boolean>), ...middleware: Middleware<any>[]): Composer<TIn, TOut, TExposed>;
194
+ guard<S extends TOut>(predicate: (context: TOut) => context is S): Composer<TIn, S, TExposed>;
195
+ guard(predicate: (context: TOut) => boolean | Promise<boolean>, ...middleware: Middleware<TOut>[]): Composer<TIn, TOut, TExposed>;
178
196
  branch(predicate: ((context: TOut) => boolean | Promise<boolean>) | boolean, onTrue: Middleware<TOut>, onFalse?: Middleware<TOut>): Composer<TIn, TOut, TExposed>;
179
197
  route<K extends string>(router: (context: TOut) => K | undefined | Promise<K | undefined>, builder: (route: RouteBuilder<TOut, K>) => void): Composer<TIn, TOut, TExposed>;
180
198
  route<K extends string>(router: (context: TOut) => K | undefined | Promise<K | undefined>, cases: Partial<Record<K, Middleware<TOut> | Middleware<TOut>[] | Composer<any, any, any>>>, fallback?: Middleware<TOut> | Middleware<TOut>[] | Composer<any, any, any>): Composer<TIn, TOut, TExposed>;
@@ -191,6 +209,20 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
191
209
  group(fn: (composer: Composer<TOut, TOut, {}>) => void): Composer<TIn, TOut, TExposed>;
192
210
  extend<UIn extends object, UOut extends UIn, UExposed extends object, UMacros extends MacroDefinitions = {}>(other: Composer<UIn, UOut, UExposed, UMacros>): Composer<TIn, TOut & UExposed, TExposed, TMacros & UMacros>;
193
211
  inspect(): MiddlewareInfo[];
212
+ /**
213
+ * Returns a Set of all event names registered via `.on()` and event-specific `.derive()`.
214
+ *
215
+ * Useful for introspecting which update types the middleware chain handles,
216
+ * e.g. to auto-derive `allowed_updates` for the Telegram Bot API.
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * composer.on("message", handler);
221
+ * composer.on(["callback_query", "inline_query"], handler);
222
+ * composer.registeredEvents(); // Set {"message", "callback_query", "inline_query"}
223
+ * ```
224
+ */
225
+ registeredEvents(): Set<string>;
194
226
  trace(handler: TraceHandler): this;
195
227
  compose(): ComposedMiddleware<TIn>;
196
228
  run(context: TIn, next?: Next): Promise<void>;
@@ -301,6 +333,7 @@ interface EventComposer<TBase extends object, TEventMap extends Record<string, T
301
333
  /** Register multiple macros at once */
302
334
  macro<const TDefs extends Record<string, MacroDef<any, any>>>(definitions: TDefs): EventComposer<TBase, TEventMap, TIn, TOut, TExposed, TDerives, TMethods, TMacros & TDefs> & TMethods;
303
335
  inspect(): MiddlewareInfo[];
336
+ registeredEvents(): Set<string>;
304
337
  trace(handler: TraceHandler): this;
305
338
  compose(): ComposedMiddleware<TIn>;
306
339
  run(context: TIn, next?: Next): Promise<void>;
@@ -317,6 +350,7 @@ interface EventComposer<TBase extends object, TEventMap extends Record<string, T
317
350
  }>;
318
351
  tracer: TraceHandler | undefined;
319
352
  macros: Record<string, MacroDef<any, any>>;
353
+ commandsMeta: Map<string, CommandMeta>;
320
354
  Derives: TDerives;
321
355
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
322
356
  Out: TOut;
@@ -500,4 +534,4 @@ declare const skip: Middleware<any>;
500
534
  declare const stop: Middleware<any>;
501
535
 
502
536
  export { Composer, EventQueue, buildFromOptions, compose, createComposer, defineComposerMethods, eventTypes, noopNext, skip, stop };
503
- export type { CompatibleEvents, ComposedMiddleware, ComposerLike, ComposerOptions, ContextCallback, ContextOf, DeriveFromOptions, DeriveHandler, ErrorHandler, EventComposer, EventComposerConstructor, EventContextOf, HandlerOptions, LazyFactory, MacroDef, MacroDefinitions, MacroDeriveType, MacroHooks, MacroOptionType, MaybeArray, Middleware, MiddlewareInfo, MiddlewareType, Next, RouteBuilder, RouteHandler, Scope, TraceHandler, WithCtx };
537
+ export type { CommandMeta, CompatibleEvents, ComposedMiddleware, ComposerLike, ComposerOptions, ContextCallback, ContextOf, DeriveFromOptions, DeriveHandler, ErrorHandler, EventComposer, EventComposerConstructor, EventContextOf, HandlerOptions, LazyFactory, MacroDef, MacroDefinitions, MacroDeriveType, MacroHooks, MacroOptionType, MaybeArray, Middleware, MiddlewareInfo, MiddlewareType, Next, RouteBuilder, RouteHandler, Scope, ScopeShorthand, TraceHandler, WithCtx };
package/dist/index.d.ts CHANGED
@@ -124,6 +124,22 @@ interface ComposerOptions {
124
124
  name?: string;
125
125
  seed?: unknown;
126
126
  }
127
+ /** Shorthand strings for common BotCommandScope types */
128
+ type ScopeShorthand = "default" | "all_private_chats" | "all_group_chats" | "all_chat_administrators";
129
+ /**
130
+ * Metadata for a bot command, used by `syncCommands()` to push
131
+ * descriptions, localized names, and visibility scopes to the Telegram API.
132
+ */
133
+ interface CommandMeta<TScope = any> {
134
+ /** Command description shown in the Telegram menu (1-256 chars) */
135
+ description: string;
136
+ /** Localized descriptions keyed by IETF language tag */
137
+ locales?: Record<string, string>;
138
+ /** Where this command is visible. Default: `["default"]` */
139
+ scopes?: (TScope | ScopeShorthand)[];
140
+ /** Exclude this command from `syncCommands()`. The handler still works. @default false */
141
+ hide?: boolean;
142
+ }
127
143
 
128
144
  /**
129
145
  * Compose an array of middleware functions into a single middleware.
@@ -154,6 +170,7 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
154
170
  }>;
155
171
  tracer: TraceHandler | undefined;
156
172
  macros: Record<string, MacroDef<any, any>>;
173
+ commandsMeta: Map<string, CommandMeta<any>>;
157
174
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
158
175
  Out: TOut;
159
176
  };
@@ -174,7 +191,8 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
174
191
  derive<D extends object>(handler: DeriveHandler<TOut, D>, options: {
175
192
  as: "scoped" | "global";
176
193
  }): Composer<TIn, TOut & D, TExposed & D>;
177
- guard<S extends TOut>(predicate: ((context: TOut) => context is S) | ((context: TOut) => boolean | Promise<boolean>), ...middleware: Middleware<any>[]): Composer<TIn, TOut, TExposed>;
194
+ guard<S extends TOut>(predicate: (context: TOut) => context is S): Composer<TIn, S, TExposed>;
195
+ guard(predicate: (context: TOut) => boolean | Promise<boolean>, ...middleware: Middleware<TOut>[]): Composer<TIn, TOut, TExposed>;
178
196
  branch(predicate: ((context: TOut) => boolean | Promise<boolean>) | boolean, onTrue: Middleware<TOut>, onFalse?: Middleware<TOut>): Composer<TIn, TOut, TExposed>;
179
197
  route<K extends string>(router: (context: TOut) => K | undefined | Promise<K | undefined>, builder: (route: RouteBuilder<TOut, K>) => void): Composer<TIn, TOut, TExposed>;
180
198
  route<K extends string>(router: (context: TOut) => K | undefined | Promise<K | undefined>, cases: Partial<Record<K, Middleware<TOut> | Middleware<TOut>[] | Composer<any, any, any>>>, fallback?: Middleware<TOut> | Middleware<TOut>[] | Composer<any, any, any>): Composer<TIn, TOut, TExposed>;
@@ -191,6 +209,20 @@ declare class Composer<TIn extends object = {}, TOut extends TIn = TIn, TExposed
191
209
  group(fn: (composer: Composer<TOut, TOut, {}>) => void): Composer<TIn, TOut, TExposed>;
192
210
  extend<UIn extends object, UOut extends UIn, UExposed extends object, UMacros extends MacroDefinitions = {}>(other: Composer<UIn, UOut, UExposed, UMacros>): Composer<TIn, TOut & UExposed, TExposed, TMacros & UMacros>;
193
211
  inspect(): MiddlewareInfo[];
212
+ /**
213
+ * Returns a Set of all event names registered via `.on()` and event-specific `.derive()`.
214
+ *
215
+ * Useful for introspecting which update types the middleware chain handles,
216
+ * e.g. to auto-derive `allowed_updates` for the Telegram Bot API.
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * composer.on("message", handler);
221
+ * composer.on(["callback_query", "inline_query"], handler);
222
+ * composer.registeredEvents(); // Set {"message", "callback_query", "inline_query"}
223
+ * ```
224
+ */
225
+ registeredEvents(): Set<string>;
194
226
  trace(handler: TraceHandler): this;
195
227
  compose(): ComposedMiddleware<TIn>;
196
228
  run(context: TIn, next?: Next): Promise<void>;
@@ -301,6 +333,7 @@ interface EventComposer<TBase extends object, TEventMap extends Record<string, T
301
333
  /** Register multiple macros at once */
302
334
  macro<const TDefs extends Record<string, MacroDef<any, any>>>(definitions: TDefs): EventComposer<TBase, TEventMap, TIn, TOut, TExposed, TDerives, TMethods, TMacros & TDefs> & TMethods;
303
335
  inspect(): MiddlewareInfo[];
336
+ registeredEvents(): Set<string>;
304
337
  trace(handler: TraceHandler): this;
305
338
  compose(): ComposedMiddleware<TIn>;
306
339
  run(context: TIn, next?: Next): Promise<void>;
@@ -317,6 +350,7 @@ interface EventComposer<TBase extends object, TEventMap extends Record<string, T
317
350
  }>;
318
351
  tracer: TraceHandler | undefined;
319
352
  macros: Record<string, MacroDef<any, any>>;
353
+ commandsMeta: Map<string, CommandMeta>;
320
354
  Derives: TDerives;
321
355
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
322
356
  Out: TOut;
@@ -500,4 +534,4 @@ declare const skip: Middleware<any>;
500
534
  declare const stop: Middleware<any>;
501
535
 
502
536
  export { Composer, EventQueue, buildFromOptions, compose, createComposer, defineComposerMethods, eventTypes, noopNext, skip, stop };
503
- export type { CompatibleEvents, ComposedMiddleware, ComposerLike, ComposerOptions, ContextCallback, ContextOf, DeriveFromOptions, DeriveHandler, ErrorHandler, EventComposer, EventComposerConstructor, EventContextOf, HandlerOptions, LazyFactory, MacroDef, MacroDefinitions, MacroDeriveType, MacroHooks, MacroOptionType, MaybeArray, Middleware, MiddlewareInfo, MiddlewareType, Next, RouteBuilder, RouteHandler, Scope, TraceHandler, WithCtx };
537
+ export type { CommandMeta, CompatibleEvents, ComposedMiddleware, ComposerLike, ComposerOptions, ContextCallback, ContextOf, DeriveFromOptions, DeriveHandler, ErrorHandler, EventComposer, EventComposerConstructor, EventContextOf, HandlerOptions, LazyFactory, MacroDef, MacroDefinitions, MacroDeriveType, MacroHooks, MacroOptionType, MaybeArray, Middleware, MiddlewareInfo, MiddlewareType, Next, RouteBuilder, RouteHandler, Scope, ScopeShorthand, TraceHandler, WithCtx };
package/dist/index.js CHANGED
@@ -78,6 +78,7 @@ class Composer {
78
78
  errorsDefinitions: {},
79
79
  tracer: void 0,
80
80
  macros: {},
81
+ commandsMeta: /* @__PURE__ */ new Map(),
81
82
  /** Phantom type accessor — never set at runtime, used by `ContextOf<T>` */
82
83
  Out: void 0
83
84
  };
@@ -277,6 +278,11 @@ class Composer {
277
278
  }
278
279
  Object.assign(this["~"].errorsDefinitions, temp["~"].errorsDefinitions);
279
280
  this["~"].onErrors.push(...temp["~"].onErrors);
281
+ if (temp["~"].commandsMeta) {
282
+ for (const [cmd, meta] of temp["~"].commandsMeta) {
283
+ this["~"].commandsMeta.set(cmd, meta);
284
+ }
285
+ }
280
286
  for (const key of temp["~"].extended) {
281
287
  this["~"].extended.add(key);
282
288
  }
@@ -336,6 +342,11 @@ class Composer {
336
342
  Object.assign(this["~"].errorsDefinitions, other["~"].errorsDefinitions);
337
343
  Object.assign(this["~"].macros, other["~"].macros);
338
344
  this["~"].onErrors.push(...other["~"].onErrors);
345
+ if (other["~"].commandsMeta) {
346
+ for (const [cmd, meta] of other["~"].commandsMeta) {
347
+ this["~"].commandsMeta.set(cmd, meta);
348
+ }
349
+ }
339
350
  const pluginName = other["~"].name;
340
351
  const isNew = (m) => {
341
352
  if (!m.plugin) return true;
@@ -383,6 +394,31 @@ class Composer {
383
394
  return info;
384
395
  });
385
396
  }
397
+ /**
398
+ * Returns a Set of all event names registered via `.on()` and event-specific `.derive()`.
399
+ *
400
+ * Useful for introspecting which update types the middleware chain handles,
401
+ * e.g. to auto-derive `allowed_updates` for the Telegram Bot API.
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * composer.on("message", handler);
406
+ * composer.on(["callback_query", "inline_query"], handler);
407
+ * composer.registeredEvents(); // Set {"message", "callback_query", "inline_query"}
408
+ * ```
409
+ */
410
+ registeredEvents() {
411
+ const events = /* @__PURE__ */ new Set();
412
+ for (const mw of this["~"].middlewares) {
413
+ if ((mw.type === "on" || mw.type === "derive") && mw.name) {
414
+ for (const part of mw.name.split("|")) {
415
+ const eventPart = part.includes(":") ? part.split(":")[0] : part;
416
+ if (eventPart) events.add(eventPart);
417
+ }
418
+ }
419
+ }
420
+ return events;
421
+ }
386
422
  trace(handler) {
387
423
  this["~"].tracer = handler;
388
424
  this.invalidate();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gramio/composer",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "description": "General-purpose, type-safe middleware composition library for TypeScript",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",