Convert environment variables into JSON object with parsed values.
npm install --save read-env
Let's say you have some environment variables starting with prefix "EXAMPLE_" like below:
EXAMPLE_OBJECT_KEY= '{"prop": "value"}',
EXAMPLE_ARRAY_KEY= '[1,2,3, "string", {"prop": "value"}, 5.2]',
EXAMPLE_TRUE_KEY= 'true',
EXAMPLE_FALSE_KEY= 'false',
EXAMPLE_INT_KEY= '5',
EXAMPLE_FLOAT_KEY= '5.2',
EXAMPLE_STRING_KEY= 'example',
your-app.js
import readEnv from 'read-env';
const options = readEnv('EXAMPLE');
console.log(options);
Output:
{
arrayKey: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
falseKey: false,
floatKey: 5.2,
intKey: 5,
objectKey: { prop: 'value' },
stringKey: 'example',
trueKey: true
}
You can pass a string prefix as first paremeter like below:
const options = readEnv('EXAMPLE');
// Output:
/**
{
arrayKey: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
falseKey: false,
floatKey: 5.2,
intKey: 5,
objectKey: { prop: 'value' },
stringKey: 'example',
trueKey: true
}
*/
const optionsLower = readEnv('EXAMPLE', 'lowercase');
// Output:
/**
{
array_key: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
false_key: false,
float_key: 5.2,
int_key: 5,
object_key: { prop: 'value' },
string_key: 'example',
true_key: true
}
*/
function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
const optionsUcfirst = readEnv('EXAMPLE', ucfirst);
// Output:
/**
{
Array_key: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
False_key: false,
Float_key: 5.2,
Int_key: 5,
Object_key: { prop: 'value' },
String_key: 'example',
True_key: true
}
*/
You can pass whole config object:
function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
const options = readEnv({
prefix: 'EXAMPLE',
includePrefix: false,
transformKey: ucfirst,
parse: {
array: false, //not gonna parse arrays
}, //still gonna parse object, int, float and boolean
});
Available Config Options:
prefix
(type: string, default: null): filters environment variables by prefixincludePrefix
(type: bool, default: false): set true if you want to keep prefix in property names.transformKey
(type: null|string|function, default: 'camelcase'): transform environment variable name.null
, doesn't transform the environment variable name.camelcase
, transforms variable name to camelCase.lowercase
, transforms variable name to lowercase.uppercase
, transforms variable name to UPPERCASE.fn(varName)
, you can write your own transformer function (varName will be provided with prefix, if includePrefix is true)
parse
(type: bool|object, default: object):false
: returns raw environment variable value{}
: allows you to define which value types are going to be parsed.object
(type: bool, default: true): parse stringified object (value must be valid JSON input, see: JSON.parse).array
(type: bool, default: true): parse stringified array (value must be valid JSON input, see: JSON.parse).int
(type: bool, default: true): parse numbers into integer (value must be consist of only digits).float
(type: bool, default: true): parse numbers into float (value must be consist of only digits with decimal point).bool
(type: bool, default: true): parse if value into bolean if it equals to 'true' or 'false' .
ignoreInvalidJSON
(type: bool, default: true): if set to false, throws exception when value is not a valid JSON input (parse.object or parse.array options must be set to true).filter
(type: null|function, default: null): filters environment variables (overrides prefix rule).null
, don't filter varaibles.fn(envVarName, index)
, custom filter function (envVarName will be provided without any transformation).
Recently, I used Nightmare for acceptance testing and had several environments which have different configurations.
Instead of writing a code like below:
import Nightmare from 'nightmare';
const nightmare = Nightmare({
show: process.env.X_NIGHTMARE_SHOW || false,
width: process.env.X_NIGHTMARE_WIDTH || 1280,
height: process.env.X_NIGHTMARE_HEIGHT || 720,
typeInterval: process.env.X_NIGHTMARE_TYPE_INTERVAL || 50,
//... other properties go forever
});
I wrote this, and nightmare is fully configurable with environment variables :)
import Nightmare from 'nightmare';
import readEnv from 'read-env';
const nightmareConfig = readEnv('X_NIGHTMARE');
const nightmare = Nightmare(nightmareConfig);
MIT (c) 2017 Mehmet Yatk谋