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

@kofile/config-factory

v2.1.0

Published

Config client with optional validation

Downloads

47

Readme

ConfigFactory

Build Status Coverage Status Commitizen friendly semantic-release

ConfigFactory is a small helper function that helps you to safely and easily create configuration objects from an externally injected source. You can also pass-in default values and Joi validation schemas, and you'll get easy-to-handle errors if your config object doesn't pass validation.

Usage

The default export is a curried function that expects a map object that translates the from the data you have to the shape you want. Here's a simple example:

// assuming `process.env.SOME_EXISTING_VAR === 'hello world'`

const map = { myCoolNewKey: 'SOME_EXISTING_VAR' }
const configFactory = makeConfigFactory(map)
const config = configFactory(process.env)

config.get(['myCoolNewKey']) === 'hello world'

In your tests, you might not want to muck around with process.env. Define an object to use instead:

const map = { myCoolNewKey: 'SOME_EXISTING_VAR' }
const configFactory = makeConfigFactory(map)
const config = configFactory({ SOME_EXISTING_VAR: 'hello world' })

config.get(['myCoolNewKey']) === 'hello world'

You can provide a default value as the third value in a tuple:

const map = { myCoolNewKey: ['SOME_EXISTING_VAR', null, 'banans'] }
const configFactory = makeConfigFactory(map)
const config = configFactory({})

config.get(['myCoolNewKey']) === 'bananas'

Also, you can pass in Joi schemas per-key as the second value in a tuple:

const map = { myCoolNewKey: ['SOME_EXISTING_VAR', joi.string().required()] }
const configFactory = makeConfigFactory(map)
const config = configFactory({})

try {
  config.get(['myCoolNewKey'])
} catch (error) {
  error.message === 'Invalid config for myCoolNewKey!'
}

You can validate your entire config by running the validate() method:

const map = { myCoolNewKey: ['SOME_EXISTING_VAR', joi.string().required()] }
const configFactory = makeConfigFactory(map)
const config = configFactory({})

config.validate()

This will appear to do nothing, and that's because when called this way, you need to subscribe to the invalid event handler like so:

config.on('invalid', error => {
  console.error(error)
  process.exit(1)
})

config.validate()

The event listener will receive error from the validation result. Get the message of the failure with error.message.

Subscribing an event handler and invoking config.validate() is the preferred way of handling configuration validation. The previous method (throwing an error on a property-by-property basis) is only there as a fail-safe against forgetting to invoke validate().

Additionally, calling validate() will skip future checks when calling get on a keypath.

NOTE: Incomplete or invalid configurations can lead a service to not start at all or start in a thoroughly broken state; therefor it's advisable to bail hard, fast, and noisily with process.exit(1) if validate() fails.

Lastly, you can also use computed properties: the function for a computed property will receive the all of the non-computed config as an argument.

NOTE: Order of computed property invocations is not guaranteed, so please don't make computed properties that depend on other computed properties. That just sounds like a headache anyways.

const env = { A: 'aaaa' }
const map = { scarletLetter: 'A', phrase: config => `${config.scarletLetter} is not B`) }
const configFactory = makeConfigFactory(map)
const config = configFactory(process.env)

config.get(['phrase']) === 'aaaa is not B'

You can use computed properties to return hard-coded values:

const map = { theAnswer: () => 42 }
const configFactory = makeConfigFactory(map)
const config = configFactory(process.env)

config.get(['theAnswer']) === 42

Computed properties can also be validated and use default values.

Why?

  • Separation of concerns We've described the final shape we want, complete with validations and default values, without hard-coding where the values come from.
  • Ease of testing We've full control over the values of env without having to fuss with globals. This is a nice side effect of the point above.
  • State isolation By treating our configuration as the product of inputs into a function, we make it much harder to pass configuration state around via require/import statements. This is a good thing!

Supported Platforms

Node 7+

Testing

Run tests with yarn test

Authors