feat(decorators): metadata typings added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 18:20:07 +03:00
parent dd59da7737
commit 25710b7908
13 changed files with 162 additions and 34 deletions

View File

@ -2,12 +2,20 @@ import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
import { HearsTriggers } from 'telegraf';
export type TelegrafActionTriggers = HearsTriggers;
export interface TelegrafActionMetadata {
triggers: TelegrafActionTriggers;
}
/**
* Registers middleware for handling callback_data actions with regular expressions.
* @param triggers Triggers
*
* https://telegraf.js.org/#/?id=action
*/
export function TelegrafAction(triggers: HearsTriggers): MethodDecorator {
export function TelegrafAction(
triggers: TelegrafActionTriggers,
): MethodDecorator {
return SetMetadata(DECORATORS.ACTION, { triggers });
}

View File

@ -1,12 +1,20 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type TelegrafCashtagCashtag = string | string[];
export interface TelegrafCashtagMetadata {
cashtag: TelegrafCashtagCashtag;
}
/**
* Cashtag handling.
* @param cashtag Cashtag
*
* https://telegraf.js.org/#/?id=cashtag
*/
export function TelegrafCashtag(cashtag: string | string[]): MethodDecorator {
export function TelegrafCashtag(
cashtag: TelegrafCashtagCashtag,
): MethodDecorator {
return SetMetadata(DECORATORS.CASHTAG, { cashtag });
}

View File

@ -1,12 +1,20 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type TelegrafCommandCommands = string | string[];
export interface TelegrafCommandMetadata {
commands: TelegrafCommandCommands;
}
/**
* Command handling.
* @param commands Commands
*
* https://telegraf.js.org/#/?id=command
*/
export function TelegrafCommand(commands: string | string[]): MethodDecorator {
export function TelegrafCommand(
commands: TelegrafCommandCommands,
): MethodDecorator {
return SetMetadata(DECORATORS.COMMAND, { commands });
}

View File

@ -1,7 +1,16 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type Entity = string | string[] | RegExp | RegExp[] | Function;
export type TelegrafEntityEntity =
| string
| string[]
| RegExp
| RegExp[]
| Function;
export interface TelegrafEntityMetadata {
entity: TelegrafEntityEntity;
}
/**
* Entity handling.
@ -9,6 +18,6 @@ export type Entity = string | string[] | RegExp | RegExp[] | Function;
*
* https://telegraf.js.org/#/?id=entity
*/
export function TelegrafEntity(entity: Entity): MethodDecorator {
export function TelegrafEntity(entity: TelegrafEntityEntity): MethodDecorator {
return SetMetadata(DECORATORS.ENTITY, { entity });
}

View File

@ -1,12 +1,20 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type TelegrafHashtagHashtag = string | string[];
export interface TelegrafHashtagMetadata {
hashtag: TelegrafHashtagHashtag;
}
/**
* Hashtag handling.
* @param hashtag Hashtag
*
* https://telegraf.js.org/#/?id=hashtag
*/
export function TelegrafHashtag(hashtag: string | string[]): MethodDecorator {
export function TelegrafHashtag(
hashtag: TelegrafHashtagHashtag,
): MethodDecorator {
return SetMetadata(DECORATORS.HASHTAG, { hashtag });
}

View File

@ -2,12 +2,20 @@ import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
import { HearsTriggers } from 'telegraf';
export type TelegrafHearsTriggers = HearsTriggers;
export interface TelegrafHearsMetadata {
triggers: TelegrafHearsTriggers;
}
/**
* Registers middleware for handling text messages.
* @param triggers Triggers
*
* https://telegraf.js.org/#/?id=hears
*/
export function TelegrafHears(triggers: HearsTriggers): MethodDecorator {
export function TelegrafHears(
triggers: TelegrafHearsTriggers,
): MethodDecorator {
return SetMetadata(DECORATORS.HEARS, { triggers: triggers });
}

View File

@ -1,7 +1,11 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type Triggers = string | string[] | RegExp | RegExp[];
export type TelegrafInlineQueryTriggers = string | string[] | RegExp | RegExp[];
export interface TelegrafInlineQueryMetadata {
triggers: TelegrafInlineQueryTriggers;
}
/**
* Registers middleware for handling inline_query actions with regular expressions.
@ -9,6 +13,8 @@ export type Triggers = string | string[] | RegExp | RegExp[];
*
* https://telegraf.js.org/#/?id=inlinequery
*/
export function TelegrafInlineQuery(triggers: Triggers): MethodDecorator {
export function TelegrafInlineQuery(
triggers: TelegrafInlineQueryTriggers,
): MethodDecorator {
return SetMetadata(DECORATORS.INLINE_QUERY, { triggers });
}

View File

@ -1,12 +1,20 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type TelegrafMentionUsername = string | string[];
export interface TelegrafMentionMetadata {
username: TelegrafMentionUsername;
}
/**
* Mention handling.
* @param username Username
*
* https://telegraf.js.org/#/?id=mention
*/
export function TelegrafMention(username: string | string[]): MethodDecorator {
export function TelegrafMention(
username: TelegrafMentionUsername,
): MethodDecorator {
return SetMetadata(DECORATORS.MENTION, { username });
}

View File

@ -2,6 +2,16 @@ import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types';
export type TelegrafOnUpdateTypes =
| UpdateType
| UpdateType[]
| MessageSubTypes
| MessageSubTypes[];
export interface TelegrafOnMetadata {
updateTypes: TelegrafOnUpdateTypes;
}
/**
* Registers middleware for provided update type.
* @param updateTypes Update type
@ -9,7 +19,7 @@ import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types';
* https://telegraf.js.org/#/?id=on
*/
export function TelegrafOn(
updateTypes: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[],
updateTypes: TelegrafOnUpdateTypes,
): MethodDecorator {
return SetMetadata(DECORATORS.ON, { updateTypes: updateTypes });
}

View File

@ -1,12 +1,18 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type TelegrafPhonePhone = string | string[];
export interface TelegrafPhoneMetadata {
phone: TelegrafPhonePhone;
}
/**
* Phone number handling.
* @param phone Phone number
*
* https://telegraf.js.org/#/?id=phone
*/
export function TelegrafPhone(phone: string | string[]): MethodDecorator {
export function TelegrafPhone(phone: TelegrafPhonePhone): MethodDecorator {
return SetMetadata(DECORATORS.PHONE, { phone });
}

View File

@ -1,6 +1,18 @@
import { Injectable, Type } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { TelegrafStart } from './decorators';
import {
TelegrafActionMetadata,
TelegrafCashtagMetadata,
TelegrafCommandMetadata,
TelegrafEntityMetadata,
TelegrafHashtagMetadata,
TelegrafHearsMetadata,
TelegrafInlineQueryMetadata,
TelegrafMentionMetadata,
TelegrafOnMetadata,
TelegrafPhoneMetadata,
TelegrafStart,
} from './decorators';
import { DECORATORS } from './telegraf.constants';
@Injectable()
@ -21,7 +33,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.ON, target);
}
getTelegrafOnMetadata(target: Type<any> | Function) {
getTelegrafOnMetadata(
target: Type<any> | Function,
): TelegrafOnMetadata | undefined {
return this.reflector.get(DECORATORS.ON, target);
}
@ -32,7 +46,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.HEARS, target);
}
getTelegrafHearsMetadata(target: Type<any> | Function) {
getTelegrafHearsMetadata(
target: Type<any> | Function,
): TelegrafHearsMetadata | undefined {
return this.reflector.get(DECORATORS.HEARS, target);
}
@ -43,7 +59,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.COMMAND, target);
}
getTelegrafCommandMetadata(target: Type<any> | Function) {
getTelegrafCommandMetadata(
target: Type<any> | Function,
): TelegrafCommandMetadata | undefined {
return this.reflector.get(DECORATORS.COMMAND, target);
}
@ -75,7 +93,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.ENTITY, target);
}
getTelegrafEntityMetadata(target: Type<any> | Function) {
getTelegrafEntityMetadata(
target: Type<any> | Function,
): TelegrafEntityMetadata | undefined {
return this.reflector.get(DECORATORS.ENTITY, target);
}
@ -86,7 +106,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.MENTION, target);
}
getTelegrafMentionMetadata(target: Type<any> | Function) {
getTelegrafMentionMetadata(
target: Type<any> | Function,
): TelegrafMentionMetadata | undefined {
return this.reflector.get(DECORATORS.MENTION, target);
}
@ -97,7 +119,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.PHONE, target);
}
getTelegrafPhoneMetadata(target: Type<any> | Function) {
getTelegrafPhoneMetadata(
target: Type<any> | Function,
): TelegrafPhoneMetadata | undefined {
return this.reflector.get(DECORATORS.PHONE, target);
}
@ -108,7 +132,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.HASHTAG, target);
}
getTelegrafHashtagMetadata(target: Type<any> | Function) {
getTelegrafHashtagMetadata(
target: Type<any> | Function,
): TelegrafHashtagMetadata | undefined {
return this.reflector.get(DECORATORS.HASHTAG, target);
}
@ -119,7 +145,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.CASHTAG, target);
}
getTelegrafCashtagMetadata(target: Type<any> | Function) {
getTelegrafCashtagMetadata(
target: Type<any> | Function,
): TelegrafCashtagMetadata | undefined {
return this.reflector.get(DECORATORS.CASHTAG, target);
}
@ -130,7 +158,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.ACTION, target);
}
getTelegrafActionMetadata(target: Type<any> | Function) {
getTelegrafActionMetadata(
target: Type<any> | Function,
): TelegrafActionMetadata | undefined {
return this.reflector.get(DECORATORS.ACTION, target);
}
@ -141,7 +171,9 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.INLINE_QUERY, target);
}
getTelegrafInlineQueryMetadata(target: Type<any> | Function) {
getTelegrafInlineQueryMetadata(
target: Type<any> | Function,
): TelegrafInlineQueryMetadata | undefined {
return this.reflector.get(DECORATORS.INLINE_QUERY, target);
}

View File

@ -7,6 +7,18 @@ import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
import { TelegrafProvider } from './telegraf.provider';
import { TELEGRAF_PROVIDER } from './telegraf.constants';
import { ContextMessageUpdate } from 'telegraf';
import {
TelegrafActionMetadata,
TelegrafCashtagMetadata,
TelegrafCommandMetadata,
TelegrafEntityMetadata,
TelegrafHashtagMetadata,
TelegrafHearsMetadata,
TelegrafInlineQueryMetadata,
TelegrafMentionMetadata,
TelegrafOnMetadata,
TelegrafPhoneMetadata,
} from './decorators';
@Injectable()
export class TelegrafExplorer implements OnModuleInit {
@ -119,7 +131,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafOnMetadata,
) {
telegraf.on(metadata.updateTypes, instance[key].bind(instance));
}
@ -128,7 +140,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafHearsMetadata,
) {
telegraf.hears(metadata.triggers, instance[key].bind(instance));
}
@ -137,7 +149,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafCommandMetadata,
) {
telegraf.command(metadata.commands, instance[key].bind(instance));
}
@ -171,7 +183,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafEntityMetadata,
) {
// @ts-ignore
telegraf.entity(metadata.entity, instance[key].bind(instance));
@ -181,7 +193,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafMentionMetadata,
) {
// @ts-ignore
telegraf.mention(metadata.username, instance[key].bind(instance));
@ -191,7 +203,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafPhoneMetadata,
) {
// @ts-ignore
telegraf.phone(metadata.phone, instance[key].bind(instance));
@ -201,7 +213,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafHashtagMetadata,
) {
// @ts-ignore
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
@ -211,7 +223,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafCashtagMetadata,
) {
// @ts-ignore
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
@ -221,7 +233,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafActionMetadata,
) {
telegraf.action(metadata.triggers, instance[key].bind(instance));
}
@ -230,7 +242,7 @@ export class TelegrafExplorer implements OnModuleInit {
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
metadata: TelegrafInlineQueryMetadata,
) {
// @ts-ignore
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));

View File

@ -3,6 +3,7 @@ import {
Inject,
OnApplicationBootstrap,
Logger,
OnApplicationShutdown,
} from '@nestjs/common';
import Telegraf, { ContextMessageUpdate } from 'telegraf';
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
@ -12,7 +13,7 @@ import { TelegrafModuleOptions } from './interfaces';
// @ts-ignore
export class TelegrafProvider<TContext extends ContextMessageUpdate>
extends Telegraf<TContext>
implements OnApplicationBootstrap {
implements OnApplicationBootstrap, OnApplicationShutdown {
private logger = new Logger('Telegraf');
constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) {
@ -25,4 +26,8 @@ export class TelegrafProvider<TContext extends ContextMessageUpdate>
});
this.startPolling();
}
async onApplicationShutdown(signal?: string) {
await this.stop();
}
}