nestjs-telegraf/lib/telegraf-core.module.ts

122 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-01-02 01:27:01 +03:00
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
import {
Module,
DynamicModule,
Provider,
Type,
Global,
Inject,
OnApplicationShutdown,
} from '@nestjs/common';
2020-03-19 16:21:35 +03:00
import {
TelegrafModuleOptions,
TelegrafModuleAsyncOptions,
TelegrafOptionsFactory,
} from './interfaces';
2021-01-03 13:24:07 +03:00
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
2021-01-02 21:40:13 +03:00
import {
MetadataAccessorService,
2021-01-03 16:11:17 +03:00
ListenersExplorerService,
2021-01-02 21:40:13 +03:00
} from './services';
2021-01-03 16:11:17 +03:00
import { getBotToken, createBotFactory } from './utils';
2020-03-19 16:21:35 +03:00
2021-01-02 01:27:01 +03:00
@Global()
2020-03-19 16:21:35 +03:00
@Module({
imports: [DiscoveryModule],
2021-01-02 21:40:13 +03:00
providers: [
2021-01-03 16:11:17 +03:00
ListenersExplorerService,
2021-01-02 21:40:13 +03:00
MetadataAccessorService,
],
2020-03-19 16:21:35 +03:00
})
2021-01-02 01:27:01 +03:00
export class TelegrafCoreModule implements OnApplicationShutdown {
constructor(
2021-01-03 13:24:07 +03:00
@Inject(TELEGRAF_MODULE_OPTIONS)
private readonly options: TelegrafModuleOptions,
2021-01-02 01:27:01 +03:00
private readonly moduleRef: ModuleRef,
2021-01-02 01:32:49 +03:00
) {}
2021-01-02 01:27:01 +03:00
2020-03-19 16:21:35 +03:00
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
const telegrafBotProvider: Provider = {
2021-01-03 13:24:07 +03:00
provide: getBotToken(options.name),
useFactory: async () => await createBotFactory(options),
2020-03-19 19:23:51 +03:00
};
2021-01-02 01:27:01 +03:00
2020-03-19 16:21:35 +03:00
return {
module: TelegrafCoreModule,
2020-03-19 19:23:51 +03:00
providers: [
2021-01-02 01:27:01 +03:00
{
provide: TELEGRAF_MODULE_OPTIONS,
useValue: options,
},
telegrafBotProvider,
2020-03-19 19:23:51 +03:00
],
2021-01-02 01:27:01 +03:00
exports: [telegrafBotProvider],
2020-03-19 16:21:35 +03:00
};
}
public static forRootAsync(
options: TelegrafModuleAsyncOptions,
): DynamicModule {
2021-01-02 01:27:01 +03:00
const telegrafBotName = getBotToken(options.botName);
const telegrafBotProvider: Provider = {
2021-01-02 01:27:01 +03:00
provide: telegrafBotName,
useFactory: async (options: TelegrafModuleOptions) =>
await createBotFactory(options),
2020-03-19 16:21:35 +03:00
inject: [TELEGRAF_MODULE_OPTIONS],
};
2021-01-02 01:27:01 +03:00
2020-03-19 16:21:35 +03:00
const asyncProviders = this.createAsyncProviders(options);
return {
module: TelegrafCoreModule,
imports: options.imports,
2021-01-03 13:24:07 +03:00
providers: [...asyncProviders, telegrafBotProvider],
2021-01-02 01:27:01 +03:00
exports: [telegrafBotProvider],
2020-03-19 16:21:35 +03:00
};
}
async onApplicationShutdown(): Promise<void> {
2021-01-03 13:24:07 +03:00
const botName = getBotToken(this.options.name);
const bot = this.moduleRef.get<any>(botName);
bot && (await bot.stop());
2021-01-02 01:27:01 +03:00
}
2020-03-19 16:21:35 +03:00
private static createAsyncProviders(
options: TelegrafModuleAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
const useClass = options.useClass as Type<TelegrafOptionsFactory>;
return [
this.createAsyncOptionsProvider(options),
{
provide: useClass,
useClass,
},
];
}
private static createAsyncOptionsProvider(
options: TelegrafModuleAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
// `as Type<TelegrafOptionsFactory>` is a workaround for microsoft/TypeScript#31603
const inject = [
(options.useClass || options.useExisting) as Type<TelegrafOptionsFactory>,
];
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
await optionsFactory.createTelegrafOptions(),
inject,
};
}
}