umgebung

0.1.0 • Public • Published

umgebung

NPM Package Build Status Dependencies

hinoki source for environment variables

npm install umgebung

provided these envvars are set in your shell:

PORT=8080
ENABLE_ETAGS=true
DATABASE_URL="postgres://localhost:5432/my_database"
COMMISSION=0.1
API_CREDENTIALS='{"user": "foo", "password": "bar"}'

then with umgebung and the hinoki dependency injection system you can ask for envvars by their name in your code:

var hinoki = require('hinoki');
var umgebung = require('umgebung');
 
hinoki(umgebung, function(
  envIntPort,
  envBoolEnableEtags,
  envStringDatabaseUrl,
  envFloatCommision,
  maybeEnvStringApiKey,
  maybeEnvIntPoolSize,
  maybeEnvJsonApiCredentials
  env
) {
  assert(envIntPort === 8080);
  assert(envBoolEnableEtags === true);
  assert(envStringDatabaseUrl === 'postgres://localhost:5432/my_database');
  assert(envFloatCommission === 0.1);
  assert(maybeEnvStringApiKey === null);
  assert(maybeEnvIntPoolSize === null);
 
  assert(maybeEnvJsonApiCredentials.user === 'foo');
  assert(maybeEnvJsonApiCredentials.password === 'bar');
 
  assert(env.PORT === '8080');
  assert(env.ENABLE_ETAGS === 'true');
  assert(env.DATABASE_URL === 'postgres://localhost:5432/my_database');
  assert(env.COMMISSION === '0.1');
  assert(env.API_CREDENTIALS === '{"user": "foo", "password": "bar"}');
});

umgebung parses type and envvar name from dependency-names (function arguments), looks them up on process.env and converts them to the specified type.

unless names start with maybe an error is thrown if no such envvar is present or it is blank.

types Int, Bool, Float and Json throw if the envvar can't be converted into the type.

all that comes after the type is converted from camelcase to underscore-delimited-uppercase and looked up on process.env.


you can add your own types, change the env prefix and much more:

var myUmgebung = umgebung.configure({
  // you can provide the env to use (defaults to `process.env`)
  env: {
    PORT: '9090',
    COMMISSION: '0.1'
  },
  // you can change the prefixes
  prefix: 'umgebungsVariable',
  maybePrefix: 'vielleicht',
  // you can change the name of the dependency where the whole `env`
  // (see above) is provided (defaults to `'env'`)
  envDependencyName: 'umgebung'
  // you can add your own types
  typeHandlers: {
    zahl: function(parsed, value) {
      var result = parseInt(value, 10);
      if (isNaN(result)) {
        throw new Error('env var ' + parsed.envVarName + 'must be an integer');
      }
      return result;
    }
  }
});
 
hinoki(myUmgebung, function(
  umgebungsVariableZahlPort,
  vielleichtUmgebungsVariableZahlPoolSize,
  umgebungsVariableFloatCommission,
  umgebung
) {
  assert(umgebungsVariableZahlPort === 9090);
  assert(vielleichtUmgebungsVariableZahlPoolSize === null);
  assert(umgebungsVariableFloatCommission === 0.1);
 
  assert(umgebung.PORT === '9090');
  assert(umgebung.COMISSION === '0.1');
});

license: MIT

Package Sidebar

Install

npm i umgebung

Weekly Downloads

5

Version

0.1.0

License

none

Last publish

Collaborators

  • snd