diff --git a/package.json b/package.json index 7a394e0..708f87e 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 4900622..0c8eb30 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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; @@ -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 @@ -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(); diff --git a/test/test.js b/test/test.js index 0dab16c..cc17714 100644 --- a/test/test.js +++ b/test/test.js @@ -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');