Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EnvironmentManager

Manages environmental variables needed by the application. It should be noted that while this class is not exported, a singleton object is exported via Environment.

Hierarchy

  • EnvironmentManager

Index

Properties

Private _registered

_registered: object

Internal object that stores default environment values as a key in the underlying object. Environmental variables must be registered in this object before retrieval.

Type declaration

Methods

Private _validateRegistered

  • Validates that an environment key is registered and optionally is a certain type.

    Parameters

    • key: string

      The key to validate internally.

    • Optional typeMatch: ValidDefaultsStrings

      A string to do a typeof compare.

    Returns void

getNumber

  • getNumber(key: string): number
  • Gets the numeric value of an environmental variable.

    Example

    Environment.register("SOME_KEY", 5);
    
    // Logs the numeric value of process.env["SOME_KEY"] if it is set and is a number.
    // otherwise the number 5 will be logged.
    console.log(Environment.getNumber("SOME_KEY"));
    throws

    NotRegisteredError when key was not registered using register.

    throws

    TypeMismatchError when key was not registered as a number.

    see

    getValue

    Parameters

    • key: string

      The environment key to retrieve.

    Returns number

    The numeric value of the environment variable.

getString

  • getString(key: string): string
  • Gets the string value of an environmental variable.

    Example

    Environment.register("SOME_KEY", "THIS IS A TEST");
    
    // Logs the numeric value of process.env["SOME_KEY"] if it is set.
    // otherwise "THIS IS A TEST" will be logged.
    console.log(Environment.getNumber("SOME_KEY"));
    throws

    NotRegisteredError when key was not registered using register.

    throws

    TypeMismatchError when key was not registered as a string.

    see

    getValue

    Parameters

    • key: string

      The environment key to retrieve.

    Returns string

    The string value of the environment variable.

getValue

  • Gets the string value of an environment variable.

    When called, this method will search for key in the environment. If a value was found, it will be returned from process.env[key]. When no value was found for key, the value that was specified in the second paramter of register will be returned.

    An environment variable must be first registered through the register method. Any attempts to access a non-registered variable will lead to a NotRegisteredError.

    Example

    Environment.register("SOME_KEY", "TEST");
    
    // Logs the value of process.env["SOME_KEY"] if it exists
    // Logs "TEST" otherwise.
    console.log(Environment.getValue("SOME_KEY"));
    throws

    NotRegisteredError when key was not registered using register.

    Parameters

    • key: string

      The environment key to retrieve.

    • Default value validationFunction: ValidationFunction = this._validateRegistered.bind(this)

      The validation scheme to use.

    Returns any

    The string value of the environment variable.

register

  • Registers an environmental variable to the application.

    Prior to any getValue method calls, an environmental must be registered to the application with a default value. The default value will ensure that type checking happens on the typed methods and that a valid value is always returned by those methods in the absence of the environment key.

    Example

    Environment
      .register("A", 5)
      .register("B", "Hello")
      .register("C", "World")
    
    // The environmental variables process.env.A, process.env.B, process.env.C
    // are now registered in the application with defaults of 5, "Hello" and "World"
    // respectively.
    throws

    DuplicateVarError when key was already registered in the application.

    Parameters

    • key: string

      The key to register.

    • defaultValue: ValidDefaults

      The default value that is returned when not present in the environment.

    Returns EnvironmentManager

    A reference to the class for method chaining.

Generated using TypeDoc