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 = ; // setup fixture data for exampleprocessenvSERVER_HOST = '0.0.0.0';processenvSERVER_PORT = 8080;processenvMAINTENANCE_MODE = 'true';processenvENABLE_PROFILER = 'NO';processenvSETTINGS = '{ "path": "/tmp", "timeout": 1000 } ';processenvACCESS_KEY = 'myAccessKey';processenvCOERCE_NULL_DEFAULT = 'null';processenvCOERCE_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" { const normalized = envConfigMaputils; if normalized === 'yes' return true; if normalized === 'no' return false; return null; } // customize redactor str; const config = ; console;console;
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 = ;
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 = processenvkey types: // define custom type "booleanYesNo" { const normalized = envConfigMaputils; if normalized === 'yes' return true; if normalized === 'no' return false; return null; } // customized redactor str coerceNull: true coerceUndefined: false;const config = ;
Misc Exports
const envConfigMap = ;const defaultOptions = envConfigMapdefaultOptions;const supportedTypes = envConfigMaptypes;const helperUtils = envConfigMaputils;
env-config-map
Example Server Config with .env
APP_NAME = envConfigMap;SERVER_PORT = 8080;ACCESS_KEY = myAccessKey;
config.js
;const envConfigMap = ; const configMap = APP_NAME: default: 'noname' SERVER_HOST: default: 'localhost' SERVER_PORT: default: 80 type: 'number' ACCESS_KEY: redact: true ;const config = ; moduleexports = config;
server.js
const http = ;const config = ; console; http ;
License
Copyright © 2019 Joe Yu. Licensed under the MIT License.