mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-03-31 05:43:44 +03:00
refactor(module options): some changes
This commit is contained in:
parent
fb067b5174
commit
aa2af992b0
@ -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()
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
export interface TelegramModuleOptions {
|
|
||||||
token: string
|
|
||||||
sitePublicUrl?: string
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
import { TelegramModuleOptions } from './TelegramModuleOptions'
|
|
||||||
|
|
||||||
export interface TelegramModuleOptionsFactory {
|
|
||||||
createOptions(): TelegramModuleOptions
|
|
||||||
}
|
|
@ -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
1
lib/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './telegraf-options.interface'
|
16
lib/interfaces/telegraf-options.interface.ts
Normal file
16
lib/interfaces/telegraf-options.interface.ts
Normal 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[]
|
||||||
|
}
|
@ -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,
|
Loading…
Reference in New Issue
Block a user