diff --git a/lib/TelegramBot.ts b/lib/TelegramBot.ts index 7f3779e..6c21f50 100644 --- a/lib/TelegramBot.ts +++ b/lib/TelegramBot.ts @@ -22,7 +22,7 @@ export class TelegramBot { public constructor( @Inject(TokenInjectionToken) factory: TelegrafOptionsFactory, ) { - const { token, sitePublicUrl } = factory.createOptions() + const { token, sitePublicUrl } = factory.createTelegrafOptions() this.sitePublicUrl = sitePublicUrl this.bot = new Telegraf(token) diff --git a/lib/TelegramClient.ts b/lib/TelegramClient.ts index 58638d1..7f007bb 100644 --- a/lib/TelegramClient.ts +++ b/lib/TelegramClient.ts @@ -11,7 +11,7 @@ export class TelegramClient { public constructor( @Inject(TokenInjectionToken) factory: TelegrafOptionsFactory, ) { - const { token } = factory.createOptions() + const { token } = factory.createTelegrafOptions() this.telegram = new Telegram(token) } diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 5dbc6c4..14338bb 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -6,11 +6,15 @@ export interface TelegrafModuleOptions { } export interface TelegrafOptionsFactory { - createOptions(): TelegrafModuleOptions + createTelegrafOptions(): TelegrafModuleOptions } export interface TelegrafModuleAsyncOptions extends Pick { + useExisting?: Type useClass?: Type + useFactory?: ( + ...args: any[] + ) => Promise | TelegrafModuleOptions inject?: any[] } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts new file mode 100644 index 0000000..6b0b936 --- /dev/null +++ b/lib/telegraf.constants.ts @@ -0,0 +1 @@ +export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS' diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index 337a9d7..0ec8ab7 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -3,9 +3,14 @@ import { Module, NestModule, DynamicModule, + Provider, } from '@nestjs/common' import { TelegramBot } from './TelegramBot' -import { TelegrafModuleAsyncOptions } from './interfaces' +import { + TelegrafModuleAsyncOptions, + TelegrafOptionsFactory, +} from './interfaces' +import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants' import { TokenInjectionToken } from './TokenInjectionToken' import { TelegramClient } from './TelegramClient' @@ -15,18 +20,53 @@ export class TelegrafModule implements NestModule { // pass } - static fromFactory(factory: TelegrafModuleAsyncOptions): DynamicModule { + static fromFactory(options: TelegrafModuleAsyncOptions): DynamicModule { return { module: TelegrafModule, + imports: options.imports || [], providers: [ + ...this.createAsyncProviders(options), TelegramBot, TelegramClient, { provide: TokenInjectionToken, - useClass: factory.useClass, + useClass: options.useClass, }, ], exports: [TelegramBot, TelegramClient], } } + + 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], + } + } }