Skip to content

Commit

Permalink
Merge branch 'master' into feat/register-as
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonHK committed May 9, 2020
2 parents 75947a1 + 8d372f7 commit 6ae118d
Show file tree
Hide file tree
Showing 10 changed files with 1,929 additions and 1,767 deletions.
5 changes: 3 additions & 2 deletions .prettierrc
@@ -1,4 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true
}
"singleQuote": true,
"arrowParens": "avoid"
}
49 changes: 26 additions & 23 deletions lib/config.module.ts
Expand Up @@ -38,31 +38,30 @@ export class ConfigModule {
*/
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
let validatedEnvConfig: Record<string, any> | undefined = undefined;
if (!options.ignoreEnvFile) {
if (options.validationSchema) {
let config = this.loadEnvFile(options);
if (!options.ignoreEnvVars) {
config = {
...process.env,
...config,
};
}
const validationOptions = this.getSchemaValidationOptions(options);
const {
error,
value: validatedConfig,
} = options.validationSchema.validate(config, validationOptions);
let config = options.ignoreEnvFile ? {} : this.loadEnvFile(options);

if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
validatedEnvConfig = validatedConfig;
this.assignVariablesToProcess(validatedConfig);
} else {
const config = this.loadEnvFile(options);
this.assignVariablesToProcess(config);
if (!options.ignoreEnvVars) {
config = {
...process.env,
...config,
};
}
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 =>
Expand Down Expand Up @@ -173,7 +172,11 @@ export class ConfigModule {
if (!isObject(config)) {
return;
}
const keys = Object.keys(config).filter(key => !(key in process.env));
const keys = Object.keys(config).filter(
key =>
!(key in process.env) ||
(key in process.env && process.env[key] !== config[key]),
);
keys.forEach(key => (process.env[key] = config[key]));
}

Expand Down
3 changes: 2 additions & 1 deletion lib/config.service.ts
Expand Up @@ -5,6 +5,7 @@ import {
CONFIGURATION_TOKEN,
VALIDATED_ENV_PROPNAME,
} from './config.constants';
import { NoInferType } from './types';

@Injectable()
export class ConfigService {
Expand All @@ -29,7 +30,7 @@ export class ConfigService {
* @param propertyPath
* @param defaultValue
*/
get<T = any>(propertyPath: string, defaultValue: T): T;
get<T = any>(propertyPath: string, defaultValue: NoInferType<T>): T;
/**
* Get a configuration value (either custom configuration or process environment variable)
* based on property path (you can use dot notation to traverse nested object, e.g. "database.host").
Expand Down
1 change: 1 addition & 0 deletions lib/types/index.ts
@@ -1,2 +1,3 @@
export * from './config.type';
export * from './config-object.type';
export * from './no-infer.type';
1 change: 1 addition & 0 deletions lib/types/no-infer.type.ts
@@ -0,0 +1 @@
export type NoInferType<T> = [T][T extends any ? 0 : never];

0 comments on commit 6ae118d

Please sign in to comment.