mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-04-20 06:33:45 +03:00
feat(decorators): TelegrafEntity added
This commit is contained in:
parent
55c6f939a4
commit
8bd20131a1
@ -4,3 +4,4 @@ export * from './telegraf-hears.decorator';
|
|||||||
export * from './telegraf-command.decorator';
|
export * from './telegraf-command.decorator';
|
||||||
export * from './telegraf-start.decorator';
|
export * from './telegraf-start.decorator';
|
||||||
export * from './telegraf-help.decorator';
|
export * from './telegraf-help.decorator';
|
||||||
|
export * from './telegraf-entity.decorator';
|
||||||
|
14
lib/decorators/telegraf-entity.decorator.ts
Normal file
14
lib/decorators/telegraf-entity.decorator.ts
Normal file
@ -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 });
|
||||||
|
}
|
@ -67,4 +67,15 @@ export class TelegrafMetadataAccessor {
|
|||||||
}
|
}
|
||||||
return !!this.reflector.get(DECORATORS.SETTINGS, target);
|
return !!this.reflector.get(DECORATORS.SETTINGS, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isTelegrafEntity(target: Type<any> | Function): boolean {
|
||||||
|
if (!target) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !!this.reflector.get(DECORATORS.ENTITY, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
getTelegrafEntityMetadata(target: Type<any> | Function) {
|
||||||
|
return this.reflector.get(DECORATORS.ENTITY, target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,4 +10,5 @@ export const DECORATORS = {
|
|||||||
START: `${DECORATORS_PREFIX}/START`,
|
START: `${DECORATORS_PREFIX}/START`,
|
||||||
HELP: `${DECORATORS_PREFIX}/HELP`,
|
HELP: `${DECORATORS_PREFIX}/HELP`,
|
||||||
SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`,
|
SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`,
|
||||||
|
ENTITY: `${DECORATORS_PREFIX}/ENTITY`,
|
||||||
};
|
};
|
||||||
|
@ -62,6 +62,11 @@ export class TelegrafExplorer implements OnModuleInit {
|
|||||||
this.handleTelegrafHelp(instance, key, telegraf);
|
this.handleTelegrafHelp(instance, key, telegraf);
|
||||||
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
|
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
|
||||||
this.handleTelegrafSettings(instance, key, telegraf);
|
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
|
// @ts-ignore
|
||||||
telegraf.settings(instance[key].bind(instance));
|
telegraf.settings(instance[key].bind(instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleTelegrafEntity(
|
||||||
|
instance: object,
|
||||||
|
key: string,
|
||||||
|
telegraf: Telegraf<ContextMessageUpdate>,
|
||||||
|
metadata: any,
|
||||||
|
) {
|
||||||
|
// @ts-ignore
|
||||||
|
telegraf.entity(metadata.entity, instance[key].bind(instance));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user