Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@scaleleap/config

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scaleleap/config

Extendable configuration base class for 12 Factor application.

  • 2.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18
increased by38.46%
Maintainers
2
Weekly downloads
 
Created
Source

@scaleleap/config

FOSSA Status

Extendable configuration base class for 12 Factor application.

Install

npm i @scaleleap/config

Usage

// in ./src/config.ts

import { BaseConfig } from '@scaleleap/config'

class Config extends BaseConfig {
  public readonly TEST = this.get('TEST').asString()
}
// in ./src/app.ts

const config = new Config()

console.log(config.TEST)

Documentation

Parent Config class exposes a single protected method get, which maps to env-var#get method.

The constructor accepts a single argument, which is an dictionary of key-value strings, that represents the environment. The value defaults to process.env.

This is useful, when you'd like to override the values for testing:

const config = new Config({ NODE_ENV: '...' })

This module will automatically read .env file contents from $PWD/.env.

You can influence the discovery path of .env file via DOTENV_CONFIG_PATH environment variable.

E.g.

DOTENV_CONFIG_PATH=.env.prod node foo.js

Using a Prefix

Sometimes it is desirable to scope all of the environment variables. This can be achieved through a prefix.

Example:

class Config extends BaseConfig {
  protected readonly prefix = 'FOO_'

  public readonly TEST = this.get('TEST').asString()
}

Will then look for environment variable named FOO_TEST instead of just TEST.

Note: The underscore _ character is not included by default and must be part of your prefix.

Be careful when working with built-in variables, as they would not be discovered, because of the prefix.

Variable Expansion

It is possible to expand variables:

FOO=bar
BAZ=${FOO}-baz

When the BAZ value is expanded, the result will be:

FOO=bar
BAZ=bar-baz

Built-in Variables

There are some universal variables that are included into the config.

For now, there is just one.

NODE_ENV

Valid values are:

  • development
  • test
  • production

Example:

NODE_ENV=production node foo.js

Also comes with convenience boolean assertion getters:

  • isDevelopment
  • isTest
  • isProduction

Example:

const config = new Config()

if (config.isDevelopment) {
  // do something destructive
}

Guidelines

  • Property names should match the environment variable name, including case. E.g. NODE_ENV.
  • Properties should be always marked as readonly.
  • Use strict methods where possible, e.g. use asBoolStrict not asBool.

License

MIT © Scale Leap

FOSSA Status

Keywords

FAQs

Package last updated on 17 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc