mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +03:00
refactor(): complex refactoring
This commit is contained in:
parent
2617c19af9
commit
b67b859810
@ -1,4 +0,0 @@
|
|||||||
import Telegraf from 'telegraf'
|
|
||||||
import { Context } from './Context'
|
|
||||||
|
|
||||||
export type Bot = Telegraf<Context>
|
|
@ -1,3 +0,0 @@
|
|||||||
import { ContextMessageUpdate } from 'telegraf'
|
|
||||||
|
|
||||||
export type Context = ContextMessageUpdate
|
|
@ -1 +0,0 @@
|
|||||||
export const TokenInjectionToken = Symbol('TokenInjectionToken')
|
|
3
lib/decorators/index.ts
Normal file
3
lib/decorators/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './pipe-context.decorator'
|
||||||
|
export * from './telegram-action-handler.decorator'
|
||||||
|
export * from './telegram-catch.decorator'
|
@ -1,6 +1,6 @@
|
|||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
import { ContextTransformer } from '../ContextTransformer'
|
import { ContextTransformer } from '../interfaces'
|
||||||
import { addHandlerToStore } from './TelegramActionHandler'
|
import { addHandlerToStore } from './'
|
||||||
|
|
||||||
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
||||||
target: Object,
|
target: Object,
|
@ -1,4 +1,4 @@
|
|||||||
import { HandleParameters } from '../HandleParameters'
|
import { HandleParameters } from '../interfaces'
|
||||||
|
|
||||||
type Decorator = (
|
type Decorator = (
|
||||||
params: HandleParameters,
|
params: HandleParameters,
|
@ -1,5 +1,5 @@
|
|||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler'
|
import { TelegramErrorHandler } from '../interfaces'
|
||||||
|
|
||||||
type Decorator = (error: any) => ClassDecorator
|
type Decorator = (error: any) => ClassDecorator
|
||||||
|
|
1
lib/exeptions/index.ts
Normal file
1
lib/exeptions/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './invalid-configuration.exeption';
|
15
lib/index.ts
15
lib/index.ts
@ -1,14 +1,5 @@
|
|||||||
export * from './telegraf.module'
|
export * from './telegraf.module'
|
||||||
export * from './interfaces'
|
export * from './interfaces'
|
||||||
|
export * from './decorators'
|
||||||
export { TelegramBot } from './TelegramBot'
|
export * from './telegraf-bot.service'
|
||||||
export { TelegramClient } from './TelegramClient'
|
export * from './telegraf-telegram-client.service'
|
||||||
|
|
||||||
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'
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ContextTransformer } from './ContextTransformer'
|
import { ContextTransformer } from './'
|
||||||
import { Type } from '@nestjs/common'
|
import { Type } from '@nestjs/common'
|
||||||
|
|
||||||
interface ArgumentTransformation {
|
interface ArgumentTransformation {
|
@ -1,4 +1,4 @@
|
|||||||
import { HandleParameters } from './HandleParameters'
|
import { HandleParameters } from './'
|
||||||
|
|
||||||
export interface Handler {
|
export interface Handler {
|
||||||
handle: (...args: any[]) => Promise<void>
|
handle: (...args: any[]) => Promise<void>
|
@ -1 +1,5 @@
|
|||||||
export * from './telegraf-options.interface'
|
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'
|
||||||
|
@ -3,28 +3,27 @@ import { ModuleRef } from '@nestjs/core'
|
|||||||
import Telegraf, { ContextMessageUpdate } from 'telegraf'
|
import Telegraf, { ContextMessageUpdate } from 'telegraf'
|
||||||
import { flatten, head } from 'lodash'
|
import { flatten, head } from 'lodash'
|
||||||
|
|
||||||
import { ContextTransformer } from './ContextTransformer'
|
import { TelegramCatch, TelegramActionHandler } from './decorators'
|
||||||
import { TelegramCatch } from './decorators/TelegramCatch'
|
import { TokenInjectionToken } from './telegraf.constants'
|
||||||
import { TelegramErrorHandler } from './interfaces/TelegramErrorHandler'
|
import {
|
||||||
import { Handler } from './Handler'
|
TelegrafOptionsFactory,
|
||||||
import { Bot } from './Bot'
|
TelegramErrorHandler,
|
||||||
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
|
Handler,
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
ContextTransformer,
|
||||||
import { TelegrafOptionsFactory } from './interfaces'
|
} from './interfaces'
|
||||||
import { InvalidConfigurationException } from './InvalidConfigurationException'
|
import { InvalidConfigurationException } from './exeptions'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegramBot {
|
export class TelegrafBotService {
|
||||||
private readonly logger = new Logger(TelegramBot.name, true);
|
private readonly logger = new Logger(TelegrafBotService.name, true)
|
||||||
private readonly sitePublicUrl?: string
|
private readonly sitePublicUrl?: string
|
||||||
private readonly bot: Bot
|
private readonly bot: Telegraf<ContextMessageUpdate>
|
||||||
private ref: ModuleRef
|
private ref: ModuleRef
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
@Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
|
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
|
||||||
) {
|
) {
|
||||||
const { token, sitePublicUrl } = factory.createTelegrafOptions()
|
const { token, sitePublicUrl } = options.createTelegrafOptions()
|
||||||
|
|
||||||
this.sitePublicUrl = sitePublicUrl
|
this.sitePublicUrl = sitePublicUrl
|
||||||
this.bot = new Telegraf(token)
|
this.bot = new Telegraf(token)
|
||||||
}
|
}
|
||||||
@ -55,7 +54,7 @@ export class TelegramBot {
|
|||||||
|
|
||||||
this.bot.telegram
|
this.bot.telegram
|
||||||
.setWebhook(url)
|
.setWebhook(url)
|
||||||
.then(() => Logger.log(`Webhook set success @ ${url}`))
|
.then(() => this.logger.log(`Webhook set success @ ${url}`))
|
||||||
|
|
||||||
return this.bot.webhookCallback(`/${path}`)
|
return this.bot.webhookCallback(`/${path}`)
|
||||||
}
|
}
|
@ -1,18 +1,17 @@
|
|||||||
import { Injectable, Inject } from '@nestjs/common'
|
import { Injectable, Inject } from '@nestjs/common'
|
||||||
const Telegram = require('telegraf/telegram')
|
const Telegram = require('telegraf/telegram')
|
||||||
|
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
import { TokenInjectionToken } from './telegraf.constants'
|
||||||
import { TelegrafOptionsFactory } from './interfaces'
|
import { TelegrafOptionsFactory } from './interfaces'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegramClient {
|
export class TelegrafTelegramClientService {
|
||||||
private telegram: any
|
private telegram: any
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
@Inject(TokenInjectionToken) factory: TelegrafOptionsFactory,
|
@Inject(TokenInjectionToken) options: TelegrafOptionsFactory,
|
||||||
) {
|
) {
|
||||||
const { token } = factory.createTelegrafOptions()
|
const { token } = options.createTelegrafOptions()
|
||||||
|
|
||||||
this.telegram = new Telegram(token)
|
this.telegram = new Telegram(token)
|
||||||
}
|
}
|
||||||
|
|
@ -1 +1,2 @@
|
|||||||
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
|
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'
|
||||||
|
export const TokenInjectionToken = Symbol('TokenInjectionToken')
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
import {
|
import { Module, DynamicModule, Provider } from '@nestjs/common'
|
||||||
Module,
|
import { TelegrafBotService } from './telegraf-bot.service'
|
||||||
DynamicModule,
|
|
||||||
Provider,
|
|
||||||
} from '@nestjs/common'
|
|
||||||
import { TelegramBot } from './TelegramBot'
|
|
||||||
import {
|
import {
|
||||||
TelegrafModuleAsyncOptions,
|
TelegrafModuleAsyncOptions,
|
||||||
TelegrafOptionsFactory,
|
TelegrafOptionsFactory,
|
||||||
} from './interfaces'
|
} from './interfaces'
|
||||||
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'
|
import { TELEGRAF_MODULE_OPTIONS, TokenInjectionToken } from './telegraf.constants'
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
import { TelegrafTelegramClientService } from './telegraf-telegram-client.service'
|
||||||
import { TelegramClient } from './TelegramClient'
|
|
||||||
|
|
||||||
@Module({})
|
@Module({})
|
||||||
export class TelegrafModule {
|
export class TelegrafModule {
|
||||||
@ -20,14 +15,14 @@ export class TelegrafModule {
|
|||||||
imports: options.imports || [],
|
imports: options.imports || [],
|
||||||
providers: [
|
providers: [
|
||||||
...this.createAsyncProviders(options),
|
...this.createAsyncProviders(options),
|
||||||
TelegramBot,
|
TelegrafBotService,
|
||||||
TelegramClient,
|
TelegrafTelegramClientService,
|
||||||
{
|
{
|
||||||
provide: TokenInjectionToken,
|
provide: TokenInjectionToken,
|
||||||
useClass: options.useClass,
|
useClass: options.useClass,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
exports: [TelegramBot, TelegramClient],
|
exports: [TelegrafBotService, TelegrafTelegramClientService],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user