mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +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()
|
@Injectable()
|
||||||
export class ListenersExplorerService
|
export class ListenersExplorerService
|
||||||
extends BaseExplorerService
|
extends BaseExplorerService
|
||||||
implements OnModuleInit {
|
implements OnModuleInit
|
||||||
|
{
|
||||||
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
||||||
private bot: Telegraf<any>;
|
private bot: Telegraf<any>;
|
||||||
|
|
||||||
@ -123,7 +124,7 @@ export class ListenersExplorerService
|
|||||||
): void {
|
): void {
|
||||||
const methodRef = prototype[methodName];
|
const methodRef = prototype[methodName];
|
||||||
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
||||||
if (!metadata) {
|
if (!metadata || metadata.length < 1) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,22 +134,22 @@ export class ListenersExplorerService
|
|||||||
methodName,
|
methodName,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { method, args } = metadata;
|
for (const { method, args } of metadata) {
|
||||||
|
/* Basic callback */
|
||||||
|
// composer[method](...args, listenerCallbackFn);
|
||||||
|
|
||||||
/* Basic callback */
|
/* Complex callback return value handing */
|
||||||
// composer[method](...args, listenerCallbackFn);
|
composer[method](
|
||||||
|
...args,
|
||||||
/* Complex callback return value handing */
|
async (ctx: Context, next: Function): Promise<void> => {
|
||||||
composer[method](
|
const result = await listenerCallbackFn(ctx, next);
|
||||||
...args,
|
if (result) {
|
||||||
async (ctx: Context, next: Function): Promise<void> => {
|
await ctx.reply(String(result));
|
||||||
const result = await listenerCallbackFn(ctx, next);
|
}
|
||||||
if (result) {
|
// TODO-Possible-Feature: Add more supported return types
|
||||||
await ctx.reply(String(result));
|
},
|
||||||
}
|
);
|
||||||
// TODO-Possible-Feature: Add more supported return types
|
}
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createContextCallback<T extends Record<string, unknown>>(
|
createContextCallback<T extends Record<string, unknown>>(
|
||||||
|
@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { Reflector } from '@nestjs/core';
|
import { Reflector } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
SCENE_METADATA,
|
SCENE_METADATA,
|
||||||
LISTENER_METADATA,
|
LISTENERS_METADATA,
|
||||||
UPDATE_METADATA,
|
UPDATE_METADATA,
|
||||||
} from '../telegraf.constants';
|
} from '../telegraf.constants';
|
||||||
import { ListenerMetadata } from '../interfaces';
|
import { ListenerMetadata } from '../interfaces';
|
||||||
@ -21,8 +21,8 @@ export class MetadataAccessorService {
|
|||||||
return !!this.reflector.get(SCENE_METADATA, target);
|
return !!this.reflector.get(SCENE_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
getListenerMetadata(target: Function): ListenerMetadata | undefined {
|
getListenerMetadata(target: Function): ListenerMetadata[] | undefined {
|
||||||
return this.reflector.get(LISTENER_METADATA, target);
|
return this.reflector.get(LISTENERS_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
getSceneMetadata(target: Function): string | undefined {
|
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 UPDATE_METADATA = 'UPDATE_METADATA';
|
||||||
export const SCENE_METADATA = 'SCENE_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;
|
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
||||||
|
|
||||||
|
@ -1,17 +1,29 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
|
||||||
import { Composer } from 'telegraf';
|
import { Composer } from 'telegraf';
|
||||||
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
||||||
import { LISTENER_METADATA } from '../telegraf.constants';
|
import { LISTENERS_METADATA } from '../telegraf.constants';
|
||||||
import { ListenerMetadata } from '../interfaces';
|
import { ListenerMetadata } from '../interfaces';
|
||||||
|
|
||||||
export function createListenerDecorator<
|
export function createListenerDecorator<
|
||||||
TComposer extends Composer<never>,
|
TComposer extends Composer<never>,
|
||||||
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>
|
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>,
|
||||||
>(method: TMethod) {
|
>(method: TMethod) {
|
||||||
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
||||||
return SetMetadata(LISTENER_METADATA, {
|
return (
|
||||||
method,
|
_target: any,
|
||||||
args,
|
_key?: string | symbol,
|
||||||
} as ListenerMetadata);
|
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