Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

strict-env-conf

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strict-env-conf

NPM package for easy work with ENV-based configuration

  • 0.5.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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 filters = require('strict-env-conf');
const sec = 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]},
      {name: "NAME", default: "my-app"}
    ],
    LDAP: [
      {name: "TEST_USER_LOGIN", default: undefined},
      {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]},
    ],
  }
};

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

Keywords

FAQs

Package last updated on 14 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc