@uzelux/config

1.1.0 • Public • Published

Config

A small configuration loader and reader.

Usage

Firstly, create a directory named Config at your project root

> cd YourAwsomeProject/
> mkdir Config
> cd Config/

Within Config, make a file index.js to act as our entry point, place the following sample config into it

module.exports = {
  App: {
    environment: process.env.APP_ENV || 'development',
    port: process.env.APP_PORT || 3000,
    secret: process.env.APP_SECRET || 'SuperSecret'
  }
}

You are all set! Just require the helper and get your config!

const Config = require('@uselux/config');
const env = Config.get('APP.environment');
console.log(env); // development

Methods

Config.get(path, default = null)

A basic getter that uses dot notation, will return default if the value in path is not set or unreachable due to any parent is undefined

Config.get('undefinedKey', 'defaultValue'); // defaultValue
Config.get('undefinedKey'); // null
Config.get('definedKey'); // defined value
Config.get('definedObjectKey') // object

Config.getOrFail(path)

Same as get(), but will throw an error if config is not defined

Config.getOrFail('undefinedKey'); // Error('Config is not defined')
Config.getOrFail('definedKey'); // defined value
Config.getOrFail('definedObjectKey') // object

Config.has(path)

Return a boolean based on the existence of the config

Config.has('undefinedKey'); // false
Config.has('definedKey'); // true

Config.hasOrFail(path)

Same as has(), but will throw an error if config is not defined, use this if you want to use try-catch instead of if-else

Config.hasOrFail('undefinedKey'); // Error('Config is not defined')
Config.has('definedKey'); // true

Recommendation

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i @uzelux/config

      Weekly Downloads

      1

      Version

      1.1.0

      License

      MIT

      Unpacked Size

      13.8 kB

      Total Files

      9

      Last publish

      Collaborators

      • uzelux