!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');

View File

@ -1 +0,0 @@
export * from './listener-menthod.enum';

View File

@ -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',
}

View File

@ -1,5 +0,0 @@
export enum UpdateParamtypes {
CONTEXT,
NEXT,
MESSAGE,
}

View 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);
};
}

View File

@ -0,0 +1,6 @@
import { UpdateMethods } from '../telegraf.types';
export interface ListenerMetadata {
method: UpdateMethods;
args: unknown[];
}

View File

@ -1,7 +1,6 @@
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
export const UPDATE_METADATA = 'UPDATE_METADATA';
export const UPDATE_LISTENER_METHOD_METADATA =
'UPDATE_LISTENER_METHOD_METADATA';
export const UPDATE_LISTENER_OPTIONS_METADATA =
'UPDATE_LISTENER_OPTIONS_METADATA';
export const UPDATE_LISTENER_METADATA = 'UPDATE_LISTENER_METADATA';
export const SCENE_METADATA = 'SCENE_METADATA';

View File

@ -1,9 +1,10 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { DiscoveryService } from '@nestjs/core';
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 { TelegrafProvider } from './telegraf.provider';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
@Injectable()
export class TelegrafExplorer implements OnModuleInit {
@ -49,12 +50,16 @@ export class TelegrafExplorer implements OnModuleInit {
const methodRef = instance[methodKey];
const middlewareFn = methodRef.bind(instance);
const listenerMethod = this.metadataAccessor.getListenerMethod(methodRef);
if (!listenerMethod) return;
const listenerMetadata = this.metadataAccessor.getListenerMetadata(
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."
(this.telegraf as any)[listenerMethod](...listenerOptions, middlewareFn);
console.log('composerMiddlewareFn', composerMiddlewareFn);
this.telegraf.use(composerMiddlewareFn);
}
}

View File

@ -1,11 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import {
UPDATE_LISTENER_METHOD_METADATA,
UPDATE_LISTENER_METADATA,
UPDATE_METADATA,
UPDATE_LISTENER_OPTIONS_METADATA,
} from './telegraf.constants';
import { ListenerMethod } from './enums';
import { ListenerMetadata } from './interfaces/listener-metadata.interface';
@Injectable()
export class TelegrafMetadataAccessor {
@ -15,11 +14,7 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(UPDATE_METADATA, target);
}
getListenerMethod(target: Function): ListenerMethod | undefined {
return this.reflector.get(UPDATE_LISTENER_METHOD_METADATA, target);
}
getListenerOptions(target: Function): unknown[] {
return this.reflector.get(UPDATE_LISTENER_OPTIONS_METADATA, target) || [];
getListenerMetadata(target: Function): ListenerMetadata | undefined {
return this.reflector.get(UPDATE_LISTENER_METADATA, target);
}
}

View File

@ -10,8 +10,8 @@ import { Context, TelegrafModuleOptions } from './interfaces';
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
@Injectable()
export class TelegrafProvider<C extends Context = Context>
extends Telegraf<C>
export class TelegrafProvider
extends Telegraf<Context>
implements OnApplicationBootstrap, OnApplicationShutdown {
private logger = new Logger('Telegraf');
private readonly launchOptions;

View File

@ -1,26 +1,24 @@
import { Type } from '@nestjs/common/interfaces/type.interface';
import { Composer, Telegraf } from 'telegraf';
import { Context } from './interfaces';
import { Composer, Middleware, Telegraf } from 'telegraf';
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<
CtxComposer[T]
>[0];
export type UpdateMethods = Exclude<
keyof Composer<never>,
'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 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 TelegrafOption = ConstructorParameters<typeof Telegraf>[1];
export type TelegrafLaunchOption = Parameters<Telegraf['launch']>[0];