-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcls.module.ts
103 lines (97 loc) · 3.73 KB
/
cls.module.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { DynamicModule, Logger, Module, Type } from '@nestjs/common';
import { ClsModuleAsyncOptions, ClsModuleOptions } from '../cls.options';
import { ClsPluginManager } from '../plugin/cls-plugin-manager';
import { ClsPlugin } from '../plugin/cls-plugin.interface';
import { ProxyProviderManager } from '../proxy-provider/proxy-provider-manager';
import { ClsModuleProxyProviderOptions } from '../proxy-provider/proxy-provider.interfaces';
import { ClsCommonModule } from './cls-common.module';
import { ClsRootModule } from './cls-root.module';
/**
* ClsModule is the main entry point for configuring the CLS module.
*/
@Module({
imports: [ClsCommonModule],
exports: [ClsCommonModule],
})
export class ClsModule {
/**
* Configures the CLS module for root.
*
* Provides the `ClsService` and registered Proxy Providers for injection.
*/
static forRoot(options?: ClsModuleOptions): DynamicModule {
return {
module: ClsModule,
imports: [ClsRootModule.forRoot(options)],
global: options?.global,
};
}
/**
* Configures the CLS module in the root with asynchronously provided configuration.
*
* Provides the `ClsService` and registered Proxy Providers for injection.
*/
static forRootAsync(asyncOptions: ClsModuleAsyncOptions): DynamicModule {
return {
module: ClsModule,
imports: [ClsRootModule.forRootAsync(asyncOptions)],
global: asyncOptions?.global,
};
}
/**
* Provides the `ClsService` for injection.
*/
static forFeature(): DynamicModule;
/**
* Registers the given Class proxy providers in the module for injection along with `ClsService`.
*
* For advanced options, use `forFeatureAsync`.
*/
static forFeature(...proxyProviderClasses: Array<Type>): DynamicModule;
static forFeature(...proxyProviderClasses: Array<Type>): DynamicModule {
const proxyProviders =
ClsRootModule.createProxyClassProviders(proxyProviderClasses);
return {
module: ClsModule,
providers: proxyProviders,
exports: proxyProviders.map((p) => p.provide),
};
}
/**
* Registers the given Class or Factory proxy providers in the module along with `ClsService`.
*
* If used with `global: true`, makes the proxy provider available globally.
*/
static forFeatureAsync(
options: ClsModuleProxyProviderOptions,
): DynamicModule {
const proxyProvider = ProxyProviderManager.createProxyProvider(options);
const providers = [...(options.extraProviders ?? [])];
return {
module: ClsModule,
imports: options.imports ?? [],
providers: [...providers, proxyProvider],
exports: [proxyProvider.provide],
global: options.global,
};
}
/**
* Registers the given Plugins the module along with `ClsService`.
* @deprecated
* All plugins must be registered in the `ClsModule.forRoot` or `ClsModule.forRootAsync` options.
*
* Since the plugin API is still experimental, this method will print a warning, throw error
* and will be eventually removed, possibly in a minor release.
*/
static registerPlugins(plugins: ClsPlugin[]): DynamicModule {
const logger = new Logger('ClsModule');
logger.warn(
'The `ClsModule.registerPlugins` method is deprecated and will be removed in a future release. ' +
'All plugins must be registered in the `ClsModule.forRoot` or `ClsModule.forRootAsync` options.',
);
return {
module: ClsModule,
imports: ClsPluginManager.registerPlugins(plugins),
};
}
}