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 = ;
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 containprocess.env
under the keyenv
.protocols
(Object) - optional mapping of protocols for shortstop. Protocols are bound with contextconfig
, whereconfig
is the configuration being resolved. Obviously this doesn't work with arrow functions.defaults
(Object | String) - optional default pre-resolved configuration values.overrides
(Object | String) - optional override pre-resolved configuration values.
- returns - a resolver.
resolver.resolve([callback])
callback
(Function) - an optional callback.- returns - a promise if
callback
is not provided.
const Determination = ;const Path = ;const Handlers = ; const config = Path; const resolver = Determination; resolver;
Config API
get(string: key)
- returns the value for the givenkey
, where a dot-delimitedkey
may traverse the configuration store.set(string: key, any: value)
- sets the givenvalue
on the givenkey
, where dot-delimitedkey
may traverse the configuration store.merge(object: value)
- merges the givenvalue
into the configuration store.use(object: store)
- merges the givenstore
into the configuration store.data
- accessor for a clone of the underlying store data (modifying this will not modify store).
config;config;config; //'another value'
Shortstop Protocol Handlers
Two protocol handlers are enabled by default:
import:path
- merges the contents of a given file, supporting comments (unlikerequire
).config:key
- copies the value under the given key (supporting dot-delimited) to the key it is declared on.
Custom Protocol Handlers
An example of utilizing a custom protocol handler is below. This takes advantage of the context bound to the handler.
config.json
and
const Determination = ;const VM = ; const protocols = { return VM; }; Determination;
Resolution Order
Configuration file contents are resolved in the following order:
- Resolve
defaults
againstprotocols
. - Merge
defaults
withconfig
. - Resolve merged
config
againstprotocols
. - Resolve
overrides
againstprotocols
. - Merge
overrides
intoconfig
. - Resolve
config
againstconfig:
protocol.