From f13eebe685e1fac749641ba39cb44d4d748c6a2e Mon Sep 17 00:00:00 2001 From: Morb0 Date: Sun, 3 Jan 2021 16:11:17 +0300 Subject: [PATCH] fix(): missing decorators & 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 | 5 ++-- 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 | 4 +-- lib/decorators/scene/scene-leave.decorator.ts | 4 +-- lib/index.ts | 13 ++-------- lib/interfaces/index.ts | 1 - lib/services/base-explorer.service.ts | 26 +++---------------- lib/services/index.ts | 5 ++-- lib/services/listeners-explorer.service.ts | 2 +- lib/services/metadata-accessor.service.ts | 7 ----- lib/telegraf-core.module.ts | 9 +++---- lib/types/index.ts | 2 +- lib/utils/create-listener-decorator.util.ts | 19 +++++++++++--- lib/utils/index.ts | 2 ++ 30 files changed, 71 insertions(+), 96 deletions(-) diff --git a/lib/decorators/listeners/action.decorator.ts b/lib/decorators/listeners/action.decorator.ts index 4b14901..1834990 100644 --- a/lib/decorators/listeners/action.decorator.ts +++ b/lib/decorators/listeners/action.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Registers middleware for handling callback_data actions with regular expressions. * * @see https://telegraf.js.org/#/?id=action */ -export const Action = createUpdateListenerDecorator('action'); +export const Action = createListenerDecorator('action'); diff --git a/lib/decorators/listeners/cashtag.decorator.ts b/lib/decorators/listeners/cashtag.decorator.ts index c3e37e9..2169873 100644 --- a/lib/decorators/listeners/cashtag.decorator.ts +++ b/lib/decorators/listeners/cashtag.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Cashtag handling. * * @see https://telegraf.js.org/#/?id=cashtag */ -export const Cashtag = createUpdateListenerDecorator('cashtag'); +export const Cashtag = createMissedListenerDecorator<[string | string[]]>('cashtag'); diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts index 0e7806e..b593f65 100644 --- a/lib/decorators/listeners/command.decorator.ts +++ b/lib/decorators/listeners/command.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Command handling. * * @see https://telegraf.js.org/#/?id=command */ -export const Command = createUpdateListenerDecorator('command'); +export const Command = createListenerDecorator('command'); diff --git a/lib/decorators/listeners/email.decorator.ts b/lib/decorators/listeners/email.decorator.ts index e9bb1a6..bf7b024 100644 --- a/lib/decorators/listeners/email.decorator.ts +++ b/lib/decorators/listeners/email.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Registers middleware for handling messages with email entity. * * @see https://telegraf.js.org/#/?id=telegraf-email */ -export const Email = createUpdateListenerDecorator('email'); +export const Email = createMissedListenerDecorator<[string | string[]]>('email'); diff --git a/lib/decorators/listeners/game-query.decorator.ts b/lib/decorators/listeners/game-query.decorator.ts index 347f998..7e639d6 100644 --- a/lib/decorators/listeners/game-query.decorator.ts +++ b/lib/decorators/listeners/game-query.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Registers middleware for handling callback_data actions with game query. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const GameQuery = createUpdateListenerDecorator('gameQuery'); +export const GameQuery = createListenerDecorator('gameQuery'); diff --git a/lib/decorators/listeners/hashtag.decorator.ts b/lib/decorators/listeners/hashtag.decorator.ts index 86e9e13..266cd8d 100644 --- a/lib/decorators/listeners/hashtag.decorator.ts +++ b/lib/decorators/listeners/hashtag.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Hashtag handling. * * @see https://telegraf.js.org/#/?id=hashtag */ -export const Hashtag = createUpdateListenerDecorator('hashtag'); +export const Hashtag = createMissedListenerDecorator<[string | string[]]>('hashtag'); diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts index 79d71af..509bf93 100644 --- a/lib/decorators/listeners/hears.decorator.ts +++ b/lib/decorators/listeners/hears.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Registers middleware for handling text messages. * * @see https://telegraf.js.org/#/?id=hears */ -export const Hears = createUpdateListenerDecorator('hears'); +export const Hears = createListenerDecorator('hears'); diff --git a/lib/decorators/listeners/help.decorator.ts b/lib/decorators/listeners/help.decorator.ts index 36ac9e4..97c29b0 100644 --- a/lib/decorators/listeners/help.decorator.ts +++ b/lib/decorators/listeners/help.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Handler for /help command. * * @see https://telegraf.js.org/#/?id=help */ -export const Help = createUpdateListenerDecorator('help'); +export const Help = createListenerDecorator('help'); diff --git a/lib/decorators/listeners/inline-query.decorator.ts b/lib/decorators/listeners/inline-query.decorator.ts index 722a348..30c15c1 100644 --- a/lib/decorators/listeners/inline-query.decorator.ts +++ b/lib/decorators/listeners/inline-query.decorator.ts @@ -1,8 +1,9 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; +import { HearsTriggers } from 'telegraf/typings/composer'; /** * Registers middleware for handling inline_query actions with regular expressions. * * @see https://telegraf.js.org/#/?id=inlinequery */ -export const InlineQuery = createUpdateListenerDecorator('inlineQuery'); +export const InlineQuery = createMissedListenerDecorator<[HearsTriggers]>('inlineQuery'); diff --git a/lib/decorators/listeners/mention.decorator.ts b/lib/decorators/listeners/mention.decorator.ts index e4341a0..0f2391a 100644 --- a/lib/decorators/listeners/mention.decorator.ts +++ b/lib/decorators/listeners/mention.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Mention handling. * * @see https://telegraf.js.org/#/?id=mention */ -export const Mention = createUpdateListenerDecorator('mention'); +export const Mention = createMissedListenerDecorator<[string | string[]]>('mention'); diff --git a/lib/decorators/listeners/on.decorator.ts b/lib/decorators/listeners/on.decorator.ts index 9f3c0fd..28724d4 100644 --- a/lib/decorators/listeners/on.decorator.ts +++ b/lib/decorators/listeners/on.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Registers middleware for provided update type. * * @see https://telegraf.js.org/#/?id=on */ -export const On = createUpdateListenerDecorator('on'); +export const On = createListenerDecorator('on'); diff --git a/lib/decorators/listeners/phone.decorator.ts b/lib/decorators/listeners/phone.decorator.ts index 0f68380..9dc2a83 100644 --- a/lib/decorators/listeners/phone.decorator.ts +++ b/lib/decorators/listeners/phone.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Phone number handling. * * @see https://telegraf.js.org/#/?id=phone */ -export const Phone = createUpdateListenerDecorator('phone'); +export const Phone = createMissedListenerDecorator<[string | string[]]>('phone'); diff --git a/lib/decorators/listeners/settings.decorator.ts b/lib/decorators/listeners/settings.decorator.ts index 6cca7c7..ed0ef8e 100644 --- a/lib/decorators/listeners/settings.decorator.ts +++ b/lib/decorators/listeners/settings.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Handler for /settings command. * * @see https://telegraf.js.org/#/?id=settings */ -export const Settings = createUpdateListenerDecorator('settings'); +export const Settings = createMissedListenerDecorator<[]>('settings'); diff --git a/lib/decorators/listeners/start.decorator.ts b/lib/decorators/listeners/start.decorator.ts index 48bdc5b..b51460e 100644 --- a/lib/decorators/listeners/start.decorator.ts +++ b/lib/decorators/listeners/start.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Handler for /start command. * * @see https://telegraf.js.org/#/?id=start */ -export const Start = createUpdateListenerDecorator('start'); +export const Start = createListenerDecorator('start'); diff --git a/lib/decorators/listeners/text-link.decorator.ts b/lib/decorators/listeners/text-link.decorator.ts index c62439a..f116efe 100644 --- a/lib/decorators/listeners/text-link.decorator.ts +++ b/lib/decorators/listeners/text-link.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Registers middleware for handling messages with text_link entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextLink = createUpdateListenerDecorator('textLink'); +export const TextLink = createMissedListenerDecorator<[string | string[]]>('textLink'); diff --git a/lib/decorators/listeners/text-mention.decorator.ts b/lib/decorators/listeners/text-mention.decorator.ts index 895a458..320128d 100644 --- a/lib/decorators/listeners/text-mention.decorator.ts +++ b/lib/decorators/listeners/text-mention.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Registers middleware for handling messages with text_mention entity. * * @see https://telegraf.js.org/#/?id=telegraf-textlink */ -export const TextMention = createUpdateListenerDecorator('textMention'); +export const TextMention = createMissedListenerDecorator<[string | string[]]>('textMention'); diff --git a/lib/decorators/listeners/url.decorator.ts b/lib/decorators/listeners/url.decorator.ts index 843aa41..17cfc89 100644 --- a/lib/decorators/listeners/url.decorator.ts +++ b/lib/decorators/listeners/url.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createMissedListenerDecorator } from '../../utils'; /** * Registers middleware for handling messages with url entity. * * @see https://telegraf.js.org/#/?id=telegraf-url */ -export const Url = createUpdateListenerDecorator('url'); +export const Url = createMissedListenerDecorator<[string | string[]]>('url'); diff --git a/lib/decorators/listeners/use.decorator.ts b/lib/decorators/listeners/use.decorator.ts index ba83af5..82bc78e 100644 --- a/lib/decorators/listeners/use.decorator.ts +++ b/lib/decorators/listeners/use.decorator.ts @@ -1,8 +1,8 @@ -import { createUpdateListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; /** * Registers a middleware. * * @see https://telegraf.js.org/#/?id=use */ -export const Use = createUpdateListenerDecorator('use'); +export const Use = createListenerDecorator('use'); diff --git a/lib/decorators/scene/scene-enter.decorator.ts b/lib/decorators/scene/scene-enter.decorator.ts index 914b2d7..96e3cdd 100644 --- a/lib/decorators/scene/scene-enter.decorator.ts +++ b/lib/decorators/scene/scene-enter.decorator.ts @@ -1,3 +1,3 @@ -import { createSceneListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; -export const SceneEnter = createSceneListenerDecorator('enter'); +export const SceneEnter = createListenerDecorator('enter'); diff --git a/lib/decorators/scene/scene-leave.decorator.ts b/lib/decorators/scene/scene-leave.decorator.ts index 19b970e..2440cf7 100644 --- a/lib/decorators/scene/scene-leave.decorator.ts +++ b/lib/decorators/scene/scene-leave.decorator.ts @@ -1,3 +1,3 @@ -import { createSceneListenerDecorator } from '../../helpers'; +import { createListenerDecorator } from '../../utils'; -export const SceneLeave = createSceneListenerDecorator('leave'); +export const SceneLeave = createListenerDecorator('leave'); diff --git a/lib/index.ts b/lib/index.ts index ace41d2..ef6e2ba 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,15 +1,6 @@ -export * as Composer from 'telegraf/composer'; -export * as Markup from 'telegraf/markup'; -export * as BaseScene from 'telegraf/scenes/base'; -export * as session from 'telegraf/session'; -export * as Stage from 'telegraf/stage'; -export * as WizardScene from 'telegraf/scenes/wizard'; -export * as Extra from 'telegraf/extra'; - export * from './decorators'; export * from './interfaces'; -export * from './helpers'; export * from './utils'; -export * from './telegraf.module'; export * from './types'; -export { Telegraf } from 'telegraf'; +export * from './telegraf.constants'; +export * from './telegraf.module'; diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 6be0df9..ef12fd5 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1,3 +1,2 @@ export * from './telegraf-options.interface'; export * from './listener-metadata.interface'; -export * from './update-metadata.interface'; diff --git a/lib/services/base-explorer.service.ts b/lib/services/base-explorer.service.ts index eff7c82..2a87d66 100644 --- a/lib/services/base-explorer.service.ts +++ b/lib/services/base-explorer.service.ts @@ -1,7 +1,6 @@ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { Module } from '@nestjs/core/injector/module'; -import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash'; -import { UpdateMetadata } from '../interfaces'; +import { flattenDeep, identity, isEmpty } from 'lodash'; export class BaseExplorerService { getModules( @@ -21,11 +20,11 @@ export class BaseExplorerService { ): Module[] { const modules = [...modulesContainer.values()]; return modules.filter(({ metatype }) => - include.some((item) => item === metatype), + include.includes(metatype), ); } - flatMap( + flatMap( modules: Module[], callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[], ): T[] { @@ -37,23 +36,4 @@ export class BaseExplorerService { }; return flattenDeep(invokeMap()).filter(identity); } - - groupMetadata(resolvers: UpdateMetadata[]) { - const groupByType = groupBy( - resolvers, - (metadata: UpdateMetadata) => metadata.type, - ); - const groupedMetadata = mapValues( - groupByType, - (resolversArr: UpdateMetadata[]) => - resolversArr.reduce( - (prev, curr) => ({ - ...prev, - [curr.name]: curr.callback, - }), - {}, - ), - ); - return groupedMetadata; - } } diff --git a/lib/services/index.ts b/lib/services/index.ts index 9859475..7660fbc 100644 --- a/lib/services/index.ts +++ b/lib/services/index.ts @@ -1,4 +1,3 @@ -export * from './updates-explorer.service'; +export * from './listeners-explorer.service'; export * from './metadata-accessor.service'; -export * from './scenes-explorer.service'; -export * from './updates-explorer.service'; +export * from './listeners-explorer.service'; diff --git a/lib/services/listeners-explorer.service.ts b/lib/services/listeners-explorer.service.ts index 44a9121..2558334 100644 --- a/lib/services/listeners-explorer.service.ts +++ b/lib/services/listeners-explorer.service.ts @@ -12,7 +12,7 @@ import { BaseExplorerService } from './base-explorer.service'; import { getBotToken } from '../utils'; @Injectable() -export class TelegrafExplorerService +export class ListenersExplorerService extends BaseExplorerService implements OnModuleInit { private readonly bot: Telegraf; diff --git a/lib/services/metadata-accessor.service.ts b/lib/services/metadata-accessor.service.ts index 3e7e044..0695e95 100644 --- a/lib/services/metadata-accessor.service.ts +++ b/lib/services/metadata-accessor.service.ts @@ -18,13 +18,6 @@ export class MetadataAccessorService { return !!this.reflector.get(UPDATE_METADATA, target); } - isUpdateListener(target: Function) { - if (!target) { - return false; - } - return !!this.reflector.get(UPDATE_LISTENER_METADATA, target); - } - isScene(target: Function): boolean { if (!target) { return false; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 2e1cfef..8707c67 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -16,18 +16,15 @@ import { import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; import { MetadataAccessorService, - ScenesExplorerService, - UpdatesExplorerService, + ListenersExplorerService, } from './services'; -import { getBotToken } from './utils'; -import { createBotFactory } from './utils/create-bot-factory.util'; +import { getBotToken, createBotFactory } from './utils'; @Global() @Module({ imports: [DiscoveryModule], providers: [ - UpdatesExplorerService, - ScenesExplorerService, + ListenersExplorerService, MetadataAccessorService, ], }) diff --git a/lib/types/index.ts b/lib/types/index.ts index 4a6dc63..7a39533 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -17,5 +17,5 @@ export type ComposerMethodArgs< U extends OnlyFunctionPropertyNames = OnlyFunctionPropertyNames > = Filter, Middleware>; -export type UpdateMethods = OnlyFunctionPropertyNames>; +export type ComposerMethods = OnlyFunctionPropertyNames>; export type SceneMethods = OnlyFunctionPropertyNames>; diff --git a/lib/utils/create-listener-decorator.util.ts b/lib/utils/create-listener-decorator.util.ts index 82dad7d..88a852c 100644 --- a/lib/utils/create-listener-decorator.util.ts +++ b/lib/utils/create-listener-decorator.util.ts @@ -4,11 +4,24 @@ import { ComposerMethodArgs, SceneMethods } from '../types'; import { UPDATE_LISTENER_METADATA } from '../telegraf.constants'; import { ListenerMetadata } from '../interfaces'; -export function createListenerDecorator( - method: Method, +export function createListenerDecorator( + method: TMethod, ) { return ( - ...args: ComposerMethodArgs, Method> + ...args: ComposerMethodArgs, TMethod> + ): MethodDecorator => { + return SetMetadata(UPDATE_LISTENER_METADATA, { + method, + args, + } as ListenerMetadata); + }; +} + +export function createMissedListenerDecorator( + method: string, +) { + return ( + ...args: TArgs ): MethodDecorator => { return SetMetadata(UPDATE_LISTENER_METADATA, { method, diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 9dc7d1d..29fd7e0 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -1 +1,3 @@ export * from './get-bot-token.util'; +export * from './create-bot-factory.util'; +export * from './create-listener-decorator.util';