Load config from env, user config, or default spec.
$ npm i xenv-config --save
const xenvConfig = require("xenv-config");
const spec = {
fooOption: { env: "FOO_OPTION", default: false },
barOption: { env: "BAR_OPTION", type: "number" },
zooOption: { default: false, type: "truthy" },
jsonOption: { env: "JSON_OPTION", type: "json" }
};
process.env.BAR_OPTION = "900";
process.env.JSON_OPTION = "{b:90}";
const config = xenvConfig(spec, { zooOption: true, jsonOption: { a: 50 } });
expect(config).to.deep.equal({
fooOption: false,
barOption: 900,
zooOption: true,
jsonOption: { a: 50, b: 90 }
});
expect(config.__$trace__).to.deep.equal({
fooOption: { src: "default" },
barOption: { src: "env", name: "BAR_OPTION" },
zooOption: { src: "option" },
jsonOption: { src: "env,option" }
});
See the full API reference for every option, type, and runtime rule.
xenvConfig(spec, userConfig, options);
spec - specification of the configsuserConfig - configuration from the user (use if not declared in env)options - optionsReturns config object.
__$trace__ that contains data to indicate where each config key's value was determined fromoptions:
_env - Object that represents environment instead of process.envmerge - function to merge json object instead of using Object.assignThe spec is a JSON object with the following format:
{
"<optionKey>": {
env: "ENV_VAR_NAME",
envMap: {},
default: <default_value>,
type: "<type>",
post: (val, trace) => {}
}
}
optionKey specifies the name of the optionenv: the name (or array of names) of the environment varialbe(s) to check first. If it's true, then use optionKey as the env variable name.envMap: an object of mapping env value to another value.default: the default value or a function to return the default value.type: indicate how to interpret and convert the string from process.env.post: callback to post process valueAll fields are
optional, if they are all skipped, then the config option will be determined fromuserConfigonly.Without either
defaultortype, the value fromenvwill remain as a string.If
defaultis a function, thentypemust be defined or it will bestring.If
envis an array, then the first one that finds a value inprocess.envwill be used.If
envistrue, then useoptionKeyas the name to look up fromprocess.env.
When loading from env, in order to indicate what value to convert the string into, the type can be one of.
string - no conversionnumber - (integer) convert with parseInt(x,10)float - (float) convert with parseFloat(x)boolean - (boolean) convert with x === "true" || x === "yes" || x === "1" || x === "on"truthy - (boolean from truthy check) convert with !!xjson - (JSON) parsed with JSON.parseIf
typeis not specified, anddefaultexist and not a function, thentypeof defaultwill be used.
The hidden field __$trace__ contain data for each key to indicate where its value was determined from.
{src: "env", name: "ENV_OPTION_NAME"}{src: "option"}{src: "default"}json type is slightly different.
default is an object like {}, then type is detected to be json, unless spec has type explicitly.env, option, default.Object.assign to combine values unless you pass in a merge function in options. For example, merge from lodash.trace.src would be a comma separate list of them. For example, "env,option", "env,option,default", or "option,default"The order of source to check are:
env if it's defined in the spec and process.env contains the variableuserConfig directly if it contains the optionKey