refactor(module options): some changes

This commit is contained in:
Aleksandr Bukhalo 2020-01-12 01:15:32 +03:00
parent fb067b5174
commit aa2af992b0
8 changed files with 28 additions and 28 deletions

View File

@ -10,7 +10,7 @@ import { Handler } from './Handler'
import { Bot } from './Bot' import { Bot } from './Bot'
import { TelegramActionHandler } from './decorators/TelegramActionHandler' import { TelegramActionHandler } from './decorators/TelegramActionHandler'
import { TokenInjectionToken } from './TokenInjectionToken' import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' import { TelegrafOptionsFactory } from './interfaces'
import { InvalidConfigurationException } from './InvalidConfigurationException' import { InvalidConfigurationException } from './InvalidConfigurationException'
@Injectable() @Injectable()
@ -20,7 +20,7 @@ export class TelegramBot {
private ref: ModuleRef private ref: ModuleRef
public constructor( public constructor(
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory, @Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
) { ) {
const { token, sitePublicUrl } = factory.createOptions() const { token, sitePublicUrl } = factory.createOptions()

View File

@ -2,14 +2,14 @@ import { Injectable, Inject } from '@nestjs/common'
const Telegram = require('telegraf/telegram') const Telegram = require('telegraf/telegram')
import { TokenInjectionToken } from './TokenInjectionToken' import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' import { TelegrafOptionsFactory } from './interfaces'
@Injectable() @Injectable()
export class TelegramClient { export class TelegramClient {
private telegram: any private telegram: any
public constructor( public constructor(
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory, @Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
) { ) {
const { token } = factory.createOptions() const { token } = factory.createOptions()

View File

@ -1,4 +0,0 @@
export interface TelegramModuleOptions {
token: string
sitePublicUrl?: string
}

View File

@ -1,5 +0,0 @@
import { TelegramModuleOptions } from './TelegramModuleOptions'
export interface TelegramModuleOptionsFactory {
createOptions(): TelegramModuleOptions
}

View File

@ -1,10 +1,9 @@
export { TelegramModule } from './telegram.module' export * from './telegraf.module'
export * from './interfaces'
export { TelegramBot } from './TelegramBot' export { TelegramBot } from './TelegramBot'
export { TelegramClient } from './TelegramClient' export { TelegramClient } from './TelegramClient'
export { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
export { TelegramModuleOptions } from './TelegramModuleOptions'
export { PipeContext } from './decorators/PipeContext' export { PipeContext } from './decorators/PipeContext'
export { TelegramActionHandler } from './decorators/TelegramActionHandler' export { TelegramActionHandler } from './decorators/TelegramActionHandler'
export { TelegramCatch } from './decorators/TelegramCatch' export { TelegramCatch } from './decorators/TelegramCatch'

1
lib/interfaces/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './telegraf-options.interface'

View File

@ -0,0 +1,16 @@
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
export interface TelegrafModuleOptions {
token: string
sitePublicUrl?: string
}
export interface TelegrafOptionsFactory {
createOptions(): TelegrafModuleOptions
}
export interface TelegrafModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
useClass?: Type<TelegrafOptionsFactory>
inject?: any[]
}

View File

@ -4,27 +4,20 @@ import {
NestModule, NestModule,
DynamicModule, DynamicModule,
} from '@nestjs/common' } from '@nestjs/common'
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
import { TelegramBot } from './TelegramBot' import { TelegramBot } from './TelegramBot'
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' import { TelegrafModuleAsyncOptions } from './interfaces'
import { TokenInjectionToken } from './TokenInjectionToken' import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegramClient } from './TelegramClient' import { TelegramClient } from './TelegramClient'
interface TelegramFactory extends Pick<ModuleMetadata, 'imports'> {
useClass?: Type<TelegramModuleOptionsFactory>
inject?: any[]
}
@Module({}) @Module({})
export class TelegramModule implements NestModule { export class TelegrafModule implements NestModule {
public configure(consumer: MiddlewareConsumer) { public configure(consumer: MiddlewareConsumer) {
// pass // pass
} }
static fromFactory(factory: TelegramFactory): DynamicModule { static fromFactory(factory: TelegrafModuleAsyncOptions): DynamicModule {
return { return {
module: TelegramModule, module: TelegrafModule,
providers: [ providers: [
TelegramBot, TelegramBot,
TelegramClient, TelegramClient,