mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-23 22:52:59 +03:00
feat(listeners): support for chaining method listeners
This commit is contained in:
parent
0339fe15ae
commit
a4cb8df434
@ -22,7 +22,8 @@ import { TelegrafModuleOptions } from '../interfaces';
|
||||
@Injectable()
|
||||
export class ListenersExplorerService
|
||||
extends BaseExplorerService
|
||||
implements OnModuleInit {
|
||||
implements OnModuleInit
|
||||
{
|
||||
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
||||
private bot: Telegraf<any>;
|
||||
|
||||
@ -123,7 +124,7 @@ export class ListenersExplorerService
|
||||
): void {
|
||||
const methodRef = prototype[methodName];
|
||||
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
||||
if (!metadata) {
|
||||
if (!metadata || metadata.length < 1) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@ -133,22 +134,22 @@ export class ListenersExplorerService
|
||||
methodName,
|
||||
);
|
||||
|
||||
const { method, args } = metadata;
|
||||
for (const { method, args } of metadata) {
|
||||
/* Basic callback */
|
||||
// composer[method](...args, listenerCallbackFn);
|
||||
|
||||
/* Basic callback */
|
||||
// composer[method](...args, listenerCallbackFn);
|
||||
|
||||
/* Complex callback return value handing */
|
||||
composer[method](
|
||||
...args,
|
||||
async (ctx: Context, next: Function): Promise<void> => {
|
||||
const result = await listenerCallbackFn(ctx, next);
|
||||
if (result) {
|
||||
await ctx.reply(String(result));
|
||||
}
|
||||
// TODO-Possible-Feature: Add more supported return types
|
||||
},
|
||||
);
|
||||
/* Complex callback return value handing */
|
||||
composer[method](
|
||||
...args,
|
||||
async (ctx: Context, next: Function): Promise<void> => {
|
||||
const result = await listenerCallbackFn(ctx, next);
|
||||
if (result) {
|
||||
await ctx.reply(String(result));
|
||||
}
|
||||
// TODO-Possible-Feature: Add more supported return types
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
createContextCallback<T extends Record<string, unknown>>(
|
||||
|
@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import {
|
||||
SCENE_METADATA,
|
||||
LISTENER_METADATA,
|
||||
LISTENERS_METADATA,
|
||||
UPDATE_METADATA,
|
||||
} from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
@ -21,8 +21,8 @@ export class MetadataAccessorService {
|
||||
return !!this.reflector.get(SCENE_METADATA, target);
|
||||
}
|
||||
|
||||
getListenerMetadata(target: Function): ListenerMetadata | undefined {
|
||||
return this.reflector.get(LISTENER_METADATA, target);
|
||||
getListenerMetadata(target: Function): ListenerMetadata[] | undefined {
|
||||
return this.reflector.get(LISTENERS_METADATA, target);
|
||||
}
|
||||
|
||||
getSceneMetadata(target: Function): string | undefined {
|
||||
|
@ -6,7 +6,7 @@ export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME';
|
||||
|
||||
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
||||
export const SCENE_METADATA = 'SCENE_METADATA';
|
||||
export const LISTENER_METADATA = 'LISTENER_METADATA';
|
||||
export const LISTENERS_METADATA = 'LISTENERS_METADATA';
|
||||
|
||||
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
||||
|
||||
|
@ -1,17 +1,29 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { Composer } from 'telegraf';
|
||||
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
||||
import { LISTENER_METADATA } from '../telegraf.constants';
|
||||
import { LISTENERS_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createListenerDecorator<
|
||||
TComposer extends Composer<never>,
|
||||
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>
|
||||
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>,
|
||||
>(method: TMethod) {
|
||||
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
||||
return SetMetadata(LISTENER_METADATA, {
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata);
|
||||
return (
|
||||
_target: any,
|
||||
_key?: string | symbol,
|
||||
descriptor?: TypedPropertyDescriptor<any>,
|
||||
) => {
|
||||
const metadata = [
|
||||
{
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata,
|
||||
];
|
||||
|
||||
const previousValue = Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || [];
|
||||
const value = [...previousValue, ...metadata];
|
||||
Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value);
|
||||
return descriptor;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user