|
1 | | -import { DynamicModule, Global, Module } from '@nestjs/common'; |
| 1 | +import { DynamicModule, Global, Module, Provider, Type } from '@nestjs/common'; |
2 | 2 | import { ReporterService } from './reporter.service'; |
3 | 3 | import { collectDefaultMetrics, Registry } from 'prom-client'; |
4 | 4 | import { MetricsConfig, ReporterAsyncOptions } from '../interfaces'; |
5 | 5 | import { MetricsService } from '../metrics/metrics.service'; |
6 | 6 | import { MetricsController } from '../metrics/metrics.controller'; |
7 | 7 | import { CONFIG_OPTIONS } from '../constants'; |
| 8 | +import { APP_INTERCEPTOR } from '@nestjs/core'; |
8 | 9 |
|
9 | 10 | @Global() |
10 | 11 | @Module( {} ) |
11 | 12 | export class ReporterModule { |
12 | 13 | static forRoot( config: MetricsConfig = {} ): DynamicModule { |
13 | 14 | const registry: Registry = this.configureRegistry( config ); |
14 | | - |
| 15 | + const providers: Provider[] = [ |
| 16 | + { |
| 17 | + provide: Registry, |
| 18 | + useValue: registry |
| 19 | + }, |
| 20 | + { |
| 21 | + provide: CONFIG_OPTIONS, |
| 22 | + useValue: config |
| 23 | + }, |
| 24 | + MetricsService, |
| 25 | + ReporterService |
| 26 | + ]; |
| 27 | + |
| 28 | + if ( config.interceptors ) { |
| 29 | + providers.push( ...config.interceptors.map( interceptor => ( { |
| 30 | + provide: APP_INTERCEPTOR, |
| 31 | + useClass: interceptor as Type<any>, |
| 32 | + } ) ) ); |
| 33 | + } |
15 | 34 | return { |
16 | 35 | module: ReporterModule, |
17 | | - providers: [ |
18 | | - { |
19 | | - provide: Registry, |
20 | | - useValue: registry |
21 | | - }, |
22 | | - { |
23 | | - provide: CONFIG_OPTIONS, |
24 | | - useValue: config |
25 | | - }, |
26 | | - MetricsService, |
27 | | - ReporterService |
28 | | - ], |
| 36 | + providers, |
29 | 37 | controllers: [ MetricsController ], |
30 | 38 | exports: [ ReporterService ] |
31 | 39 | }; |
32 | 40 | } |
33 | 41 |
|
34 | | - static forRootAsync( options: ReporterAsyncOptions ): DynamicModule { |
| 42 | + static async forRootAsync( options: ReporterAsyncOptions ): Promise<DynamicModule> { |
| 43 | + const providers: Provider[] = [ |
| 44 | + { |
| 45 | + provide: CONFIG_OPTIONS, |
| 46 | + useFactory: options.useFactory, |
| 47 | + inject: options.inject, |
| 48 | + }, |
| 49 | + { |
| 50 | + provide: Registry, |
| 51 | + useFactory: async ( config: MetricsConfig ) => { |
| 52 | + return ReporterModule.configureRegistry( config ); |
| 53 | + }, |
| 54 | + inject: [CONFIG_OPTIONS], |
| 55 | + }, |
| 56 | + MetricsService, |
| 57 | + ReporterService |
| 58 | + ]; |
| 59 | + |
| 60 | + const asyncConfig = await options.useFactory( ...( options.inject || [] ) ); |
| 61 | + if ( asyncConfig.interceptors ) { |
| 62 | + providers.push( ...asyncConfig.interceptors.map( interceptor => ( { |
| 63 | + provide: APP_INTERCEPTOR, |
| 64 | + useClass: interceptor as Type<any>, |
| 65 | + } ) ) ); |
| 66 | + } |
| 67 | + |
35 | 68 | return { |
36 | 69 | module: ReporterModule, |
37 | 70 | imports: options.imports, |
38 | | - providers: [ |
39 | | - { |
40 | | - provide: CONFIG_OPTIONS, |
41 | | - useFactory: options.useFactory, |
42 | | - inject: options.inject, |
43 | | - }, |
44 | | - { |
45 | | - provide: Registry, |
46 | | - useFactory: async ( config: MetricsConfig ) => { |
47 | | - return ReporterModule.configureRegistry( config ); |
48 | | - }, |
49 | | - inject: [ CONFIG_OPTIONS ], |
50 | | - }, |
51 | | - MetricsService, |
52 | | - ReporterService |
53 | | - ], |
54 | | - controllers: [ MetricsController ], |
55 | | - exports: [ ReporterService ] |
| 71 | + providers, |
| 72 | + controllers: [MetricsController], |
| 73 | + exports: [ReporterService] |
56 | 74 | }; |
57 | 75 | } |
58 | 76 |
|
|
0 commit comments