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

@confconf/confconf

v2.2.1

Published

A node.js configuration manager

Downloads

231

Readme

confconf

confconf is an opinionated library for loading and validating application configuration.

Features

  • Loading and merging: configuration is loaded from a number of configuration providers and merged
  • Multiple supported loaders: configuration can be loaded e.g. from environment variables, json files, .env files, AWS secrets manager
  • Nested structure: keys and values can be organized in a tree structure
  • Validation: configurations are validated against a JSON schema using ajv
  • Type safe: Written in TypeScript. Type definitions included.

Install

npm i --save @confconf/confconf

Usage

The library is used as follows:

  1. define a JSON schema for the configuration. Something like TypeBox or purify-ts Codecs are recommended for this.
  2. define where the configuration is loaded from using configuration providers. See list of providers here
  3. load and validate the configuration

With TypeBox

Use @confconf/confconf-typebox

With purify-ts

Use @confconf/confconf-purify

With plain JSON schema

import { confconf, envConfig } from "@confconf/confconf";

type Config = {
  port: number;
  db: {
    host: string;
    name: string;
  };
};

// Create the configuration loader
const configLoader = confconf<Config>({
  schema: {
    type: "object",
    properties: {
      port: { type: "number" },
      db: {
        type: "object",
        properties: {
          host: { type: "string" },
          name: { type: "string" },
        },
        required: ["host", "name"],
      },
    },
    required: ["port", "db"],
  },
  providers: [
    // Load from env variables
    envConfig({
      // Map the specifc env variables into a specific structure
      structure: {
        port: "PORT",
        db: {
          host: "DB_HOST",
          name: "DB_NAME",
        },
      },
    }),
  ],
});

// Load configuration and validate it against the schema
const config = await configLoader.loadAndValidate();

Default values

Default values can be defined either using the JSON schema or by using the static configuration provider.

With JSON schema:

import { confconf } from "@confconf/confconf";

// Define a schema using JSON schema
const configSchema = {
  type: "object",
  properties: {
    port: {
      type: "number",
      default: 3000,
    },
  },
  required: ["port"],
};

With static configuration provider:

import { confconf, staticConfig } from "@confconf/confconf";

// Define a schema using JSON schema
const configSchema = {
  type: "object",
  properties: {
    port: { type: "number" },
  },
  required: ["port"],
};

const configLoader = confconf({
  schema: configSchema,
  providers: [
    // Static config is loaded first, later defined providers can override its values
    staticConfig({
      port: 3000,
    }),
    // Other providers
  ],
});

Configuration loading and merging

The partial configurations are loaded from all the defined configuration providers when loadAndValidate is called. After the partial configurations have been loaded, they are merged into a single JS object. The order is such that later defined configuration providers can override the configuration from earlier providers.

Validation

After the configuration is loaded and merged, it is validated against the provided schema. This is done using ajv:

  • Any additional properties that have not been defined in the schema are removed
  • Values are coerced into their corresponding types whenever possible. This is because certain providers (such as environment variables) can only provide string values. See Coercion section for more details.
  • The returned configuration is frozen, so it can't be mutated. This can change by instantiating confconf with { freezeConfig: false }.

Coercion

See Ajv's type coercion rules for more details how the type coercion works.

Debugging

Confconf uses debug to provide debug logging. Debug logging can be enabled by setting DEBUG=confconf env variable.

DEBUG=confconf node app.js

Example of how the logs look like:

Example how debug logging looks like

Providers

confconf is bundled with the following providers:

  • envConfig : Load configuration from the environment variables
  • staticConfig : Provide configuration using static value
  • devOnlyConfig : Provide a static value configuration that is loaded only when NODE_ENV=development

In addition these are coming available soon: