-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathconditional.module.ts
68 lines (62 loc) · 2.1 KB
/
conditional.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
import { DynamicModule, ForwardReference, Logger, ModuleMetadata, Type } from '@nestjs/common';
import { ConfigModule } from './config.module';
/**
* Same logic as in `@nestjs/core` package.
* @param instance The instance which should get the name from
* @returns The name of an instance or `undefined`
*/
const getInstanceName = (instance: unknown): string | undefined => {
if ((instance as ForwardReference)?.forwardRef) {
return (instance as ForwardReference).forwardRef()?.name;
}
if ((instance as DynamicModule).module) {
return (instance as DynamicModule).module?.name;
}
return (instance as Type).name;
};
/**
* @publicApi
*/
export class ConditionalModule {
/**
* @publicApi
*/
static async registerWhen(
module: Required<ModuleMetadata>['imports'][number],
condition: string | ((env: NodeJS.ProcessEnv) => boolean),
options?: { timeout?: number; debug?: boolean },
) {
const { timeout = 5000, debug = true } = options ?? {};
const moduleName = getInstanceName(module) || module.toString();
const timer = setTimeout(() => {
throw new Error(
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${moduleName} should be registered or not`,
);
}, timeout);
timer.unref();
const returnModule: Required<
Pick<DynamicModule, 'module' | 'imports' | 'exports'>
> = { module: ConditionalModule, imports: [], exports: [] };
if (typeof condition === 'string') {
const key = condition;
condition = env => {
return env[key]?.toLowerCase() !== 'false';
};
}
await ConfigModule.envVariablesLoaded;
clearTimeout(timer);
const evaluation = condition(process.env);
if (evaluation) {
returnModule.imports.push(module);
returnModule.exports.push(module);
} else {
if (debug) {
Logger.debug(
`${condition.toString()} evaluated to false. Skipping the registration of ${moduleName}`,
ConditionalModule.name,
);
}
}
return returnModule;
}
}