From 4ca6e18d12e18be431f795a219161d7819f41031 Mon Sep 17 00:00:00 2001 From: Morb0 Date: Wed, 6 Jan 2021 20:49:50 +0300 Subject: [PATCH] feat(param-decorators): add new paramtypes & make possible to get property from object --- lib/decorators/params/index.ts | 3 ++- .../params/message-text.decorator.ts | 10 --------- lib/decorators/params/message.decorator.ts | 21 +++++++++++++++++++ lib/decorators/params/sender.decorator.ts | 21 +++++++++++++++++++ lib/enums/telegraf-paramtype.enum.ts | 3 +-- lib/factories/telegraf-params-factory.ts | 6 ++---- 6 files changed, 47 insertions(+), 17 deletions(-) delete mode 100644 lib/decorators/params/message-text.decorator.ts create mode 100644 lib/decorators/params/message.decorator.ts create mode 100644 lib/decorators/params/sender.decorator.ts diff --git a/lib/decorators/params/index.ts b/lib/decorators/params/index.ts index 6c5b237..92a211a 100644 --- a/lib/decorators/params/index.ts +++ b/lib/decorators/params/index.ts @@ -1,3 +1,4 @@ export * from './context.decorator'; export * from './next.decorator'; -export * from './message-text.decorator'; +export * from './message.decorator'; +export * from './sender.decorator'; diff --git a/lib/decorators/params/message-text.decorator.ts b/lib/decorators/params/message-text.decorator.ts deleted file mode 100644 index 4130186..0000000 --- a/lib/decorators/params/message-text.decorator.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { PipeTransform, Type } from '@nestjs/common'; -import { createTelegrafPipesParamDecorator } from '../../utils/param-decorator.util'; -import { TelegrafParamtype } from '../../enums/telegraf-paramtype.enum'; - -export function MessageText(...pipes: (Type | PipeTransform)[]) { - return createTelegrafPipesParamDecorator(TelegrafParamtype.MESSAGE_TEXT)( - undefined, - ...pipes, - ); -} diff --git a/lib/decorators/params/message.decorator.ts b/lib/decorators/params/message.decorator.ts new file mode 100644 index 0000000..c77e512 --- /dev/null +++ b/lib/decorators/params/message.decorator.ts @@ -0,0 +1,21 @@ +import { PipeTransform, Type } from '@nestjs/common'; +import { createTelegrafPipesParamDecorator } from '../../utils/param-decorator.util'; +import { TelegrafParamtype } from '../../enums/telegraf-paramtype.enum'; + +export function Message(): ParameterDecorator; +export function Message( + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; +export function Message( + property: string, + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; +export function Message( + property?: string | (Type | PipeTransform), + ...pipes: (Type | PipeTransform)[] +) { + return createTelegrafPipesParamDecorator(TelegrafParamtype.MESSAGE)( + property, + ...pipes, + ); +} diff --git a/lib/decorators/params/sender.decorator.ts b/lib/decorators/params/sender.decorator.ts new file mode 100644 index 0000000..bd504da --- /dev/null +++ b/lib/decorators/params/sender.decorator.ts @@ -0,0 +1,21 @@ +import { PipeTransform, Type } from '@nestjs/common'; +import { createTelegrafPipesParamDecorator } from '../../utils/param-decorator.util'; +import { TelegrafParamtype } from '../../enums/telegraf-paramtype.enum'; + +export function Sender(): ParameterDecorator; +export function Sender( + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; +export function Sender( + property: string, + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; +export function Sender( + property?: string | (Type | PipeTransform), + ...pipes: (Type | PipeTransform)[] +) { + return createTelegrafPipesParamDecorator(TelegrafParamtype.SENDER)( + property, + ...pipes, + ); +} diff --git a/lib/enums/telegraf-paramtype.enum.ts b/lib/enums/telegraf-paramtype.enum.ts index 2b013d9..980aeb6 100644 --- a/lib/enums/telegraf-paramtype.enum.ts +++ b/lib/enums/telegraf-paramtype.enum.ts @@ -3,6 +3,5 @@ export enum TelegrafParamtype { NEXT, SENDER, MESSAGE, - MESSAGE_TEXT, - // TODO: Add more + // TODO-Possible-Feature: Add more paramtypes } diff --git a/lib/factories/telegraf-params-factory.ts b/lib/factories/telegraf-params-factory.ts index e52d6ba..2dad83b 100644 --- a/lib/factories/telegraf-params-factory.ts +++ b/lib/factories/telegraf-params-factory.ts @@ -18,11 +18,9 @@ export class TelegrafParamsFactory implements ParamsFactory { case TelegrafParamtype.NEXT: return next; case TelegrafParamtype.SENDER: - return ctx.from; + return ctx.from ? ctx.from[data as string] : ctx.from; case TelegrafParamtype.MESSAGE: - return ctx.message; - case TelegrafParamtype.MESSAGE_TEXT: - return ctx.message.text; + return ctx.message ? ctx.message[data as string] : ctx.message; default: return null; }