@northernv/config

2.0.0 • Public • Published

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/config

Usage

// 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.5

Sugar

// Values can be accessed directly

config.IS_AWESOME // => true

as opposed to the longer way of 

config.get('IS_AWESOME') // => true

Types

  • Strings
  • Booleans
  • Integers
  • Floats
  • CSV => Array

NOTES

  • Precedence

    1. Environment Variables trump all
    2. .env in the root directory takes second
    3. Passed in defaults object
  • 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'
    }
    
  • .env was deliberately added to git for testing

Readme

Keywords

none

Package Sidebar

Install

npm i @northernv/config

Weekly Downloads

8

Version

2.0.0

License

MIT

Unpacked Size

11.7 kB

Total Files

11

Last publish

Collaborators

  • shanestillwell