UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ConditionalModule = void 0;
4const common_1 = require("@nestjs/common");
5const config_module_1 = require("./config.module");
6/**
7 * @publicApi
8 */
9class ConditionalModule {
10 /**
11 * @publicApi
12 */
13 static async registerWhen(module, condition, options) {
14 const { timeout = 5000, debug = true } = options ?? {};
15 const timer = setTimeout(() => {
16 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 ${module.toString()} should be registered or not`);
17 }, timeout);
18 timer.unref();
19 const returnModule = { module: ConditionalModule, imports: [], exports: [] };
20 if (typeof condition === 'string') {
21 const key = condition;
22 condition = env => {
23 return env[key]?.toLowerCase() !== 'false';
24 };
25 }
26 await config_module_1.ConfigModule.envVariablesLoaded;
27 clearTimeout(timer);
28 const evaluation = condition(process.env);
29 if (evaluation) {
30 returnModule.imports.push(module);
31 returnModule.exports.push(module);
32 }
33 else {
34 if (debug) {
35 common_1.Logger.debug(`${condition.toString()} evaluated to false. Skipping the registration of ${module.toString()}`, ConditionalModule.name);
36 }
37 }
38 return returnModule;
39 }
40}
41exports.ConditionalModule = ConditionalModule;