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

env-config-map

v1.0.4

Published

Generates a config object from a config map using any input sources.

Downloads

13

Readme

env-config-map

Version Build Coverage Dependencies License

Generates a config object from a config map using any input sources. Mapping includes the most commonly encountered patterns such as setting default value, type casting, coercing null, coercing undefined, and redacting secrets for logging. Default input source are environment variables from process.env.

  • Zero dependency.
  • Supported types:
    • string
    • number
    • boolean
    • object
    • arrayCommaDelim
    • also supports custom types
  • redact option to redact value for logging.
  • coerceNull option to coerce "null" to null.
  • coerceUndefined option to coerce "undefined" to undefined.
  • getter option to get input values from other sources. Default source is process.env.

Installation

npm install env-config-map

Run Sandbox Example

npm run sandbox

Sandbox Example

// source from .env (optional)
// require('dotenv').config();

const envConfigMap = require('env-config-map');

// setup fixture data for example
process.env.SERVER_HOST = '0.0.0.0';
process.env.SERVER_PORT = 8080;
process.env.MAINTENANCE_MODE = 'true';
process.env.ENABLE_PROFILER = 'NO';
process.env.SETTINGS = '{ "path": "/tmp", "timeout": 1000 } ';
process.env.ACCESS_KEY = 'myAccessKey';
process.env.COERCE_NULL_DEFAULT = 'null';
process.env.COERCE_NULL_DISABLED = 'null';
// process.env.LOG_LEVEL = undefined;

const configMap = {
  SERVER_HOST: { default: 'localhost' },
  SERVER_PORT: { default: 80, type: 'number' },
  MAINTENANCE_MODE: { default: false, type: 'boolean' },
  ENABLE_PROFILER: { default: false, type: 'booleanYesNo' },
  SETTINGS: { type: 'object' },
  ACCESS_KEY: { redact: true },
  COERCE_NULL_DEFAULT: {},
  COERCE_NULL_DISABLED: { coerceNull: false },
  LOG_LEVEL: { default: 'info' },
};

const options = {
  types: {
    // define custom type "booleanYesNo"
    booleanYesNo: (str) => {
      const normalized = envConfigMap.utils.lowerTrim(str);
      if (normalized === 'yes') return true;
      if (normalized === 'no') return false;
      return null;
    },
  },
  // customize redactor
  redactor: (str) => str.replace(/.+/, '*** REDACTED ***'),
};

const config = envConfigMap(configMap, options);

console.log(config);
console.log(config.getRedacted());

console.log(config);

{
  SERVER_HOST: '0.0.0.0',
  SERVER_PORT: 8080,
  MAINTENANCE_MODE: true,
  ENABLE_PROFILER: false,
  SETTINGS: { path: '/tmp', timeout: 1000 },
  ACCESS_KEY: 'myAccessKey',
  COERCE_NULL_DEFAULT: null,
  COERCE_NULL_DISABLED: 'null',
  LOG_LEVEL: 'info',
  getRedacted: [Function],
  getOptions: [Function]
}

console.log(config.getRedacted());

{
  SERVER_HOST: '0.0.0.0',
  SERVER_PORT: 8080,
  MAINTENANCE_MODE: true,
  ENABLE_PROFILER: false,
  SETTINGS: { path: '/tmp', timeout: 1000 },
  ACCESS_KEY: '*** REDACTED ***',
  COERCE_NULL_DEFAULT: null,
  COERCE_NULL_DISABLED: 'null',
  LOG_LEVEL: 'info'
}

configMap

  • default : mixed
    • Sets the default value.
  • type : string (default: string)
    • Specify the type for the key. Cast operation will call the type casting function defined in options.types.
      • string
      • number
      • boolean
      • object
      • arrayCommaDelim
  • redact : boolean
    • Flag to indicate if the value is a secret and needs to be redacted.
  • coerceNull : boolean
    • Coerce string 'null' to null. Supersedes options.coerceNull.
  • coerceUndefined : boolean
    • Coerce string 'undefined' to undefined. Supersedes options.coerceNull.
const configMap = {
  SERVER_PORT: { default: 80, type: 'number' },
  ACCESS_KEY: { redact: true },
  ENABLE_PROFILER: { default: false, type: 'booleanYesNo' },
  COERCE_DISABLED: { coerceNull: false, coerceUndefined: false },
};
const config = envConfigMap(configMap, options);

options

  • getter : function
    • Get input value for key. Default source is process.env.
  • types : object
    • For defining additional types and it will merge with the supported types.
  • redactor : function
    • Function to redact value flaged with the redact configMap option.
  • coerceNull : boolean (default: true)
    • Coerce string 'null' to null.
  • coerceUndefined : boolean (default: true)
    • Coerce string 'undefined' to undefined.
const options = {
  getter: (key) => process.env[key],
  types: {
    // define custom type "booleanYesNo"
    booleanYesNo: (str) => {
      const normalized = envConfigMap.utils.lowerTrim(str);
      if (normalized === 'yes') return true;
      if (normalized === 'no') return false;
      return null;
    },
  },
  // customized redactor
  redactor: (str) => str.replace(/.+/, '*** REDACTED ***'),
  coerceNull: true,
  coerceUndefined: false,
};
const config = envConfigMap(configMap, options);

Misc Exports

const envConfigMap = require('env-config-map');
const defaultOptions = envConfigMap.defaultOptions;
const supportedTypes = envConfigMap.types;
const helperUtils = envConfigMap.utils;

Example Server Config with env-config-map

.env

APP_NAME = envConfigMap;
SERVER_PORT = 8080;
ACCESS_KEY = myAccessKey;

config.js

require('dotenv').config();
const envConfigMap = require('env-config-map');

const configMap = {
  APP_NAME: { default: 'noname' },
  SERVER_HOST: { default: 'localhost' },
  SERVER_PORT: { default: 80, type: 'number' },
  ACCESS_KEY: { redact: true },
};
const config = envConfigMap(configMap);

module.exports = config;

server.js

const http = require('http');
const config = require('./config.js');

console.log('Configs loaded.', config.getRedacted());

http
  .createServer((req, res) => {
    res.write(`Hello ${config.APP_NAME}!`);
    res.end();
  })
  .listen(config.SERVER_PORT, config.SERVER_HOST, () => console.log(
    `server listening on ${config.SERVER_HOST}:${config.SERVER_PORT}`,
  ));

License

Copyright © 2019 Joe Yu. Licensed under the MIT License.