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

Constructors

  • Parameters

    • store: Dictionary<string> = ...

    Returns Env

Methods

  • Gets an array of all definitely assigned key-value pairs from the underlying envar store.

    Returns KeyValue<string>[]

  • 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.

    • def: boolean = false

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

    Returns boolean

  • 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 extends Record<string, unknown>

    Parameters

    • key: string

      The name of the envar.

    • obj: T

      The object providing the keys to test with.

    • Optionaltransform: ((k: string) => string)

      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
        • (k): string
        • Parameters

          • k: string

          Returns string

    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 extends Record<string, unknown>

    Parameters

    • key: string

      The name of the envar.

    • obj: T

      The object providing the keys to test with.

    • def: string

      A default value.

    • Optionaltransform: ((k: string) => string)

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

        • (k): string
        • Parameters

          • k: string

          Returns string

    Returns Extract<keyof T, 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[]

  • Gets a number value for a given key. Returns the default value if no value was found.

    Parameters

    • key: string

      The name of the envar.

    • def: number

      A default number, which itself defaults to undefined if not otherwise supplied.

    Returns number

  • Parameters

    • key: string
    • Optionaldef: undefined

    Returns Optional<number>

  • 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

  • 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.

    Parameters

    • key: string

      The name of the envar.

    • values: string[]

      The finite set of expected values.

    • def: string

      A default value.

    Returns string

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

  • 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

  • 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

  • Sets a number 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<number>

      The value to set.

    Returns 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

  • Unsets a value for a given key.

    Parameters

    • key: string

      The name of the envar.

    Returns void