From 3e585859d0789949b8c827cddd472eb02509d4a7 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 30 Dec 2020 01:49:09 +0300 Subject: [PATCH] feat(): add middleware support & refactor --- lib/decorators/listeners/action.decorator.ts | 4 +-- lib/decorators/listeners/cashtag.decorator.ts | 4 +-- lib/decorators/listeners/command.decorator.ts | 4 +-- lib/decorators/listeners/email.decorator.ts | 4 +-- .../listeners/game-query.decorator.ts | 4 +-- lib/decorators/listeners/hashtag.decorator.ts | 4 +-- lib/decorators/listeners/hears.decorator.ts | 4 +-- lib/decorators/listeners/help.decorator.ts | 4 +-- .../listeners/inline-query.decorator.ts | 4 +-- lib/decorators/listeners/mention.decorator.ts | 4 +-- lib/decorators/listeners/on.decorator.ts | 4 +-- lib/decorators/listeners/phone.decorator.ts | 4 +-- .../listeners/settings.decorator.ts | 4 +-- lib/decorators/listeners/start.decorator.ts | 4 +-- .../listeners/text-link.decorator.ts | 4 +-- .../listeners/text-mention.decorator.ts | 4 +-- lib/decorators/listeners/url.decorator.ts | 4 +-- lib/decorators/listeners/use.decorator.ts | 4 +-- lib/decorators/scene/scene-enter.decorator.ts | 7 ++--- lib/decorators/scene/scene-leave.decorator.ts | 7 ++--- lib/enums/scene-event-type.enum.ts | 4 --- .../telegraf-scene.explorer.ts | 15 +++++------ .../telegraf-update.explorer.ts | 2 +- .../create-scene-listener-decorator.helper.ts | 18 +++++++++++++ ...reate-update-listener-decorator.helper.ts} | 9 ++++--- lib/helpers/index.ts | 3 ++- lib/interfaces/listener-metadata.interface.ts | 4 +-- lib/interfaces/telegraf-options.interface.ts | 4 ++- lib/telegraf.constants.ts | 1 - lib/telegraf.module.ts | 4 +-- lib/telegraf.providers.ts | 3 ++- lib/telegraf.types.ts | 26 ++++++++++++------- sample/app.module.ts | 2 ++ sample/middleware/session.middleware.ts | 3 +++ sample/scenes/hello.scene.ts | 2 +- 35 files changed, 105 insertions(+), 81 deletions(-) delete mode 100644 lib/enums/scene-event-type.enum.ts rename lib/{ => explorers}/telegraf-scene.explorer.ts (90%) rename lib/{ => explorers}/telegraf-update.explorer.ts (96%) create mode 100644 lib/helpers/create-scene-listener-decorator.helper.ts rename lib/helpers/{create-update-decorator.helper.ts => create-update-listener-decorator.helper.ts} (51%) create mode 100644 sample/middleware/session.middleware.ts diff --git a/lib/decorators/listeners/action.decorator.ts b/lib/decorators/listeners/action.decorator.ts index 6a4c2ac..4b14901 100644 --- a/lib/decorators/listeners/action.decorator.ts +++ b/lib/decorators/listeners/action.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling callback_data actions with regular expressions. * * @see https://telegraf.js.org/#/?id=action */ -export const Action = createUpdateDecorator('action'); +export const Action = createUpdateListenerDecorator('action'); diff --git a/lib/decorators/listeners/cashtag.decorator.ts b/lib/decorators/listeners/cashtag.decorator.ts index ab48729..c3e37e9 100644 --- a/lib/decorators/listeners/cashtag.decorator.ts +++ b/lib/decorators/listeners/cashtag.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Cashtag handling. * * @see https://telegraf.js.org/#/?id=cashtag */ -export const Cashtag = createUpdateDecorator('cashtag'); +export const Cashtag = createUpdateListenerDecorator('cashtag'); diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts index ec6e958..0e7806e 100644 --- a/lib/decorators/listeners/command.decorator.ts +++ b/lib/decorators/listeners/command.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Command handling. * * @see https://telegraf.js.org/#/?id=command */ -export const Command = createUpdateDecorator('command'); +export const Command = createUpdateListenerDecorator('command'); diff --git a/lib/decorators/listeners/email.decorator.ts b/lib/decorators/listeners/email.decorator.ts index 2364d71..e9bb1a6 100644 --- a/lib/decorators/listeners/email.decorator.ts +++ b/lib/decorators/listeners/email.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling messages with email entity. * * @see https://telegraf.js.org/#/?id=telegraf-email */ -export const Email = createUpdateDecorator('email'); +export const Email = createUpdateListenerDecorator('email'); diff --git a/lib/decorators/listeners/game-query.decorator.ts b/lib/decorators/listeners/game-query.decorator.ts index 3b8c227..347f998 100644 --- a/lib/decorators/listeners/game-query.decorator.ts +++ b/lib/decorators/listeners/game-query.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling callback_data actions with game query. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const GameQuery = createUpdateDecorator('gameQuery'); +export const GameQuery = createUpdateListenerDecorator('gameQuery'); diff --git a/lib/decorators/listeners/hashtag.decorator.ts b/lib/decorators/listeners/hashtag.decorator.ts index 3c7514a..86e9e13 100644 --- a/lib/decorators/listeners/hashtag.decorator.ts +++ b/lib/decorators/listeners/hashtag.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Hashtag handling. * * @see https://telegraf.js.org/#/?id=hashtag */ -export const Hashtag = createUpdateDecorator('hashtag'); +export const Hashtag = createUpdateListenerDecorator('hashtag'); diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts index b8f19a9..79d71af 100644 --- a/lib/decorators/listeners/hears.decorator.ts +++ b/lib/decorators/listeners/hears.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling text messages. * * @see https://telegraf.js.org/#/?id=hears */ -export const Hears = createUpdateDecorator('hears'); +export const Hears = createUpdateListenerDecorator('hears'); diff --git a/lib/decorators/listeners/help.decorator.ts b/lib/decorators/listeners/help.decorator.ts index 599d646..36ac9e4 100644 --- a/lib/decorators/listeners/help.decorator.ts +++ b/lib/decorators/listeners/help.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Handler for /help command. * * @see https://telegraf.js.org/#/?id=help */ -export const Help = createUpdateDecorator('help'); +export const Help = createUpdateListenerDecorator('help'); diff --git a/lib/decorators/listeners/inline-query.decorator.ts b/lib/decorators/listeners/inline-query.decorator.ts index abbdd39..722a348 100644 --- a/lib/decorators/listeners/inline-query.decorator.ts +++ b/lib/decorators/listeners/inline-query.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling inline_query actions with regular expressions. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const InlineQuery = createUpdateDecorator('inlineQuery'); +export const InlineQuery = createUpdateListenerDecorator('inlineQuery'); diff --git a/lib/decorators/listeners/mention.decorator.ts b/lib/decorators/listeners/mention.decorator.ts index d6be512..e4341a0 100644 --- a/lib/decorators/listeners/mention.decorator.ts +++ b/lib/decorators/listeners/mention.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Mention handling. * * @see https://telegraf.js.org/#/?id=mention */ -export const Mention = createUpdateDecorator('mention'); +export const Mention = createUpdateListenerDecorator('mention'); diff --git a/lib/decorators/listeners/on.decorator.ts b/lib/decorators/listeners/on.decorator.ts index a6f717c..9f3c0fd 100644 --- a/lib/decorators/listeners/on.decorator.ts +++ b/lib/decorators/listeners/on.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for provided update type. * * @see https://telegraf.js.org/#/?id=on */ -export const On = createUpdateDecorator('on'); +export const On = createUpdateListenerDecorator('on'); diff --git a/lib/decorators/listeners/phone.decorator.ts b/lib/decorators/listeners/phone.decorator.ts index 8feb92f..0f68380 100644 --- a/lib/decorators/listeners/phone.decorator.ts +++ b/lib/decorators/listeners/phone.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Phone number handling. * * @see https://telegraf.js.org/#/?id=phone */ -export const Phone = createUpdateDecorator('phone'); +export const Phone = createUpdateListenerDecorator('phone'); diff --git a/lib/decorators/listeners/settings.decorator.ts b/lib/decorators/listeners/settings.decorator.ts index 8f705f9..6cca7c7 100644 --- a/lib/decorators/listeners/settings.decorator.ts +++ b/lib/decorators/listeners/settings.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Handler for /settings command. * * @see https://telegraf.js.org/#/?id=settings */ -export const Settings = createUpdateDecorator('settings'); +export const Settings = createUpdateListenerDecorator('settings'); diff --git a/lib/decorators/listeners/start.decorator.ts b/lib/decorators/listeners/start.decorator.ts index 6a084f0..48bdc5b 100644 --- a/lib/decorators/listeners/start.decorator.ts +++ b/lib/decorators/listeners/start.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Handler for /start command. * * @see https://telegraf.js.org/#/?id=start */ -export const Start = createUpdateDecorator('start'); +export const Start = createUpdateListenerDecorator('start'); diff --git a/lib/decorators/listeners/text-link.decorator.ts b/lib/decorators/listeners/text-link.decorator.ts index 0e91ad7..c62439a 100644 --- a/lib/decorators/listeners/text-link.decorator.ts +++ b/lib/decorators/listeners/text-link.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling messages with text_link entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextLink = createUpdateDecorator('textLink'); +export const TextLink = createUpdateListenerDecorator('textLink'); diff --git a/lib/decorators/listeners/text-mention.decorator.ts b/lib/decorators/listeners/text-mention.decorator.ts index ebd30e3..895a458 100644 --- a/lib/decorators/listeners/text-mention.decorator.ts +++ b/lib/decorators/listeners/text-mention.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling messages with text_mention entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextMention = createUpdateDecorator('textMention'); +export const TextMention = createUpdateListenerDecorator('textMention'); diff --git a/lib/decorators/listeners/url.decorator.ts b/lib/decorators/listeners/url.decorator.ts index 36653fe..843aa41 100644 --- a/lib/decorators/listeners/url.decorator.ts +++ b/lib/decorators/listeners/url.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers middleware for handling messages with url entity. * * @see https://telegraf.js.org/#/?id=telegraf-url */ -export const Url = createUpdateDecorator('url'); +export const Url = createUpdateListenerDecorator('url'); diff --git a/lib/decorators/listeners/use.decorator.ts b/lib/decorators/listeners/use.decorator.ts index a0b1ec4..ba83af5 100644 --- a/lib/decorators/listeners/use.decorator.ts +++ b/lib/decorators/listeners/use.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; +import { createUpdateListenerDecorator } from '../../helpers'; /** * Registers a middleware. * * @see https://telegraf.js.org/#/?id=use */ -export const Use = createUpdateDecorator('use'); +export const Use = createUpdateListenerDecorator('use'); diff --git a/lib/decorators/scene/scene-enter.decorator.ts b/lib/decorators/scene/scene-enter.decorator.ts index 7307e48..914b2d7 100644 --- a/lib/decorators/scene/scene-enter.decorator.ts +++ b/lib/decorators/scene/scene-enter.decorator.ts @@ -1,6 +1,3 @@ -import { SetMetadata } from '@nestjs/common'; -import { SCENE_LISTENER_METADATA } from '../../telegraf.constants'; -import { SceneEventType } from '../../enums/scene-event-type.enum'; +import { createSceneListenerDecorator } from '../../helpers'; -export const SceneEnter = (): MethodDecorator => - SetMetadata(SCENE_LISTENER_METADATA, SceneEventType.Enter); +export const SceneEnter = createSceneListenerDecorator('enter'); diff --git a/lib/decorators/scene/scene-leave.decorator.ts b/lib/decorators/scene/scene-leave.decorator.ts index 09eb509..19b970e 100644 --- a/lib/decorators/scene/scene-leave.decorator.ts +++ b/lib/decorators/scene/scene-leave.decorator.ts @@ -1,6 +1,3 @@ -import { SetMetadata } from '@nestjs/common'; -import { SCENE_LISTENER_METADATA } from '../../telegraf.constants'; -import { SceneEventType } from '../../enums/scene-event-type.enum'; +import { createSceneListenerDecorator } from '../../helpers'; -export const SceneLeave = (): MethodDecorator => - SetMetadata(SCENE_LISTENER_METADATA, SceneEventType.Leave); +export const SceneLeave = createSceneListenerDecorator('leave'); diff --git a/lib/enums/scene-event-type.enum.ts b/lib/enums/scene-event-type.enum.ts deleted file mode 100644 index ebd37d2..0000000 --- a/lib/enums/scene-event-type.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum SceneEventType { - Enter = 'enter', - Leave = 'leave', -} diff --git a/lib/telegraf-scene.explorer.ts b/lib/explorers/telegraf-scene.explorer.ts similarity index 90% rename from lib/telegraf-scene.explorer.ts rename to lib/explorers/telegraf-scene.explorer.ts index 74baad9..dbf36c6 100644 --- a/lib/telegraf-scene.explorer.ts +++ b/lib/explorers/telegraf-scene.explorer.ts @@ -3,17 +3,21 @@ import { DiscoveryService } from '@nestjs/core'; import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { BaseScene as Scene, Stage, Telegraf } from 'telegraf'; -import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor'; +import { TelegrafMetadataAccessor } from '../telegraf.metadata-accessor'; @Injectable() export class TelegrafSceneExplorer implements OnModuleInit { + private readonly stage = new Stage(); + constructor( @Inject(Telegraf) private readonly telegraf: Telegraf, private readonly discoveryService: DiscoveryService, private readonly metadataAccessor: TelegrafMetadataAccessor, private readonly metadataScanner: MetadataScanner, - ) {} + ) { + this.telegraf.use(this.stage.middleware()); + } onModuleInit(): void { this.explore(); @@ -21,7 +25,6 @@ export class TelegrafSceneExplorer implements OnModuleInit { private explore(): void { const sceneClasses = this.filterSceneClasses(); - const stage = new Stage(); sceneClasses.forEach((wrapper) => { const { instance } = wrapper; @@ -30,7 +33,7 @@ export class TelegrafSceneExplorer implements OnModuleInit { instance.constructor, ); const scene = new Scene(sceneId); - stage.register(scene); + this.stage.register(scene); const prototype = Object.getPrototypeOf(instance); this.metadataScanner.scanFromPrototype( @@ -39,11 +42,7 @@ export class TelegrafSceneExplorer implements OnModuleInit { (methodKey: string) => this.registerIfListener(scene, instance, methodKey), ); - - stage.register(scene); }); - - this.telegraf.use(stage.middleware()); } private filterSceneClasses(): InstanceWrapper[] { diff --git a/lib/telegraf-update.explorer.ts b/lib/explorers/telegraf-update.explorer.ts similarity index 96% rename from lib/telegraf-update.explorer.ts rename to lib/explorers/telegraf-update.explorer.ts index 4e4640c..eac2625 100644 --- a/lib/telegraf-update.explorer.ts +++ b/lib/explorers/telegraf-update.explorer.ts @@ -3,7 +3,7 @@ import { DiscoveryService } from '@nestjs/core'; import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { Telegraf } from 'telegraf'; -import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor'; +import { TelegrafMetadataAccessor } from '../telegraf.metadata-accessor'; @Injectable() export class TelegrafUpdateExplorer implements OnModuleInit { diff --git a/lib/helpers/create-scene-listener-decorator.helper.ts b/lib/helpers/create-scene-listener-decorator.helper.ts new file mode 100644 index 0000000..c997ffc --- /dev/null +++ b/lib/helpers/create-scene-listener-decorator.helper.ts @@ -0,0 +1,18 @@ +import { SetMetadata } from '@nestjs/common'; +import { BaseScene as Scene } from 'telegraf'; +import { ComposerMethodArgs, SceneMethods } from '../telegraf.types'; +import { UPDATE_LISTENER_METADATA } from '../telegraf.constants'; +import { ListenerMetadata } from '../interfaces'; + +export function createSceneListenerDecorator( + method: Method, +) { + return ( + ...args: ComposerMethodArgs, Method> + ): MethodDecorator => { + return SetMetadata(UPDATE_LISTENER_METADATA, { + method, + args, + } as ListenerMetadata); + }; +} diff --git a/lib/helpers/create-update-decorator.helper.ts b/lib/helpers/create-update-listener-decorator.helper.ts similarity index 51% rename from lib/helpers/create-update-decorator.helper.ts rename to lib/helpers/create-update-listener-decorator.helper.ts index 887a552..6c28eb2 100644 --- a/lib/helpers/create-update-decorator.helper.ts +++ b/lib/helpers/create-update-listener-decorator.helper.ts @@ -1,12 +1,15 @@ import { SetMetadata } from '@nestjs/common'; -import { UpdateMethodArgs, UpdateMethods } from '../telegraf.types'; +import { Composer } from 'telegraf'; +import { ComposerMethodArgs, UpdateMethods } from '../telegraf.types'; import { UPDATE_LISTENER_METADATA } from '../telegraf.constants'; import { ListenerMetadata } from '../interfaces'; -export function createUpdateDecorator( +export function createUpdateListenerDecorator( method: Method, ) { - return (...args: UpdateMethodArgs): MethodDecorator => { + return ( + ...args: ComposerMethodArgs, Method> + ): MethodDecorator => { return SetMetadata(UPDATE_LISTENER_METADATA, { method, args, diff --git a/lib/helpers/index.ts b/lib/helpers/index.ts index 4a414bd..20b6077 100644 --- a/lib/helpers/index.ts +++ b/lib/helpers/index.ts @@ -1 +1,2 @@ -export * from './create-update-decorator.helper'; +export * from './create-update-listener-decorator.helper'; +export * from './create-scene-listener-decorator.helper'; diff --git a/lib/interfaces/listener-metadata.interface.ts b/lib/interfaces/listener-metadata.interface.ts index b4f7f5c..9dc4925 100644 --- a/lib/interfaces/listener-metadata.interface.ts +++ b/lib/interfaces/listener-metadata.interface.ts @@ -1,6 +1,4 @@ -import { UpdateMethods } from '../telegraf.types'; - export interface ListenerMetadata { - method: UpdateMethods; + method: string; args: unknown[]; } diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 9f7dc6d..417d5bf 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -1,10 +1,12 @@ import { ModuleMetadata, Type } from '@nestjs/common/interfaces'; +import { Middleware, Context } from 'telegraf'; import { TelegrafLaunchOption, TelegrafOption } from '../telegraf.types'; -export interface TelegrafModuleOptions { +export interface TelegrafModuleOptions { token: string; options?: TelegrafOption; launchOptions?: TelegrafLaunchOption; + middlewares?: Middleware[]; } export interface TelegrafOptionsFactory { diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index adb1268..1790594 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -4,4 +4,3 @@ export const UPDATE_METADATA = 'UPDATE_METADATA'; export const UPDATE_LISTENER_METADATA = 'UPDATE_LISTENER_METADATA'; export const SCENE_METADATA = 'SCENE_METADATA'; -export const SCENE_LISTENER_METADATA = 'SCENE_LISTENER_METADATA'; diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index f40ca9e..4e9f958 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -15,8 +15,8 @@ import { } from './interfaces'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor'; -import { TelegrafUpdateExplorer } from './telegraf-update.explorer'; -import { TelegrafSceneExplorer } from './telegraf-scene.explorer'; +import { TelegrafUpdateExplorer } from './explorers/telegraf-update.explorer'; +import { TelegrafSceneExplorer } from './explorers/telegraf-scene.explorer'; import { createProviders, TelegrafProvider } from './telegraf.providers'; @Module({ diff --git a/lib/telegraf.providers.ts b/lib/telegraf.providers.ts index 7c708c4..399efe7 100644 --- a/lib/telegraf.providers.ts +++ b/lib/telegraf.providers.ts @@ -5,11 +5,12 @@ import { TelegrafModuleOptions } from './interfaces'; export const TelegrafProvider = { provide: Telegraf, - inject: [TELEGRAF_MODULE_OPTIONS], useFactory: (options: TelegrafModuleOptions) => { const telegraf = new Telegraf(options.token, options.options); + telegraf.use(...options.middlewares); return telegraf; }, + inject: [TELEGRAF_MODULE_OPTIONS], }; export function createProviders(options: TelegrafModuleOptions): Provider[] { diff --git a/lib/telegraf.types.ts b/lib/telegraf.types.ts index 6f2d6bb..c319853 100644 --- a/lib/telegraf.types.ts +++ b/lib/telegraf.types.ts @@ -1,4 +1,4 @@ -import { Composer, Middleware, Telegraf } from 'telegraf'; +import { Composer, Middleware, BaseScene, Telegraf } from 'telegraf'; export type Filter = T extends [] ? [] @@ -8,14 +8,22 @@ export type Filter = T extends [] : [Head, ...Filter] : []; -export type UpdateMethods = Exclude< - keyof Composer, - 'middleware' | 'guard' | 'filter' | 'drop' ->; -export type UpdateMethodArgs = Filter< - Parameters[T]>, - Middleware ->; +export type OnlyFunctionPropertyNames = { + [K in keyof T]: T[K] extends (...args: any) => any ? K : never; +}[keyof T]; + +// export type FilterComposerMethods = Exclude< +// T, +// 'middleware' | 'guard' | 'filter' | 'drop' +// >; + +export type ComposerMethodArgs< + T extends Composer, + U extends OnlyFunctionPropertyNames = OnlyFunctionPropertyNames +> = Filter, Middleware>; + +export type UpdateMethods = OnlyFunctionPropertyNames>; +export type SceneMethods = OnlyFunctionPropertyNames>; export type TelegrafOption = ConstructorParameters[1]; export type TelegrafLaunchOption = Parameters[0]; diff --git a/sample/app.module.ts b/sample/app.module.ts index 8d4f95d..2d177de 100644 --- a/sample/app.module.ts +++ b/sample/app.module.ts @@ -3,11 +3,13 @@ import { TelegrafModule } from '../lib'; import { EchoService } from './echo.service'; import { AppUpdate } from './app.update'; import { HelloScene } from './scenes/hello.scene'; +import { sessionMiddleware } from './middleware/session.middleware'; @Module({ imports: [ TelegrafModule.forRoot({ token: '1467731595:AAHCvH65H9VQYKF9jE-E8c2rXsQBVAYseg8', // Don't steal >:( + middlewares: [sessionMiddleware], }), ], providers: [EchoService, AppUpdate, HelloScene], diff --git a/sample/middleware/session.middleware.ts b/sample/middleware/session.middleware.ts new file mode 100644 index 0000000..c203f15 --- /dev/null +++ b/sample/middleware/session.middleware.ts @@ -0,0 +1,3 @@ +import { session } from 'telegraf'; + +export const sessionMiddleware = session(); diff --git a/sample/scenes/hello.scene.ts b/sample/scenes/hello.scene.ts index beab49a..f84a6f4 100644 --- a/sample/scenes/hello.scene.ts +++ b/sample/scenes/hello.scene.ts @@ -11,7 +11,7 @@ export class HelloScene { } @SceneLeave() - async onSceneLeave(): Promise { + async onSceneLeave(ctx: Context): Promise { console.log('Leave from scene'); await ctx.reply('Bye Bye 👋'); }