1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6 | desc = { enumerable: true, get: function() { return m[k]; } };
|
7 | }
|
8 | Object.defineProperty(o, k2, desc);
|
9 | }) : (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | o[k2] = m[k];
|
12 | }));
|
13 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15 | }) : function(o, v) {
|
16 | o["default"] = v;
|
17 | });
|
18 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
19 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
20 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
21 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
22 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
23 | };
|
24 | var __importStar = (this && this.__importStar) || function (mod) {
|
25 | if (mod && mod.__esModule) return mod;
|
26 | var result = {};
|
27 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
28 | __setModuleDefault(result, mod);
|
29 | return result;
|
30 | };
|
31 | var ConfigModule_1;
|
32 | Object.defineProperty(exports, "__esModule", { value: true });
|
33 | exports.ConfigModule = void 0;
|
34 | const common_1 = require("@nestjs/common");
|
35 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
36 | const dotenv = __importStar(require("dotenv"));
|
37 | const dotenv_expand_1 = require("dotenv-expand");
|
38 | const fs = __importStar(require("fs"));
|
39 | const path_1 = require("path");
|
40 | const config_host_module_1 = require("./config-host.module");
|
41 | const config_constants_1 = require("./config.constants");
|
42 | const config_service_1 = require("./config.service");
|
43 | const create_config_factory_util_1 = require("./utils/create-config-factory.util");
|
44 | const get_registration_token_util_1 = require("./utils/get-registration-token.util");
|
45 | const merge_configs_util_1 = require("./utils/merge-configs.util");
|
46 | let ConfigModule = ConfigModule_1 = class ConfigModule {
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 | static get envVariablesLoaded() {
|
53 | return this._envVariablesLoaded;
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | static forRoot(options = {}) {
|
61 | let validatedEnvConfig = undefined;
|
62 | let config = options.ignoreEnvFile ? {} : this.loadEnvFile(options);
|
63 | if (!options.ignoreEnvVars) {
|
64 | config = Object.assign(Object.assign({}, config), process.env);
|
65 | }
|
66 | if (options.validate) {
|
67 | const validatedConfig = options.validate(config);
|
68 | validatedEnvConfig = validatedConfig;
|
69 | this.assignVariablesToProcess(validatedConfig);
|
70 | }
|
71 | else if (options.validationSchema) {
|
72 | const validationOptions = this.getSchemaValidationOptions(options);
|
73 | const { error, value: validatedConfig } = options.validationSchema.validate(config, validationOptions);
|
74 | if (error) {
|
75 | throw new Error(`Config validation error: ${error.message}`);
|
76 | }
|
77 | validatedEnvConfig = validatedConfig;
|
78 | this.assignVariablesToProcess(validatedConfig);
|
79 | }
|
80 | else {
|
81 | this.assignVariablesToProcess(config);
|
82 | }
|
83 | const isConfigToLoad = options.load && options.load.length;
|
84 | const providers = (options.load || [])
|
85 | .map(factory => (0, create_config_factory_util_1.createConfigProvider)(factory))
|
86 | .filter(item => item);
|
87 | const configProviderTokens = providers.map(item => item.provide);
|
88 | const configServiceProvider = {
|
89 | provide: config_service_1.ConfigService,
|
90 | useFactory: (configService) => {
|
91 | if (options.cache) {
|
92 | configService.isCacheEnabled = true;
|
93 | }
|
94 | return configService;
|
95 | },
|
96 | inject: [config_constants_1.CONFIGURATION_SERVICE_TOKEN, ...configProviderTokens],
|
97 | };
|
98 | providers.push(configServiceProvider);
|
99 | if (validatedEnvConfig) {
|
100 | const validatedEnvConfigLoader = {
|
101 | provide: config_constants_1.VALIDATED_ENV_LOADER,
|
102 | useFactory: (host) => {
|
103 | host[config_constants_1.VALIDATED_ENV_PROPNAME] = validatedEnvConfig;
|
104 | },
|
105 | inject: [config_constants_1.CONFIGURATION_TOKEN],
|
106 | };
|
107 | providers.push(validatedEnvConfigLoader);
|
108 | }
|
109 | this.environmentVariablesLoadedSignal();
|
110 | return {
|
111 | module: ConfigModule_1,
|
112 | global: options.isGlobal,
|
113 | providers: isConfigToLoad
|
114 | ? [
|
115 | ...providers,
|
116 | {
|
117 | provide: config_constants_1.CONFIGURATION_LOADER,
|
118 | useFactory: (host, ...configurations) => {
|
119 | configurations.forEach((item, index) => this.mergePartial(host, item, providers[index]));
|
120 | },
|
121 | inject: [config_constants_1.CONFIGURATION_TOKEN, ...configProviderTokens],
|
122 | },
|
123 | ]
|
124 | : providers,
|
125 | exports: [config_service_1.ConfigService, ...configProviderTokens],
|
126 | };
|
127 | }
|
128 | |
129 |
|
130 |
|
131 |
|
132 | static forFeature(config) {
|
133 | const configProvider = (0, create_config_factory_util_1.createConfigProvider)(config);
|
134 | const serviceProvider = {
|
135 | provide: config_service_1.ConfigService,
|
136 | useFactory: (configService) => configService,
|
137 | inject: [config_constants_1.CONFIGURATION_SERVICE_TOKEN, configProvider.provide],
|
138 | };
|
139 | return {
|
140 | module: ConfigModule_1,
|
141 | providers: [
|
142 | configProvider,
|
143 | serviceProvider,
|
144 | {
|
145 | provide: config_constants_1.CONFIGURATION_LOADER,
|
146 | useFactory: (host, partialConfig) => {
|
147 | this.mergePartial(host, partialConfig, configProvider);
|
148 | },
|
149 | inject: [config_constants_1.CONFIGURATION_TOKEN, configProvider.provide],
|
150 | },
|
151 | ],
|
152 | exports: [config_service_1.ConfigService, configProvider.provide],
|
153 | };
|
154 | }
|
155 | static loadEnvFile(options) {
|
156 | const envFilePaths = Array.isArray(options.envFilePath)
|
157 | ? options.envFilePath
|
158 | : [options.envFilePath || (0, path_1.resolve)(process.cwd(), '.env')];
|
159 | let config = {};
|
160 | for (const envFilePath of envFilePaths) {
|
161 | if (fs.existsSync(envFilePath)) {
|
162 | config = Object.assign(dotenv.parse(fs.readFileSync(envFilePath)), config);
|
163 | if (options.expandVariables) {
|
164 | const expandOptions = typeof options.expandVariables === 'object' ? options.expandVariables : {};
|
165 | config = (0, dotenv_expand_1.expand)(Object.assign(Object.assign({}, expandOptions), { parsed: config })).parsed || config;
|
166 | }
|
167 | }
|
168 | }
|
169 | return config;
|
170 | }
|
171 | static assignVariablesToProcess(config) {
|
172 | if (!(0, shared_utils_1.isObject)(config)) {
|
173 | return;
|
174 | }
|
175 | const keys = Object.keys(config).filter(key => !(key in process.env));
|
176 | keys.forEach(key => (process.env[key] = config[key]));
|
177 | }
|
178 | static mergePartial(host, item, provider) {
|
179 | const factoryRef = provider.useFactory;
|
180 | const token = (0, get_registration_token_util_1.getRegistrationToken)(factoryRef);
|
181 | (0, merge_configs_util_1.mergeConfigObject)(host, item, token);
|
182 | }
|
183 | static getSchemaValidationOptions(options) {
|
184 | if (options.validationOptions) {
|
185 | if (typeof options.validationOptions.allowUnknown === 'undefined') {
|
186 | options.validationOptions.allowUnknown = true;
|
187 | }
|
188 | return options.validationOptions;
|
189 | }
|
190 | return {
|
191 | abortEarly: false,
|
192 | allowUnknown: true,
|
193 | };
|
194 | }
|
195 | };
|
196 | ConfigModule._envVariablesLoaded = new Promise(resolve => (ConfigModule_1.environmentVariablesLoadedSignal = resolve));
|
197 | ConfigModule = ConfigModule_1 = __decorate([
|
198 | (0, common_1.Module)({
|
199 | imports: [config_host_module_1.ConfigHostModule],
|
200 | providers: [
|
201 | {
|
202 | provide: config_service_1.ConfigService,
|
203 | useExisting: config_constants_1.CONFIGURATION_SERVICE_TOKEN,
|
204 | },
|
205 | ],
|
206 | exports: [config_host_module_1.ConfigHostModule, config_service_1.ConfigService],
|
207 | })
|
208 | ], ConfigModule);
|
209 | exports.ConfigModule = ConfigModule;
|