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

@wymp/config-node

v1.1.0

Published

A small, minimal-dependency library for elegantly handling config in nodejs environments

Downloads

1

Readme

Config (for NodeJS Environments)

This is a very small library that provides rich functionality for dealing with application configuration in a NodeJS environment. It provides a single export config, which is a function (Weenie-compatible) that takes config from a number of optional sources and merges it all into a single, type-checked config object.

NOTE: See also config-simple (pkg) for an alternative simpler config system.

Primary features

  • Sources include any or all of the following:
    • environment variables
    • defaults JSON file (version-controlled)
    • "locals" JSON file (non-version-controlled),
    • docker secrets directory (e.g., /secrets)
  • Sources are combined in the following order: defaults file < locals file < env < secrets dir
  • Variable names can contain numeric suffixes (__001, __1, etc.) for "secrets versioning" (necessary for some orchestrators, including docker swarm). Variables are sorted in ascending order with later "versions" overriding earlier versions.
  • Variable values are automatically coerced into what they look like (number, boolean, null, undefined).
  • Values can contain explicit casts to guarantee type (e.g., <string>3 results in the string "3"
  • Pass a runtype type-checker in for automatic runtime type checking and fast-fail on errors. (Pass a dummy type-checker in to bypass.)
  • Supports arbitrary namespacing of env vars (APP_, APP_CONFIG_, MYCNF_, whatever)
  • Optional changing of delimiter (defaults to _, very rarely needs to be changed).

Examples

Common Use: Store default variables in a version-controlled config.json file and selectively override with environment variables. This keeps dev configs together with the repo and also serves as documentation about possible config values. We'll use a runtype to validate the type.

import * as rt from "runtypes";
import { config } from "@wymp/config-node";

// Create our config validator
const validator = rt.Record({
  api: rt.Record({
    url: rt.String,
    key: rt.String,
    secret: rt.String,
  }),
  logLevel: rt.Union(
    rt.Literal("debug"),
    rt.Literal("info"),
    rt.Literal("notice"),
    rt.Literal("warning"),
    rt.Literal("error"),
    rt.Literal("critical"),
    rt.Literal("emergency"),
  ),
  someOptionalTimeout: rt.Optional(rt.Number),
});

// Use the config function to create a dependency container with a `config` object
const deps = config(`APP_`, { env: process.env, defaultsFile: "config.json" }, validator);

// We now KNOW that these values exist as expected
console.log(
  deps.config.api.url,
  deps.config.logLevel,
  deps.config.someOptionalTimeout === undefined
    ? "(unknown)"
    : deps.config.someOptionalTimeout,
);

Weenie Usage: This library was originally intended for use (and works well) with the Weenie dependency framework. Use it to simply kick off a Weenie run:

import * as Weenie from "@wymp/weenie-framework";
import { validator } from "./Types";

// NOTE: this library is included and re-exported by Weenie, so we can use it directly from there
const deps = Weenie.Weenie(Weenie.config(
  `APP_`,
  {
    env: process.env,
    defaultsFile: "config.json",
    localsFile: "config.local.json",
    secretsDir: "/secrets",
  },
  validator
))
  .and(Weenie.logger)
  .and(Weenie.mysql)
  .and(Weenie.amqp)
  .and(Weenie.cron)
  .done(d => d);

// Now we have a lot of nice, well-typed dependencies, including our config
deps.logger.notice(deps.config.logLevel);
deps.logger.notice(deps.config.api.url);

Not smart but poissible: Just get config from environment with no type checking (use at your own risk!!)

import { config } from "@wymp/config-node";

const deps = config(`APP_`, { env: process.env }, { check: (c: any) => c });

// Who knows if this really exists! But you can do this if you'd like
console.log(deps.config.api.url, deps.config.logLevel);