From 8bd20131a10840a391fc52f5aabe7d04d948acf2 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:54:34 +0300 Subject: [PATCH] feat(decorators): TelegrafEntity added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-entity.decorator.ts | 14 ++++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 lib/decorators/telegraf-entity.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 63c6636..dd968ab 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -4,3 +4,4 @@ export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; export * from './telegraf-help.decorator'; +export * from './telegraf-entity.decorator'; diff --git a/lib/decorators/telegraf-entity.decorator.ts b/lib/decorators/telegraf-entity.decorator.ts new file mode 100644 index 0000000..cf1173d --- /dev/null +++ b/lib/decorators/telegraf-entity.decorator.ts @@ -0,0 +1,14 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +export type Entity = string | string[] | RegExp | RegExp[] | Function; + +/** + * Entity handling. + * @param entity Entity name + * + * https://telegraf.js.org/#/?id=entity + */ +export function TelegrafEntity(entity: Entity): MethodDecorator { + return SetMetadata(DECORATORS.ENTITY, { entity }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 7895bce..34c5f06 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -67,4 +67,15 @@ export class TelegrafMetadataAccessor { } return !!this.reflector.get(DECORATORS.SETTINGS, target); } + + isTelegrafEntity(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.ENTITY, target); + } + + getTelegrafEntityMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.ENTITY, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index e48a9a8..fa18e0a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -10,4 +10,5 @@ export const DECORATORS = { START: `${DECORATORS_PREFIX}/START`, HELP: `${DECORATORS_PREFIX}/HELP`, SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, + ENTITY: `${DECORATORS_PREFIX}/ENTITY`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 1528843..2665b28 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -62,6 +62,11 @@ export class TelegrafExplorer implements OnModuleInit { this.handleTelegrafHelp(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafSettings(instance[key])) { this.handleTelegrafSettings(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafEntity(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafEntityMetadata( + instance[key], + ); + this.handleTelegrafEntity(instance, key, telegraf, metadata); } }, ); @@ -127,4 +132,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.settings(instance[key].bind(instance)); } + + handleTelegrafEntity( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.entity(metadata.entity, instance[key].bind(instance)); + } }