Giter Club home page Giter Club logo

node-yapec's Introduction

YAPEC

Yet Another Parser for Environmental Configuration

Because there just aren't enough npm modules for getting config values from your enviroment already!

Travis Status

Build Status

NPM

Installation

Be sane, use npm

$ npm install yapec

otherwise clone this repo via git

$ git clone https://github.com/sandfox/node-yapec.git

Usage

var yapec = require('yapec');
var config = yapec(['PRE_FIX'], configSpec, process.env, opts);

yapec takes a spec in the form of an object (which can be nested to your heart's content) where the leaf of every path must be a string which dictates how to parse the corresponding ENV_VAR string. The path itself is converted into UPPERCASE, and dot seperators exchanged for underscores*.

An optional prefix may be supplied as the first arg which will act as a mask when searching the env object. An optional options object may be supplied, so far the only option is 'ignoreMissing' that accepts a bool, is false by default, and when true rather than throwing an exception if an ENV VAR is missing, instead returns a null for that value.

*yes I realise this is probably not the clearest way to describe what it does but my brain is failing me at this point in time

Examples

Some of these examples can also be found in the examples folder inside this project.

var yapec = require('yapec');

//Represents something we could expect process.env to return
var env = {
    APP_PATH: '/opt/appy',
    APP_NAME: 'super server',
    APP_SERVER_ENABLED: 'true',
    APP_SERVER_PROCS: '8',
    APP_SERVER_MAGIC: '2e2'
}

var configSpec = {
    app : {
        path: 'string',
        name: 'string',
        server: {
            enabled: 'bool',
            procs: 'int',
            magic: 'float'
        }
    }
}

var config = yapec(configSpec, env);

console.log(config)
//outputs the following
{ app:
   { path: '/opt/appy',
     name: 'super server',
     server: {
        enabled: true,
        procs: 8,
        magic: 200 }
    }
}

Optionally a prefix can be supplied as the first arguement which acts as a mask when looking through the enviroment variables.

Example

var yapec = require('yapec');

var env = {
    FALLOVER: 'true',
    MY_APP_FALLOVER: 'false'
}

var spec = {
    fallover: 'bool'
}

var config = yapec('MY_APP_', spec, env);

console.log(config);

{fallover:false}

Caveats

Due to the way this modules works certain combinations of ENV VAR strings are forbidden, for example the following would fail because it could not be resolved into an object in any sane way because app could not be both a string and object at the same time.

APP="super app"
APP_DB_NAME="megadb"
APP_DB_PORT="8000"

Helpers

yapec also comes with helpers for creating configs from process.env style objects and for creating ENV VAR strings from a config object. Checkout the examples folder as it should be pretty self explanatory. todo - document this better

yapec.getSpec([prefix], process.env)

and

yapec.getEnvStrings([prefix], config)

Stability Index

Based up on node.js stability index

Stability: 2 - Unstable

Testing

Code is tested with mocha + should, just run npm test as usual. The tests aren't bad, but they could be more complete. There are travis tests too!

Upgrades, fixes, ideas

All ideas, bug fixes, suggestions etc are gladly excepted so feel free to raise pull requests and issues.

License

(The MIT License)

Copyright (c) 2013 James Edward Butler AKA sandfox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-yapec's People

Contributors

sandfox avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

node-yapec's Issues

current thrown errors are horrid

This module really should throw it's own errors rather than just rethrowing or passing through underlying module errors. It's pretty confusing to debug and the stack traces are mad

Field validation and default values

This should probably be a module wrapped around yapec,

but in short the ability to more richly specify the contents, defaults and validatations. As yapec expects a POJS object this should be simples and allow the helpers to still work!

as mostly mentioned/requested by @fabriziomoscon

Allow arrays somehow

Would be nice to allow some (probably ugly) way getting values into arrays automatically.

idea

FOO_1="this"
FOO_2="that"

becomes the equivalent of this

var foo = ["this","that"]

Several ways off the top of my head

spec

{
   "foo": ["string"]
}

ENV

FOO_1="something"
FOO_2="another thing"
FOO_3="whatevers"

The 'thing' inside the array specifies the spec/schema that each item in the array must have/match.
This would limit the array to only having identical formed items inside it (not a bad thing)

It's more of a side effect than an intended design but the following might actually be possible depending on implementation

spec

{
  "foo" : [
    {
      "bar" : "string",
      "rod" : "int"
    }
  ]
}

gives the equivalent of

var foo = [
  {
    bar: "lalala",
    rod: 34
  },
  {
    bar: "frgsgrgs",
    rod: 5566
  }
];

from an ENV of

FOO_1_BAR="lalala"
FOO_1_ROD="34"
FOO_2_BAR="frgsgrgs"
FOO_2_ROD="5566"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.