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

ctrl-env

v2.2.0

Published

An alternative way to assert environment variables.

Downloads

3

Readme

ctrl-env

Build Status Codacy Badge Codacy Badge

An alternative way to assert environment variables.

Installation

Yarn is recommended for installation.

$ yarn add ctrl-env

But you can still use npm:

$ npm install --save ctrl-env

Usage

// Example app.js:

const CtrlEnv = require('ctrl-env')

const ctrlEnv = new CtrlEnv([
  ['SECRET']
, ['PORT', {required: false}]
], {
  prefix: 'TWITTER_FEED'
})

ctrlEnv.assert()

// Some Twitter Feed and Server pseudo code:

http.on('/feed', (request, response) => {
  Twitter.getFeed({
    handle: '_experiments'
  , secret: ctrlEnv.SECRET
  }, (error, feed) => {
    if (error) {
      throw error
    }

    response.send(feed)
  })
})

http.listen(ctrlEnv.PORT)
$ TWITTER_FEED_SECRET='twitter_secret_key' TWITTER_FEED_PORT=8080 node app.js
$ # or
$ TWITTER_FEED_SECRET='twitter_secret_key' node app.js

Methods v2

constructor(Array environmentVariables, Object options)

The constructor takes an array of environment variables to expect. The variables themselves can be configured:

const exampleEnvVars = [
  // Variable is required and can be any value:
  ['REQUIRED_VARIABLE']

  // Variable is optional and can be any value:
, ['OPTIONAL_VARIABLE', {required: false}]

  // Variable is required and can only be yes or no:
, ['LIMITED_VARIABLE', {values: ['yes', 'no']}]

  // Variable is required and can only be an integer:
, ['TYPED_VARIABLE', {type: 'integer'}]

  // Variable is optional and can only be yes or no:
, ['OPTIONAL_LIMITED_VARIABLE', {required: false, values: ['yes', 'no']}]

  // Variable is required but is not prefixed:
, ['NODE_ENV', {prefixed: false}]
]

const ctrlEnv = new CtrlEnv(exampleEnvVars)

The constructor also takes an optional prefix and separator. The separator only determines what seperates the prefix and the variable name:

// Variables must be labeled TEST_(VARIABLENAME):
new CtrlEnv(..., {prefix: 'TEST'})

// Variables must be labeled TEST___(VARIABLENAME)
new CtrlEnv(..., {prefix: 'TEST', separator: '___'})

ctrlEnv.assert()

This method actually reads the environment variables and asserts that they exist or have the required values. Note: This method is synchronous.

get ctrlEnv.VARIABLE_NAME

To read the environment variable, simply use the variable name without the prefix as a property of your CtrlEnv instance. This property is a proper ES2015 getter that does not have a setter. It cannot be overwritten without using Object.defineProperty.

get ctrlEnv.all

This returns an object with all asserted environment variables. This property is a propert ES2015 getter that does not have a setter. It cannot be overwritten without Object.defineProperty.

Note: This deprecates #get() from v1.

License

Copyright (c) 2017 Martin Experiments LLC

MIT (https://www.opensource.org/licenses/MIT)