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

centig

v1.1.3

Published

The Configuration Management library for your JavaScript application

Downloads

2,213

Readme

Centig

NPM version size Build Status codecov

The Configuration Management library for your JavaScript application

Introduction

The goal with Centig is to give the developer the opportunity to include all configuration in one single file with a logical hierarchical structure. The Centig configuration schema API aims to be very distinct with a simple overview of the context and the origin of each config value. Therefore, Centig does not believe in overriding and extending configurations. Each value can be configured very differently, depending on how important or dynamic the value can be. Centig has a validation-first approach (by default) where it should NOT be possible to launch the application without having all the values ​​present that were defined in the schema.

Install

npm install centig

Usage

Here is an example of a config file with some static and some configs coming from environment variables.

const { centig } = require('centig');

const config = centig({
  db: {
    host: 'localhost',
    port: 5050,
    name: 'admin',
  },
  api: {
    url: {
      type: String,
      env: 'API_URL',
      validate: value => new URL(value),
    },
    key: {
      type: Number,
      env: 'API_KEY',
      preprocess: value => Number(value),
    },
  },
  newFeature: {
    isSupported: {
      type: Boolean,
      env: 'NEW_FEATURE_SUPPORT',
      preprocess: value => Boolean(Number(value)),
    },
    regex: {
      type: RegExp,
      value: /\babc\b/,
    },
  },
  logLevel: {
    type: String,
    env: 'LOG_LEVEL',
    validate: value => {
      const logLevels = ['debug', 'trace', 'info', 'warn', 'error', 'fatal'];
      if (!logLevels.includes(value)) {
        throw Error(
          `The value - ${value} for logLevel must be one of ${logLevels}`,
        );
      }
    },
  },
});

module.exports = config;

Then we can in any other file import and use the config module as seen below.

const config = require('./config');

const apiUrl = config.get('api').url;
console.log(`This is the api url: ${apiUrl}`);

const isNewFeatureSupported = config.get('newFeature.isSupported');
console.log(`This is the newFeature isSupported: ${isNewFeatureSupported}`);

API

const config = centig(schema)

The configuration schema passed into the Centig module can be configured very differently depending on the needs. Eather we use the shortcut alternative or we pass a Centig specific configuration object. The following properties can be used:

| Properties | Type | Required | Description | | ---------- | --------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------- | | type | constructor | ✘ | Simple type validation. Supported are: String, Number, Boolean, Array, Object, RegExp. | | env | string | ✓ (if no value) | This value is grabbed from process.env. | | value | any | ✓ (if no env) | If the config value is not from process.env we can use this property. But maybe the shortcut alternative fits better. | | preprocess | (value: any) => value | ✘ | A custom function if we wanna process the value. Useful if we wanna perform any conversion before the validation. | | validate | (value: any) => void | ✘ | A custom function if we wanna perform extra validation. Remember to throw Error if the value is not validating. | | optional | boolean | ✘ | Defaults to false. We can flip this to true if we don't wanna require a value to be present. No validation will be made | | defaultValue | any | ✘ | A value that will be used if either 'env' or 'value' is empty/undefined |

Example:

const { centig } = require('centig');

const config = centig({
  api: {
    url: {
      type: String,
      env: 'API_URL', // Trying to grab process.env.API_URL
      validate: value => new URL(value),
    },
    key: {
      type: Number,
      env: 'API_KEY', // Trying to grab process.env.API_KEY
      preprocess: value => Number(value),
    },
  },
});

To use the shortcut method we simply define key-value pairs, where the value could be whatever we want, such as a number or a string. This may be a good choice if no validation or processing is needed. See the example below.

const { centig } = require('centig');

const config = centig({
  db: {
    host: 'localhost',
    port: 5050,
    name: 'admin',
  },
  api: {
    url: 'https://api.url.com',
    key: 'api-key',
  },
});

config.get(key)

Returns the value by key name.

config.get('api').url;
// or
config.get('api.url');

config.all()

Returns all configurations

Typescript

Centig comes with types and to take advantage of this we can define an interface for our schema and pass it when we call centig<Interface>(schema).

import centig from 'centig';

interface Schema {
  db: {
    host: string;
    port: number;
    name: string;
  };
  api: {
    url: string;
    key: number;
  };
}

const config = centig<Schema>({
  db: {
    host: 'localhost',
    port: 5050,
    name: 'admin',
  },
  api: {
    url: {
      type: String,
      env: 'API_URL', // Trying to grab process.env.API_URL
      validate: value => new URL(value),
    },
    key: {
      type: Number,
      env: 'API_KEY', // Trying to grab process.env.API_KEY
      preprocess: value => Number(value),
    },
  },
});

config.get('api').url; // is now fully typed

Calling .get() with a dotted path is not yet supported.

Inspiration