@hackbg/conf
TypeScript icon, indicating that this package has built-in type declarations

3.1.6 • Public • Published

@hackbg/conf

Loads configuration from environment.

Parses the values of "empty string", false, no, and 0 as negatives.

Example usage with TypeScript:

import { Env, EnvConfig } from '@hackbg/conf'

export class MyConfig extends EnvConfig {

  /** This constructor must be copied into every subclass to support the passing of defaults.
    * This is dumb, TODO fix in next version. */
  constructor (
    env:      Env               = {},
    cwd:      string            = '',
    defaults: Partial<MyConfig> = {}
  ) {
    super(env, cwd)
    this.override(defaults)
  }

  /* If you provide no default, when the env var is not defined and error will be thrown */
  myStringField
    = this.getString('MY_STRING_FIELD')

  /* To provide a default value pass a function that returns the default */
  myNumberField
    = this.getNumber('MY_NUMBER_FIELD', () => 0)

  /* To provide a default value pass a function that returns the default */
  myBooleanField
    = this.getFlag('MY_BOOLEAN_FIELD', () =>
      this.getFlag('MY_FLAG',          () =>
      this.getFlag('MY_OTHER_FLAG',    () => {
        if (Math.random() > 0.5) {
          return true
        } else {
          return false
        }
      })))

}

const currentConfig = new MyConfig(process.env, process.cwd, /* {}: defaults */)

Made with #%&! @ Hack.bg

Readme

Keywords

none

Package Sidebar

Install

npm i @hackbg/conf

Weekly Downloads

1

Version

3.1.6

License

MIT

Unpacked Size

7.27 kB

Total Files

5

Last publish

Collaborators

  • exactlywhoyouthinkitis
  • denismaxim0v
  • aakamenov
  • atanas-krondev
  • imollov
  • mradkov