1.0.2 • Published 6 years ago

ezconfig-node v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

ezconfig

npm version Build Status Coverage Status dependencies Status devDependencies Status

Tool to simplify the configuration of a node application.

Installation

You can get the latest release and the type definitions using npm:

$ npm install ezconfig-node --save

Example

import * as ezconfig from 'ezconfig-node';

const defaultConfig = {
    api: {
        port: 8080,
        specialPort: 7070
    },
    otherComponent: {
        array: [],
        arrayString: '[]'
    }
};

// example only
process.env['PORT'] = '9090';
process.env['APP_OTHERCOMPONENT_ARRAY'] = '[1,2,3,4]';
process.env['APP_OTHERCOMPONENT_ARRAYSTRING'] = '"[1,2,3,4]"';

const config = ezconfig
    .create(defaultConfig)
    .json({ something: 'good' })
    .jsonFileSync('config.json') // file: { "api": { "specialPort": 6060 } }
    .envAll('APP')
    .env('PORT', 'api.port')
    .get();

console.log(config);

Result:

{
    "api": {
        "port": 9090,
        "specialPort": 6060
    },
    "otherComponent": {
        "array": [1,2,3,4],
        "arrayString": "[1,2,3,4]"
    },
    "something": "good"
}

Methods

json

Merge the old configuration with a new json object.

// creates configuration { "a": 2, "b": 3 }
ezconfig.create({ a: 1 }).json({ a: 2, b: 3 })

jsonFileSync

ezconfig.create().jsonFileSync('config.json').get()

env

Use the specific environment variable to overwrite a property of the config.

// Creates the object { "api": { port: API_PORT } }
ezconfig.create({ api: { port: 8080 } }).env('API_PORT').get()

Use the second parameter to choose the property.

// Creates the object { "api": { port: PORT_VALUE } }
ezconfig.create({ api: { port: 8080 }).env('PORT', 'api.port').get()

If the property path is not set, it will use the environment variable name in lowercase.

// Creates the object { "api": { port: 8080 }, "port": PORT_VALUE }
ezconfig.create({ api: { port: 8080 }).env('PORT').get()

envAll

Use all the environment variables that start with the specific prefix.

// APP_API_PORT = '8080'
// APP_SOMETHING = '"good"'

ezconfig.create().envAll('APP').get()

// result: { "api": { "port": 8080 }, "something": "good" }

get

Returns the current configuration.

// returns object { "a": 1 }
ezconfig.create({ a: 1 }).get()

Caveat

The method env and envAll will keep the properties original names if they already exist with uppercase letters, example:

// SOMETHINGGREAT = "ezconfig again"

ezconfig.create({ somethingGreat: 'ezconfig' }).env('SOMETHINGGREAT').get()

// result: { "somethingGreat": "ezconfig again" }
1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago