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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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 './interfaces';
export * from './helpers';
export * from './utils';
export * from './telegraf.module';
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 './listener-metadata.interface';
export * from './update-metadata.interface';

View File

@ -1,7 +1,6 @@
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { Module } from '@nestjs/core/injector/module';
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash';
import { UpdateMetadata } from '../interfaces';
import { flattenDeep, identity, isEmpty } from 'lodash';
export class BaseExplorerService {
getModules(
@ -21,11 +20,11 @@ export class BaseExplorerService {
): Module[] {
const modules = [...modulesContainer.values()];
return modules.filter(({ metatype }) =>
include.some((item) => item === metatype),
include.includes(metatype),
);
}
flatMap<T = UpdateMetadata>(
flatMap<T>(
modules: Module[],
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
): T[] {
@ -37,23 +36,4 @@ export class BaseExplorerService {
};
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 './scenes-explorer.service';
export * from './updates-explorer.service';
export * from './listeners-explorer.service';

View File

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

View File

@ -18,13 +18,6 @@ export class MetadataAccessorService {
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 {
if (!target) {
return false;

View File

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

View File

@ -17,5 +17,5 @@ export type ComposerMethodArgs<
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
> = Filter<Parameters<T[U]>, Middleware<never>>;
export type UpdateMethods = OnlyFunctionPropertyNames<Composer<never>>;
export type ComposerMethods = OnlyFunctionPropertyNames<Composer<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 { ListenerMetadata } from '../interfaces';
export function createListenerDecorator<Method extends SceneMethods>(
method: Method,
export function createListenerDecorator<TMethod extends SceneMethods>(
method: TMethod,
) {
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 => {
return SetMetadata(UPDATE_LISTENER_METADATA, {
method,

View File

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