env-config-map
v1.0.4
Published
Generates a config object from a config map using any input sources.
Downloads
13
Maintainers
Readme
env-config-map
Generates a config object from a config map using any input sources. Mapping includes the most commonly encountered patterns such as setting default value, type casting, coercing null, coercing undefined, and redacting secrets for logging. Default input source are environment variables from process.env
.
- Zero dependency.
- Supported types:
string
number
boolean
object
arrayCommaDelim
- also supports custom types
redact
option to redact value for logging.coerceNull
option to coerce"null"
tonull
.coerceUndefined
option to coerce"undefined"
toundefined
.getter
option to get input values from other sources. Default source isprocess.env
.
Installation
npm install env-config-map
Run Sandbox Example
npm run sandbox
Sandbox Example
// source from .env (optional)
// require('dotenv').config();
const envConfigMap = require('env-config-map');
// setup fixture data for example
process.env.SERVER_HOST = '0.0.0.0';
process.env.SERVER_PORT = 8080;
process.env.MAINTENANCE_MODE = 'true';
process.env.ENABLE_PROFILER = 'NO';
process.env.SETTINGS = '{ "path": "/tmp", "timeout": 1000 } ';
process.env.ACCESS_KEY = 'myAccessKey';
process.env.COERCE_NULL_DEFAULT = 'null';
process.env.COERCE_NULL_DISABLED = 'null';
// process.env.LOG_LEVEL = undefined;
const configMap = {
SERVER_HOST: { default: 'localhost' },
SERVER_PORT: { default: 80, type: 'number' },
MAINTENANCE_MODE: { default: false, type: 'boolean' },
ENABLE_PROFILER: { default: false, type: 'booleanYesNo' },
SETTINGS: { type: 'object' },
ACCESS_KEY: { redact: true },
COERCE_NULL_DEFAULT: {},
COERCE_NULL_DISABLED: { coerceNull: false },
LOG_LEVEL: { default: 'info' },
};
const options = {
types: {
// define custom type "booleanYesNo"
booleanYesNo: (str) => {
const normalized = envConfigMap.utils.lowerTrim(str);
if (normalized === 'yes') return true;
if (normalized === 'no') return false;
return null;
},
},
// customize redactor
redactor: (str) => str.replace(/.+/, '*** REDACTED ***'),
};
const config = envConfigMap(configMap, options);
console.log(config);
console.log(config.getRedacted());
console.log(config);
{
SERVER_HOST: '0.0.0.0',
SERVER_PORT: 8080,
MAINTENANCE_MODE: true,
ENABLE_PROFILER: false,
SETTINGS: { path: '/tmp', timeout: 1000 },
ACCESS_KEY: 'myAccessKey',
COERCE_NULL_DEFAULT: null,
COERCE_NULL_DISABLED: 'null',
LOG_LEVEL: 'info',
getRedacted: [Function],
getOptions: [Function]
}
console.log(config.getRedacted());
{
SERVER_HOST: '0.0.0.0',
SERVER_PORT: 8080,
MAINTENANCE_MODE: true,
ENABLE_PROFILER: false,
SETTINGS: { path: '/tmp', timeout: 1000 },
ACCESS_KEY: '*** REDACTED ***',
COERCE_NULL_DEFAULT: null,
COERCE_NULL_DISABLED: 'null',
LOG_LEVEL: 'info'
}
configMap
default
: mixed- Sets the default value.
type
: string (default: string)- Specify the type for the key. Cast operation will call the type casting function defined in
options.types
.string
number
boolean
object
arrayCommaDelim
- Specify the type for the key. Cast operation will call the type casting function defined in
redact
: boolean- Flag to indicate if the value is a secret and needs to be redacted.
coerceNull
: boolean- Coerce string
'null'
tonull
. Supersedesoptions.coerceNull
.
- Coerce string
coerceUndefined
: boolean- Coerce string
'undefined'
toundefined
. Supersedesoptions.coerceNull
.
- Coerce string
const configMap = {
SERVER_PORT: { default: 80, type: 'number' },
ACCESS_KEY: { redact: true },
ENABLE_PROFILER: { default: false, type: 'booleanYesNo' },
COERCE_DISABLED: { coerceNull: false, coerceUndefined: false },
};
const config = envConfigMap(configMap, options);
options
getter
: function- Get input value for key. Default source is
process.env
.
- Get input value for key. Default source is
types
: object- For defining additional types and it will merge with the supported types.
redactor
: function- Function to redact value flaged with the
redact
configMap option.
- Function to redact value flaged with the
coerceNull
: boolean (default: true)- Coerce string
'null'
tonull
.
- Coerce string
coerceUndefined
: boolean (default: true)- Coerce string
'undefined'
toundefined
.
- Coerce string
const options = {
getter: (key) => process.env[key],
types: {
// define custom type "booleanYesNo"
booleanYesNo: (str) => {
const normalized = envConfigMap.utils.lowerTrim(str);
if (normalized === 'yes') return true;
if (normalized === 'no') return false;
return null;
},
},
// customized redactor
redactor: (str) => str.replace(/.+/, '*** REDACTED ***'),
coerceNull: true,
coerceUndefined: false,
};
const config = envConfigMap(configMap, options);
Misc Exports
const envConfigMap = require('env-config-map');
const defaultOptions = envConfigMap.defaultOptions;
const supportedTypes = envConfigMap.types;
const helperUtils = envConfigMap.utils;
Example Server Config with env-config-map
.env
APP_NAME = envConfigMap;
SERVER_PORT = 8080;
ACCESS_KEY = myAccessKey;
config.js
require('dotenv').config();
const envConfigMap = require('env-config-map');
const configMap = {
APP_NAME: { default: 'noname' },
SERVER_HOST: { default: 'localhost' },
SERVER_PORT: { default: 80, type: 'number' },
ACCESS_KEY: { redact: true },
};
const config = envConfigMap(configMap);
module.exports = config;
server.js
const http = require('http');
const config = require('./config.js');
console.log('Configs loaded.', config.getRedacted());
http
.createServer((req, res) => {
res.write(`Hello ${config.APP_NAME}!`);
res.end();
})
.listen(config.SERVER_PORT, config.SERVER_HOST, () => console.log(
`server listening on ${config.SERVER_HOST}:${config.SERVER_PORT}`,
));
License
Copyright © 2019 Joe Yu. Licensed under the MIT License.