From 48c183a8ff4da0b315d5a261a6b348015dfe1605 Mon Sep 17 00:00:00 2001 From: Morb0 Date: Sat, 6 Mar 2021 15:00:13 +0300 Subject: [PATCH] feat(): make stage provider & export from module --- lib/services/listeners-explorer.service.ts | 4 +++- lib/stage.provider.ts | 8 ++++++++ lib/telegraf-core.module.ts | 15 +++++++++++++-- lib/telegraf.constants.ts | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 lib/stage.provider.ts diff --git a/lib/services/listeners-explorer.service.ts b/lib/services/listeners-explorer.service.ts index 233a26b..6a5dbc8 100644 --- a/lib/services/listeners-explorer.service.ts +++ b/lib/services/listeners-explorer.service.ts @@ -12,6 +12,7 @@ import { PARAM_ARGS_METADATA, TELEGRAF_BOT_NAME, TELEGRAF_MODULE_OPTIONS, + TELEGRAF_STAGE, } from '../telegraf.constants'; import { BaseExplorerService } from './base-explorer.service'; import { TelegrafParamsFactory } from '../factories/telegraf-params-factory'; @@ -23,10 +24,11 @@ export class ListenersExplorerService extends BaseExplorerService implements OnModuleInit { private readonly telegrafParamsFactory = new TelegrafParamsFactory(); - private readonly stage = new Scenes.Stage(); private bot: Telegraf; constructor( + @Inject(TELEGRAF_STAGE) + private readonly stage: Scenes.Stage, @Inject(TELEGRAF_MODULE_OPTIONS) private readonly telegrafOptions: TelegrafModuleOptions, @Inject(TELEGRAF_BOT_NAME) diff --git a/lib/stage.provider.ts b/lib/stage.provider.ts new file mode 100644 index 0000000..c4dbabb --- /dev/null +++ b/lib/stage.provider.ts @@ -0,0 +1,8 @@ +import { Provider } from '@nestjs/common'; +import { Scenes } from 'telegraf'; +import { TELEGRAF_STAGE } from './telegraf.constants'; + +export const telegrafStageProvider: Provider = { + provide: TELEGRAF_STAGE, + useClass: Scenes.Stage, +}; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index b50e0c4..9b4ab34 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -18,6 +18,7 @@ import { TELEGRAF_MODULE_OPTIONS, } from './telegraf.constants'; import { ListenersExplorerService, MetadataAccessorService } from './services'; +import { telegrafStageProvider } from './stage.provider'; import { createBotFactory, getBotToken } from './utils'; @Global() @@ -52,10 +53,15 @@ export class TelegrafCoreModule implements OnApplicationShutdown { provide: TELEGRAF_MODULE_OPTIONS, useValue: options, }, + telegrafStageProvider, + telegrafBotNameProvider, + telegrafBotProvider, + ], + exports: [ + telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, ], - exports: [telegrafBotProvider], }; } @@ -82,10 +88,15 @@ export class TelegrafCoreModule implements OnApplicationShutdown { imports: options.imports, providers: [ ...asyncProviders, + telegrafStageProvider, + telegrafBotNameProvider, + telegrafBotProvider, + ], + exports: [ + telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, ], - exports: [telegrafBotNameProvider, telegrafBotProvider], }; } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 5d6888b..56e78f2 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -9,3 +9,5 @@ export const SCENE_METADATA = 'SCENE_METADATA'; export const LISTENER_METADATA = 'LISTENER_METADATA'; export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA; + +export const TELEGRAF_STAGE = 'TelegrafStage';