mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-12 07:01:26 +03:00
feat: TelegrafUse decorator added
This commit is contained in:
parent
9fedcb964f
commit
6fddfd26f0
@ -1,3 +1,4 @@
|
|||||||
|
export * from './telegraf-use.decorator';
|
||||||
export * from './telegraf-on.decorator';
|
export * from './telegraf-on.decorator';
|
||||||
export * from './telegraf-hears.decorator';
|
export * from './telegraf-hears.decorator';
|
||||||
export * from './telegraf-start.decorator';
|
export * from './telegraf-start.decorator';
|
||||||
|
11
lib/decorators/telegraf-use.decorator.ts
Normal file
11
lib/decorators/telegraf-use.decorator.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { SetMetadata } from '@nestjs/common';
|
||||||
|
import { DECORATORS } from '../telegraf.constants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a middleware.
|
||||||
|
*
|
||||||
|
* https://telegraf.js.org/#/?id=use
|
||||||
|
*/
|
||||||
|
export function TelegrafUse(): MethodDecorator {
|
||||||
|
return SetMetadata(DECORATORS.USE, {});
|
||||||
|
}
|
@ -7,6 +7,13 @@ import { DECORATORS } from './telegraf.constants';
|
|||||||
export class TelegrafMetadataAccessor {
|
export class TelegrafMetadataAccessor {
|
||||||
constructor(private readonly reflector: Reflector) {}
|
constructor(private readonly reflector: Reflector) {}
|
||||||
|
|
||||||
|
isTelegrafUse(target: Type<any> | Function): boolean {
|
||||||
|
if (!target) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !!this.reflector.get(DECORATORS.USE, target);
|
||||||
|
}
|
||||||
|
|
||||||
isTelegrafStart(target: Type<any> | Function): boolean {
|
isTelegrafStart(target: Type<any> | Function): boolean {
|
||||||
if (!target) {
|
if (!target) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -3,6 +3,7 @@ export const TELEGRAF_PROVIDER = 'TELEGRAF_PROVIDER';
|
|||||||
|
|
||||||
export const DECORATORS_PREFIX = 'TELEGRAF';
|
export const DECORATORS_PREFIX = 'TELEGRAF';
|
||||||
export const DECORATORS = {
|
export const DECORATORS = {
|
||||||
|
USE: `${DECORATORS_PREFIX}/USE`,
|
||||||
ON: `${DECORATORS_PREFIX}/ON`,
|
ON: `${DECORATORS_PREFIX}/ON`,
|
||||||
HEARS: `${DECORATORS_PREFIX}/HEARS`,
|
HEARS: `${DECORATORS_PREFIX}/HEARS`,
|
||||||
START: `${DECORATORS_PREFIX}/START`,
|
START: `${DECORATORS_PREFIX}/START`,
|
||||||
|
@ -39,7 +39,9 @@ export class TelegrafExplorer implements OnModuleInit {
|
|||||||
instance,
|
instance,
|
||||||
Object.getPrototypeOf(instance),
|
Object.getPrototypeOf(instance),
|
||||||
(key: string) => {
|
(key: string) => {
|
||||||
if (this.metadataAccessor.isTelegrafStart(instance[key])) {
|
if (this.metadataAccessor.isTelegrafUse(instance[key])) {
|
||||||
|
this.handleTelegrafUse(instance, key, telegraf);
|
||||||
|
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
|
||||||
this.handleTelegrafStart(instance, key, telegraf);
|
this.handleTelegrafStart(instance, key, telegraf);
|
||||||
} else if (this.metadataAccessor.isTelegrafOn(instance[key])) {
|
} else if (this.metadataAccessor.isTelegrafOn(instance[key])) {
|
||||||
const metadata = this.metadataAccessor.getTelegrafOnMetadata(
|
const metadata = this.metadataAccessor.getTelegrafOnMetadata(
|
||||||
@ -57,6 +59,14 @@ export class TelegrafExplorer implements OnModuleInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleTelegrafUse(
|
||||||
|
instance: object,
|
||||||
|
key: string,
|
||||||
|
telegraf: Telegraf<ContextMessageUpdate>,
|
||||||
|
) {
|
||||||
|
telegraf.use(instance[key].bind(instance));
|
||||||
|
}
|
||||||
|
|
||||||
handleTelegrafOn(
|
handleTelegrafOn(
|
||||||
instance: object,
|
instance: object,
|
||||||
key: string,
|
key: string,
|
||||||
|
Loading…
Reference in New Issue
Block a user