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

strict-env-conf

v0.5.2

Published

NPM package for easy work with ENV-based configuration

Downloads

5

Readme

strict-env-conf

NPM version

NPM package for easy work with ENV-based configuration

Usage

Just two examles...

The first idea: if something wrong with your environment variables - your application must crashes on startup, not in arbitrary time when running.

const sec = require('strict-env-conf');

const tpl = {
  scopes: {
    DB: [
      {name: "PASSWORD"}
    ]
  }
};

sec(tpl);

Output:

...
Error: Required env variable DB_PASSWORD is not set
...

The second idea - parsing string values from environment variables to convenient JS-object with several types of properties.

const sec = require('strict-env-conf');
const filters = require('strict-env-conf/src/filters');

const envVars = {
  SEC_APP_HOST:                   'example.com',
  SEC_APP_PORT:                   '80',
  SEC_DB_EXTERNAL_AUTH:           'true',
  SEC_DB_PORT:                    '444',
  SEC_LDAP_TEST_USER_EMAIL:       'null',
  SEC_LDAP_TEST_USER_ID:          '4321',
};

for (let varName in envVars) {
  process.env[varName] = envVars[varName];
}

const template = {
  prefix: 'SEC_', // env variables app-level prefix for safety
  // filters: [str => String(str).trim()] <-- this is by default. Set "filters: []" to avoid this behavior.
  scopes: {
    APP: [
      {name: "HOST", default: "localhost"},
      {name: "PORT", default: 3000} // "filters: [Number]" is redundant here, Number is default filter if (typeof default === 'number')
    ],
    DB: [
      {name: "EXTERNAL_AUTH", default: false}, // default filter for boolean types works with "0", "1", "true", "false"
      {name: "PORT", filters: [Number]}, // no default value here, just the Number filter - the variable is required 
      {name: "NAME", default: "my-app"}
    ],
    LDAP: [
      {name: "TEST_USER_LOGIN", default: undefined}, // if default === undefined - you must check in your application code what conf object have this property
      {name: "TEST_USER_EMAIL", default: undefined, filters: [filters.maybeNull]},
      {name: "TEST_USER_ID", default: undefined, filters: [Number]},
    ],
    DATA: [
      {name: "CATEGORIES", default: "2,1,E", filters: [filters.csv2array]}, // you also can use your own filters. Filter function example: stringValue => 'MY_' + stringValue 
    ],
  }
};

const testConf = sec(template);
console.log(JSON.stringify(testConf, null, '    '));

Output:

{
    "app": {
        "host": "example.com",
        "port": 80
    },
    "db": {
        "external_auth": true,
        "port": 444,
        "name": "my-app"
    },
    "ldap": {
        "test_user_email": null,
        "test_user_id": 4321
    },
    "data": {
        "categories": [
            "2",
            "1",
            "E"
        ]
    }
}

Installation

npm i strict-env-conf

License

MIT