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

@jrh-works/adapt

v2.3.1

Published

Flexible configuration and environment variable management for Node.js applications.

Downloads

4

Readme

@jrh-works/adapt

Flexible configuration and environment variable management for Node.js applications.

For a quickstart, see the usage example.

Installation

npm install @jrh-works/adapt

The Configuration Function

Usage

const configureAdapt = require('@jrh-works/adapt')

Syntax

configureAdapt(options)

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | options | Object: Options | Options for configuring the selecting function. |

Returns

| Type | Description | | :-- | :-- | | Function | A selecting function which can be used to access the configuration object. |

Exceptions

Throws a standard Error if:

  • The configuration object is not present.
  • The environment object is not present.
  • The environment object does not contain the mode key.

The Options Object

| Attribute | Type | Description | | :-- | :-- | :-- | | configuration | Object: Configuration | Configuration data which will be accessed by the selecting function. | | environment | Object: Environment | Environment data used to change the which configuration data will be accessed. |


The Configuration Object

The configuration object contains the configuration data for your application. It can contain special keys and values.

Example Structure

{
  api: {
    keys: {
      _default: 'bcd-234',
      staging: 'abc-123'
      production: '[production_key]'
    }
  },

  general: {
    arbitrary_list: [ 1, 2, 3 ],
    number: 5,
    release_date: '2020-01-01'
  }
}

Normal Keys

Normal keys can be accessed by name with the selecting function:

adapt('general.arbitrary_list') // => [ 1, 2, 3 ]
adapt('general.number') // => 5
adapt('general.release_date') // => '2020-01-01'

Special Keys

Plural Keys

Plural keys, such as api.keys above, will be accessed when the singular form of the selector (api.key) can't be found.

The value returned will be based on the mode defined in the environment object. If the mode matches a key within the plural key (such as api.keys.production), that mode-specific value will be returned.

// Environment: { mode: 'staging' }
adapt('api.key') // => 'abc-123'
The _default Key

The _default key may be defined as a child of a plural key (api.keys._default). This key will be accessed if no key matching the mode can be found within the plural key.

// Environment: { mode: 'development' }
adapt('api.key') // => 'bcd-234'

Special Values

Environment Values

Environment values are surrounded in brackets ([value_from_environment]). This tells the selecting function to search the environment object for a matching value.

This is useful for private values that you don't want committed to source control.

// Environment: { mode: 'production', production_key: 'xyz-345' }
adapt('api.token') // => 'xyz-345'

If the configuration value contains text in addition to the environment value, that text will be preserved.

Multiple environment values can be included in a single configuration value.

// Configuration: { phrase: 'begin-[word_1]-[word_2]-end' }
// Environment: { word_1: 'hello', word_2: 'world' }

adapt('phrase') // => 'begin-hello-world-end'

The Environment Object

The environment object contains the application's environment data and mode.

The simplest form of this could be to use process.env:

const adapt = require('@jrh-works/adapt')({
  configuration: {},
  environment: process.env
})

You can also mix together sources. This is useful when you want to keep an environment file (or files) on your local machine, while relying on private values in remote environments.

let local = {}
try { local = require('./environment.json') } catch {}

const adapt = require('@jrh-works/adapt')({
  configuration: {},

  environment: {
    ...local,
    ...process.env
  }
})

Example Structure

{
  mode: 'staging',

  production_key: 'xyz-654',
  private_api_key: 'vfx-125'
}

Special Keys

The Mode Key

The environment object must contain a mode key which contains the running mode of the application (i.e. development or production).

This tells the selecting function which part of the configuration object to use when a plural key is accessed.

For an example, see the usage example.

The Selecting Function

The selecting function is generated by the configuration function and can be used throughout your application to access configuration data.

Syntax

adapt(selector)

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | selector | String | A string in dot notation describing which part of the configuration data to return. |

Example

adapt('api.key')

Exceptions

Throws a standard Error if:

Usage Example

environment.json

{
  "api_key_development": "development_value"
}

adapt.json

{
  "api": {
    "keys": {
      "_default": "[api_key_development]",
      "production": "[api_key_production]"
    }
  }
}

adapt.js

let local = {}
try { local = require('./environment.json') } catch {}

module.exports = require('@jrh-works/adapt')({
  configuration: require('./adapt.json'),

  environment: {
    ...local,
    ...process.env
  }
})

app.js

const adapt = require('./adapt')

// With process.env = { mode: 'development' }:
adapt('api.key') // => 'development_value'

// With process.env = { mode: 'production', api_key_production: 'production_value' }:
adapt('api.key') // => 'production_value'