npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

config-getter

v0.1.0

Published

JSON config reader with different ways of variable overrides and placeholders!

Downloads

31

Readme

config-getter

Build Status NPM version

Simple json configuration reader with file and env variables overrides. Can fetch / override variables using:

  • Local file
  • Env variables
  • Consul key-value

For 0.0.x versions documentation see older README.md

Installation

$ npm install config-getter

Usage

default.js

module.exports = {
    "db": {
        "host": "localhost",
        "port": "12345"
    }
}

overrides.json

{
    "db": {
        "host": "google.com"
    }
}

app.js

const getConfig = require('config-getter').getConfig;

//
// Obtain final configuration object.
//
// Important Notice: even that underlying fetchers use async IO,
// interface to getConfig is sync for usability purposes. 
// As a result, Application that use config-getter should do getConfig once at startup time.
//
let config = getConfig(__dirname + '/default.js', {

    /* ----- optional parameters ----- */

    // if true, replaces arrays values
    // If false, merge arrays
    replaceArrays: false,

    // if true, replaces objects values
    // If false, merge objects
    replaceObjects: false,

    // If true, ignore fetcher exceptions and continue work
    // If false, throw/propagate all exceptions
    ignoreFailedFetchers: false,

    // Optional fetchers
    // Values from these fetchers will be merged into default config
    // in order of their appear in the following array:
    fetchers: [

        /* ----- one or more of the following ----- */

        {
            type: 'consul',
            opts: {
                baseUrl: 'http://localhost:8500/v1',  // base url for Consul REST API
                key: 'config'                         // key to look for JSON value
            }
        },

        {
            type: 'file',
            opts: {
                path: process.env.PATH_TO_CONFIG       // path to json file
            }
        },

        {
            type: 'env',
            opts: {
                prefix: 'CONFIG_',                    // prefix of env variable used to override values
                ignore: '^PATH_TO_CONFIG$'            // regexp to ignore env variable if there is a match
            }
        }
    ]

});

console.log(config);

Simple config loading

$ node app.js

Override config with overrides.json values

$ PATH_TO_CONFIG=./overrides.json node app.js

Override config with overrides.json values and single key value

$ PATH_TO_CONFIG=./overrides.json CONFIG_db_port=7777 node app.js

Placeholders

It's possible to make a reference to previous defined value in some config string value:

{
   "who": "world",
   "phrase": "hello, $(who)!"
}

So phrase will become "hello, world!". It is possbile to make reference in inner objects too:

"$(some.obj.value)"

Placeholders also may be relative. Currently the same level, and parent level are supported.

{
    "a": {
        "a1": {
            "name": "Vasya",
            "phrase": "My name is $(.name)" // link to the same level var starts with single dot '.'
       },
       "a2": {
           "name": "$(..a1.name)" // link to upper level var starts with two dots '..'
       }
    }
}

Tests

$ sudo npm install nodeunit -g
$ npm test

Author

License

MIT