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

@strbjun/nconfig

v0.2.0

Published

A configuration library that supports environment variables, JSON files, and .env files

Downloads

3

Readme

nConfig

A basic configuration library that can retrieve options from either an environment variable, a .env file, or a .json file.

How to use

Import the Config class in order to get started. Pass along your configuration setup.

Configuration Object

The configuration object to pass onto new NConfig(ConfigurationObject, options) is:

{
  [config key1]: {
    value: value for setting
    type: This would be the variable type using the ValueTypes enum.
    ttl: Time to expire from time last retrieved
    keys: [
      {
        key: "key to use within config parameter",
        type: use ConfigFileTypes enum to declare if json or env
        file: "Set the file whether a .env file or json file, if .env you do not need a file as if no file is set it'll only check the environment variables but it will always check the terminal environment variables first"
      }... and can add more, though it will process in order for the first match
    ]
  },
  [config key2]: value for setting also allowed here
}

The options object isn't required though if you decide to customize this you can pass:

{
  defaultFiles?: {
    [ConfigFileTypes type]: "default filename to use if not set above"
  },
  defaultKeyType?: set to the default ConfigFileTypes for if not set above
  fileTTL?: the amount of time to cache any file. Default is 5 minutes, in ms.
  defaultTTL?: the amount of time to cache any configuration value, if 0 it will keep it saved. in ms.
  defaultType?: the default variable type for type checking of values. Default is none. Uses ValueTypes enum.
}

That is all that is needed. Otherwise just declare the config in a global position so it doesn't keep retrieving the data every time, and then you can use the following functions:

config.get(key)

  • This will get the config value for the current key config.set(key, value)
  • You can replace the value no matter what it is to the given value, but it will not rewrite it to a file config.forEach((value, key) => { ... })
  • Will let you run a forEach on every config value config.json()
  • Can get a json formatted return for every single config option

Using Spaces support

There is an addition of namespacing or spaces for separating configuration into different categories or sections as well.

You just have to include the ConfigSpace object.

const space = new ConfigSpace(SpaceOptions, ConfigOptions)

  • The config options work similar to the above and will pass it on to every config by default
  • SpaceOptions is for setting up various different config spaces. It is pretty easy to understand its approach as it works the following way:
{
  [space key]: ConfigurationObject similar to how it is done for the NConfig class.
}

After, the following commands will work:

space.get(key, configKey)

  • It will get the value from the config key in the space in key. space.set(key, configKey, value)
  • Will allow for running the set command within the space 'key'. space.json()
  • Will get a json object for every configuration object in the sapce. space.forEach((config, key) => ...)
  • Will run a foreach loop on every configuration space and give the key for it space.forEachValues((config, configKey, key) => ...)
  • Will run a foreach loop on every value in every config value

To do

  • add other configuration file support including YAML