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

config-decorators

v0.2.0

Published

Decorator lib for ENV-based configuration

Downloads

1,405

Readme

Config Decorators

This is a configuration helper library that enables the efficient management of default dev settings and prod settings from environment variables. Useful for separating dev and prod environments, dockerization, etc.

Install

npm i config-decorators -S

Usage

  • Create a class with the config props
    • Use inline settings for default values
    • Use the ENV or CLI decorator to define overrides if the ENV variable / CLI argument is available
  • Instantiate the class with the loadConfig call
    • You CAN instantiate the same class more than once
    • You CAN create several classes with various decorators

Aaand... Done!

Config class

import { loadConfig, ENV } from './config-decorators';

export class Config {
	@ENV('MONGO_URL')
	mongoUrl = 'mongodb://localhost/test2';

	// Use it with a transform function
	@ENV('SERVER_PORT', parseInt)
	port = 8080;

	// Or use the 'number' shortcut
	@ENV('SERVER_PORT_2', 'number')
	@CLI('port2')
	port2 = 8081;

	// Rules for boolean values:
	// '0', '', 'false' will be parsed as `false`.
	// Everything else will be `true`.
	@ENV('ENABLE_AUTH', 'boolean')
	@CLI('enable-auth')
	enableAuth = true;
}

export const config = loadConfig(Config);

Import

import * as mongoose from 'mongoose';
import { config } from './config';

mongoose.connect(config.mongoUrl);

CLI variables

Note that CLI has higher priority than ENV. test.js:

class CliTest1 {
	@CLI('PATH')
	@ENV('PATH')
	path: string;
}
const config = loadConfig(CliTest1);
console.log(config.path);

When calling node test.js --PATH test will print test, not the PATH environmental variable.

Required variables

The ENV/CLI variable can be required; loadConfig will throw an error if there's no such ENV variable and no CLI argument. Note that NOT the class decorators but the loadConfig throws the error.

export class Config {
	@ENV('MONGO_URL', true)
	mongoUrl: string;

	@ENV('SERVER_PORT', parseInt, true)
	port: number;
}

// Here comes the error
export const config = loadConfig(Config);

How does it work?

The main stuff is at loadConfig:

  • First, it instantiates your class
  • Then it checks each prop having ENV or CLI decorators
    • If there's no ENV/CLI variable with the given name then steps to next prop
    • If there's a given transformator then the value is transformed
    • Overrides the value of the prop

Tests

  • Clone the repo
  • npm i
  • npm run build -- You should have tsc (TypeScript) in your PATH
  • npm run test

Tested on Node.js with TypeScript (2.1.4+) classes, ES6 build.

Further Dev Plans -- NOT READY YET

Create an issue if you need something.

Validation

	@ENV('SERVER_PORT', parseInt)
	@Validate(value => value > 1000)
	port = 8080;

Print help

if (config.help) {
	printHelp(ConfigClass1, ConfigClass2); // <- here
	return;
}

Wider interface to minimist, e.g. aliases