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

135 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-01-02 01:27:01 +03:00
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
import {
DynamicModule,
Global,
Inject,
2021-01-05 13:36:12 +03:00
Module,
2021-01-02 01:27:01 +03:00
OnApplicationShutdown,
2021-01-05 13:36:12 +03:00
Provider,
Type,
2021-01-02 01:27:01 +03:00
} from '@nestjs/common';
2020-03-19 16:21:35 +03:00
import {
TelegrafModuleAsyncOptions,
2021-01-05 13:36:12 +03:00
TelegrafModuleOptions,
2020-03-19 16:21:35 +03:00
TelegrafOptionsFactory,
} from './interfaces';
2021-01-05 13:36:12 +03:00
import {
TELEGRAF_BOT_NAME,
TELEGRAF_MODULE_OPTIONS,
} from './telegraf.constants';
import { ListenersExplorerService, MetadataAccessorService } from './services';
import { createBotFactory, getBotToken } 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-05 12:42:41 +03:00
providers: [ListenersExplorerService, 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-05 13:36:12 +03:00
@Inject(TELEGRAF_BOT_NAME)
private readonly botName: string,
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 {
2021-01-05 13:36:12 +03:00
const telegrafBotName = getBotToken(options.botName);
const telegrafBotNameProvider = {
provide: TELEGRAF_BOT_NAME,
useValue: telegrafBotName,
};
const telegrafBotProvider: Provider = {
2021-01-05 13:36:12 +03:00
provide: telegrafBotName,
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,
},
2021-01-05 13:36:12 +03:00
telegrafBotNameProvider,
2021-01-02 01:27:01 +03:00
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-05 13:36:12 +03:00
const telegrafBotName = getBotToken(options.botName);
const telegrafBotNameProvider = {
provide: TELEGRAF_BOT_NAME,
useValue: telegrafBotName,
};
2021-01-02 01:27:01 +03:00
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-05 13:36:12 +03:00
providers: [
...asyncProviders,
telegrafBotNameProvider,
telegrafBotProvider,
],
exports: [telegrafBotNameProvider, telegrafBotProvider],
2020-03-19 16:21:35 +03:00
};
}
async onApplicationShutdown(): Promise<void> {
2021-01-05 13:36:12 +03:00
const bot = this.moduleRef.get<any>(this.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,
};
}
}