fix(module): import other modules with useClass

This commit is contained in:
Aleksandr Bukhalo 2020-01-12 01:36:53 +03:00
parent aa2af992b0
commit e9d793aeaa
5 changed files with 51 additions and 6 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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[]
}

View File

@ -0,0 +1 @@
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'

View File

@ -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],
}
}
}