fix(): missing decorators & refactor

This commit is contained in:
Morb0 2021-01-03 16:11:17 +03:00
parent 90cb548551
commit f13eebe685
30 changed files with 71 additions and 96 deletions

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('action'); export const Action = createListenerDecorator('action');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* Cashtag handling. * Cashtag handling.
* *
* @see https://telegraf.js.org/#/?id=cashtag * @see https://telegraf.js.org/#/?id=cashtag
*/ */
export const Cashtag = createUpdateListenerDecorator('cashtag'); export const Cashtag = createMissedListenerDecorator<[string | string[]]>('cashtag');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* Command handling. * Command handling.
* *
* @see https://telegraf.js.org/#/?id=command * @see https://telegraf.js.org/#/?id=command
*/ */
export const Command = createUpdateListenerDecorator('command'); export const Command = createListenerDecorator('command');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('email'); export const Email = createMissedListenerDecorator<[string | string[]]>('email');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('gameQuery'); export const GameQuery = createListenerDecorator('gameQuery');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* Hashtag handling. * Hashtag handling.
* *
* @see https://telegraf.js.org/#/?id=hashtag * @see https://telegraf.js.org/#/?id=hashtag
*/ */
export const Hashtag = createUpdateListenerDecorator('hashtag'); export const Hashtag = createMissedListenerDecorator<[string | string[]]>('hashtag');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('hears'); export const Hears = createListenerDecorator('hears');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('help'); export const Help = createListenerDecorator('help');

View File

@ -1,8 +1,9 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
import { HearsTriggers } from 'telegraf/typings/composer';
/** /**
* 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 = createUpdateListenerDecorator('inlineQuery'); export const InlineQuery = createMissedListenerDecorator<[HearsTriggers<unknown>]>('inlineQuery');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* Mention handling. * Mention handling.
* *
* @see https://telegraf.js.org/#/?id=mention * @see https://telegraf.js.org/#/?id=mention
*/ */
export const Mention = createUpdateListenerDecorator('mention'); export const Mention = createMissedListenerDecorator<[string | string[]]>('mention');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('on'); export const On = createListenerDecorator('on');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* Phone number handling. * Phone number handling.
* *
* @see https://telegraf.js.org/#/?id=phone * @see https://telegraf.js.org/#/?id=phone
*/ */
export const Phone = createUpdateListenerDecorator('phone'); export const Phone = createMissedListenerDecorator<[string | string[]]>('phone');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('settings'); export const Settings = createMissedListenerDecorator<[]>('settings');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('start'); export const Start = createListenerDecorator('start');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('textLink'); export const TextLink = createMissedListenerDecorator<[string | string[]]>('textLink');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('textMention'); export const TextMention = createMissedListenerDecorator<[string | string[]]>('textMention');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createMissedListenerDecorator } from '../../utils';
/** /**
* 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 = createUpdateListenerDecorator('url'); export const Url = createMissedListenerDecorator<[string | string[]]>('url');

View File

@ -1,8 +1,8 @@
import { createUpdateListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
/** /**
* Registers a middleware. * Registers a middleware.
* *
* @see https://telegraf.js.org/#/?id=use * @see https://telegraf.js.org/#/?id=use
*/ */
export const Use = createUpdateListenerDecorator('use'); export const Use = createListenerDecorator('use');

View File

