mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
feat: add initial code
This commit is contained in:
18
lib/decorators/PipeContext.ts
Normal file
18
lib/decorators/PipeContext.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Type } from '@nestjs/common'
|
||||
import { ContextTransformer } from '../ContextTransformer'
|
||||
import { addHandlerToStore } from './TelegramActionHandler'
|
||||
|
||||
export const PipeContext = <T>(transform: Type<ContextTransformer<T>>) => (
|
||||
target: Object,
|
||||
propertyKey: string,
|
||||
parameterIndex: number,
|
||||
) => {
|
||||
addHandlerToStore(target, propertyKey, {
|
||||
transformations: [
|
||||
{
|
||||
index: parameterIndex,
|
||||
transform,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
44
lib/decorators/TelegramActionHandler.ts
Normal file
44
lib/decorators/TelegramActionHandler.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { HandleParameters } from '../HandleParameters'
|
||||
|
||||
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 || []),
|
||||
],
|
||||
})
|
||||
}
|
18
lib/decorators/TelegramCatch.ts
Normal file
18
lib/decorators/TelegramCatch.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Type } from '@nestjs/common'
|
||||
import { TelegramErrorHandler } from '../interfaces/TelegramErrorHandler'
|
||||
|
||||
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