mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
feat: complete rewrite
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
export * from './pipe-context.decorator'
|
||||
export * from './telegram-action-handler.decorator'
|
||||
export * from './telegram-catch.decorator'
|
||||
export * from './telegraf-on.decorator';
|
||||
export * from './telegraf-hears.decorator';
|
||||
export * from './telegraf-start.decorator';
|
||||
|
@@ -1,18 +0,0 @@
|
||||
import { Type } from '@nestjs/common'
|
||||
import { ContextTransformer } from '../interfaces'
|
||||
import { addHandlerToStore } from './'
|
||||
|
||||
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
||||
target: Object,
|
||||
propertyKey: string,
|
||||
parameterIndex: number,
|
||||
) => {
|
||||
addHandlerToStore(target, propertyKey, {
|
||||
transformations: [
|
||||
{
|
||||
index: parameterIndex,
|
||||
transform,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
13
lib/decorators/telegraf-hears.decorator.ts
Normal file
13
lib/decorators/telegraf-hears.decorator.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { DECORATORS } from '../telegraf.constants';
|
||||
import { HearsTriggers } from 'telegraf';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling text messages.
|
||||
* @param triggers Triggers
|
||||
*
|
||||
* https://telegraf.js.org/#/?id=hears
|
||||
*/
|
||||
export function TelegrafHears(triggers: HearsTriggers): MethodDecorator {
|
||||
return SetMetadata(DECORATORS.HEARS, { triggers: triggers });
|
||||
}
|
15
lib/decorators/telegraf-on.decorator.ts
Normal file
15
lib/decorators/telegraf-on.decorator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { DECORATORS } from '../telegraf.constants';
|
||||
import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types';
|
||||
|
||||
/**
|
||||
* Registers middleware for provided update type.
|
||||
* @param updateTypes Update type
|
||||
*
|
||||
* https://telegraf.js.org/#/?id=on
|
||||
*/
|
||||
export function TelegrafOn(
|
||||
updateTypes: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[],
|
||||
): MethodDecorator {
|
||||
return SetMetadata(DECORATORS.ON, { updateTypes: updateTypes });
|
||||
}
|
11
lib/decorators/telegraf-start.decorator.ts
Normal file
11
lib/decorators/telegraf-start.decorator.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { DECORATORS } from '../telegraf.constants';
|
||||
|
||||
/**
|
||||
* Handler for /start command.
|
||||
*
|
||||
* https://telegraf.js.org/#/?id=start
|
||||
*/
|
||||
export function TelegrafStart(): MethodDecorator {
|
||||
return SetMetadata(DECORATORS.START, {});
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
import { HandleParameters } from '../interfaces'
|
||||
|
||||
type Decorator = (
|
||||
params: HandleParameters,
|
||||
) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void
|
||||
|
||||
type HandlerDecorator = Decorator & {
|
||||
handlers?: Map<any, Map<string, HandleParameters>>
|
||||
}
|
||||
|
||||
export const TelegramActionHandler: HandlerDecorator = (
|
||||
parameters: HandleParameters,
|
||||
) => (target: any, propertyKey: string) => {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
addHandlerToStore(target, propertyKey, parameters)
|
||||
}
|
||||
|
||||
export const addHandlerToStore = (
|
||||
instance: Object,
|
||||
name: string,
|
||||
config: HandleParameters,
|
||||
) => {
|
||||
const handlerClass = instance.constructor
|
||||
|
||||
if (!TelegramActionHandler.handlers) {
|
||||
TelegramActionHandler.handlers = new Map()
|
||||
}
|
||||
|
||||
if (!TelegramActionHandler.handlers.get(handlerClass)) {
|
||||
TelegramActionHandler.handlers.set(handlerClass, new Map())
|
||||
}
|
||||
|
||||
const oldParameters =
|
||||
TelegramActionHandler.handlers.get(handlerClass).get(name) || {}
|
||||
|
||||
TelegramActionHandler.handlers.get(handlerClass).set(name, {
|
||||
...oldParameters,
|
||||
...config,
|
||||
transformations: [
|
||||
...(oldParameters.transformations || []),
|
||||
...(config.transformations || []),
|
||||
],
|
||||
})
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
import { Type } from '@nestjs/common'
|
||||
import { TelegramErrorHandler } from '../interfaces'
|
||||
|
||||
type Decorator = (error: any) => ClassDecorator
|
||||
|
||||
type HandlerDecorator = Decorator & {
|
||||
handlers?: Map<Error, Type<TelegramErrorHandler>>
|
||||
}
|
||||
|
||||
export const TelegramCatch: HandlerDecorator = error => target => {
|
||||
if (!TelegramCatch.handlers) {
|
||||
TelegramCatch.handlers = new Map()
|
||||
}
|
||||
|
||||
TelegramCatch.handlers.set(error, target as any)
|
||||
|
||||
return target
|
||||
}
|
Reference in New Issue
Block a user