kvz / environmental

Node project that aims to provide code and conventions to deal with environment vars for configuration in a structured, reusable, pleasant way
https://www.npmjs.org/package/environmental
MIT License
13 stars 1 forks source link

Get environment variables by prefix #2

Open koenpunt opened 10 years ago

koenpunt commented 10 years ago

I like the idea of parsing the variables into objects, although parsing environment variables like TWITTER_ACCESS_TOKEN_SECRET result is something like:

twitter:
  { access:
     { token:
       { key: 'xxxx',
         secret: 'xxxx' } }
    consumer:
     { key: 'xxxxxxx',
       secret: 'xxxxxxx' } }

Where I previously could use a JSON object like this:

var client = new Tuiter(config.twitter);

I now have to supply every property separately:

var client = new Tuiter({
  consumer_key: config.twitter.consumer.key,
  consumer_secret: config.twitter.consumer.secret,
  access_token_key: config.twitter.access.token.key,
  access_token_secret: config.twitter.access.token.secret
});

And I know with environment variables would it be the same:

var client = new Tuiter({
  consumer_key: config.twitter..TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

But I think it would be nice to fetch some variables by prefix, but not completely teared apart.

Something in the likes of:

var env = require('environmental');
var twitter = env.filter('twitter');
// Where twitter is
{
  consumer_key: 'xxxxx',
  consumer_secret: 'xxxxx',
  access_token_key: 'xxxxx',
  access_token_secret: 'xxxxx'
}
kvz commented 10 years ago

I see a few options:

  1. access the var via process.env
  2. name the var TWITTER_KEYS_CONSUMER vs TWITTER_CONSUMER_KEY
  3. name the var TWITTER_CONSUMERKEY vs TWITTER_CONSUMER_KEY
  4. make the delimiter configurable. eg. __ and then name the var TWITTER__CONSUMER_KEY vs TWITTER_CONSUMER_KEY
  5. provide a method that let's you flat/unflatten on a level that the developer chooses (your solution)

For me personally, 3 has worked well enough. But I think if Environmental needs to address this, I feel most for 4.

What do you think?

koenpunt commented 10 years ago

The problem is that in this case the library (Tuiter) expects the keys of the configuration to be like

{
  consumer_key: 'xxxxx',
  consumer_secret: 'xxxxx',
  access_token_key: 'xxxxx',
  access_token_secret: 'xxxxx'
}

so this rules out option 3.

Customizable delimiter will convenient anyway.

Another option could be to limit nesting with a numeric value. When set to 1, TWITTER_ACCESS_TOKEN_SECRET results in:

twitter: {
   access_token_secret: 'xxx'
}

When set to 2 it results in:

twitter: {
   access: {
      token_secret: 'xxx'
   }
}

Etc.

kvz commented 10 years ago

I like the idea of making the delimiter and nesting level configurable! Would you also be interested in hacking on that?