!feat(): use dynamic types for listener decorators

This commit is contained in:
unknown
2020-12-27 21:35:01 +03:00
parent 16969365f2
commit b3dc258c70
28 changed files with 97 additions and 270 deletions

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafActionTriggers } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling callback_data actions with regular expressions.
*
* @see https://telegraf.js.org/#/?id=action
*/
export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Action),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
);
};
export const Action = createUpdateDecorator('action');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafCashtag } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Cashtag handling.
*
* @see https://telegraf.js.org/#/?id=cashtag
*/
export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Cashtag),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [cashtag]),
);
};
export const Cashtag = createUpdateDecorator('cashtag');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafCommand } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Command handling.
*
* @see https://telegraf.js.org/#/?id=command
*/
export const Command = (command: TelegrafCommand): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Command),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [command]),
);
};
export const Command = createUpdateDecorator('command');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafEmail } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling messages with email entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-email
*/
export const Email = (email: TelegrafEmail): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Email),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [email]),
);
};
export const Email = createUpdateDecorator('email');

View File

@@ -1,12 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling callback_data actions with game query.
*
* @see https://telegraf.js.org/#/?id=inlinequery
*/
export const GameQuery = (): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.GameQuery);
};
export const GameQuery = createUpdateDecorator('gameQuery');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafHashtag } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Hashtag handling.
*
* @see https://telegraf.js.org/#/?id=hashtag
*/
export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hashtag),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [hashtag]),
);
};
export const Hashtag = createUpdateDecorator('hashtag');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafHearsTriggers } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling text messages.
*
* @see https://telegraf.js.org/#/?id=hears
*/
export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hears),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
);
};
export const Hears = createUpdateDecorator('hears');

View File

@@ -1,12 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Handler for /help command.
*
* @see https://telegraf.js.org/#/?id=help
*/
export const Help = (): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Help);
};
export const Help = createUpdateDecorator('help');

View File

@@ -1,21 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafInlineQueryTriggers } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling inline_query actions with regular expressions.
*
* @see https://telegraf.js.org/#/?id=inlinequery
*/
export const InlineQuery = (
triggers: TelegrafInlineQueryTriggers,
): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.InlineQuery),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
);
};
export const InlineQuery = createUpdateDecorator('inlineQuery');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafMention } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Mention handling.
*
* @see https://telegraf.js.org/#/?id=mention
*/
export const Mention = (mention: TelegrafMention): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Mention),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
);
};
export const Mention = createUpdateDecorator('mention');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafUpdateType } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for provided update type.
*
* @see https://telegraf.js.org/#/?id=on
*/
export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.On),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [updateTypes]),
);
};
export const On = createUpdateDecorator('on');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafPhone } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Phone number handling.
*
* @see https://telegraf.js.org/#/?id=phone
*/
export const Phone = (phone: TelegrafPhone): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Phone),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [phone]),
);
};
export const Phone = createUpdateDecorator('phone');

View File

@@ -1,12 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Handler for /settings command.
*
* @see https://telegraf.js.org/#/?id=settings
*/
export const Settings = (): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Settings);
};
export const Settings = createUpdateDecorator('settings');

View File

@@ -1,12 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Handler for /start command.
*
* @see https://telegraf.js.org/#/?id=start
*/
export const Start = (): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Start);
};
export const Start = createUpdateDecorator('start');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafTextLink } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling messages with text_link entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-textlink
*/
export const TextLink = (link: TelegrafTextLink): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextLink),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [link]),
);
};
export const TextLink = createUpdateDecorator('textLink');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafTextMention } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling messages with text_mention entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-textlink
*/
export const TextMention = (mention: TelegrafTextMention): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextMention),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
);
};
export const TextMention = createUpdateDecorator('textMention');

View File

@@ -1,19 +1,8 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import {
UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_METHOD_METADATA,
} from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { TelegrafUrl } from '../../telegraf.types';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers middleware for handling messages with url entity.
*
* @see https://telegraf.js.org/#/?id=telegraf-url
*/
export const Url = (url: TelegrafUrl): MethodDecorator => {
return applyDecorators(
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Url),
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [url]),
);
};
export const Url = createUpdateDecorator('url');

View File

@@ -1,12 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
import { ListenerMethod } from '../../enums';
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
/**
* Registers a middleware.
*
* @see https://telegraf.js.org/#/?id=use
*/
export const Use = (): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Use);
};
export const Use = createUpdateDecorator('use');