mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-11 22:51:06 +03:00
fix(module): import other modules with useClass
This commit is contained in:
parent
aa2af992b0
commit
e9d793aeaa
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -6,11 +6,15 @@ export interface TelegrafModuleOptions {
|
||||
}
|
||||
|
||||
export interface TelegrafOptionsFactory {
|
||||
createOptions(): TelegrafModuleOptions
|
||||
createTelegrafOptions(): TelegrafModuleOptions
|
||||
}
|
||||
|
||||
export interface TelegrafModuleAsyncOptions
|
||||
extends Pick<ModuleMetadata, 'imports'> {
|
||||
useExisting?: Type<TelegrafOptionsFactory>
|
||||
useClass?: Type<TelegrafOptionsFactory>
|
||||
useFactory?: (
|
||||
...args: any[]
|
||||
) => Promise<TelegrafModuleOptions> | TelegrafModuleOptions
|
||||
inject?: any[]
|
||||
}
|
||||
|
1
lib/telegraf.constants.ts
Normal file
1
lib/telegraf.constants.ts
Normal file
@ -0,0 +1 @@
|
||||
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
|
@ -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],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user