Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "good-env",
"version": "7.3.0",
"version": "7.4.0",
"description": "Better environment variable handling for Twelve-Factor node apps",
"main": "src/index.js",
"scripts": {
Expand Down
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ const isFunction = x => is(x) === '[object Function]';
const parse = (items, converter) => items.map(t => converter(t, 10));
const mapNums = items => parse(items, parseInt);
const validType = item => ['number', 'boolean', 'string'].includes(item);
let store = { ...process.env };

module.exports = Object
.create({
async _mergeSecrets_ ({ fetcherFunc }) {
const secret = await fetcherFunc();
store = { ...store, ...secret };
return this;
},
/**
* @description Fetches an IP address from the environment. If the value found under the specified key is not a valid IPv4
* or IPv6 IP and there's no default value, null is returned. If a default value is provided and it is a valid IPv4 or IPv6
Expand Down Expand Up @@ -123,8 +129,8 @@ module.exports = Object
}

keys.some(key => {
if (ok(process.env[key])) {
value = process.env[key];
if (ok(store[key])) {
value = store[key];
return true;
}
return false;
Expand Down Expand Up @@ -172,7 +178,7 @@ module.exports = Object
* @param {(string|string[])} keys - A unique key or array of keys
*
*/
ok: (...keys) => keys.every(key => ok(process.env[key])),
ok: (...keys) => keys.every(key => ok(store[key])),

/**
* @description This method ensures 1 to many environment variables either
Expand Down Expand Up @@ -262,7 +268,7 @@ module.exports = Object
getBool (key, defaultVal) {
let value;

value = process.env[key];
value = store[key];

if (ok(value)) {
value = value.toLowerCase().trim();
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ require('dotenv').config({ path: 'test/test.env' });
const test = require('tape');
const env = require('../src/index');

test('it merges secrets', async (t) => {
const fetcherFunc = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
FOOB: 'bar',
BARZ: 'baz'
});
}, 10);
});
};
const env2 = await env._mergeSecrets_({ fetcherFunc });
t.equals(env2.get('FOOB'), 'bar');
t.equals(env2.get('BARZ'), 'baz');
t.end();
});

test('it gets an IP address', (t) => {
const ip = env.getIP('VALID_IP');
t.equals(ip, '192.168.1.60');
Expand Down