2020-12-26 14:54:10 +03:00
|
|
|
import { DiscoveryModule } from '@nestjs/core';
|
|
|
|
import { Module, DynamicModule, Provider } from '@nestjs/common';
|
2020-01-12 14:18:48 +03:00
|
|
|
import {
|
2020-03-19 16:21:35 +03:00
|
|
|
TelegrafModuleOptions,
|
2020-01-12 14:18:48 +03:00
|
|
|
TelegrafModuleAsyncOptions,
|
2020-12-26 14:54:10 +03:00
|
|
|
TelegrafOptionsFactory,
|
2020-01-12 14:18:48 +03:00
|
|
|
} from './interfaces';
|
2020-12-26 14:54:10 +03:00
|
|
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
2020-12-26 16:11:09 +03:00
|
|
|
import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor';
|
2020-12-26 14:54:10 +03:00
|
|
|
import { TelegrafExplorer } from './telegraf.explorer';
|
|
|
|
import { TelegrafProvider } from './telegraf.provider';
|
2019-02-28 11:29:26 +03:00
|
|
|
|
2020-12-26 14:54:10 +03:00
|
|
|
@Module({
|
|
|
|
imports: [DiscoveryModule],
|
|
|
|
providers: [TelegrafMetadataAccessor, TelegrafExplorer],
|
|
|
|
})
|
2020-01-12 01:45:10 +03:00
|
|
|
export class TelegrafModule {
|
2020-05-03 11:06:25 +03:00
|
|
|
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
2020-12-26 14:54:10 +03:00
|
|
|
const providers = [...this.createProviders(options), TelegrafProvider];
|
|
|
|
|
2019-02-28 11:29:26 +03:00
|
|
|
return {
|
2020-01-12 01:15:32 +03:00
|
|
|
module: TelegrafModule,
|
2020-12-26 14:54:10 +03:00
|
|
|
providers,
|
|
|
|
exports: providers,
|
2020-01-12 14:18:48 +03:00
|
|
|
};
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|
2020-01-12 01:36:53 +03:00
|
|
|
|
2020-12-26 14:54:10 +03:00
|
|
|
private static createProviders(options: TelegrafModuleOptions): Provider[] {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useValue: options,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
public static forRootAsync(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): DynamicModule {
|
2020-12-26 14:54:10 +03:00
|
|
|
const providers = [...this.createAsyncProviders(options), TelegrafProvider];
|
|
|
|
|
2020-01-12 01:36:53 +03:00
|
|
|
return {
|
2020-03-19 16:21:35 +03:00
|
|
|
module: TelegrafModule,
|
2020-12-26 14:54:10 +03:00
|
|
|
imports: options.imports || [],
|
|
|
|
providers,
|
|
|
|
exports: providers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createAsyncProviders(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): Provider[] {
|
|
|
|
if (options.useExisting || options.useFactory) {
|
|
|
|
return [this.createAsyncOptionsProvider(options)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
this.createAsyncOptionsProvider(options),
|
|
|
|
{
|
|
|
|
provide: options.useClass,
|
|
|
|
useClass: options.useClass,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createAsyncOptionsProvider(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): Provider {
|
|
|
|
if (options.useFactory) {
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: options.useFactory,
|
|
|
|
inject: options.inject || [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
|
|
|
|
await optionsFactory.createTelegrafOptions(),
|
|
|
|
inject: [options.useExisting || options.useClass],
|
2020-01-12 14:18:48 +03:00
|
|
|
};
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|