determination
Configuration resolver. determination
loads a JSON configuration file, resolving against criteria using confidence and shortstop protocol handlers.
In addition, determination
supports javascript style comments in your JSON using shush.
Note: determination
borrows heavily from confit, but prefers confidence
for resolving environment as well as other criteria for filtering.
Usage
const Determination = require('determination');
Determination.create(options)
options
(Object) - an options object containing:
config
(String) - required path to a JSON configuration.criteria
(Object) - optional resolution criteria. See confidence. Minimally will always contain process.env
under the key env
.protocols
(Object) - optional mapping of protocols for shortstop.defaults
(Object | String) - optional default configuration values.overrides
(Object | String) - optional override configuration values.
- returns - a resolver.
resolver.resolve([callback])
callback
(Function) - an optional callback.- returns - a promise if
callback
is not provided.
const Determination = require('determination');
const Path = require('path');
const Handlers = require('shortstop-handlers');
const config = Path.join('.', 'config', 'config.json');
const resolver = Determination.create({
config,
protocols: {
require: Handlers.require(Path.dirname(config))
}
});
resolver.resolve((error, config) => {
});
Config API
get(string: key)
- returns the value for the given key
, where a dot-delimited key
may traverse the configuration store.set(string: key, any: value)
- sets the given value
on the given key
, where dot-delimited key
may traverse the configuration store.merge(object: value)
- merges the given value
into the configuration store.use(object: store)
- merges the given store
into the configuration store.data
- accessor for a clone of the underlying store data (modifying this will not modify store).
config.set('some.key.name', 'value');
config.merge({ some: { key: other: 'another value' }});
config.get('some.key.other');
Shortstop Protocol Handlers
Two protocol handlers are enabled by default:
import:path
- merges the contents of a given file, supporting comments (unlike require
).config:key
- copies the value under the given key (supporting dot-delimited) to the key it is declared on.
Resolution Order
Configuration file contents are resolved in the following order:
- Resolve
defaults
against protocols
. - Merge
defaults
with config
. - Resolve merged
config
against protocols
. - Resolve
overrides
against protocols
. - Merge
overrides
into config
. - Resolve
config
against config:
protocol.