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

161 lines
4.1 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 { telegrafStageProvider } from './stage.provider';
2022-07-09 13:15:13 +03:00
import {
allBotsMap,
telegrafAllBotsProvider,
} from './telegraf-all-bots.provider';
2021-01-05 13:36:12 +03:00
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,
2022-07-09 13:15:13 +03:00
useFactory: async () => {
const bot = await createBotFactory(options);
allBotsMap.set(telegrafBotName, bot);
return bot;
},
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,
},
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
2022-07-09 13:15:13 +03:00
telegrafAllBotsProvider,
],
exports: [
telegrafStageProvider,
2021-01-05 13:36:12 +03:00
telegrafBotNameProvider,
2021-01-02 01:27:01 +03:00
telegrafBotProvider,
2022-07-09 13:15:13 +03:00
telegrafAllBotsProvider,
2020-03-19 19:23:51 +03:00
],
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,
2022-07-09 13:15:13 +03:00
useFactory: async (options: TelegrafModuleOptions) => {
const bot = await createBotFactory(options);
allBotsMap.set(telegrafBotName, bot);
return bot;
},
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,
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
2022-07-09 13:15:13 +03:00
telegrafAllBotsProvider,
],
exports: [
telegrafStageProvider,
2021-01-05 13:36:12 +03:00
telegrafBotNameProvider,
telegrafBotProvider,
2022-07-09 13:15:13 +03:00
telegrafAllBotsProvider,
2021-01-05 13:36:12 +03:00
],
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,
};
}
}