2020-01-12 14:18:48 +03:00
|
|
|
import { Module, DynamicModule, Provider } from '@nestjs/common';
|
|
|
|
import {
|
|
|
|
TelegrafModuleAsyncOptions,
|
|
|
|
TelegrafOptionsFactory
|
|
|
|
} from './interfaces';
|
|
|
|
import {
|
|
|
|
TELEGRAF_MODULE_OPTIONS,
|
|
|
|
TokenInjectionToken
|
|
|
|
} from './telegraf.constants';
|
|
|
|
import { TelegrafService, TelegrafTelegramService } from './';
|
2019-02-28 11:29:26 +03:00
|
|
|
|
|
|
|
@Module({})
|
2020-01-12 01:45:10 +03:00
|
|
|
export class TelegrafModule {
|
2020-01-12 01:36:53 +03:00
|
|
|
static fromFactory(options: TelegrafModuleAsyncOptions): DynamicModule {
|
2019-02-28 11:29:26 +03:00
|
|
|
return {
|
2020-01-12 01:15:32 +03:00
|
|
|
module: TelegrafModule,
|
2020-01-12 01:36:53 +03:00
|
|
|
imports: options.imports || [],
|
2019-02-28 11:29:26 +03:00
|
|
|
providers: [
|
2020-01-12 01:36:53 +03:00
|
|
|
...this.createAsyncProviders(options),
|
2020-01-12 10:53:45 +03:00
|
|
|
TelegrafService,
|
|
|
|
TelegrafTelegramService,
|
2019-02-28 11:29:26 +03:00
|
|
|
{
|
|
|
|
provide: TokenInjectionToken,
|
2020-01-12 14:18:48 +03:00
|
|
|
useClass: options.useClass
|
|
|
|
}
|
2019-02-28 11:29:26 +03:00
|
|
|
],
|
2020-01-12 14:18:48 +03:00
|
|
|
exports: [TelegrafService, TelegrafTelegramService]
|
|
|
|
};
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|
2020-01-12 01:36:53 +03:00
|
|
|
|
|
|
|
private static createAsyncProviders(
|
2020-01-12 14:18:48 +03:00
|
|
|
options: TelegrafModuleAsyncOptions
|
2020-01-12 01:36:53 +03:00
|
|
|
): Provider[] {
|
|
|
|
if (options.useExisting || options.useFactory) {
|
2020-01-12 14:18:48 +03:00
|
|
|
return [this.createAsyncOptionsProvider(options)];
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
|
|
|
return [
|
|
|
|
this.createAsyncOptionsProvider(options),
|
|
|
|
{
|
|
|
|
provide: options.useClass,
|
2020-01-12 14:18:48 +03:00
|
|
|
useClass: options.useClass
|
|
|
|
}
|
|
|
|
];
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static createAsyncOptionsProvider(
|
2020-01-12 14:18:48 +03:00
|
|
|
options: TelegrafModuleAsyncOptions
|
2020-01-12 01:36:53 +03:00
|
|
|
): Provider {
|
|
|
|
if (options.useFactory) {
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: options.useFactory,
|
2020-01-12 14:18:48 +03:00
|
|
|
inject: options.inject || []
|
|
|
|
};
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
|
|
|
|
await optionsFactory.createTelegrafOptions(),
|
2020-01-12 14:18:48 +03:00
|
|
|
inject: [options.useExisting || options.useClass]
|
|
|
|
};
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|