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

urbanjs-config

v0.2.0

Published

Simple utility functions for configuration management

Downloads

8

Readme

urbanjs-config

Build Status

Install

yarn add urbanjs-config

Usage

For advanced use cases see examples

Example config.js file:

'use strict';

const es6TemplateResolver = require('es6-template-strings');
const config = require('urbanjs-config');

const defaults = {
  port: 3000,
  serverOrigin: 'http://localhost:${port}',
  useSecureCookies: false
};

module.exports =
  config.resolveReferences(
    config.applyEnvironmentVariables(defaults, 'MY_APP'),
    es6TemplateResolver
  );

Motivation

urbanjs-config does not want to implement all features of configuration management. This package tries to add the bare minimum utility functions to set up the configuration of applications with ease. Combine this package with dotenv and you're good to go.

Other features (if necessary):

  • custom config storage - config should be a plain object literal and json pointer or lodash can be used to retrieve data
  • complex coercing - config should be a plain object literal with number, string and boolean values
  • validation - use json schema for this purpose, see example
  • support of command line arguments - use environment variables based on the twelve-factor app methodology
  • support of config files - use environment variables based on the twelve-factor app methodology
  • event listeners - why would config change at runtime?

API

.applyEnvironmentVariables(config, [prefix], [store])

Updates the given config with the environment variables. If prefix is given, environment variables must start with <PREFIX>__.

Defaults:

  • prefix: empty string
  • store: process.env
import { applyEnvironmentVariables } from 'urbanjs-config';

type Configuration = {
  port: number;
  serverOrigin: string;
  useSecureCookies: boolean;
};

const defaults: Configuration = {
  port: 3000,
  serverOrigin: 'http://localhost:3000',
  useSecureCookies: false
};

export const config = applyEnvironmentVariables<Configuration>(
  defaults,
  'MY_PREFIX'
);

Note: the example above will update the configuration if either MY_PREFIX__PORT, MY_PREFIX__SERVER_ORIGIN or MY_PREFIX__USE_SECURE_COOKIES are defined within process.env.

Note: Environment variables are always strings but they are transformed based on the original type of the config value. Only boolean, string and number values are supported.

Note: Nested objects are also supported.

Note: config key is transformed based on the .toConstantCase() api

.resolveReferences(config, resolver)

Resolves references within the config value. A reference refers to another config value based on its key.

A resolver method can be implemented based on:

export type Resolver = (value: string, config: object) => string;

import es6TemplateResolver = require('es6-template-strings');
import { resolveReferences } from 'urbanjs-config';

type Configuration = {
  port: number;
  serverOrigin: string;
};

const defaults: Configuration = {
  port: 3000,
  serverOrigin: 'http://localhost:${port}'
};

export const config = resolveReferences(
  defaults,
  es6TemplateResolver
);