-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenobserve.module.ts
More file actions
55 lines (49 loc) · 1.84 KB
/
openobserve.module.ts
File metadata and controls
55 lines (49 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { OpenobserveModuleAsyncOptions, OpenobserveModuleOptions } from './openobserve.model';
import { OpenobserveService } from './openobserve.service';
@Module({})
export class OpenobserveModule {
public static registerAsync(options: OpenobserveModuleAsyncOptions): DynamicModule {
const asyncProviders = this.createAsyncProviders(options);
return {
module: OpenobserveModule,
imports: options.imports || [],
providers: [...asyncProviders, OpenobserveService, ...(options.extraProviders || [])],
exports: [OpenobserveService],
};
}
private static createAsyncProviders(options: OpenobserveModuleAsyncOptions): Array<Provider> {
if (options.useFactory) {
return [
{
provide: 'OPENOBSERVE_CONFIG',
useFactory: options.useFactory,
inject: options.inject || [],
},
];
}
if (options.useClass) {
return [
{
provide: 'OPENOBSERVE_CONFIG',
useFactory: async (optionsFactory: OpenobserveModuleOptions) => optionsFactory,
inject: [options.useClass],
},
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
if (options.useExisting) {
return [
{
provide: 'OPENOBSERVE_CONFIG',
useFactory: async (optionsFactory: OpenobserveModuleOptions) => optionsFactory,
inject: [options.useExisting],
},
];
}
throw new Error('Invalid async options');
}
}