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

@miroculus/config

v1.1.7

Published

Configure your node apps in a clear, easy and type safe way. Just define the configuration values with the types you want to have, and set them using environment variables or setting them on an `.env` file.

Downloads

31

Readme

@miroculus/config

Configure your node apps in a clear, easy and type safe way. Just define the configuration values with the types you want to have, and set them using environment variables or setting them on an .env file.

Getting Started

Install it:

npm install @miroculus/config

Somewhere on your project, create a file where you define and load your configuration values, for example on src/config.js:

const loadConfig = require('@miroculus/config')

module.exports = loadConfig({
  PORT: { type: 'number', default: 3000 },
  TLS_ENABLED: 'boolean',
  MONGODB_URI: { type: 'string', default: 'mongodb://localhost/database' },
  PASSWORD_SALT: { type: 'string', required: true },
  AUTH: { type: 'json', default: () => ({ user: 'admin', password: 'admin' }) }
})

Then, you can set you configs using environment variables, or creating a file on the root of you project named .env (Thanks dotenv):

PORT=8080
TLS_ENABLED=true
MONGODB_URI=mongodb://localhost/myCustomName
PASSWORD_SALT=somethingreallylongplease
AUTH={ "user": "admin", "password": "some-complicated-password" }

Then, just use it:

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

config.PORT === 8080 // true
!!config.TLS_ENABLED // true

config.MONGODB_URI === 'mongodb://localhost/myCustomName' // true

config.AUTH.user === 'admin' // true
config.AUTH.password === 'some-complicated-password' // true

Config Schema Definition

For each configuration value you can define it's type, if it is required, or if it has a default value. This are all the possible options:

Schema.type

  • string
    • All string values will be parsed as is.
    • The value will be trimmed
      • e.g.: BAR= value foo => config.BAR === 'value foo'
  • number
    • Number values will be coerced to Number
      • e.g.: PORT=3000 => config.PORT === 3000
    • Only Integer values are allowed (If you set NUMBER=3.2 it will throw an error)
    • Negative numbers are allowed, e.g.: NUMBER=-32
    • Numbers must be "well written", e.g.:
      • 0
      • 00
      • 0123
      • 12300
      • -123
      • -0
  • boolean
    • Can be setted using the words true or false
    • Can have uppercase letters, e.g.: True or TRUE
    • When a boolean is not setted and is not required, its value will be undefined, unless it has a default value.
  • json
    • Json values will be parsed using JSON.parse
    • Examples:
      • VAL={"some": "json-value"}
      • VAL=["an", "array", "of", "strings"]
      • VAL={}
      • VAL=[]
  • array
    • Array values are a custom type for defining an array of strings.
    • Is the same as defining type: 'string' but the value will be splitted, trimmed, and empty values filtered using:
      • val.split(',').map((str) => str.trim()).filter((v) => !!v)
    • For exmple VAL=a, b, c will be parsed to config.VAL = ['a', 'b', 'c']

Schema.default

The default config is to set a value when something is not configured, it can take any value to set, or a function that will be executed and the result setted. e.g.:

  • PORT: { type: 'number', default: 3000 }
  • PORT: { type: 'number', default: () => randomNumberBetween(3000, 8000) }

Schema.required

You can set your value as required: true if you want to throw an error when a value is not setted, or its setted to an empty value.

Schema.validate

This props serves to add a custom validation for the given value. It can be a function that should return true or false, or a RegExp. e.g. both of the following validations check that the configured value is 1, 2 or 3:

module.exports = loadConfig({
  STRING_VALIDATE: { type: 'number', validate: (val, key, config) => [1, 2, 3].includes(val)},
  REGEXP_VALIDATE: { type: 'number', validate: /^(1|2|3)$/ }
})

Schema.enum

The enum property allows you to set a finite set of value that the given configuration can have. e.g.:

module.exports = loadConfig({
  LOG_LEVEL: { type: 'string', enum: ['debug', 'info', 'warn', 'error'] }
})

Test

npm run test
npm run test:watch # for development purposes

License

MIT