feat(param-decorators): add new paramtypes & make possible to get property from object

This commit is contained in:
Morb0 2021-01-06 20:49:50 +03:00
parent dc22f9f191
commit 4ca6e18d12
6 changed files with 47 additions and 17 deletions

View File

@ -1,3 +1,4 @@
export * from './context.decorator'; export * from './context.decorator';
export * from './next.decorator'; export * from './next.decorator';
export * from './message-text.decorator'; export * from './message.decorator';
export * from './sender.decorator';

View File

@ -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> | PipeTransform)[]) {
return createTelegrafPipesParamDecorator(TelegrafParamtype.MESSAGE_TEXT)(
undefined,
...pipes,
);
}

View File

@ -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> | PipeTransform)[]
): ParameterDecorator;
export function Message(
property: string,
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator;
export function Message(
property?: string | (Type<PipeTransform> | PipeTransform),
...pipes: (Type<PipeTransform> | PipeTransform)[]
) {
return createTelegrafPipesParamDecorator(TelegrafParamtype.MESSAGE)(
property,
...pipes,
);
}

View File

@ -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> | PipeTransform)[]
): ParameterDecorator;
export function Sender(
property: string,
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator;
export function Sender(
property?: string | (Type<PipeTransform> | PipeTransform),
...pipes: (Type<PipeTransform> | PipeTransform)[]
) {
return createTelegrafPipesParamDecorator(TelegrafParamtype.SENDER)(
property,
...pipes,
);
}

View File

@ -3,6 +3,5 @@ export enum TelegrafParamtype {
NEXT, NEXT,
SENDER, SENDER,
MESSAGE, MESSAGE,
MESSAGE_TEXT, // TODO-Possible-Feature: Add more paramtypes
// TODO: Add more
} }

View File

@ -18,11 +18,9 @@ export class TelegrafParamsFactory implements ParamsFactory {
case TelegrafParamtype.NEXT: case TelegrafParamtype.NEXT:
return next; return next;
case TelegrafParamtype.SENDER: case TelegrafParamtype.SENDER:
return ctx.from; return ctx.from ? ctx.from[data as string] : ctx.from;
case TelegrafParamtype.MESSAGE: case TelegrafParamtype.MESSAGE:
return ctx.message; return ctx.message ? ctx.message[data as string] : ctx.message;
case TelegrafParamtype.MESSAGE_TEXT:
return ctx.message.text;
default: default:
return null; return null;
} }