Options
All
  • Public
  • Public/Protected
  • All
Menu

An injectable abstraction on top of process.env with various convenience functions for accessing environment variables of different anticipated shapes.

Hierarchy

  • Env

Index

Constructors

constructor

  • new Env(store?: Dictionary<string>): Env
  • Parameters

    • Default value store: Dictionary<string> = (process && process.env) || {}

    Returns Env

Methods

entries

  • entries(): Array<KeyValue<string>>
  • Gets an array of all definitely assigned key-value pairs from the underlying envar store.

    Returns Array<KeyValue<string>>

getBoolean

  • getBoolean(key: string, def?: boolean): boolean
  • Gets a boolean value for a given key. Returns the default value if no value was found.

    Parameters

    • key: string

      The name of the envar.

    • Default value def: boolean = false

      A default boolean, which itself defaults to false if not otherwise supplied.

    Returns boolean

getKeyOf

  • getKeyOf<T>(key: string, obj: T, transform?: undefined | function): Optional<Extract<keyof T, string>>
  • getKeyOf<T>(key: string, obj: T, def: string, transform?: undefined | function): Extract<keyof T, string>
  • Gets a string value from a finite set of expected values derived from the keys of a given object of type T, matched case-insensitively. An optional transform may be provided that will preprocess both the found value and any provided default before testing for membership in the target object's key set.

    Type parameters

    • T: any

    Parameters

    • key: string

      The name of the envar.

    • obj: T

      The object providing the keys to test with.

    • Optional transform: undefined | function

      A transform function applied to both the default and value before testing that either is a key of T.

      enum Mode { TEST = 'test', DEMO = 'demo' }
      env.setString('MY_ENVAR', Mode.DEMO);
      const check = env.getString('MY_ENVAR');
      // check -> 'demo'
      // typeof check -> string
      const value = env.getKeyOf('MY_ENVAR', Mode, v => v.toUpperCase());
      // value -> 'DEMO'
      // typeof value -> 'TEST' | 'DEMO' (i.e. Extract<keyof typeof Mode, string>)
      const enumValue = Mode[value];
      // enumValue -> 'demo'
      // typeof enumValue -> Mode

    Returns Optional<Extract<keyof T, string>>

  • Gets a string value from a finite set of expected values derived from the keys of a given object of type T, matched case-insensitively, using a default if not found. An optional transform may be provided that will preprocess both the found value and any provided default before testing for membership in the target object's key set.

    Type parameters

    • T: any

    Parameters

    • key: string

      The name of the envar.

    • obj: T

      The object providing the keys to test with.

    • def: string

      A default value.

    • Optional transform: undefined | function

      A transform function applied to both the default and value before testing that either is a key of T.

    Returns Extract<keyof T, string>

getList

  • getList(key: string): Optional<string[]>
  • getList(key: string, def: string[]): string[]
  • Gets a list of string values for a given key by splitting the raw value on , chars.

    Parameters

    • key: string

      The name of the envar.

    Returns Optional<string[]>

  • Gets a list of string values for a given key by splitting the raw value on , chars.

    Parameters

    • key: string

      The name of the envar.

    • def: string[]

      A default list of values.

    Returns string[]

getString

  • getString(key: string): Optional<string>
  • getString(key: string, def: string): string
  • Gets a string value for a given key.

    Parameters

    • key: string

      The name of the envar.

    Returns Optional<string>

  • Gets a string value for a given key.

    Parameters

    • key: string

      The name of the envar.

    • def: string

      A default value.

    Returns string

getStringIn

  • getStringIn(key: string, values: string[]): Optional<string>
  • getStringIn(key: string, values: string[], def: string): string
  • Gets a string value from a finite set of expected values, matched case-insensitively.

    Parameters

    • key: string

      The name of the envar.

    • values: string[]

      The finite set of expected values.

    Returns Optional<string>

  • Gets a string value from a finite set of expected values, matched case-insensitively, using a default if not found.

    throws

    InvalidDefaultEnvValueError If the provided default value is not a member of the expected set.

    Parameters

    • key: string

      The name of the envar.

    • values: string[]

      The finite set of expected values.

    • def: string

      A default value.

    Returns string

setBoolean

  • setBoolean(key: string, value: Nullable<boolean>): void
  • Sets a boolean value for a given key, or removes the current value when no value is given.

    Parameters

    • key: string

      The name of the envar.

    • value: Nullable<boolean>

      The value to set.

    Returns void

setList

  • setList(key: string, values: Nullable<string[]>): void
  • Sets a string value from a list for a given key by joining values with a , into a raw string value, or removes the current value when no value is given.

    Parameters

    • key: string

      The name of the envar.

    • values: Nullable<string[]>

      The values to set.

    Returns void

setString

  • setString(key: string, value: Nullable<string>): void
  • Sets a string value for a given key, or removes the current value when no value is given.

    Parameters

    • key: string

      The name of the envar.

    • value: Nullable<string>

      The value to set.

    Returns void

unset

  • unset(key: string): void
  • Unsets a value for a given key.

    Parameters

    • key: string

      The name of the envar.

    Returns void