From b3dc258c70150c79b7d49d1362d2bc9fba365ae9 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 27 Dec 2020 21:35:01 +0300 Subject: [PATCH] !feat(): use dynamic types for listener decorators --- lib/decorators/listeners/action.decorator.ts | 15 +------ lib/decorators/listeners/cashtag.decorator.ts | 15 +------ lib/decorators/listeners/command.decorator.ts | 15 +------ lib/decorators/listeners/email.decorator.ts | 15 +------ .../listeners/game-query.decorator.ts | 8 +--- lib/decorators/listeners/hashtag.decorator.ts | 15 +------ lib/decorators/listeners/hears.decorator.ts | 15 +------ lib/decorators/listeners/help.decorator.ts | 8 +--- .../listeners/inline-query.decorator.ts | 17 +------- lib/decorators/listeners/mention.decorator.ts | 15 +------ lib/decorators/listeners/on.decorator.ts | 15 +------ lib/decorators/listeners/phone.decorator.ts | 15 +------ .../listeners/settings.decorator.ts | 8 +--- lib/decorators/listeners/start.decorator.ts | 8 +--- .../listeners/text-link.decorator.ts | 15 +------ .../listeners/text-mention.decorator.ts | 15 +------ lib/decorators/listeners/url.decorator.ts | 15 +------ lib/decorators/listeners/use.decorator.ts | 8 +--- lib/enums/index.ts | 1 - lib/enums/listener-menthod.enum.ts | 20 --------- lib/enums/update-paramtypes.enum.ts | 5 --- lib/helpers/create-update-decorator.helper.ts | 15 +++++++ lib/interfaces/listener-metadata.interface.ts | 6 +++ lib/telegraf.constants.ts | 7 ++-- lib/telegraf.explorer.ts | 17 +++++--- lib/telegraf.metadata-accessor.ts | 13 ++---- lib/telegraf.provider.ts | 4 +- lib/telegraf.types.ts | 42 +++++++++---------- 28 files changed, 97 insertions(+), 270 deletions(-) delete mode 100644 lib/enums/index.ts delete mode 100644 lib/enums/listener-menthod.enum.ts delete mode 100644 lib/enums/update-paramtypes.enum.ts create mode 100644 lib/helpers/create-update-decorator.helper.ts create mode 100644 lib/interfaces/listener-metadata.interface.ts diff --git a/lib/decorators/listeners/action.decorator.ts b/lib/decorators/listeners/action.decorator.ts index 4fae6e7..6a4c2ac 100644 --- a/lib/decorators/listeners/action.decorator.ts +++ b/lib/decorators/listeners/action.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafActionTriggers } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling callback_data actions with regular expressions. * * @see https://telegraf.js.org/#/?id=action */ -export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Action), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]), - ); -}; +export const Action = createUpdateDecorator('action'); diff --git a/lib/decorators/listeners/cashtag.decorator.ts b/lib/decorators/listeners/cashtag.decorator.ts index a43dbcc..ab48729 100644 --- a/lib/decorators/listeners/cashtag.decorator.ts +++ b/lib/decorators/listeners/cashtag.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafCashtag } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Cashtag handling. * * @see https://telegraf.js.org/#/?id=cashtag */ -export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Cashtag), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [cashtag]), - ); -}; +export const Cashtag = createUpdateDecorator('cashtag'); diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts index 6c668d6..ec6e958 100644 --- a/lib/decorators/listeners/command.decorator.ts +++ b/lib/decorators/listeners/command.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafCommand } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Command handling. * * @see https://telegraf.js.org/#/?id=command */ -export const Command = (command: TelegrafCommand): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Command), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [command]), - ); -}; +export const Command = createUpdateDecorator('command'); diff --git a/lib/decorators/listeners/email.decorator.ts b/lib/decorators/listeners/email.decorator.ts index 5658bdb..2364d71 100644 --- a/lib/decorators/listeners/email.decorator.ts +++ b/lib/decorators/listeners/email.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafEmail } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling messages with email entity. * * @see https://telegraf.js.org/#/?id=telegraf-email */ -export const Email = (email: TelegrafEmail): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Email), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [email]), - ); -}; +export const Email = createUpdateDecorator('email'); diff --git a/lib/decorators/listeners/game-query.decorator.ts b/lib/decorators/listeners/game-query.decorator.ts index 525791c..3b8c227 100644 --- a/lib/decorators/listeners/game-query.decorator.ts +++ b/lib/decorators/listeners/game-query.decorator.ts @@ -1,12 +1,8 @@ -import { SetMetadata } from '@nestjs/common'; -import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling callback_data actions with game query. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const GameQuery = (): MethodDecorator => { - return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.GameQuery); -}; +export const GameQuery = createUpdateDecorator('gameQuery'); diff --git a/lib/decorators/listeners/hashtag.decorator.ts b/lib/decorators/listeners/hashtag.decorator.ts index 012e723..3c7514a 100644 --- a/lib/decorators/listeners/hashtag.decorator.ts +++ b/lib/decorators/listeners/hashtag.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafHashtag } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Hashtag handling. * * @see https://telegraf.js.org/#/?id=hashtag */ -export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hashtag), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [hashtag]), - ); -}; +export const Hashtag = createUpdateDecorator('hashtag'); diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts index 8c2bfd2..b8f19a9 100644 --- a/lib/decorators/listeners/hears.decorator.ts +++ b/lib/decorators/listeners/hears.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafHearsTriggers } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling text messages. * * @see https://telegraf.js.org/#/?id=hears */ -export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hears), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]), - ); -}; +export const Hears = createUpdateDecorator('hears'); diff --git a/lib/decorators/listeners/help.decorator.ts b/lib/decorators/listeners/help.decorator.ts index c7a17fb..599d646 100644 --- a/lib/decorators/listeners/help.decorator.ts +++ b/lib/decorators/listeners/help.decorator.ts @@ -1,12 +1,8 @@ -import { SetMetadata } from '@nestjs/common'; -import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Handler for /help command. * * @see https://telegraf.js.org/#/?id=help */ -export const Help = (): MethodDecorator => { - return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Help); -}; +export const Help = createUpdateDecorator('help'); diff --git a/lib/decorators/listeners/inline-query.decorator.ts b/lib/decorators/listeners/inline-query.decorator.ts index 641af40..abbdd39 100644 --- a/lib/decorators/listeners/inline-query.decorator.ts +++ b/lib/decorators/listeners/inline-query.decorator.ts @@ -1,21 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafInlineQueryTriggers } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling inline_query actions with regular expressions. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const InlineQuery = ( - triggers: TelegrafInlineQueryTriggers, -): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.InlineQuery), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]), - ); -}; +export const InlineQuery = createUpdateDecorator('inlineQuery'); diff --git a/lib/decorators/listeners/mention.decorator.ts b/lib/decorators/listeners/mention.decorator.ts index 9eaeaee..d6be512 100644 --- a/lib/decorators/listeners/mention.decorator.ts +++ b/lib/decorators/listeners/mention.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafMention } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Mention handling. * * @see https://telegraf.js.org/#/?id=mention */ -export const Mention = (mention: TelegrafMention): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Mention), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]), - ); -}; +export const Mention = createUpdateDecorator('mention'); diff --git a/lib/decorators/listeners/on.decorator.ts b/lib/decorators/listeners/on.decorator.ts index c467e51..a6f717c 100644 --- a/lib/decorators/listeners/on.decorator.ts +++ b/lib/decorators/listeners/on.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafUpdateType } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for provided update type. * * @see https://telegraf.js.org/#/?id=on */ -export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.On), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [updateTypes]), - ); -}; +export const On = createUpdateDecorator('on'); diff --git a/lib/decorators/listeners/phone.decorator.ts b/lib/decorators/listeners/phone.decorator.ts index b217804..8feb92f 100644 --- a/lib/decorators/listeners/phone.decorator.ts +++ b/lib/decorators/listeners/phone.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafPhone } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Phone number handling. * * @see https://telegraf.js.org/#/?id=phone */ -export const Phone = (phone: TelegrafPhone): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Phone), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [phone]), - ); -}; +export const Phone = createUpdateDecorator('phone'); diff --git a/lib/decorators/listeners/settings.decorator.ts b/lib/decorators/listeners/settings.decorator.ts index d53048c..8f705f9 100644 --- a/lib/decorators/listeners/settings.decorator.ts +++ b/lib/decorators/listeners/settings.decorator.ts @@ -1,12 +1,8 @@ -import { SetMetadata } from '@nestjs/common'; -import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Handler for /settings command. * * @see https://telegraf.js.org/#/?id=settings */ -export const Settings = (): MethodDecorator => { - return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Settings); -}; +export const Settings = createUpdateDecorator('settings'); diff --git a/lib/decorators/listeners/start.decorator.ts b/lib/decorators/listeners/start.decorator.ts index ec13aa0..6a084f0 100644 --- a/lib/decorators/listeners/start.decorator.ts +++ b/lib/decorators/listeners/start.decorator.ts @@ -1,12 +1,8 @@ -import { SetMetadata } from '@nestjs/common'; -import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Handler for /start command. * * @see https://telegraf.js.org/#/?id=start */ -export const Start = (): MethodDecorator => { - return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Start); -}; +export const Start = createUpdateDecorator('start'); diff --git a/lib/decorators/listeners/text-link.decorator.ts b/lib/decorators/listeners/text-link.decorator.ts index 9f7fa44..0e91ad7 100644 --- a/lib/decorators/listeners/text-link.decorator.ts +++ b/lib/decorators/listeners/text-link.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafTextLink } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling messages with text_link entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextLink = (link: TelegrafTextLink): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextLink), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [link]), - ); -}; +export const TextLink = createUpdateDecorator('textLink'); diff --git a/lib/decorators/listeners/text-mention.decorator.ts b/lib/decorators/listeners/text-mention.decorator.ts index 0791ab6..ebd30e3 100644 --- a/lib/decorators/listeners/text-mention.decorator.ts +++ b/lib/decorators/listeners/text-mention.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafTextMention } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling messages with text_mention entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextMention = (mention: TelegrafTextMention): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextMention), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]), - ); -}; +export const TextMention = createUpdateDecorator('textMention'); diff --git a/lib/decorators/listeners/url.decorator.ts b/lib/decorators/listeners/url.decorator.ts index 04f9704..36653fe 100644 --- a/lib/decorators/listeners/url.decorator.ts +++ b/lib/decorators/listeners/url.decorator.ts @@ -1,19 +1,8 @@ -import { applyDecorators, SetMetadata } from '@nestjs/common'; -import { - UPDATE_LISTENER_OPTIONS_METADATA, - UPDATE_LISTENER_METHOD_METADATA, -} from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; -import { TelegrafUrl } from '../../telegraf.types'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers middleware for handling messages with url entity. * * @see https://telegraf.js.org/#/?id=telegraf-url */ -export const Url = (url: TelegrafUrl): MethodDecorator => { - return applyDecorators( - SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Url), - SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [url]), - ); -}; +export const Url = createUpdateDecorator('url'); diff --git a/lib/decorators/listeners/use.decorator.ts b/lib/decorators/listeners/use.decorator.ts index 84c7d2c..a0b1ec4 100644 --- a/lib/decorators/listeners/use.decorator.ts +++ b/lib/decorators/listeners/use.decorator.ts @@ -1,12 +1,8 @@ -import { SetMetadata } from '@nestjs/common'; -import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants'; -import { ListenerMethod } from '../../enums'; +import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper'; /** * Registers a middleware. * * @see https://telegraf.js.org/#/?id=use */ -export const Use = (): MethodDecorator => { - return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Use); -}; +export const Use = createUpdateDecorator('use'); diff --git a/lib/enums/index.ts b/lib/enums/index.ts deleted file mode 100644 index 5f145a0..0000000 --- a/lib/enums/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './listener-menthod.enum'; diff --git a/lib/enums/listener-menthod.enum.ts b/lib/enums/listener-menthod.enum.ts deleted file mode 100644 index 05a55eb..0000000 --- a/lib/enums/listener-menthod.enum.ts +++ /dev/null @@ -1,20 +0,0 @@ -export enum ListenerMethod { - Use = 'use', - On = 'on', - Hears = 'hears', - Command = 'command', - Start = 'start', - Help = 'help', - Settings = 'settings', - Mention = 'mention', - Phone = 'phone', - Hashtag = 'hashtag', - Cashtag = 'cashtag', - Email = 'email', - Url = 'url', - TextLink = 'textLink', - TextMention = 'textMention', - Action = 'action', - InlineQuery = 'inlineQuery', - GameQuery = 'gameQuery', -} diff --git a/lib/enums/update-paramtypes.enum.ts b/lib/enums/update-paramtypes.enum.ts deleted file mode 100644 index e5a3303..0000000 --- a/lib/enums/update-paramtypes.enum.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum UpdateParamtypes { - CONTEXT, - NEXT, - MESSAGE, -} diff --git a/lib/helpers/create-update-decorator.helper.ts b/lib/helpers/create-update-decorator.helper.ts new file mode 100644 index 0000000..3db5787 --- /dev/null +++ b/lib/helpers/create-update-decorator.helper.ts @@ -0,0 +1,15 @@ +import { SetMetadata } from '@nestjs/common'; +import { UpdateMethodArgs, UpdateMethods } from '../telegraf.types'; +import { UPDATE_LISTENER_METADATA } from '../telegraf.constants'; +import { ListenerMetadata } from '../interfaces/listener-metadata.interface'; + +export function createUpdateDecorator( + method: Method, +) { + return (...args: UpdateMethodArgs): MethodDecorator => { + return SetMetadata(UPDATE_LISTENER_METADATA, { + method, + args, + } as ListenerMetadata); + }; +} diff --git a/lib/interfaces/listener-metadata.interface.ts b/lib/interfaces/listener-metadata.interface.ts new file mode 100644 index 0000000..b4f7f5c --- /dev/null +++ b/lib/interfaces/listener-metadata.interface.ts @@ -0,0 +1,6 @@ +import { UpdateMethods } from '../telegraf.types'; + +export interface ListenerMetadata { + method: UpdateMethods; + args: unknown[]; +} diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 2b61bcc..1790594 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,7 +1,6 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; export const UPDATE_METADATA = 'UPDATE_METADATA'; -export const UPDATE_LISTENER_METHOD_METADATA = - 'UPDATE_LISTENER_METHOD_METADATA'; -export const UPDATE_LISTENER_OPTIONS_METADATA = - 'UPDATE_LISTENER_OPTIONS_METADATA'; +export const UPDATE_LISTENER_METADATA = 'UPDATE_LISTENER_METADATA'; + +export const SCENE_METADATA = 'SCENE_METADATA'; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 6d9f427..f52d601 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -1,9 +1,10 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { DiscoveryService } from '@nestjs/core'; import { MetadataScanner } from '@nestjs/core/metadata-scanner'; +import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; +import { Composer } from 'telegraf'; import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor'; import { TelegrafProvider } from './telegraf.provider'; -import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; @Injectable() export class TelegrafExplorer implements OnModuleInit { @@ -49,12 +50,16 @@ export class TelegrafExplorer implements OnModuleInit { const methodRef = instance[methodKey]; const middlewareFn = methodRef.bind(instance); - const listenerMethod = this.metadataAccessor.getListenerMethod(methodRef); - if (!listenerMethod) return; + const listenerMetadata = this.metadataAccessor.getListenerMetadata( + methodRef, + ); + if (!listenerMetadata) return; - const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef); + const { method, args } = listenerMetadata; + const composerMiddlewareFn = Composer[method](...args, middlewareFn); - // NOTE: Disable spread operator checking because of error: "Expected at least 1 arguments, but got 1 or more." - (this.telegraf as any)[listenerMethod](...listenerOptions, middlewareFn); + console.log('composerMiddlewareFn', composerMiddlewareFn); + + this.telegraf.use(composerMiddlewareFn); } } diff --git a/lib/telegraf.metadata-accessor.ts b/lib/telegraf.metadata-accessor.ts index bb86898..c2ec1ef 100644 --- a/lib/telegraf.metadata-accessor.ts +++ b/lib/telegraf.metadata-accessor.ts @@ -1,11 +1,10 @@ import { Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { - UPDATE_LISTENER_METHOD_METADATA, + UPDATE_LISTENER_METADATA, UPDATE_METADATA, - UPDATE_LISTENER_OPTIONS_METADATA, } from './telegraf.constants'; -import { ListenerMethod } from './enums'; +import { ListenerMetadata } from './interfaces/listener-metadata.interface'; @Injectable() export class TelegrafMetadataAccessor { @@ -15,11 +14,7 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(UPDATE_METADATA, target); } - getListenerMethod(target: Function): ListenerMethod | undefined { - return this.reflector.get(UPDATE_LISTENER_METHOD_METADATA, target); - } - - getListenerOptions(target: Function): unknown[] { - return this.reflector.get(UPDATE_LISTENER_OPTIONS_METADATA, target) || []; + getListenerMetadata(target: Function): ListenerMetadata | undefined { + return this.reflector.get(UPDATE_LISTENER_METADATA, target); } } diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index f6bfb2f..0602704 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -10,8 +10,8 @@ import { Context, TelegrafModuleOptions } from './interfaces'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; @Injectable() -export class TelegrafProvider - extends Telegraf +export class TelegrafProvider + extends Telegraf implements OnApplicationBootstrap, OnApplicationShutdown { private logger = new Logger('Telegraf'); private readonly launchOptions; diff --git a/lib/telegraf.types.ts b/lib/telegraf.types.ts index 9b50ff3..fbb6737 100644 --- a/lib/telegraf.types.ts +++ b/lib/telegraf.types.ts @@ -1,26 +1,24 @@ -import { Type } from '@nestjs/common/interfaces/type.interface'; -import { Composer, Telegraf } from 'telegraf'; -import { Context } from './interfaces'; +import { Composer, Middleware, Telegraf } from 'telegraf'; -type CtxComposer = Composer; +export type Filter = T extends [] + ? [] + : T extends [infer Head, ...infer Tail] + ? Head extends F + ? Filter + : [Head, ...Filter] + : []; -type ComposerMethodFirstArg = Parameters< - CtxComposer[T] ->[0]; +export type UpdateMethods = Exclude< + keyof Composer, + 'middleware' | 'guard' | 'filter' | 'drop' +>; +export type UpdateMethodArgs = Filter< + Parameters[T]>, + Middleware +>; +// type Test0 = Filter<[['foo', 'bar', 'booz'], ...Middleware[]], Middleware>; +// type Test1 = UpdateMethodArgs<'on'>; +// type Test2 = Parameters['on']>; -export type TelegrafActionTriggers = ComposerMethodFirstArg<'action'>; -export type TelegrafHearsTriggers = ComposerMethodFirstArg<'hears'>; -export type TelegrafInlineQueryTriggers = ComposerMethodFirstArg<'inlineQuery'>; -export type TelegrafEmail = ComposerMethodFirstArg<'email'>; -export type TelegrafUrl = ComposerMethodFirstArg<'url'>; -export type TelegrafTextLink = ComposerMethodFirstArg<'textLink'>; -export type TelegrafTextMention = ComposerMethodFirstArg<'textMention'>; -export type TelegrafCashtag = ComposerMethodFirstArg<'cashtag'>; -export type TelegrafHashtag = ComposerMethodFirstArg<'hashtag'>; -export type TelegrafCommand = ComposerMethodFirstArg<'command'>; -export type TelegrafMention = ComposerMethodFirstArg<'mention'>; -export type TelegrafPhone = ComposerMethodFirstArg<'phone'>; -export type TelegrafUpdateType = ComposerMethodFirstArg<'on'>; - -export type TelegrafOption = ConstructorParameters>>[1]; +export type TelegrafOption = ConstructorParameters[1]; export type TelegrafLaunchOption = Parameters[0];