mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +03:00
!feat(): use dynamic types for listener decorators
This commit is contained in:
parent
16969365f2
commit
b3dc258c70
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafActionTriggers } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling callback_data actions with regular expressions.
|
* Registers middleware for handling callback_data actions with regular expressions.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=action
|
* @see https://telegraf.js.org/#/?id=action
|
||||||
*/
|
*/
|
||||||
export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => {
|
export const Action = createUpdateDecorator('action');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Action),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafCashtag } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cashtag handling.
|
* Cashtag handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=cashtag
|
* @see https://telegraf.js.org/#/?id=cashtag
|
||||||
*/
|
*/
|
||||||
export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => {
|
export const Cashtag = createUpdateDecorator('cashtag');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Cashtag),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [cashtag]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafCommand } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command handling.
|
* Command handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=command
|
* @see https://telegraf.js.org/#/?id=command
|
||||||
*/
|
*/
|
||||||
export const Command = (command: TelegrafCommand): MethodDecorator => {
|
export const Command = createUpdateDecorator('command');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Command),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [command]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafEmail } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with email entity.
|
* Registers middleware for handling messages with email entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-email
|
* @see https://telegraf.js.org/#/?id=telegraf-email
|
||||||
*/
|
*/
|
||||||
export const Email = (email: TelegrafEmail): MethodDecorator => {
|
export const Email = createUpdateDecorator('email');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Email),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [email]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling callback_data actions with game query.
|
* Registers middleware for handling callback_data actions with game query.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||||
*/
|
*/
|
||||||
export const GameQuery = (): MethodDecorator => {
|
export const GameQuery = createUpdateDecorator('gameQuery');
|
||||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.GameQuery);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafHashtag } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashtag handling.
|
* Hashtag handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=hashtag
|
* @see https://telegraf.js.org/#/?id=hashtag
|
||||||
*/
|
*/
|
||||||
export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => {
|
export const Hashtag = createUpdateDecorator('hashtag');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hashtag),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [hashtag]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafHearsTriggers } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling text messages.
|
* Registers middleware for handling text messages.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=hears
|
* @see https://telegraf.js.org/#/?id=hears
|
||||||
*/
|
*/
|
||||||
export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => {
|
export const Hears = createUpdateDecorator('hears');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hears),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /help command.
|
* Handler for /help command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=help
|
* @see https://telegraf.js.org/#/?id=help
|
||||||
*/
|
*/
|
||||||
export const Help = (): MethodDecorator => {
|
export const Help = createUpdateDecorator('help');
|
||||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Help);
|
|
||||||
};
|
|
||||||
|
@ -1,21 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafInlineQueryTriggers } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling inline_query actions with regular expressions.
|
* Registers middleware for handling inline_query actions with regular expressions.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||||
*/
|
*/
|
||||||
export const InlineQuery = (
|
export const InlineQuery = createUpdateDecorator('inlineQuery');
|
||||||
triggers: TelegrafInlineQueryTriggers,
|
|
||||||
): MethodDecorator => {
|
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.InlineQuery),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafMention } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mention handling.
|
* Mention handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=mention
|
* @see https://telegraf.js.org/#/?id=mention
|
||||||
*/
|
*/
|
||||||
export const Mention = (mention: TelegrafMention): MethodDecorator => {
|
export const Mention = createUpdateDecorator('mention');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Mention),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafUpdateType } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for provided update type.
|
* Registers middleware for provided update type.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=on
|
* @see https://telegraf.js.org/#/?id=on
|
||||||
*/
|
*/
|
||||||
export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => {
|
export const On = createUpdateDecorator('on');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.On),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [updateTypes]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafPhone } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phone number handling.
|
* Phone number handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=phone
|
* @see https://telegraf.js.org/#/?id=phone
|
||||||
*/
|
*/
|
||||||
export const Phone = (phone: TelegrafPhone): MethodDecorator => {
|
export const Phone = createUpdateDecorator('phone');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Phone),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [phone]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /settings command.
|
* Handler for /settings command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=settings
|
* @see https://telegraf.js.org/#/?id=settings
|
||||||
*/
|
*/
|
||||||
export const Settings = (): MethodDecorator => {
|
export const Settings = createUpdateDecorator('settings');
|
||||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Settings);
|
|
||||||
};
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /start command.
|
* Handler for /start command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=start
|
* @see https://telegraf.js.org/#/?id=start
|
||||||
*/
|
*/
|
||||||
export const Start = (): MethodDecorator => {
|
export const Start = createUpdateDecorator('start');
|
||||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Start);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafTextLink } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with text_link entity.
|
* Registers middleware for handling messages with text_link entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||||
*/
|
*/
|
||||||
export const TextLink = (link: TelegrafTextLink): MethodDecorator => {
|
export const TextLink = createUpdateDecorator('textLink');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextLink),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [link]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafTextMention } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with text_mention entity.
|
* Registers middleware for handling messages with text_mention entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||||
*/
|
*/
|
||||||
export const TextMention = (mention: TelegrafTextMention): MethodDecorator => {
|
export const TextMention = createUpdateDecorator('textMention');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextMention),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import {
|
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
|
||||||
} from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
import { TelegrafUrl } from '../../telegraf.types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with url entity.
|
* Registers middleware for handling messages with url entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-url
|
* @see https://telegraf.js.org/#/?id=telegraf-url
|
||||||
*/
|
*/
|
||||||
export const Url = (url: TelegrafUrl): MethodDecorator => {
|
export const Url = createUpdateDecorator('url');
|
||||||
return applyDecorators(
|
|
||||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Url),
|
|
||||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [url]),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
import { createUpdateDecorator } from '../../helpers/create-update-decorator.helper';
|
||||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
|
||||||
import { ListenerMethod } from '../../enums';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a middleware.
|
* Registers a middleware.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=use
|
* @see https://telegraf.js.org/#/?id=use
|
||||||
*/
|
*/
|
||||||
export const Use = (): MethodDecorator => {
|
export const Use = createUpdateDecorator('use');
|
||||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Use);
|
|
||||||
};
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
export * from './listener-menthod.enum';
|
|
@ -1,20 +0,0 @@
|
|||||||
export enum ListenerMethod {
|
|
||||||
Use = 'use',
|
|
||||||
On = 'on',
|
|
||||||
Hears = 'hears',
|
|
||||||
Command = 'command',
|
|
||||||
Start = 'start',
|
|
||||||
Help = 'help',
|
|
||||||
Settings = 'settings',
|
|
||||||
Mention = 'mention',
|
|
||||||
Phone = 'phone',
|
|
||||||
Hashtag = 'hashtag',
|
|
||||||
Cashtag = 'cashtag',
|
|
||||||
Email = 'email',
|
|
||||||
Url = 'url',
|
|
||||||
TextLink = 'textLink',
|
|
||||||
TextMention = 'textMention',
|
|
||||||
Action = 'action',
|
|
||||||
InlineQuery = 'inlineQuery',
|
|
||||||
GameQuery = 'gameQuery',
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
export enum UpdateParamtypes {
|
|
||||||
CONTEXT,
|
|
||||||
NEXT,
|
|
||||||
MESSAGE,
|
|
||||||
}
|
|
15
lib/helpers/create-update-decorator.helper.ts
Normal file
15
lib/helpers/create-update-decorator.helper.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { SetMetadata } from '@nestjs/common';
|
||||||
|
import { UpdateMethodArgs, UpdateMethods } from '../telegraf.types';
|
||||||
|
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||||
|
import { ListenerMetadata } from '../interfaces/listener-metadata.interface';
|
||||||
|
|
||||||
|
export function createUpdateDecorator<Method extends UpdateMethods>(
|
||||||
|
method: Method,
|
||||||
|
) {
|
||||||
|
return (...args: UpdateMethodArgs<Method>): MethodDecorator => {
|
||||||
|
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||||
|
method,
|
||||||
|
args,
|
||||||
|
} as ListenerMetadata);
|
||||||
|
};
|
||||||
|
}
|
6
lib/interfaces/listener-metadata.interface.ts
Normal file
6
lib/interfaces/listener-metadata.interface.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { UpdateMethods } from '../telegraf.types';
|
||||||
|
|
||||||
|
export interface ListenerMetadata {
|
||||||
|
method: UpdateMethods;
|
||||||
|
args: unknown[];
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
|
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
|
||||||
|
|
||||||
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
||||||
export const UPDATE_LISTENER_METHOD_METADATA =
|
export const UPDATE_LISTENER_METADATA = 'UPDATE_LISTENER_METADATA';
|
||||||
'UPDATE_LISTENER_METHOD_METADATA';
|
|
||||||
export const UPDATE_LISTENER_OPTIONS_METADATA =
|
export const SCENE_METADATA = 'SCENE_METADATA';
|
||||||
'UPDATE_LISTENER_OPTIONS_METADATA';
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
import { DiscoveryService } from '@nestjs/core';
|
import { DiscoveryService } from '@nestjs/core';
|
||||||
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
||||||
|
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||||
|
import { Composer } from 'telegraf';
|
||||||
import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor';
|
import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor';
|
||||||
import { TelegrafProvider } from './telegraf.provider';
|
import { TelegrafProvider } from './telegraf.provider';
|
||||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegrafExplorer implements OnModuleInit {
|
export class TelegrafExplorer implements OnModuleInit {
|
||||||
@ -49,12 +50,16 @@ export class TelegrafExplorer implements OnModuleInit {
|
|||||||
const methodRef = instance[methodKey];
|
const methodRef = instance[methodKey];
|
||||||
const middlewareFn = methodRef.bind(instance);
|
const middlewareFn = methodRef.bind(instance);
|
||||||
|
|
||||||
const listenerMethod = this.metadataAccessor.getListenerMethod(methodRef);
|
const listenerMetadata = this.metadataAccessor.getListenerMetadata(
|
||||||
if (!listenerMethod) return;
|
methodRef,
|
||||||
|
);
|
||||||
|
if (!listenerMetadata) return;
|
||||||
|
|
||||||
const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef);
|
const { method, args } = listenerMetadata;
|
||||||
|
const composerMiddlewareFn = Composer[method](...args, middlewareFn);
|
||||||
|
|
||||||
// NOTE: Disable spread operator checking because of error: "Expected at least 1 arguments, but got 1 or more."
|
console.log('composerMiddlewareFn', composerMiddlewareFn);
|
||||||
(this.telegraf as any)[listenerMethod](...listenerOptions, middlewareFn);
|
|
||||||
|
this.telegraf.use(composerMiddlewareFn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Reflector } from '@nestjs/core';
|
import { Reflector } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
UPDATE_LISTENER_METHOD_METADATA,
|
UPDATE_LISTENER_METADATA,
|
||||||
UPDATE_METADATA,
|
UPDATE_METADATA,
|
||||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
|
||||||
} from './telegraf.constants';
|
} from './telegraf.constants';
|
||||||
import { ListenerMethod } from './enums';
|
import { ListenerMetadata } from './interfaces/listener-metadata.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegrafMetadataAccessor {
|
export class TelegrafMetadataAccessor {
|
||||||
@ -15,11 +14,7 @@ export class TelegrafMetadataAccessor {
|
|||||||
return !!this.reflector.get(UPDATE_METADATA, target);
|
return !!this.reflector.get(UPDATE_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
getListenerMethod(target: Function): ListenerMethod | undefined {
|
getListenerMetadata(target: Function): ListenerMetadata | undefined {
|
||||||
return this.reflector.get(UPDATE_LISTENER_METHOD_METADATA, target);
|
return this.reflector.get(UPDATE_LISTENER_METADATA, target);
|
||||||
}
|
|
||||||
|
|
||||||
getListenerOptions(target: Function): unknown[] {
|
|
||||||
return this.reflector.get(UPDATE_LISTENER_OPTIONS_METADATA, target) || [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ import { Context, TelegrafModuleOptions } from './interfaces';
|
|||||||
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegrafProvider<C extends Context = Context>
|
export class TelegrafProvider
|
||||||
extends Telegraf<C>
|
extends Telegraf<Context>
|
||||||
implements OnApplicationBootstrap, OnApplicationShutdown {
|
implements OnApplicationBootstrap, OnApplicationShutdown {
|
||||||
private logger = new Logger('Telegraf');
|
private logger = new Logger('Telegraf');
|
||||||
private readonly launchOptions;
|
private readonly launchOptions;
|
||||||
|
@ -1,26 +1,24 @@
|
|||||||
import { Type } from '@nestjs/common/interfaces/type.interface';
|
import { Composer, Middleware, Telegraf } from 'telegraf';
|
||||||
import { Composer, Telegraf } from 'telegraf';
|
|
||||||
import { Context } from './interfaces';
|
|
||||||
|
|
||||||
type CtxComposer = Composer<Context>;
|
export type Filter<T extends any[], F> = T extends []
|
||||||
|
? []
|
||||||
|
: T extends [infer Head, ...infer Tail]
|
||||||
|
? Head extends F
|
||||||
|
? Filter<Tail, F>
|
||||||
|
: [Head, ...Filter<Tail, F>]
|
||||||
|
: [];
|
||||||
|
|
||||||
type ComposerMethodFirstArg<T extends keyof CtxComposer> = Parameters<
|
export type UpdateMethods = Exclude<
|
||||||
CtxComposer[T]
|
keyof Composer<never>,
|
||||||
>[0];
|
'middleware' | 'guard' | 'filter' | 'drop'
|
||||||
|
>;
|
||||||
|
export type UpdateMethodArgs<T extends UpdateMethods> = Filter<
|
||||||
|
Parameters<Composer<never>[T]>,
|
||||||
|
Middleware<any>
|
||||||
|
>;
|
||||||
|
// type Test0 = Filter<[['foo', 'bar', 'booz'], ...Middleware<any>[]], Middleware<any>>;
|
||||||
|
// type Test1 = UpdateMethodArgs<'on'>;
|
||||||
|
// type Test2 = Parameters<Composer<never>['on']>;
|
||||||
|
|
||||||
export type TelegrafActionTriggers = ComposerMethodFirstArg<'action'>;
|
export type TelegrafOption = ConstructorParameters<typeof Telegraf>[1];
|
||||||
export type TelegrafHearsTriggers = ComposerMethodFirstArg<'hears'>;
|
|
||||||
export type TelegrafInlineQueryTriggers = ComposerMethodFirstArg<'inlineQuery'>;
|
|
||||||
export type TelegrafEmail = ComposerMethodFirstArg<'email'>;
|
|
||||||
export type TelegrafUrl = ComposerMethodFirstArg<'url'>;
|
|
||||||
export type TelegrafTextLink = ComposerMethodFirstArg<'textLink'>;
|
|
||||||
export type TelegrafTextMention = ComposerMethodFirstArg<'textMention'>;
|
|
||||||
export type TelegrafCashtag = ComposerMethodFirstArg<'cashtag'>;
|
|
||||||
export type TelegrafHashtag = ComposerMethodFirstArg<'hashtag'>;
|
|
||||||
export type TelegrafCommand = ComposerMethodFirstArg<'command'>;
|
|
||||||
export type TelegrafMention = ComposerMethodFirstArg<'mention'>;
|
|
||||||
export type TelegrafPhone = ComposerMethodFirstArg<'phone'>;
|
|
||||||
export type TelegrafUpdateType = ComposerMethodFirstArg<'on'>;
|
|
||||||
|
|
||||||
export type TelegrafOption = ConstructorParameters<Type<Telegraf<Context>>>[1];
|
|
||||||
export type TelegrafLaunchOption = Parameters<Telegraf['launch']>[0];
|
export type TelegrafLaunchOption = Parameters<Telegraf['launch']>[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user