uberconfig
configuration manager for multi-module configs.
In order to...
In order to share configuration between parts of an application that do not even know each other.
Multiple configuration consumers come into an agreement by passing a default value with each request for a configuration value. — Given the default values are equal, we assume the consumers are up to the same thing.
This will hopefully lead to a world with fever unnecessary namespaces and more communication between maintainers of modules that share same intentions. In the case of conflicting default values, I would love to see the conflicting parties come to an agreement together instead of expanding the namespaces.
<3
Install
npm install uberconfig
Use
const Uberconfig = ;const config = foo: 'bar' lorem: ipsum: 'dolor' sit: 'amet' ; config;// 'bar' config;// 'dolor' config;// 'C' config;// throws 'can not get uberconfig "foo" without default value' config;// throws 'config value for "lorem.sit" must be of type boolean but is string' config;// throws 'conflicting default values for uberconfig "foo" expected "D" to equal "A"' config;// 'bar' config;// {newKey: 'amet', bar: {foo: 'baz'}}
environment variables and CLI params
Given script foo.js
:
const Uberconfig = ;console;
Shell usage:
node foo.js# logs: hey UBERCONFIG_FOO_BAR=ho node foo.js# logs: ho node foo.js --uc-foo-bar='lets go'# logs: lets go
CLI param > environment variable > config value > default value
Options
can be passed as second parameter into construction
config options;
-
###
options.envPrefix:String
Default:UBERCONFIG_
prefix for environment variable lookup
-
###
options.envConverter:Function
function converting a config key to environment variable name
default will converta.foo
toA_FOO
In combination withenvPrefix
a.foo
can be set usingUBERCONFIG_A_FOO=bar
-
###
options.cliPrefix:String
Default:uc-
prefix for CLI parameter lookup
-
###
options.cliConverter:Function
function converting a config key to CLI parameter name
default will converta.foo
toa-foo
In combination withcliPrefix
a.foo
can be set using--uc-a-foo=bar
Multi type values
In case you do not know which type a configuration variable must have by default, wrap it up as a MultiTypeValue.
Hint: Do not do this, it's an anti-pattern.
May be required when you pass though parts of the uberconfig API...
See implementation of MultiTypeValue for details
const Uberconfig = ;const MultiTypeValue = UberconfigMultiTypeValue; { if value === 5 return true; return false; } { if value === 5 return 'ipsum'; return value + ''; } const someUserInput = 5;const myConf = foo: bar: someUserInput ; const booleanDefaultVal = false;console;// logs true const stringDefaultVal = 'lorem';console;// logs 'ipsum'
License
The MIT License
Copyright (C) 2016 Hannes Diercks
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.