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

confeta

v1.2.6

Published

Confeta core package

Downloads

22

Readme

Confeta

Delicious configuration library

Usage:

import { Confeta, types } from 'confeta'
import ConfetaEnv from 'confeta-env'
import ConfetaArgv from 'confeta-argv'
import ConfetaFile from 'confeta-file'
import changeCase from 'change-case'

let config = Confeta({
  mongoUrl: {
    type: types.string,
    description: 'Mongo Url',
    default: 'mongo://localhost:27017'
  },
  timeout: {
    type: types.integer
  },
  host: {
    type: {  // nested object
      domain: {
        type: types.string,
        description: 'Domain name',
        required: true
      },
      port: {
        type: types.integer,
        description: 'Port',
        required: true
      }
    }
  }
})
.addSource(ConfetaEnv({prefix: 'MYAPP__', separator: '__'}), changeCase.constantCase)
.addSource(ConfetaArgv())
.addSource(ConfetaFile({parseFn: JSON.parse, path: 'config.json'}))
.build()

Full example can be seen in ./example

Why? We already have nconf...

I got tired of nconf inability to handle different naming conventions between different sources (e.g. CONSTANT_CASE in process.env vs camelCase in json file). So I created a new, more customisable library where you can specify schema for your configuration and map schema names to different sources. With schema you can specify exactly what type do you expect and avoid annoying situation when you have to parse value manually after getting it from config. Also some simple validation I think is nice.

Separate packages?

Yes, core confeta package has no sources in it. You have to either install a separate package with source you need for your app or implement your own one (which is very simple btw)

Officially supported sources:

  • confeta-env
  • confeta-args
  • confeta-file (Customisable to read from any format you want, given you provide parse function, reads JSON by default)

Api

Confeta(schema, options)

options:

  • strict - (boolean) when true throw errors if types from config source don't match specified in config, when false just tries to parse them (default: false)

.addSource(source, mapKeyFn)

  • source - object that used to fetch settings from specific source
  • mapKeyFn - function that is used to map schema key to source key (e.g. if you want to turn pascalCase name of schema into CONSTANT_CASE name from environment)

.build()

Returns object containting all settings fetched from sources

Custom sources

It's very simple, all you have to do is provide object which exposes .get(segments) function. It will be called every time Confeta tries to fetch value and segments will be an array of strings which represents a path to value in a schema tree.

License

MIT