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 |
|
47 |
|
48 |
|
49 | let ConfigModule = ConfigModule_1 = class ConfigModule {
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | static get envVariablesLoaded() {
|
56 | return this._envVariablesLoaded;
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | static forRoot(options = {}) {
|
64 | const envFilePaths = Array.isArray(options.envFilePath)
|
65 | ? options.envFilePath
|
66 | : [options.envFilePath || (0, path_1.resolve)(process.cwd(), '.env')];
|
67 | let validatedEnvConfig = undefined;
|
68 | let config = options.ignoreEnvFile
|
69 | ? {}
|
70 | : this.loadEnvFile(envFilePaths, options);
|
71 | if (!options.ignoreEnvVars) {
|
72 | config = {
|
73 | ...config,
|
74 | ...process.env,
|
75 | };
|
76 | }
|
77 | if (options.validate) {
|
78 | const validatedConfig = options.validate(config);
|
79 | validatedEnvConfig = validatedConfig;
|
80 | this.assignVariablesToProcess(validatedConfig);
|
81 | }
|
82 | else if (options.validationSchema) {
|
83 | const validationOptions = this.getSchemaValidationOptions(options);
|
84 | const { error, value: validatedConfig } = options.validationSchema.validate(config, validationOptions);
|
85 | if (error) {
|
86 | throw new Error(`Config validation error: ${error.message}`);
|
87 | }
|
88 | validatedEnvConfig = validatedConfig;
|
89 | this.assignVariablesToProcess(validatedConfig);
|
90 | }
|
91 | else {
|
92 | this.assignVariablesToProcess(config);
|
93 | }
|
94 | const isConfigToLoad = options.load && options.load.length;
|
95 | const providers = (options.load || [])
|
96 | .map(factory => (0, create_config_factory_util_1.createConfigProvider)(factory))
|
97 | .filter(item => item);
|
98 | const configProviderTokens = providers.map(item => item.provide);
|
99 | const configServiceProvider = {
|
100 | provide: config_service_1.ConfigService,
|
101 | useFactory: (configService) => {
|
102 | if (options.cache) {
|
103 | configService.isCacheEnabled = true;
|
104 | }
|
105 | configService.setEnvFilePaths(envFilePaths);
|
106 | return configService;
|
107 | },
|
108 | inject: [config_constants_1.CONFIGURATION_SERVICE_TOKEN, ...configProviderTokens],
|
109 | };
|
110 | providers.push(configServiceProvider);
|
111 | if (validatedEnvConfig) {
|
112 | const validatedEnvConfigLoader = {
|
113 | provide: config_constants_1.VALIDATED_ENV_LOADER,
|
114 | useFactory: (host) => {
|
115 | host[config_constants_1.VALIDATED_ENV_PROPNAME] = validatedEnvConfig;
|
116 | },
|
117 | inject: [config_constants_1.CONFIGURATION_TOKEN],
|
118 | };
|
119 | providers.push(validatedEnvConfigLoader);
|
120 | }
|
121 | this.environmentVariablesLoadedSignal();
|
122 | return {
|
123 | module: ConfigModule_1,
|
124 | global: options.isGlobal,
|
125 | providers: isConfigToLoad
|
126 | ? [
|
127 | ...providers,
|
128 | {
|
129 | provide: config_constants_1.CONFIGURATION_LOADER,
|
130 | useFactory: (host, ...configurations) => {
|
131 | configurations.forEach((item, index) => this.mergePartial(host, item, providers[index]));
|
132 | },
|
133 | inject: [config_constants_1.CONFIGURATION_TOKEN, ...configProviderTokens],
|
134 | },
|
135 | ]
|
136 | : providers,
|
137 | exports: [config_service_1.ConfigService, ...configProviderTokens],
|
138 | };
|
139 | }
|
140 | |
141 |
|
142 |
|
143 |
|
144 | static forFeature(config) {
|
145 | const configProvider = (0, create_config_factory_util_1.createConfigProvider)(config);
|
146 | const serviceProvider = {
|
147 | provide: config_service_1.ConfigService,
|
148 | useFactory: (configService) => configService,
|
149 | inject: [config_constants_1.CONFIGURATION_SERVICE_TOKEN, configProvider.provide],
|
150 | };
|
151 | return {
|
152 | module: ConfigModule_1,
|
153 | providers: [
|
154 | configProvider,
|
155 | serviceProvider,
|
156 | {
|
157 | provide: config_constants_1.CONFIGURATION_LOADER,
|
158 | useFactory: (host, partialConfig) => {
|
159 | this.mergePartial(host, partialConfig, configProvider);
|
160 | },
|
161 | inject: [config_constants_1.CONFIGURATION_TOKEN, configProvider.provide],
|
162 | },
|
163 | ],
|
164 | exports: [config_service_1.ConfigService, configProvider.provide],
|
165 | };
|
166 | }
|
167 | static loadEnvFile(envFilePaths, options) {
|
168 | let config = {};
|
169 | for (const envFilePath of envFilePaths) {
|
170 | if (fs.existsSync(envFilePath)) {
|
171 | config = Object.assign(dotenv.parse(fs.readFileSync(envFilePath)), config);
|
172 | if (options.expandVariables) {
|
173 | const expandOptions = typeof options.expandVariables === 'object'
|
174 | ? options.expandVariables
|
175 | : {};
|
176 | config =
|
177 | (0, dotenv_expand_1.expand)({ ...expandOptions, parsed: config }).parsed || config;
|
178 | }
|
179 | }
|
180 | }
|
181 | return config;
|
182 | }
|
183 | static assignVariablesToProcess(config) {
|
184 | if (!(0, shared_utils_1.isObject)(config)) {
|
185 | return;
|
186 | }
|
187 | const keys = Object.keys(config).filter(key => !(key in process.env));
|
188 | keys.forEach(key => {
|
189 | const value = config[key];
|
190 | if (typeof value === 'string') {
|
191 | process.env[key] = value;
|
192 | }
|
193 | else if (typeof value === 'boolean' || typeof value === 'number') {
|
194 | process.env[key] = `${value}`;
|
195 | }
|
196 | });
|
197 | }
|
198 | static mergePartial(host, item, provider) {
|
199 | const factoryRef = provider.useFactory;
|
200 | const token = (0, get_registration_token_util_1.getRegistrationToken)(factoryRef);
|
201 | (0, merge_configs_util_1.mergeConfigObject)(host, item, token);
|
202 | }
|
203 | static getSchemaValidationOptions(options) {
|
204 | if (options.validationOptions) {
|
205 | if (typeof options.validationOptions.allowUnknown === 'undefined') {
|
206 | options.validationOptions.allowUnknown = true;
|
207 | }
|
208 | return options.validationOptions;
|
209 | }
|
210 | return {
|
211 | abortEarly: false,
|
212 | allowUnknown: true,
|
213 | };
|
214 | }
|
215 | };
|
216 | exports.ConfigModule = ConfigModule;
|
217 | ConfigModule._envVariablesLoaded = new Promise(resolve => (ConfigModule_1.environmentVariablesLoadedSignal = resolve));
|
218 | exports.ConfigModule = ConfigModule = ConfigModule_1 = __decorate([
|
219 | (0, common_1.Module)({
|
220 | imports: [config_host_module_1.ConfigHostModule],
|
221 | providers: [
|
222 | {
|
223 | provide: config_service_1.ConfigService,
|
224 | useExisting: config_constants_1.CONFIGURATION_SERVICE_TOKEN,
|
225 | },
|
226 | ],
|
227 | exports: [config_host_module_1.ConfigHostModule, config_service_1.ConfigService],
|
228 | })
|
229 | ], ConfigModule);
|