-
Notifications
You must be signed in to change notification settings - Fork 90
/
config.module.ts
243 lines (226 loc) · 7.57 KB
/
config.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { DynamicModule, Module } from '@nestjs/common';
import { FactoryProvider } from '@nestjs/common/interfaces';
import { isObject } from '@nestjs/common/utils/shared.utils';
import * as dotenv from 'dotenv';
import { DotenvExpandOptions, expand } from 'dotenv-expand';
import * as fs from 'fs';
import { resolve } from 'path';
import { ConfigHostModule } from './config-host.module';
import {
CONFIGURATION_LOADER,
CONFIGURATION_SERVICE_TOKEN,
CONFIGURATION_TOKEN,
VALIDATED_ENV_LOADER,
VALIDATED_ENV_PROPNAME,
} from './config.constants';
import { ConfigService } from './config.service';
import { ConfigFactory, ConfigModuleOptions } from './interfaces';
import { ConfigFactoryKeyHost } from './utils';
import { createConfigProvider } from './utils/create-config-factory.util';
import { getRegistrationToken } from './utils/get-registration-token.util';
import { mergeConfigObject } from './utils/merge-configs.util';
/**
* @publicApi
*/
@Module({
imports: [ConfigHostModule],
providers: [
{
provide: ConfigService,
useExisting: CONFIGURATION_SERVICE_TOKEN,
},
],
exports: [ConfigHostModule, ConfigService],
})
export class ConfigModule {
/**
* This promise resolves when "dotenv" completes loading environment variables.
* When "ignoreEnvFile" is set to true, then it will resolve immediately after the
* "ConfigModule#forRoot" method is called.
*/
public static get envVariablesLoaded() {
return this._envVariablesLoaded;
}
private static environmentVariablesLoadedSignal: () => void;
private static readonly _envVariablesLoaded = new Promise<void>(
resolve => (ConfigModule.environmentVariablesLoadedSignal = resolve),
);
/**
* Loads process environment variables depending on the "ignoreEnvFile" flag and "envFilePath" value.
* Also, registers custom configurations globally.
* @param options
*/
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
const envFilePaths = Array.isArray(options.envFilePath)
? options.envFilePath
: [options.envFilePath || resolve(process.cwd(), '.env')];
let validatedEnvConfig: Record<string, any> | undefined = undefined;
let config = options.ignoreEnvFile
? {}
: this.loadEnvFile(envFilePaths, options);
if (!options.ignoreEnvVars) {
config = {
...config,
...process.env,
};
}
if (options.validate) {
const validatedConfig = options.validate(config);
validatedEnvConfig = validatedConfig;
this.assignVariablesToProcess(validatedConfig);
} else if (options.validationSchema) {
const validationOptions = this.getSchemaValidationOptions(options);
const { error, value: validatedConfig } =
options.validationSchema.validate(config, validationOptions);
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
validatedEnvConfig = validatedConfig;
this.assignVariablesToProcess(validatedConfig);
} else {
this.assignVariablesToProcess(config);
}
const isConfigToLoad = options.load && options.load.length;
const providers = (options.load || [])
.map(factory =>
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
)
.filter(item => item) as FactoryProvider[];
const configProviderTokens = providers.map(item => item.provide);
const configServiceProvider = {
provide: ConfigService,
useFactory: (configService: ConfigService) => {
if (options.cache) {
(configService as any).isCacheEnabled = true;
}
configService.setEnvFilePaths(envFilePaths);
return configService;
},
inject: [CONFIGURATION_SERVICE_TOKEN, ...configProviderTokens],
};
providers.push(configServiceProvider);
if (validatedEnvConfig) {
const validatedEnvConfigLoader = {
provide: VALIDATED_ENV_LOADER,
useFactory: (host: Record<string, any>) => {
host[VALIDATED_ENV_PROPNAME] = validatedEnvConfig;
},
inject: [CONFIGURATION_TOKEN],
};
providers.push(validatedEnvConfigLoader);
}
this.environmentVariablesLoadedSignal();
return {
module: ConfigModule,
global: options.isGlobal,
providers: isConfigToLoad
? [
...providers,
{
provide: CONFIGURATION_LOADER,
useFactory: (
host: Record<string, any>,
...configurations: Record<string, any>[]
) => {
configurations.forEach((item, index) =>
this.mergePartial(host, item, providers[index]),
);
},
inject: [CONFIGURATION_TOKEN, ...configProviderTokens],
},
]
: providers,
exports: [ConfigService, ...configProviderTokens],
};
}
/**
* Registers configuration object (partial registration).
* @param config
*/
static forFeature(config: ConfigFactory): DynamicModule {
const configProvider = createConfigProvider(
config as ConfigFactory & ConfigFactoryKeyHost,
);
const serviceProvider = {
provide: ConfigService,
useFactory: (configService: ConfigService) => configService,
inject: [CONFIGURATION_SERVICE_TOKEN, configProvider.provide],
};
return {
module: ConfigModule,
providers: [
configProvider,
serviceProvider,
{
provide: CONFIGURATION_LOADER,
useFactory: (
host: Record<string, any>,
partialConfig: Record<string, any>,
) => {
this.mergePartial(host, partialConfig, configProvider);
},
inject: [CONFIGURATION_TOKEN, configProvider.provide],
},
],
exports: [ConfigService, configProvider.provide],
};
}
private static loadEnvFile(
envFilePaths: string[],
options: ConfigModuleOptions,
): Record<string, any> {
let config: ReturnType<typeof dotenv.parse> = {};
for (const envFilePath of envFilePaths) {
if (fs.existsSync(envFilePath)) {
config = Object.assign(
dotenv.parse(fs.readFileSync(envFilePath)),
config,
);
if (options.expandVariables) {
const expandOptions: DotenvExpandOptions =
typeof options.expandVariables === 'object'
? options.expandVariables
: {};
config =
expand({ ...expandOptions, parsed: config }).parsed || config;
}
}
}
return config;
}
private static assignVariablesToProcess(config: Record<string, unknown>) {
if (!isObject(config)) {
return;
}
const keys = Object.keys(config).filter(key => !(key in process.env));
keys.forEach(key => {
const value = config[key];
if (typeof value === 'string') {
process.env[key] = value;
} else if (typeof value === 'boolean' || typeof value === 'number') {
process.env[key] = `${value}`;
}
});
}
private static mergePartial(
host: Record<string, any>,
item: Record<string, any>,
provider: FactoryProvider,
) {
const factoryRef = provider.useFactory;
const token = getRegistrationToken(factoryRef);
mergeConfigObject(host, item, token);
}
private static getSchemaValidationOptions(options: ConfigModuleOptions) {
if (options.validationOptions) {
if (typeof options.validationOptions.allowUnknown === 'undefined') {
options.validationOptions.allowUnknown = true;
}
return options.validationOptions;
}
return {
abortEarly: false,
allowUnknown: true,
};
}
}