From b67b85981028428c84247b17331eec6c2cc006d7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Sun, 12 Jan 2020 02:41:27 +0300 Subject: [PATCH] refactor(): complex refactoring --- lib/Bot.ts | 4 --- lib/Context.ts | 3 -- lib/TokenInjectionToken.ts | 1 - lib/decorators/index.ts | 3 ++ ...peContext.ts => pipe-context.decorator.ts} | 4 +-- ...s => telegram-action-handler.decorator.ts} | 2 +- ...amCatch.ts => telegram-catch.decorator.ts} | 2 +- lib/exeptions/index.ts | 1 + .../invalid-configuration.exeption.ts} | 0 lib/index.ts | 15 ++------- .../context-transformer.interface.ts} | 0 .../handle-parameters.interface.ts} | 2 +- .../handler.interface.ts} | 2 +- lib/interfaces/index.ts | 4 +++ ...ts => telegram-error-handler.interface.ts} | 0 ...TelegramBot.ts => telegraf-bot.service.ts} | 31 +++++++++---------- ...ts => telegraf-telegram-client.service.ts} | 9 +++--- lib/telegraf.constants.ts | 1 + lib/telegraf.module.ts | 19 +++++------- 19 files changed, 44 insertions(+), 59 deletions(-) delete mode 100644 lib/Bot.ts delete mode 100644 lib/Context.ts delete mode 100644 lib/TokenInjectionToken.ts create mode 100644 lib/decorators/index.ts rename lib/decorators/{PipeContext.ts => pipe-context.decorator.ts} (73%) rename lib/decorators/{TelegramActionHandler.ts => telegram-action-handler.decorator.ts} (95%) rename lib/decorators/{TelegramCatch.ts => telegram-catch.decorator.ts} (84%) create mode 100644 lib/exeptions/index.ts rename lib/{InvalidConfigurationException.ts => exeptions/invalid-configuration.exeption.ts} (100%) rename lib/{ContextTransformer.ts => interfaces/context-transformer.interface.ts} (100%) rename lib/{HandleParameters.ts => interfaces/handle-parameters.interface.ts} (82%) rename lib/{Handler.ts => interfaces/handler.interface.ts} (65%) rename lib/interfaces/{TelegramErrorHandler.ts => telegram-error-handler.interface.ts} (100%) rename lib/{TelegramBot.ts => telegraf-bot.service.ts} (81%) rename lib/{TelegramClient.ts => telegraf-telegram-client.service.ts} (73%) diff --git a/lib/Bot.ts b/lib/Bot.ts deleted file mode 100644 index dfdf0c2..0000000 --- a/lib/Bot.ts +++ /dev/null @@ -1,4 +0,0 @@ -import Telegraf from 'telegraf' -import { Context } from './Context' - -export type Bot = Telegraf diff --git a/lib/Context.ts b/lib/Context.ts deleted file mode 100644 index f900a4b..0000000 --- a/lib/Context.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { ContextMessageUpdate } from 'telegraf' - -export type Context = ContextMessageUpdate diff --git a/lib/TokenInjectionToken.ts b/lib/TokenInjectionToken.ts deleted file mode 100644 index 47dfa28..0000000 --- a/lib/TokenInjectionToken.ts +++ /dev/null @@ -1 +0,0 @@ -export const TokenInjectionToken = Symbol('TokenInjectionToken') diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts new file mode 100644 index 0000000..246b998 --- /dev/null +++ b/lib/decorators/index.ts @@ -0,0 +1,3 @@ +export * from './pipe-context.decorator' +export * from './telegram-action-handler.decorator' +export * from './telegram-catch.decorator' diff --git a/lib/decorators/PipeContext.ts b/lib/decorators/pipe-context.decorator.ts similarity index 73% rename from lib/decorators/PipeContext.ts rename to lib/decorators/pipe-context.decorator.ts index 5293103..01aeec0 100644 --- a/lib/decorators/PipeContext.ts +++ b/lib/decorators/pipe-context.decorator.ts @@ -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 = (transform: Type>) => ( target: Object, diff --git a/lib/decorators/TelegramActionHandler.ts b/lib/decorators/telegram-action-handler.decorator.ts similarity index 95% rename from lib/decorators/TelegramActionHandler.ts rename to lib/decorators/telegram-action-handler.decorator.ts index e7499d6..b19a619 100644 --- a/lib/decorators/TelegramActionHandler.ts +++ b/lib/decorators/telegram-action-handler.decorator.ts @@ -1,4 +1,4 @@ -import { HandleParameters } from '../HandleParameters' +import { HandleParameters } from '../interfaces' type Decorator = ( params: HandleParameters, diff --git a/lib/decorators/TelegramCatch.ts b/lib/decorators/telegram-catch.decorator.ts similarity index 84% rename from lib/decorators/TelegramCatch.ts rename to lib/decorators/telegram-catch.decorator.ts index 7754cd5..f924ea9 100644 --- a/lib/decorators/TelegramCatch.ts +++ b/lib/decorators/telegram-catch.decorator.ts @@ -1,5 +1,5 @@ import { Type } from '@nestjs/common' -import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler' +import { TelegramErrorHandler } from '../interfaces' type Decorator = (error: any) => ClassDecorator diff --git a/lib/exeptions/index.ts b/lib/exeptions/index.ts new file mode 100644 index 0000000..4ab02f7 --- /dev/null +++ b/lib/exeptions/index.ts @@ -0,0 +1 @@ +export * from './invalid-configuration.exeption'; diff --git a/lib/InvalidConfigurationException.ts b/lib/exeptions/invalid-configuration.exeption.ts similarity index 100% rename from lib/InvalidConfigurationException.ts rename to lib/exeptions/invalid-configuration.exeption.ts diff --git a/lib/index.ts b/lib/index.ts index 9eb8c5f..37afd79 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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' diff --git a/lib/ContextTransformer.ts b/lib/interfaces/context-transformer.interface.ts similarity index 100% rename from lib/ContextTransformer.ts rename to lib/interfaces/context-transformer.interface.ts diff --git a/lib/HandleParameters.ts b/lib/interfaces/handle-parameters.interface.ts similarity index 82% rename from lib/HandleParameters.ts rename to lib/interfaces/handle-parameters.interface.ts index 2bccb39..e2b96fb 100644 --- a/lib/HandleParameters.ts +++ b/lib/interfaces/handle-parameters.interface.ts @@ -1,4 +1,4 @@ -import { ContextTransformer } from './ContextTransformer' +import { ContextTransformer } from './' import { Type } from '@nestjs/common' interface ArgumentTransformation { diff --git a/lib/Handler.ts b/lib/interfaces/handler.interface.ts similarity index 65% rename from lib/Handler.ts rename to lib/interfaces/handler.interface.ts index 7a3f9df..ac61f9b 100644 --- a/lib/Handler.ts +++ b/lib/interfaces/handler.interface.ts @@ -1,4 +1,4 @@ -import { HandleParameters } from './HandleParameters' +import { HandleParameters } from './' export interface Handler { handle: (...args: any[]) => Promise diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 1eaae1b..4f31766 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -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' diff --git a/lib/interfaces/TelegramErrorHandler.ts b/lib/interfaces/telegram-error-handler.interface.ts similarity index 100% rename from lib/interfaces/TelegramErrorHandler.ts rename to lib/interfaces/telegram-error-handler.interface.ts diff --git a/lib/TelegramBot.ts b/lib/telegraf-bot.service.ts similarity index 81% rename from lib/TelegramBot.ts rename to lib/telegraf-bot.service.ts index faba9ea..68af500 100644 --- a/lib/TelegramBot.ts +++ b/lib/telegraf-bot.service.ts @@ -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 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}`) } diff --git a/lib/TelegramClient.ts b/lib/telegraf-telegram-client.service.ts similarity index 73% rename from lib/TelegramClient.ts rename to lib/telegraf-telegram-client.service.ts index 7f007bb..251689c 100644 --- a/lib/TelegramClient.ts +++ b/lib/telegraf-telegram-client.service.ts @@ -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) } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 6b0b936..b62570c 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1 +1,2 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS' +export const TokenInjectionToken = Symbol('TokenInjectionToken') diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index eda448a..366bbf9 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -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], } }