UNPKG

3.23 kBMarkdownView Raw
1# confit
2
3Simple, environment-based configuration. `confit` loads a default JSON
4configuration file, additionally loading environment-specific files, if applicable.
5It will also process the loaded files using any configured
6[shortstop](https://github.com/paypal/shortstop) protocol handlers.
7(See **Options** below.)
8
9[![Build Status](https://travis-ci.org/totherik/confit.png)](https://travis-ci.org/totherik/confit)
10
11## Usage
12```javascript
13var confit = require('confit');
14```
15
16### confit(options, callback);
17* `options` (*String* | *Object*) - the base directory in which config files live
18or a configuration object.
19* `callback` (*Function*) - the callback to be called with the error or config object.
20Signature `function (err, config) { /* ... */}`
21
22```javascript
23'use strict';
24
25var path = require('path');
26var confit = require('confit');
27
28var basedir = path.join(__dirname, 'config');
29confit(basedir, function (err, config) {
30 config.get; // Function
31 config.set; // Function
32 config.use; // Function
33
34 config.get('env:env'); // 'development'
35});
36```
37
38## Options
39* `basedir` (*String*) - the base directory in which config files can be found.
40* `protocols` (*Object*) - An object containing a mapping of
41[shortstop](https://github.com/paypal/shortstop) protocols to handler implementations.
42This protocols will be used to process the config data prior to registration.
43* `defaults` (*String*) - the name of the file containing all default values.
44Defaults to `config.json`.
45
46```javascript
47'use strict';
48
49var path = require('path');
50var confit = require('confit');
51var handlers = require('shortstop-handlers');
52
53
54var options = {
55 basedir: path.join(__dirname, 'config');
56 protocols: {
57 file: handlers.file,
58 glob: handlers.glob
59 }
60};
61
62confit(options, function (err, config) {
63 config.get; // Function
64 config.set; // Function
65 config.use; // Function
66
67 config.get('env:env'); // 'development'
68});
69```
70
71
72## API
73* `get(key)` - Retrieve the value for a given key.
74* `set(key, value)` - Set a value for the given key.
75* `use(obj)` - merge provided object into config.
76
77```javascript
78config.set('foo', 'bar');
79config.get('foo'); // 'bar'
80
81config.use({ foo: 'baz' });
82config.get('foo'); // 'baz'
83```
84
85## Default Behavior
86By default, `confit` loads `process.env` and `argv` values upon initialization. Additionally,
87it creates convenience environment properties prefixed with `env:` based on the
88current `NODE_ENV` setting, defaulting to `development`. It also normalizes
89`NODE_ENV` settings to the long form, so `dev` becomes `development`, `prod`
90becomes `production`, etc.
91```javascript
92// NODE_ENV='dev'
93config.get('NODE_ENV'); // 'dev'
94config.get('env:env'); // 'development'
95config.get('env:development'); // true
96config.get('env:test'); // false
97config.get('env:staging'); // false
98config.get('env:production'); // false
99```
100
101```javascript
102// NODE_ENV='custom'
103config.get('NODE_ENV'); // 'custom'
104config.get('env:env'); // 'custom'
105config.get('env:development'); // false
106config.get('env:test'); // false
107config.get('env:staging'); // false
108config.get('env:production'); // false
109config.get('env:custom'); // true
110```