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

@gitzup/config

v0.0.9

Published

TypeScript Configuration Utility

Downloads

4

Readme

config

npm npm (scoped) npm Codecov Build Status

Node.js utility for configuration.

Installation

npm install @gitzup/config --save

Usage

Getting Started


// create a configuration object
const config = require( '@gitzup/config' )( {
    key1: new StringConfigValue( { default: "val1" } ),
    path1: {
        key2: new IntegerConfigValue( { default: 17 } ),
        key3: new IntegerConfigValue( { default: 29 } ),
    }
});

// pretty-print all key/value paths
console.info(); 
console.info( "Pretty print (useful for application startup):" ); 
config.print();

// access and print the "key1" config key
console.info(); 
console.info( "Custom/manual access (to access configuration during runtime):" ); 
console.info( "  -> Configuration key 'key1' is: " + config.key1.value );
console.info( "  -> Configuration key 'path1.key2' is: " + config.path1.key2.value );

Output:

$ KEY1=custom-val
$ PATH1_KEY2=999
$ node ./app.js

Pretty print (useful for application startup):
  -> key1....................................custom-val 
  -> path1.key2..............................999 
  -> path1.key3..............................29 

Custom/manual access (how to access configuration during runtime):
  -> Configuration key 'key1' is: custom-val 
  -> Configuration key 'path1.key2' is: 999 

Configuration Value Providers

The config hash must be composed of keys (strings) and value providers, where each provider must be an instance of one of the classes below. The providers can be configured by a hash provided to their constructor - see each provider for the options it supports.

  • BooleanConfigValue accepts true, 1, on & yes as truthy values; everything else will cause false to be returned.

    Options:

    • default: an optional value to use when the environment variable is not set. If not provided, then the environment variable is required to be set.
  • CalculatedConfigValue does not read environment variables at all, instead always calls a getter function and returns its result. This is useful for including things in your config hash that are inferred from other configuration values or from something else entirely.

    Options:

    • getter: required function to invoke when a value is requested.
  • EnumConfigValue accepts a hash of acceptable key/value pairs, and expects the environment variable it is mapped to have a value that equal one of the keys in the given hash; that key's value will be returned. This enables a level of indirection between the actual environment variable value and the actual value returned by this provider.

    Options:

    • values: required hash of key/values that together comprise the set of valid options for the environment variable. Each key in this hash is a valid value; if that key is indeed the value of the variable, the key's value will be returned as this provider's value.
    • default: an optional value to use when the environment variable is not set. If not provided, the environment variable must be set.
  • IntegerConfigValue reads integer values from the environment variable (currently does not really validate it's an integer rather than a decimal number).

    Options:

    • min: optional minimum value for the number.
    • max: optional maximum value for the number.
    • default: an optional value to use when the environment variable is not set. If not provided, the environment variable must be set.
  • StringConfigValue reads a string from the environment variable.

    Options:

    • minLength: optional minimum length for the value.
    • maxLength: optional maximum length for the value.
    • pattern: optional pattern that must match the value.
    • sensitive: if true, the value will be masked when printed by the print function.
    • default: an optional value to use when the environment variable is not set. If not provided, the environment variable must be set.

These objects are used as values to keys in the config hash. When the hash is passed to the factory (what you get from the require statement) these objects are populated with the parsed values from the environment.

Special keys

The returned configuration hash will always contain the following key/value pairs:

  • env will always be either production (if NODE_ENV is one of prd, prod or production) or development (anything else or empty).

  • prod will be true if env === 'production'

  • dev will be true if env === 'development'

  • print will be a function that pretty-prints the configuration hash.

Contributing

Please see our contributing document.

Resources

Coverage setup:

  • https://azimi.me/2016/09/30/nyc-mocha-typescript.1.html
  • https://istanbul.js.org/docs/tutorials/typescript/