2.0.0 • Published 5 years ago
@northernv/config v2.0.0
Config
A configuration manager utilizing nconf. The claim to fame: casts the incoming process.env strings to their corrisponding default types
Install
yarn install @northernv/configUsage
// config.js
const Config = require('@northernv/config')
const defaults = {
WWW_URL: 'https://www.example.com',
IS_AWESOME: true,
SCORE: 100,
COLORS: ['red', 'green', 'blue'],
RATE: 3.5,
}
module.exports = Config(defaults)// index.js
const config = require('./config')
// Is cast to a boolean, because the default is a Boolean
config.get('IS_AWESOME') // => true
// Is cast to integer because the default is a integer
config.get('SCORE') // => 100
// Is cast to a float because the default is a float
config.get('RATE') // => 3.5Sugar
// Values can be accessed directly
config.IS_AWESOME // => true
as opposed to the longer way of
config.get('IS_AWESOME') // => trueTypes
- Strings
- Booleans
- Integers
- Floats
- CSV => Array
NOTES
Precedence
- Environment Variables trump all
.envin the root directory takes second- Passed in
defaultsobject
Casting
All values should have a default in the type they expect to be. For example, if you use a variable string
WWW_URL = 'https://www.example.com'. You should declare a default value so it knows how to handle incoming environment variables.Never store sensitive information in code. Instead just use
const default = { DB_PASSWORD: 'REDACTED' }.envwas deliberately added to git for testing