@ -1,3 +1,3 @@
import { createSceneListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
export const SceneEnter = createSceneListenerDecorator('enter'); export const SceneEnter = createListenerDecorator('enter');

View File

@ -1,3 +1,3 @@
import { createSceneListenerDecorator } from '../../helpers'; import { createListenerDecorator } from '../../utils';
export const SceneLeave = createSceneListenerDecorator('leave'); export const SceneLeave = createListenerDecorator('leave');

View File

@ -1,15 +1,6 @@
export * as Composer from 'telegraf/composer';
export * as Markup from 'telegraf/markup';
export * as BaseScene from 'telegraf/scenes/base';
export * as session from 'telegraf/session';
export * as Stage from 'telegraf/stage';
export * as WizardScene from 'telegraf/scenes/wizard';
export * as Extra from 'telegraf/extra';
export * from './decorators'; export * from './decorators';
export * from './interfaces'; export * from './interfaces';
export * from './helpers';
export * from './utils'; export * from './utils';
export * from './telegraf.module';
export * from './types'; export * from './types';
export { Telegraf } from 'telegraf'; export * from './telegraf.constants';
export * from './telegraf.module';

View File

@ -1,3 +1,2 @@
export * from './telegraf-options.interface'; export * from './telegraf-options.interface';
export * from './listener-metadata.interface'; export * from './listener-metadata.interface';
export * from './update-metadata.interface';

View File

@ -1,7 +1,6 @@
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { Module } from '@nestjs/core/injector/module'; import { Module } from '@nestjs/core/injector/module';
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash'; import { flattenDeep, identity, isEmpty } from 'lodash';
import { UpdateMetadata } from '../interfaces';
export class BaseExplorerService { export class BaseExplorerService {
getModules( getModules(
@ -21,11 +20,11 @@ export class BaseExplorerService {
): Module[] { ): Module[] {
const modules = [...modulesContainer.values()]; const modules = [...modulesContainer.values()];
return modules.filter(({ metatype }) => return modules.filter(({ metatype }) =>
include.some((item) => item === metatype), include.includes(metatype),
); );
} }
flatMap<T = UpdateMetadata>( flatMap<T>(
modules: Module[], modules: Module[],
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[], callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
): T[] { ): T[] {
@ -37,23 +36,4 @@ export class BaseExplorerService {
}; };
return flattenDeep(invokeMap()).filter(identity); return flattenDeep(invokeMap()).filter(identity);
} }
groupMetadata(resolvers: UpdateMetadata[]) {
const groupByType = groupBy(
resolvers,
(metadata: UpdateMetadata) => metadata.type,
);
const groupedMetadata = mapValues(
groupByType,
(resolversArr: UpdateMetadata[]) =>
resolversArr.reduce(
(prev, curr) => ({
...prev,
[curr.name]: curr.callback,
}),
{},
),
);
return groupedMetadata;
}
} }

View File

@ -1,4 +1,3 @@
export * from './updates-explorer.service'; export * from './listeners-explorer.service';
export * from './metadata-accessor.service'; export * from './metadata-accessor.service';
export * from './scenes-explorer.service'; export * from './listeners-explorer.service';
export * from './updates-explorer.service';

View File

@ -12,7 +12,7 @@ import { BaseExplorerService } from './base-explorer.service';
import { getBotToken } from '../utils'; import { getBotToken } from '../utils';
@Injectable() @Injectable()
export class TelegrafExplorerService export class ListenersExplorerService
extends BaseExplorerService extends BaseExplorerService
implements OnModuleInit { implements OnModuleInit {
private readonly bot: Telegraf<any>; private readonly bot: Telegraf<any>;

View File

@ -18,13 +18,6 @@ export class MetadataAccessorService {
return !!this.reflector.get(UPDATE_METADATA, target); return !!this.reflector.get(UPDATE_METADATA, target);
} }
isUpdateListener(target: Function) {
if (!target) {
return false;
}
return !!this.reflector.get(UPDATE_LISTENER_METADATA, target);
}
isScene(target: Function): boolean { isScene(target: Function): boolean {
if (!target) { if (!target) {
return false; return false;

View File

@ -16,18 +16,15 @@ import {
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
import { import {
MetadataAccessorService, MetadataAccessorService,
ScenesExplorerService, ListenersExplorerService,
UpdatesExplorerService,
} from './services'; } from './services';
import { getBotToken } from './utils'; import { getBotToken, createBotFactory } from './utils';
import { createBotFactory } from './utils/create-bot-factory.util';
@Global() @Global()
@Module({ @Module({
imports: [DiscoveryModule], imports: [DiscoveryModule],
providers: [ providers: [
UpdatesExplorerService, ListenersExplorerService,
ScenesExplorerService,
MetadataAccessorService, MetadataAccessorService,
], ],
}) })

View File

@ -17,5 +17,5 @@ export type ComposerMethodArgs<
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T> U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
> = Filter<Parameters<T[U]>, Middleware<never>>; > = Filter<Parameters<T[U]>, Middleware<never>>;
export type UpdateMethods = OnlyFunctionPropertyNames<Composer<never>>; export type ComposerMethods = OnlyFunctionPropertyNames<Composer<never>>;
export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>; export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>;

View File

@ -4,11 +4,24 @@ import { ComposerMethodArgs, SceneMethods } from '../types';
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants'; import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
import { ListenerMetadata } from '../interfaces'; import { ListenerMetadata } from '../interfaces';
export function createListenerDecorator<Method extends SceneMethods>( export function createListenerDecorator<TMethod extends SceneMethods>(
method: Method, method: TMethod,
) { ) {
return ( return (
...args: ComposerMethodArgs<Scene<never>, Method> ...args: ComposerMethodArgs<Scene<never>, TMethod>
): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METADATA, {
method,
args,
} as ListenerMetadata);
};
}
export function createMissedListenerDecorator<TArgs extends any[]>(
method: string,
) {
return (
...args: TArgs
): MethodDecorator => { ): MethodDecorator => {
return SetMetadata(UPDATE_LISTENER_METADATA, { return SetMetadata(UPDATE_LISTENER_METADATA, {
method, method,

View File

@ -1 +1,3 @@
export * from './get-bot-token.util'; export * from './get-bot-token.util';
export * from './create-bot-factory.util';
export * from './create-listener-decorator.util';