refactor(): complex refactoring

This commit is contained in:
Aleksandr Bukhalo 2020-01-12 02:41:27 +03:00
parent 2617c19af9
commit b67b859810
19 changed files with 44 additions and 59 deletions

View File

@ -1,4 +0,0 @@
import Telegraf from 'telegraf'
import { Context } from './Context'
export type Bot = Telegraf<Context>

View File

@ -1,3 +0,0 @@
import { ContextMessageUpdate } from 'telegraf'
export type Context = ContextMessageUpdate

View File

@ -1 +0,0 @@
export const TokenInjectionToken = Symbol('TokenInjectionToken')

3
lib/decorators/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './pipe-context.decorator'
export * from './telegram-action-handler.decorator'
export * from './telegram-catch.decorator'

View File

@ -1,6 +1,6 @@
import { Type } from '@nestjs/common'
import { ContextTransformer } from '../ContextTransformer'
import { addHandlerToStore } from './TelegramActionHandler'
import { ContextTransformer } from '../interfaces'
import { addHandlerToStore } from './'
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
target: Object,

View File

@ -1,4 +1,4 @@
import { HandleParameters } from '../HandleParameters'
import { HandleParameters } from '../interfaces'
type Decorator = (
params: HandleParameters,

View File

@ -1,5 +1,5 @@
import { Type } from '@nestjs/common'
import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler'
import { TelegramErrorHandler } from '../interfaces'
type Decorator = (error: any) => ClassDecorator

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

@ -0,0 +1 @@
export * from './invalid-configuration.exeption';

View File

@ -1,14 +1,5 @@
export * from './telegraf.module'
export * from './interfaces'
export { TelegramBot } from './TelegramBot'
export { TelegramClient } from './TelegramClient'
export { PipeContext } from './decorators/PipeContext'
export { TelegramActionHandler } from './decorators/TelegramActionHandler'
export { TelegramCatch } from './decorators/TelegramCatch'
export { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
export { ContextTransformer } from './ContextTransformer'
export { Context } from './Context'
export * from './decorators'
export * from './telegraf-bot.service'
export * from './telegraf-telegram-client.service'

View File

@ -1,4 +1,4 @@
import { ContextTransformer } from './ContextTransformer'
import { ContextTransformer } from './'
import { Type } from '@nestjs/common'
interface ArgumentTransformation {

View File

@ -1,4 +1,4 @@
import { HandleParameters } from './HandleParameters'
import { HandleParameters } from './'
export interface Handler {
handle: (...args: any[]) => Promise<void>

View File

@ -1 +1,5 @@
export * from './telegraf-options.interface'
export * from './handler.interface'
export * from './handle-parameters.interface'
export * from './telegram-error-handler.interface'
export * from './context-transformer.interface'

View File

@ -3,28 +3,27 @@ import { ModuleRef } from '@nestjs/core'
import Telegraf, { ContextMessageUpdate } from 'telegraf'
import { flatten, head } from 'lodash'
import { ContextTransformer } from './ContextTransformer'
import { TelegramCatch } from './decorators/TelegramCatch'
import { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
import { Handler } from './Handler'
import { Bot } from './Bot'
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegrafOptionsFactory } from './interfaces'
import { InvalidConfigurationException } from './InvalidConfigurationException'
import { TelegramCatch, TelegramActionHandler } from './decorators'
import { TokenInjectionToken } from './telegraf.constants'
import {
TelegrafOptionsFactory,
TelegramErrorHandler,
Handler,
ContextTransformer,
} from './interfaces'
import { InvalidConfigurationException } from './exeptions'
@Injectable()
export class TelegramBot {
private readonly logger = new Logger(TelegramBot.name, true);
export class TelegrafBotService {
private readonly logger = new Logger(TelegrafBotService.name, true)
private readonly sitePublicUrl?: string
private readonly bot: Bot
private readonly bot: Telegraf<ContextMessageUpdate>
private ref: ModuleRef
public constructor(
@Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
) {
const { token, sitePublicUrl } = factory.createTelegrafOptions()
const { token, sitePublicUrl } = options.createTelegrafOptions()
this.sitePublicUrl = sitePublicUrl
this.bot = new Telegraf(token)
}
@ -55,7 +54,7 @@ export class TelegramBot {
this.bot.telegram
.setWebhook(url)
.then(() => Logger.log(`Webhook set success @ ${url}`))
.then(() => this.logger.log(`Webhook set success @ ${url}`))
return this.bot.webhookCallback(`/${path}`)
}

View File

@ -1,18 +1,17 @@
import { Injectable, Inject } from '@nestjs/common'
const Telegram = require('telegraf/telegram')
import { TokenInjectionToken } from './TokenInjectionToken'
import { TokenInjectionToken } from './telegraf.constants'
import { TelegrafOptionsFactory } from './interfaces'
@Injectable()
export class TelegramClient {
export class TelegrafTelegramClientService {
private telegram: any
public constructor(
@Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
) {
const { token } = factory.createTelegrafOptions()
const { token } = options.createTelegrafOptions()
this.telegram = new Telegram(token)
}

View File

@ -1 +1,2 @@
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
export const TokenInjectionToken = Symbol('TokenInjectionToken')

View File

@ -1,16 +1,11 @@
import {
Module,
DynamicModule,
Provider,
} from '@nestjs/common'
import { TelegramBot } from './TelegramBot'
import { Module, DynamicModule, Provider } from '@nestjs/common'
import { TelegrafBotService } from './telegraf-bot.service'
import {
TelegrafModuleAsyncOptions,
TelegrafOptionsFactory,
} from './interfaces'
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'
import { TokenInjectionToken } from './TokenInjectionToken'
import { TelegramClient } from './TelegramClient'
import { TELEGRAF_MODULE_OPTIONS, TokenInjectionToken } from './telegraf.constants'
import { TelegrafTelegramClientService } from './telegraf-telegram-client.service'
@Module({})
export class TelegrafModule {
@ -20,14 +15,14 @@ export class TelegrafModule {
imports: options.imports || [],
providers: [
...this.createAsyncProviders(options),
TelegramBot,
TelegramClient,
TelegrafBotService,
TelegrafTelegramClientService,
{
provide: TokenInjectionToken,
useClass: options.useClass,
},
],
exports: [TelegramBot, TelegramClient],
exports: [TelegrafBotService, TelegrafTelegramClientService],
}
}