Skip to content

Commit

Permalink
fix(env): make $secure work without $default
Browse files Browse the repository at this point in the history
Fix #27
  • Loading branch information
dsferruzza committed Oct 29, 2018
1 parent c4f0b76 commit 465890f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ module.exports = function envFactory() {
throw new Error('$aliases must be an array');
}

const aliaseOrSecureIsDefined = !_.isUndefined(config.$aliases) || !_.isUndefined(config.$secure);
if (aliaseOrSecureIsDefined && (_.isUndefined(config.$type) && _.isUndefined(config.$default))) {
if (!_.isUndefined(config.$aliases) && (_.isUndefined(config.$type) && _.isUndefined(config.$default))) {
throw new CommonEnvInvalidConfiguration(context.fullKeyName);
}

Expand All @@ -159,7 +158,7 @@ module.exports = function envFactory() {

// only try to get an env var if memo is undefined
if (isLast) {
return _.isUndefined(config.$default) ? getOrDieWithAliases(varEnvName, $typeConverter, aliases) : getOrElse(varEnvName, config.$default, $typeConverter, config.$secure);
return _.isUndefined(config.$default) ? getOrDieWithAliases(varEnvName, $typeConverter, aliases, config.$secure) : getOrElse(varEnvName, config.$default, $typeConverter, config.$secure);
}

return getOrElse(varEnvName, null, $typeConverter, config.$secure);
Expand Down Expand Up @@ -189,8 +188,8 @@ module.exports = function envFactory() {
}

function getOrDieFactory(f) {
return function(fullKeyName, $typeConverter /* args */ ) {
var value = getOrElse(fullKeyName, null, $typeConverter);
return function(fullKeyName, $typeConverter, aliases, $secure) {
var value = getOrElse(fullKeyName, null, $typeConverter, $secure);

if (value === null) {
f.apply(null, arguments);
Expand Down Expand Up @@ -248,7 +247,7 @@ function isArrayOfAtom(array) {
}

function isConfigurationObject(value) {
return _.isObject(value) && (_.has(value, '$default') || _.has(value, '$aliases') || _.has(value, '$type'));
return _.isObject(value) && (_.has(value, '$default') || _.has(value, '$aliases') || _.has(value, '$type') || _.has(value, '$secure'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function CommonEnvRootConfigurationObjectException(object, prefix) {
util.inherits(CommonEnvRootConfigurationObjectException, Error);

function CommonEnvInvalidConfiguration(key) {
this.message = 'Invalid configuration, `$aliases` or `$secure` must be defined along side $default or $type in key "{key}"'.replace('{key}', key);
this.message = 'Invalid configuration, `$aliases` must be defined along side $default or $type in key "{key}"'.replace('{key}', key);
this.name = 'CommonEnvInvalidConfiguration';
}
util.inherits(CommonEnvInvalidConfiguration, Error);
Expand Down
15 changes: 15 additions & 0 deletions src/withLogger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ describe('withLogger', function() {
}
});
});
});

it('should handle $secure without $default', function() {
process.env = {};
process.env.PASSWORD = 'ohMyGodImSoSecret';

var config = env.getOrElseAll({
password: {
$secure: true
}
});
t.strictEqual(config.password, process.env.PASSWORD);
t.deepEqual(logger.calls, [
["[env] %s was defined, using: %s", "PASSWORD", "***"]
]);
});

});

0 comments on commit 465890f

Please sign in to comment.