From b1a6fc33190feeef847780e45a1e1d1ad2855381 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 23 Dec 2020 21:35:40 +0300 Subject: [PATCH] feat(wip): rework listener decorators --- lib/decorators/action.decorator.ts | 19 ------------- lib/decorators/cashtag.decorator.ts | 17 ----------- lib/decorators/command.decorator.ts | 17 ----------- lib/decorators/{ => core}/update.decorator.ts | 4 +-- lib/decorators/entity.decorator.ts | 22 --------------- lib/decorators/hashtag.decorator.ts | 17 ----------- lib/decorators/hears.decorator.ts | 19 ------------- lib/decorators/help.decorator.ts | 11 -------- lib/decorators/inline-query.decorator.ts | 28 ------------------- lib/decorators/listeners/action.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/cashtag.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/command.decorator.ts | 25 +++++++++++++++++ .../{ => listeners}/game-query.decorator.ts | 5 ++-- lib/decorators/listeners/hashtag.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/hears.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/help.decorator.ts | 12 ++++++++ .../listeners/inline-query.decorator.ts | 27 ++++++++++++++++++ lib/decorators/listeners/mention.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/on.decorator.ts | 25 +++++++++++++++++ lib/decorators/listeners/phone.decorator.ts | 25 +++++++++++++++++ .../listeners/settings.decorator.ts | 12 ++++++++ lib/decorators/listeners/start.decorator.ts | 12 ++++++++ lib/decorators/listeners/use.decorator.ts | 12 ++++++++ lib/decorators/mention.decorator.ts | 17 ----------- lib/decorators/on.decorator.ts | 22 --------------- lib/decorators/phone.decorator.ts | 17 ----------- lib/decorators/settings.decorator.ts | 11 -------- lib/decorators/start.decorator.ts | 11 -------- lib/decorators/use.decorator.ts | 11 -------- lib/enums/listener-type.enum.ts | 16 +++++++++++ lib/telegraf.constants.ts | 23 ++------------- lib/telegraf.provider.ts | 11 ++++---- lib/telegraf.types.ts | 13 +++++++++ 33 files changed, 318 insertions(+), 268 deletions(-) delete mode 100644 lib/decorators/action.decorator.ts delete mode 100644 lib/decorators/cashtag.decorator.ts delete mode 100644 lib/decorators/command.decorator.ts rename lib/decorators/{ => core}/update.decorator.ts (53%) delete mode 100644 lib/decorators/entity.decorator.ts delete mode 100644 lib/decorators/hashtag.decorator.ts delete mode 100644 lib/decorators/hears.decorator.ts delete mode 100644 lib/decorators/help.decorator.ts delete mode 100644 lib/decorators/inline-query.decorator.ts create mode 100644 lib/decorators/listeners/action.decorator.ts create mode 100644 lib/decorators/listeners/cashtag.decorator.ts create mode 100644 lib/decorators/listeners/command.decorator.ts rename lib/decorators/{ => listeners}/game-query.decorator.ts (54%) create mode 100644 lib/decorators/listeners/hashtag.decorator.ts create mode 100644 lib/decorators/listeners/hears.decorator.ts create mode 100644 lib/decorators/listeners/help.decorator.ts create mode 100644 lib/decorators/listeners/inline-query.decorator.ts create mode 100644 lib/decorators/listeners/mention.decorator.ts create mode 100644 lib/decorators/listeners/on.decorator.ts create mode 100644 lib/decorators/listeners/phone.decorator.ts create mode 100644 lib/decorators/listeners/settings.decorator.ts create mode 100644 lib/decorators/listeners/start.decorator.ts create mode 100644 lib/decorators/listeners/use.decorator.ts delete mode 100644 lib/decorators/mention.decorator.ts delete mode 100644 lib/decorators/on.decorator.ts delete mode 100644 lib/decorators/phone.decorator.ts delete mode 100644 lib/decorators/settings.decorator.ts delete mode 100644 lib/decorators/start.decorator.ts delete mode 100644 lib/decorators/use.decorator.ts create mode 100644 lib/enums/listener-type.enum.ts create mode 100644 lib/telegraf.types.ts diff --git a/lib/decorators/action.decorator.ts b/lib/decorators/action.decorator.ts deleted file mode 100644 index e54ace0..0000000 --- a/lib/decorators/action.decorator.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; -import { HearsTriggers } from 'telegraf/typings/composer'; -import { Context } from '../interfaces'; - -export type TelegrafActionTriggers = HearsTriggers; - -export interface ActionOptions { - triggers: TelegrafActionTriggers; -} - -/** - * Registers middleware for handling callback_data actions with regular expressions. - * - * @see https://telegraf.js.org/#/?id=action - */ -export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => { - return SetMetadata(DECORATORS.ACTION, { triggers }); -}; diff --git a/lib/decorators/cashtag.decorator.ts b/lib/decorators/cashtag.decorator.ts deleted file mode 100644 index b98b2da..0000000 --- a/lib/decorators/cashtag.decorator.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafCashtagCashtag = string | string[]; - -export interface CashtagOptions { - cashtag: TelegrafCashtagCashtag; -} - -/** - * Cashtag handling. - * - * @see https://telegraf.js.org/#/?id=cashtag - */ -export const Cashtag = (cashtag: TelegrafCashtagCashtag): MethodDecorator => { - return SetMetadata(DECORATORS.CASHTAG, { cashtag }); -}; diff --git a/lib/decorators/command.decorator.ts b/lib/decorators/command.decorator.ts deleted file mode 100644 index 2fb6e7a..0000000 --- a/lib/decorators/command.decorator.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafCommandCommands = string | string[]; - -export interface CommandOptions { - commands: TelegrafCommandCommands; -} - -/** - * Command handling. - * - * @see https://telegraf.js.org/#/?id=command - */ -export const Command = (commands: TelegrafCommandCommands): MethodDecorator => { - return SetMetadata(DECORATORS.COMMAND, { commands }); -}; diff --git a/lib/decorators/update.decorator.ts b/lib/decorators/core/update.decorator.ts similarity index 53% rename from lib/decorators/update.decorator.ts rename to lib/decorators/core/update.decorator.ts index 1946199..a2ea7f2 100644 --- a/lib/decorators/update.decorator.ts +++ b/lib/decorators/core/update.decorator.ts @@ -1,8 +1,8 @@ import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; +import { TELEGRAF_UPDATE } from '../../telegraf.constants'; /** * `@Update` decorator, it's like NestJS `@Controller` decorator, * but for Telegram Bot API updates. */ -export const Update = (): ClassDecorator => SetMetadata(DECORATORS.UPDATE, {}); +export const Update = (): ClassDecorator => SetMetadata(TELEGRAF_UPDATE, true); diff --git a/lib/decorators/entity.decorator.ts b/lib/decorators/entity.decorator.ts deleted file mode 100644 index 7c16011..0000000 --- a/lib/decorators/entity.decorator.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafEntityEntity = - | string - | string[] - | RegExp - | RegExp[] - | Function; - -export interface EntityOptions { - entity: TelegrafEntityEntity; -} - -/** - * Entity handling. - * - * @see https://telegraf.js.org/#/?id=entity - */ -export const Entity = (entity: TelegrafEntityEntity): MethodDecorator => { - return SetMetadata(DECORATORS.ENTITY, { entity }); -}; diff --git a/lib/decorators/hashtag.decorator.ts b/lib/decorators/hashtag.decorator.ts deleted file mode 100644 index 9269411..0000000 --- a/lib/decorators/hashtag.decorator.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafHashtagHashtag = string | string[]; - -export interface HashtagOptions { - hashtag: TelegrafHashtagHashtag; -} - -/** - * Hashtag handling. - * - * @see https://telegraf.js.org/#/?id=hashtag - */ -export const Hashtag = (hashtag: TelegrafHashtagHashtag): MethodDecorator => { - return SetMetadata(DECORATORS.HASHTAG, { hashtag }); -}; diff --git a/lib/decorators/hears.decorator.ts b/lib/decorators/hears.decorator.ts deleted file mode 100644 index 6e62bdd..0000000 --- a/lib/decorators/hears.decorator.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; -import { HearsTriggers } from 'telegraf/typings/composer'; -import { Context } from '../interfaces'; - -export type TelegrafHearsTriggers = HearsTriggers; - -export interface HearsOptions { - triggers: TelegrafHearsTriggers; -} - -/** - * Registers middleware for handling text messages. - * - * @see https://telegraf.js.org/#/?id=hears - */ -export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => { - return SetMetadata(DECORATORS.HEARS, { triggers: triggers }); -}; diff --git a/lib/decorators/help.decorator.ts b/lib/decorators/help.decorator.ts deleted file mode 100644 index 159a5c5..0000000 --- a/lib/decorators/help.decorator.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -/** - * Handler for /help command. - * - * @see https://telegraf.js.org/#/?id=help - */ -export const Help = (): MethodDecorator => { - return SetMetadata(DECORATORS.HELP, {}); -}; diff --git a/lib/decorators/inline-query.decorator.ts b/lib/decorators/inline-query.decorator.ts deleted file mode 100644 index 71706de..0000000 --- a/lib/decorators/inline-query.decorator.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; -import * as tt from 'telegraf/typings/telegram-types'; - -export type TelegrafInlineQueryTriggers = string | string[] | RegExp | RegExp[]; - -export interface InlineQueryOptions { - triggers?: TelegrafInlineQueryTriggers; - updateType: - | tt.UpdateType - | tt.UpdateType[] - | tt.MessageSubTypes - | tt.MessageSubTypes[]; -} - -/** - * Registers middleware for handling inline_query actions with regular expressions. - * - * @see https://telegraf.js.org/#/?id=inlinequery - */ -export const InlineQuery = ( - triggers?: TelegrafInlineQueryTriggers, -): MethodDecorator => { - return SetMetadata(DECORATORS.INLINE_QUERY, { - triggers, - updateType: 'inline_query', - }); -}; diff --git a/lib/decorators/listeners/action.decorator.ts b/lib/decorators/listeners/action.decorator.ts new file mode 100644 index 0000000..1933405 --- /dev/null +++ b/lib/decorators/listeners/action.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { TelegrafActionTriggers } from '../../telegraf.types'; +import { ListenerType } from '../../enums/listener-type.enum'; + +export interface ActionOptions { + triggers: TelegrafActionTriggers; +} + +/** + * 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(TELEGRAF_LISTENER_TYPE, ListenerType.Action), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + triggers, + } as ActionOptions), + ); +}; diff --git a/lib/decorators/listeners/cashtag.decorator.ts b/lib/decorators/listeners/cashtag.decorator.ts new file mode 100644 index 0000000..692bdf0 --- /dev/null +++ b/lib/decorators/listeners/cashtag.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { TelegrafCashtag } from '../../telegraf.types'; +import { ListenerType } from '../../enums/listener-type.enum'; + +export interface CashtagOptions { + cashtag: TelegrafCashtag; +} + +/** + * Cashtag handling. + * + * @see https://telegraf.js.org/#/?id=cashtag + */ +export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Cashtag), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + cashtag, + } as CashtagOptions), + ); +}; diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts new file mode 100644 index 0000000..ee3de72 --- /dev/null +++ b/lib/decorators/listeners/command.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafCommand } from '../../telegraf.types'; + +export interface CommandOptions { + commands: TelegrafCommand; +} + +/** + * Command handling. + * + * @see https://telegraf.js.org/#/?id=command + */ +export const Command = (commands: TelegrafCommand): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + commands, + } as CommandOptions), + ); +}; diff --git a/lib/decorators/game-query.decorator.ts b/lib/decorators/listeners/game-query.decorator.ts similarity index 54% rename from lib/decorators/game-query.decorator.ts rename to lib/decorators/listeners/game-query.decorator.ts index dd1138c..9bb173d 100644 --- a/lib/decorators/game-query.decorator.ts +++ b/lib/decorators/listeners/game-query.decorator.ts @@ -1,5 +1,6 @@ import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; +import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; /** * Registers middleware for handling callback_data actions with game query. @@ -7,5 +8,5 @@ import { DECORATORS } from '../telegraf.constants'; * @see https://telegraf.js.org/#/?id=inlinequery */ export const GameQuery = (): MethodDecorator => { - return SetMetadata(DECORATORS.GAME_QUERY, {}); + return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.GameQuery); }; diff --git a/lib/decorators/listeners/hashtag.decorator.ts b/lib/decorators/listeners/hashtag.decorator.ts new file mode 100644 index 0000000..5447cfa --- /dev/null +++ b/lib/decorators/listeners/hashtag.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafHashtag } from '../../telegraf.types'; + +export interface HashtagOptions { + hashtag: TelegrafHashtag; +} + +/** + * Hashtag handling. + * + * @see https://telegraf.js.org/#/?id=hashtag + */ +export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hashtag), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + hashtag, + } as HashtagOptions), + ); +}; diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts new file mode 100644 index 0000000..f3c6986 --- /dev/null +++ b/lib/decorators/listeners/hears.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafHearsTriggers } from '../../telegraf.types'; + +export interface HearsOptions { + triggers: TelegrafHearsTriggers; +} + +/** + * Registers middleware for handling text messages. + * + * @see https://telegraf.js.org/#/?id=hears + */ +export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hears), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + triggers, + } as HearsOptions), + ); +}; diff --git a/lib/decorators/listeners/help.decorator.ts b/lib/decorators/listeners/help.decorator.ts new file mode 100644 index 0000000..ea16432 --- /dev/null +++ b/lib/decorators/listeners/help.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; + +/** + * Handler for /help command. + * + * @see https://telegraf.js.org/#/?id=help + */ +export const Help = (): MethodDecorator => { + return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Help); +}; diff --git a/lib/decorators/listeners/inline-query.decorator.ts b/lib/decorators/listeners/inline-query.decorator.ts new file mode 100644 index 0000000..2299f6b --- /dev/null +++ b/lib/decorators/listeners/inline-query.decorator.ts @@ -0,0 +1,27 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafInlineQueryTriggers } from '../../telegraf.types'; + +export interface InlineQueryOptions { + triggers: TelegrafInlineQueryTriggers; +} + +/** + * 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(TELEGRAF_LISTENER_TYPE, ListenerType.InlineQuery), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + triggers, + } as InlineQueryOptions), + ); +}; diff --git a/lib/decorators/listeners/mention.decorator.ts b/lib/decorators/listeners/mention.decorator.ts new file mode 100644 index 0000000..d9de1d7 --- /dev/null +++ b/lib/decorators/listeners/mention.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafMention } from '../../telegraf.types'; + +export interface MentionOptions { + mention: TelegrafMention; +} + +/** + * Mention handling. + * + * @see https://telegraf.js.org/#/?id=mention + */ +export const Mention = (mention: TelegrafMention): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Mention), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + mention, + } as MentionOptions), + ); +}; diff --git a/lib/decorators/listeners/on.decorator.ts b/lib/decorators/listeners/on.decorator.ts new file mode 100644 index 0000000..37b25a4 --- /dev/null +++ b/lib/decorators/listeners/on.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafUpdateType } from '../../telegraf.types'; + +export interface OnOptions { + updateTypes: TelegrafUpdateType; +} + +/** + * Registers middleware for provided update type. + * + * @see https://telegraf.js.org/#/?id=on + */ +export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.On), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + updateTypes, + } as OnOptions), + ); +}; diff --git a/lib/decorators/listeners/phone.decorator.ts b/lib/decorators/listeners/phone.decorator.ts new file mode 100644 index 0000000..d5f956b --- /dev/null +++ b/lib/decorators/listeners/phone.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafPhone } from '../../telegraf.types'; + +export interface PhoneOptions { + phone: TelegrafPhone; +} + +/** + * Phone number handling. + * + * @see https://telegraf.js.org/#/?id=phone + */ +export const Phone = (phone: TelegrafPhone): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Phone), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + phone, + } as PhoneOptions), + ); +}; diff --git a/lib/decorators/listeners/settings.decorator.ts b/lib/decorators/listeners/settings.decorator.ts new file mode 100644 index 0000000..2087c93 --- /dev/null +++ b/lib/decorators/listeners/settings.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; + +/** + * Handler for /settings command. + * + * @see https://telegraf.js.org/#/?id=settings + */ +export const Settings = (): MethodDecorator => { + return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Settings); +}; diff --git a/lib/decorators/listeners/start.decorator.ts b/lib/decorators/listeners/start.decorator.ts new file mode 100644 index 0000000..9031bda --- /dev/null +++ b/lib/decorators/listeners/start.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; + +/** + * Handler for /start command. + * + * @see https://telegraf.js.org/#/?id=start + */ +export const Start = (): MethodDecorator => { + return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Start); +}; diff --git a/lib/decorators/listeners/use.decorator.ts b/lib/decorators/listeners/use.decorator.ts new file mode 100644 index 0000000..c2e911e --- /dev/null +++ b/lib/decorators/listeners/use.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; + +/** + * Registers a middleware. + * + * @see https://telegraf.js.org/#/?id=use + */ +export const Use = (): MethodDecorator => { + return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Use); +}; diff --git a/lib/decorators/mention.decorator.ts b/lib/decorators/mention.decorator.ts deleted file mode 100644 index 004f41b..0000000 --- a/lib/decorators/mention.decorator.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafMentionUsername = string | string[]; - -export interface MentionOptions { - username: TelegrafMentionUsername; -} - -/** - * Mention handling. - * - * @see https://telegraf.js.org/#/?id=mention - */ -export const Mention = (username: TelegrafMentionUsername): MethodDecorator => { - return SetMetadata(DECORATORS.MENTION, { username }); -}; diff --git a/lib/decorators/on.decorator.ts b/lib/decorators/on.decorator.ts deleted file mode 100644 index 9e3752a..0000000 --- a/lib/decorators/on.decorator.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; -import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types'; - -export type TelegrafOnUpdateTypes = - | UpdateType - | UpdateType[] - | MessageSubTypes - | MessageSubTypes[]; - -export interface OnOptions { - updateTypes: TelegrafOnUpdateTypes; -} - -/** - * Registers middleware for provided update type. - * - * @see https://telegraf.js.org/#/?id=on - */ -export const On = (updateTypes: TelegrafOnUpdateTypes): MethodDecorator => { - return SetMetadata(DECORATORS.ON, { updateTypes: updateTypes }); -}; diff --git a/lib/decorators/phone.decorator.ts b/lib/decorators/phone.decorator.ts deleted file mode 100644 index 39a0b27..0000000 --- a/lib/decorators/phone.decorator.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -export type TelegrafPhonePhone = string | string[]; - -export interface PhoneOptions { - phone: TelegrafPhonePhone; -} - -/** - * Phone number handling. - * - * @see https://telegraf.js.org/#/?id=phone - */ -export const Phone = (phone: TelegrafPhonePhone): MethodDecorator => { - return SetMetadata(DECORATORS.PHONE, { phone }); -}; diff --git a/lib/decorators/settings.decorator.ts b/lib/decorators/settings.decorator.ts deleted file mode 100644 index 9378e7f..0000000 --- a/lib/decorators/settings.decorator.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -/** - * Handler for /settings command. - * - * @see https://telegraf.js.org/#/?id=settings - */ -export const Settings = (): MethodDecorator => { - return SetMetadata(DECORATORS.SETTINGS, {}); -}; diff --git a/lib/decorators/start.decorator.ts b/lib/decorators/start.decorator.ts deleted file mode 100644 index 9429753..0000000 --- a/lib/decorators/start.decorator.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -/** - * Handler for /start command. - * - * @see https://telegraf.js.org/#/?id=start - */ -export const Start = (): MethodDecorator => { - return SetMetadata(DECORATORS.START, {}); -}; diff --git a/lib/decorators/use.decorator.ts b/lib/decorators/use.decorator.ts deleted file mode 100644 index a7f68fa..0000000 --- a/lib/decorators/use.decorator.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { SetMetadata } from '@nestjs/common'; -import { DECORATORS } from '../telegraf.constants'; - -/** - * Registers a middleware. - * - * @see https://telegraf.js.org/#/?id=use - */ -export const Use = (): MethodDecorator => { - return SetMetadata(DECORATORS.USE, {}); -}; diff --git a/lib/enums/listener-type.enum.ts b/lib/enums/listener-type.enum.ts new file mode 100644 index 0000000..5ebf548 --- /dev/null +++ b/lib/enums/listener-type.enum.ts @@ -0,0 +1,16 @@ +export enum ListenerType { + Use = 'use', + On = 'on', + Hears = 'hears', + Command = 'command', + Start = 'start', + Help = 'help', + Settings = 'settings', + Mention = 'mention', + Phone = 'phone', + Hashtag = 'hashtag', + Cashtag = 'cashtag', + Action = 'action', + InlineQuery = 'inlineQuery', + GameQuery = 'gameQuery', +} diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 1affe84..88b4194 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,23 +1,6 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; export const TELEGRAF_PROVIDER = 'TelegrafProvider'; -export const DECORATORS_PREFIX = 'TELEGRAF'; -export const DECORATORS = { - USE: `${DECORATORS_PREFIX}/USE`, - ON: `${DECORATORS_PREFIX}/ON`, - HEARS: `${DECORATORS_PREFIX}/HEARS`, - COMMAND: `${DECORATORS_PREFIX}/COMMAND`, - START: `${DECORATORS_PREFIX}/START`, - HELP: `${DECORATORS_PREFIX}/HELP`, - SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, - ENTITY: `${DECORATORS_PREFIX}/ENTITY`, - MENTION: `${DECORATORS_PREFIX}/MENTION`, - PHONE: `${DECORATORS_PREFIX}/PHONE`, - HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, - CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, - ACTION: `${DECORATORS_PREFIX}/ACTION`, - INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`, - GAME_QUERY: `${DECORATORS_PREFIX}/GAME_QUERY`, - UPDATE: `${DECORATORS_PREFIX}/UPDATE`, - UPDATE_HOOK: `${DECORATORS_PREFIX}/UPDATE_HOOK`, -}; +export const TELEGRAF_UPDATE = 'TELEGRAF_UPDATE'; +export const TELEGRAF_LISTENER_TYPE = 'TELEGRAF_LISTENER_TYPE'; +export const TELEGRAF_LISTENER_OPTIONS = 'TELEGRAF_LISTENER_OPTIONS'; diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index 58be5b1..f6bfb2f 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -10,7 +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; @@ -20,18 +21,18 @@ export class TelegrafProvider extends Telegraf this.launchOptions = options.launchOptions; } - async onApplicationBootstrap() { - this.catch((err, ctx: Context) => { + async onApplicationBootstrap(): Promise { + this.catch(async (err, ctx) => { this.logger.error( `Encountered an error for ${ctx.updateType} update type`, - err, + err as string, ); }); await this.launch(this.launchOptions); } - async onApplicationShutdown() { + async onApplicationShutdown(): Promise { await this.stop(); } } diff --git a/lib/telegraf.types.ts b/lib/telegraf.types.ts new file mode 100644 index 0000000..f46c261 --- /dev/null +++ b/lib/telegraf.types.ts @@ -0,0 +1,13 @@ +import { Context, Composer } from 'telegraf'; + +export type TelegrafActionTriggers = Parameters['action']>[0]; +export type TelegrafHearsTriggers = Parameters['hears']>[0]; +export type TelegrafInlineQueryTriggers = Parameters< + Composer['inlineQuery'] +>[0]; +export type TelegrafCashtag = Parameters['cashtag']>[0]; +export type TelegrafHashtag = Parameters['hashtag']>[0]; +export type TelegrafCommand = Parameters['command']>[0]; +export type TelegrafMention = Parameters['mention']>[0]; +export type TelegrafPhone = Parameters['phone']>[0]; +export type TelegrafUpdateType = Parameters['on']>[0];