mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-25 15:34:38 +03:00
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
import { DiscoveryService } from '@nestjs/core';
|
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
|
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 {
|
|
constructor(
|
|
private readonly telegraf: TelegrafProvider,
|
|
private readonly discoveryService: DiscoveryService,
|
|
private readonly metadataAccessor: TelegrafMetadataAccessor,
|
|
private readonly metadataScanner: MetadataScanner,
|
|
) {}
|
|
|
|
onModuleInit(): void {
|
|
this.explore();
|
|
}
|
|
|
|
explore(): void {
|
|
const updateClasses = this.filterUpdateClasses();
|
|
|
|
updateClasses.forEach((wrapper) => {
|
|
const { instance } = wrapper;
|
|
|
|
const prototype = Object.getPrototypeOf(instance);
|
|
this.metadataScanner.scanFromPrototype(
|
|
instance,
|
|
prototype,
|
|
(methodKey: string) => this.registerIfListener(instance, methodKey),
|
|
);
|
|
});
|
|
}
|
|
|
|
private filterUpdateClasses(): InstanceWrapper[] {
|
|
return this.discoveryService
|
|
.getProviders()
|
|
.filter((wrapper) => wrapper.instance)
|
|
.filter((wrapper) =>
|
|
this.metadataAccessor.isUpdate(wrapper.instance.constructor),
|
|
);
|
|
}
|
|
|
|
private registerIfListener(
|
|
instance: Record<string, Function>,
|
|
methodKey: string,
|
|
): void {
|
|
const methodRef = instance[methodKey];
|
|
const middlewareFn = methodRef.bind(instance);
|
|
|
|
const listenerMethod = this.metadataAccessor.getListenerMethod(methodRef);
|
|
if (!listenerMethod) return;
|
|
|
|
const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef);
|
|
|
|
// 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);
|
|
}
|
|
}
|