1.0.0 • Published 5 years ago

zenver v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Zenver

Easy-to-use module for integrating your javascript objects with environment variables.

What's the pain?

Have you ever written the Node JS code like this?

const config = {
  express: {
    port: process.env.EXPRESS_PORT || '8000',
    host: process.env.EXPRESS_HOST || 'localhost'
  },
};

module.exports = config;

We all know - it's really handy to use the environment variables. And it's a pretty good pattern - for example if you're using the Docker. But using process.env may be really wearisome in case of lot of properties.

Zenver allows you to use env variables, based on their object path. So if you have the following object:

const example = {
  some: {
    nested: {
      prop: zenverVar({ default: 'some-value' }),
    }
  }
}

All you need to do to change the example.some.nested.prop is to run export example_some_nested_prop=new-value. That's it!

What exactly zenver can do for me?

  • Build the environment variable path from the object properties
  • Use the environment variable value if it's specified, or use the default value
  • Throw an error if the value is required, but not specified
  • Validate the value with your validator function
  • Sanitize the value with your sanitizer function
  • Show the verbose list of used environment variables

How can I use zenver?

Here's the example with pretty much all Zenver features used:

const { zenverVar, zenver } = require('zenver');

const configExample = {
  express: {
    port: zenverVar({ default: 8000 }), // 8000 by default or 'express_port'
    host: zenverVar({ default: 'localhost' }), // 'localhost' by default or 'express_host'
  },
  deterministicVar: 'this-prop-isnt-assosiated-with-any-environment',
  requiredVar: zenverVar({ required: true }), // Throws an error if no env value specified
  varWithValidation: zenverVar({
    validator: (valueFromEnv) => { return true; }, // Validator should return true, otherwise an error will be thrown
  }),
  varWithSanitizing: zenverVar({
    sanitizer: (valueFromEnv) => { return parseInt(valueFromEnv, 10); },
  }),
  varWithDescription: zenverVar({ description: 'Some description for variable' }), // Helpful for .getEnvigVariables() call, read bellow
};

const config = zenver(configExample);

console.log(config.express.host); // 8000

Getting the environment paths for an object

It's pretty easy to get the list of all used env paths. Here's a small snippet for getting the list of

const { getZenverDetails } = require('zenver');

const program = require('commander');
const config = zenver(configExample);

program
  .option(('--zenver', 'Get the list of paths for used environment variables');

program.parse(process.argv);

if (program.zenver) {
  console.log(getZenverDetails(config));
}

Specifing the path options

const config = zenver(
  configExample,
  {
    upperCase: true, // 'express_host' -> 'EXPRESS_HOST'
    delimiter: '__', // 'EXPRESS_HOST' -> 'EXPRESS__HOST'
    prefix: 'prefix_string', // 'EXPRESS__HOST' -> 'PREFIX_STRING__EXPRESS__HOST'
  },
);