From 80853be80c8cc18cbe23b19f550e8104839a9762 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 26 Dec 2020 14:54:10 +0300 Subject: [PATCH] refactor(wip): remove core module & change tokens --- lib/decorators/core/on.decorator.ts | 8 +- lib/decorators/core/update.decorator.ts | 5 +- lib/decorators/core/use.decorator.ts | 4 +- lib/decorators/inject-bot.decorator.ts | 4 +- lib/decorators/listeners/action.decorator.ts | 8 +- lib/decorators/listeners/cashtag.decorator.ts | 8 +- lib/decorators/listeners/command.decorator.ts | 8 +- lib/decorators/listeners/email.decorator.ts | 8 +- .../listeners/game-query.decorator.ts | 4 +- lib/decorators/listeners/hashtag.decorator.ts | 8 +- lib/decorators/listeners/hears.decorator.ts | 8 +- lib/decorators/listeners/help.decorator.ts | 4 +- .../listeners/inline-query.decorator.ts | 8 +- lib/decorators/listeners/mention.decorator.ts | 8 +- lib/decorators/listeners/phone.decorator.ts | 8 +- .../listeners/settings.decorator.ts | 4 +- lib/decorators/listeners/start.decorator.ts | 4 +- .../listeners/text-link.decorator.ts | 8 +- .../listeners/text-mention.decorator.ts | 8 +- lib/decorators/listeners/url.decorator.ts | 8 +- lib/index.ts | 8 -- lib/telegraf-core.module.ts | 91 ------------------- lib/telegraf-metadata.accessor.ts | 14 ++- lib/telegraf.constants.ts | 7 +- lib/telegraf.explorer.ts | 16 ++-- lib/telegraf.module.ts | 71 +++++++++++++-- 26 files changed, 155 insertions(+), 185 deletions(-) delete mode 100644 lib/telegraf-core.module.ts diff --git a/lib/decorators/core/on.decorator.ts b/lib/decorators/core/on.decorator.ts index 37b25a4..3bef944 100644 --- a/lib/decorators/core/on.decorator.ts +++ b/lib/decorators/core/on.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafUpdateType } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface OnOptions { */ export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.On), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.On), + SetMetadata(LISTENER_OPTIONS_METADATA, { updateTypes, } as OnOptions), ); diff --git a/lib/decorators/core/update.decorator.ts b/lib/decorators/core/update.decorator.ts index a2ea7f2..fde0d76 100644 --- a/lib/decorators/core/update.decorator.ts +++ b/lib/decorators/core/update.decorator.ts @@ -1,8 +1,9 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_UPDATE } from '../../telegraf.constants'; +import { TELEGRAF_UPDATE_METADATA } from '../../telegraf.constants'; /** * `@Update` decorator, it's like NestJS `@Controller` decorator, * but for Telegram Bot API updates. */ -export const Update = (): ClassDecorator => SetMetadata(TELEGRAF_UPDATE, true); +export const Update = (): ClassDecorator => + SetMetadata(TELEGRAF_UPDATE_METADATA, true); diff --git a/lib/decorators/core/use.decorator.ts b/lib/decorators/core/use.decorator.ts index c2e911e..72fc62e 100644 --- a/lib/decorators/core/use.decorator.ts +++ b/lib/decorators/core/use.decorator.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { LISTENER_TYPE_METADATA } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; /** @@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum'; * @see https://telegraf.js.org/#/?id=use */ export const Use = (): MethodDecorator => { - return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Use); + return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Use); }; diff --git a/lib/decorators/inject-bot.decorator.ts b/lib/decorators/inject-bot.decorator.ts index 84f1c50..76ef284 100644 --- a/lib/decorators/inject-bot.decorator.ts +++ b/lib/decorators/inject-bot.decorator.ts @@ -1,4 +1,4 @@ import { Inject } from '@nestjs/common'; -import { TELEGRAF_PROVIDER } from '../telegraf.constants'; +import { TelegrafProvider } from '../telegraf.provider'; -export const InjectBot = (): ParameterDecorator => Inject(TELEGRAF_PROVIDER); +export const InjectBot = (): ParameterDecorator => Inject(TelegrafProvider); diff --git a/lib/decorators/listeners/action.decorator.ts b/lib/decorators/listeners/action.decorator.ts index 1933405..502b1b0 100644 --- a/lib/decorators/listeners/action.decorator.ts +++ b/lib/decorators/listeners/action.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { TelegrafActionTriggers } from '../../telegraf.types'; import { ListenerType } from '../../enums/listener-type.enum'; @@ -17,8 +17,8 @@ export interface ActionOptions { */ export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Action), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Action), + SetMetadata(LISTENER_OPTIONS_METADATA, { triggers, } as ActionOptions), ); diff --git a/lib/decorators/listeners/cashtag.decorator.ts b/lib/decorators/listeners/cashtag.decorator.ts index 692bdf0..d9784cf 100644 --- a/lib/decorators/listeners/cashtag.decorator.ts +++ b/lib/decorators/listeners/cashtag.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { TelegrafCashtag } from '../../telegraf.types'; import { ListenerType } from '../../enums/listener-type.enum'; @@ -17,8 +17,8 @@ export interface CashtagOptions { */ export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Cashtag), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Cashtag), + SetMetadata(LISTENER_OPTIONS_METADATA, { cashtag, } as CashtagOptions), ); diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts index 683b405..eb4b01e 100644 --- a/lib/decorators/listeners/command.decorator.ts +++ b/lib/decorators/listeners/command.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafCommand } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface CommandOptions { */ export const Command = (command: TelegrafCommand): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Command), + SetMetadata(LISTENER_OPTIONS_METADATA, { command: command, } as CommandOptions), ); diff --git a/lib/decorators/listeners/email.decorator.ts b/lib/decorators/listeners/email.decorator.ts index a0b0b91..9f890fe 100644 --- a/lib/decorators/listeners/email.decorator.ts +++ b/lib/decorators/listeners/email.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafEmail } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface EmailOptions { */ export const Email = (email: TelegrafEmail): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Email), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Email), + SetMetadata(LISTENER_OPTIONS_METADATA, { email, } as EmailOptions), ); diff --git a/lib/decorators/listeners/game-query.decorator.ts b/lib/decorators/listeners/game-query.decorator.ts index 9bb173d..25e0410 100644 --- a/lib/decorators/listeners/game-query.decorator.ts +++ b/lib/decorators/listeners/game-query.decorator.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { LISTENER_TYPE_METADATA } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; /** @@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum'; * @see https://telegraf.js.org/#/?id=inlinequery */ export const GameQuery = (): MethodDecorator => { - return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.GameQuery); + return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.GameQuery); }; diff --git a/lib/decorators/listeners/hashtag.decorator.ts b/lib/decorators/listeners/hashtag.decorator.ts index 5447cfa..4c86618 100644 --- a/lib/decorators/listeners/hashtag.decorator.ts +++ b/lib/decorators/listeners/hashtag.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafHashtag } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface HashtagOptions { */ export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hashtag), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Hashtag), + SetMetadata(LISTENER_OPTIONS_METADATA, { hashtag, } as HashtagOptions), ); diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts index f3c6986..cef3453 100644 --- a/lib/decorators/listeners/hears.decorator.ts +++ b/lib/decorators/listeners/hears.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafHearsTriggers } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface HearsOptions { */ export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hears), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Hears), + SetMetadata(LISTENER_OPTIONS_METADATA, { triggers, } as HearsOptions), ); diff --git a/lib/decorators/listeners/help.decorator.ts b/lib/decorators/listeners/help.decorator.ts index ea16432..2fec408 100644 --- a/lib/decorators/listeners/help.decorator.ts +++ b/lib/decorators/listeners/help.decorator.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { LISTENER_TYPE_METADATA } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; /** @@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum'; * @see https://telegraf.js.org/#/?id=help */ export const Help = (): MethodDecorator => { - return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Help); + return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Help); }; diff --git a/lib/decorators/listeners/inline-query.decorator.ts b/lib/decorators/listeners/inline-query.decorator.ts index 2299f6b..70da417 100644 --- a/lib/decorators/listeners/inline-query.decorator.ts +++ b/lib/decorators/listeners/inline-query.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafInlineQueryTriggers } from '../../telegraf.types'; @@ -19,8 +19,8 @@ export const InlineQuery = ( triggers: TelegrafInlineQueryTriggers, ): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.InlineQuery), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.InlineQuery), + SetMetadata(LISTENER_OPTIONS_METADATA, { triggers, } as InlineQueryOptions), ); diff --git a/lib/decorators/listeners/mention.decorator.ts b/lib/decorators/listeners/mention.decorator.ts index d9de1d7..a703a23 100644 --- a/lib/decorators/listeners/mention.decorator.ts +++ b/lib/decorators/listeners/mention.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafMention } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface MentionOptions { */ export const Mention = (mention: TelegrafMention): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Mention), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Mention), + SetMetadata(LISTENER_OPTIONS_METADATA, { mention, } as MentionOptions), ); diff --git a/lib/decorators/listeners/phone.decorator.ts b/lib/decorators/listeners/phone.decorator.ts index d5f956b..b0b794b 100644 --- a/lib/decorators/listeners/phone.decorator.ts +++ b/lib/decorators/listeners/phone.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafPhone } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface PhoneOptions { */ export const Phone = (phone: TelegrafPhone): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Phone), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Phone), + SetMetadata(LISTENER_OPTIONS_METADATA, { phone, } as PhoneOptions), ); diff --git a/lib/decorators/listeners/settings.decorator.ts b/lib/decorators/listeners/settings.decorator.ts index 2087c93..6bbb86b 100644 --- a/lib/decorators/listeners/settings.decorator.ts +++ b/lib/decorators/listeners/settings.decorator.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { LISTENER_TYPE_METADATA } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; /** @@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum'; * @see https://telegraf.js.org/#/?id=settings */ export const Settings = (): MethodDecorator => { - return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Settings); + return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Settings); }; diff --git a/lib/decorators/listeners/start.decorator.ts b/lib/decorators/listeners/start.decorator.ts index 9031bda..b6d3025 100644 --- a/lib/decorators/listeners/start.decorator.ts +++ b/lib/decorators/listeners/start.decorator.ts @@ -1,5 +1,5 @@ import { SetMetadata } from '@nestjs/common'; -import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; +import { LISTENER_TYPE_METADATA } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; /** @@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum'; * @see https://telegraf.js.org/#/?id=start */ export const Start = (): MethodDecorator => { - return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Start); + return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Start); }; diff --git a/lib/decorators/listeners/text-link.decorator.ts b/lib/decorators/listeners/text-link.decorator.ts index f6a484e..7dedd6f 100644 --- a/lib/decorators/listeners/text-link.decorator.ts +++ b/lib/decorators/listeners/text-link.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafTextLink } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface TextLinkOptions { */ export const TetxLink = (link: TelegrafTextLink): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextLink), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.TextLink), + SetMetadata(LISTENER_OPTIONS_METADATA, { link, } as TextLinkOptions), ); diff --git a/lib/decorators/listeners/text-mention.decorator.ts b/lib/decorators/listeners/text-mention.decorator.ts index aea29ce..b098c4e 100644 --- a/lib/decorators/listeners/text-mention.decorator.ts +++ b/lib/decorators/listeners/text-mention.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafTextMention } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface TextMentionOptions { */ export const TetxMention = (mention: TelegrafTextMention): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextMention), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.TextMention), + SetMetadata(LISTENER_OPTIONS_METADATA, { mention, } as TextMentionOptions), ); diff --git a/lib/decorators/listeners/url.decorator.ts b/lib/decorators/listeners/url.decorator.ts index e8c1448..e38167e 100644 --- a/lib/decorators/listeners/url.decorator.ts +++ b/lib/decorators/listeners/url.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators, SetMetadata } from '@nestjs/common'; import { - TELEGRAF_LISTENER_OPTIONS, - TELEGRAF_LISTENER_TYPE, + LISTENER_OPTIONS_METADATA, + LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafUrl } from '../../telegraf.types'; @@ -17,8 +17,8 @@ export interface UrlOptions { */ export const Url = (url: TelegrafUrl): MethodDecorator => { return applyDecorators( - SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Url), - SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Url), + SetMetadata(LISTENER_OPTIONS_METADATA, { url, } as UrlOptions), ); diff --git a/lib/index.ts b/lib/index.ts index 39b657d..bca10ec 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,11 +1,3 @@ -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 './telegraf.module'; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts deleted file mode 100644 index 9be69ed..0000000 --- a/lib/telegraf-core.module.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { DiscoveryModule } from '@nestjs/core'; -import { Module, DynamicModule, Provider, Type } from '@nestjs/common'; -import { - TelegrafModuleOptions, - TelegrafModuleAsyncOptions, - TelegrafOptionsFactory, -} from './interfaces'; -import { - TELEGRAF_MODULE_OPTIONS, - TELEGRAF_PROVIDER, -} from './telegraf.constants'; -import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; -import { TelegrafExplorer } from './telegraf.explorer'; -import { TelegrafProvider } from './telegraf.provider'; - -@Module({ - imports: [DiscoveryModule], - providers: [TelegrafMetadataAccessor, TelegrafExplorer], -}) -export class TelegrafCoreModule { - public static forRoot(options: TelegrafModuleOptions): DynamicModule { - const telegrafProvider = { - provide: TELEGRAF_PROVIDER, - useClass: TelegrafProvider, - inject: [TELEGRAF_MODULE_OPTIONS], - }; - return { - module: TelegrafCoreModule, - providers: [ - { provide: TELEGRAF_MODULE_OPTIONS, useValue: options }, - telegrafProvider, - ], - exports: [telegrafProvider], - }; - } - - public static forRootAsync( - options: TelegrafModuleAsyncOptions, - ): DynamicModule { - const telegrafProvider = { - provide: TELEGRAF_PROVIDER, - useClass: TelegrafProvider, - inject: [TELEGRAF_MODULE_OPTIONS], - }; - const asyncProviders = this.createAsyncProviders(options); - return { - module: TelegrafCoreModule, - imports: options.imports, - providers: [...asyncProviders, telegrafProvider], - exports: [telegrafProvider], - }; - } - - private static createAsyncProviders( - options: TelegrafModuleAsyncOptions, - ): Provider[] { - if (options.useExisting || options.useFactory) { - return [this.createAsyncOptionsProvider(options)]; - } - const useClass = options.useClass as Type; - return [ - this.createAsyncOptionsProvider(options), - { - provide: useClass, - useClass, - }, - ]; - } - - private static createAsyncOptionsProvider( - options: TelegrafModuleAsyncOptions, - ): Provider { - if (options.useFactory) { - return { - provide: TELEGRAF_MODULE_OPTIONS, - useFactory: options.useFactory, - inject: options.inject || [], - }; - } - // `as Type` is a workaround for microsoft/TypeScript#31603 - const inject = [ - (options.useClass || options.useExisting) as Type, - ]; - return { - provide: TELEGRAF_MODULE_OPTIONS, - useFactory: async (optionsFactory: TelegrafOptionsFactory) => - await optionsFactory.createTelegrafOptions(), - inject, - }; - } -} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 504db39..f61bb3a 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -1,17 +1,25 @@ import { Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { - TELEGRAF_LISTENER_TYPE, + LISTENER_TYPE_METADATA, TELEGRAF_MODULE_OPTIONS, + TELEGRAF_UPDATE_METADATA, } from './telegraf.constants'; -import { ListenerType } from './enums/listener-type.enum'; +import { ListenerType } from './enums'; @Injectable() export class TelegrafMetadataAccessor { constructor(private readonly reflector: Reflector) {} + isUpdate(target: Function): boolean { + return !!this.reflector.get( + TELEGRAF_UPDATE_METADATA, + target, + ); + } + getListenerType(target: Function): ListenerType | undefined { - return this.reflector.get(TELEGRAF_LISTENER_TYPE, target); + return this.reflector.get(LISTENER_TYPE_METADATA, target); } getListenerOptions(target: Function): unknown | undefined { diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 88b4194..16fa981 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,6 +1,5 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; -export const TELEGRAF_PROVIDER = 'TelegrafProvider'; -export const TELEGRAF_UPDATE = 'TELEGRAF_UPDATE'; -export const TELEGRAF_LISTENER_TYPE = 'TELEGRAF_LISTENER_TYPE'; -export const TELEGRAF_LISTENER_OPTIONS = 'TELEGRAF_LISTENER_OPTIONS'; +export const TELEGRAF_UPDATE_METADATA = 'TELEGRAF_UPDATE_METADATA'; +export const LISTENER_TYPE_METADATA = 'LISTENER_TYPE_METADATA'; +export const LISTENER_OPTIONS_METADATA = 'LISTENER_OPTIONS_METADATA'; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index da3a6b6..fa805b9 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -3,8 +3,7 @@ import { DiscoveryService, ModuleRef } from '@nestjs/core'; import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafProvider } from './telegraf.provider'; -import { TELEGRAF_PROVIDER } from './telegraf.constants'; -import { ListenerType } from './enums/listener-type.enum'; +import { ListenerType } from './enums'; import { ActionOptions, CashtagOptions, @@ -33,7 +32,7 @@ export class TelegrafExplorer implements OnModuleInit { private telegraf: TelegrafProvider; onModuleInit(): void { - this.telegraf = this.moduleRef.get(TELEGRAF_PROVIDER, { + this.telegraf = this.moduleRef.get(TelegrafProvider, { strict: false, }); this.explore(); @@ -50,13 +49,18 @@ export class TelegrafExplorer implements OnModuleInit { this.metadataScanner.scanFromPrototype( instance, prototype, - (methodKey: string) => { - this.registerIfListener(instance, methodKey); - }, + (methodKey: string) => this.registerIfUpdate(instance, methodKey), ); }); } + private registerIfUpdate( + instance: Record, + methodKey: string, + ): void { + const isUpdate = this.metadataAccessor.isUpdate(instance); + } + private registerIfListener( instance: Record, methodKey: string, diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index fb3cdeb..77bf8b5 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -1,27 +1,84 @@ -import { Module, DynamicModule } from '@nestjs/common'; -import { TelegrafCoreModule } from './telegraf-core.module'; +import { DiscoveryModule } from '@nestjs/core'; +import { Module, DynamicModule, Provider } from '@nestjs/common'; import { TelegrafModuleOptions, TelegrafModuleAsyncOptions, + TelegrafOptionsFactory, } from './interfaces'; +import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; +import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; +import { TelegrafExplorer } from './telegraf.explorer'; +import { TelegrafProvider } from './telegraf.provider'; -@Module({}) +@Module({ + imports: [DiscoveryModule], + providers: [TelegrafMetadataAccessor, TelegrafExplorer], +}) export class TelegrafModule { public static forRoot(options: TelegrafModuleOptions): DynamicModule { + const providers = [...this.createProviders(options), TelegrafProvider]; + return { module: TelegrafModule, - imports: [TelegrafCoreModule.forRoot(options)], - exports: [TelegrafCoreModule], + providers, + exports: providers, }; } + private static createProviders(options: TelegrafModuleOptions): Provider[] { + return [ + { + provide: TELEGRAF_MODULE_OPTIONS, + useValue: options, + }, + ]; + } + public static forRootAsync( options: TelegrafModuleAsyncOptions, ): DynamicModule { + const providers = [...this.createAsyncProviders(options), TelegrafProvider]; + return { module: TelegrafModule, - imports: [TelegrafCoreModule.forRootAsync(options)], - exports: [TelegrafCoreModule], + imports: options.imports || [], + providers, + exports: providers, + }; + } + + private static createAsyncProviders( + options: TelegrafModuleAsyncOptions, + ): Provider[] { + if (options.useExisting || options.useFactory) { + return [this.createAsyncOptionsProvider(options)]; + } + + return [ + this.createAsyncOptionsProvider(options), + { + provide: options.useClass, + useClass: options.useClass, + }, + ]; + } + + private static createAsyncOptionsProvider( + options: TelegrafModuleAsyncOptions, + ): Provider { + if (options.useFactory) { + return { + provide: TELEGRAF_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject || [], + }; + } + + return { + provide: TELEGRAF_MODULE_OPTIONS, + useFactory: async (optionsFactory: TelegrafOptionsFactory) => + await optionsFactory.createTelegrafOptions(), + inject: [options.useExisting || options.useClass], }; } }