Documentation
    Preparing search index...

    Class Env<EnvValues>

    A wrapper over "process.env" with types information.

    const validate = Env.rules({
    PORT: Env.schema.number()
    })

    const validatedEnvVars = validate(process.env)

    const env = new EnvValues(validatedEnvVars)
    env.get('PORT') // type === number

    Type Parameters

    • EnvValues extends Record<string, any>
    Index

    Constructors

    Properties

    schema: {} = envSchema

    The schema builder for defining validation rules

    Methods

    • Create an instance of the env class by validating the environment variables. Also, the .env files are loaded from the appRoot

      Type Parameters

      • Schema extends { [key: string]: ValidateFn<unknown> }

      Parameters

      • appRoot: URL

        The application root directory URL

      • schema: Schema

        Validation schema for environment variables

      Returns Promise<Env<{ [K in string | number | symbol]: ReturnType<Schema[K]> }>>

      Promise resolving to an Env instance with validated values

    • Define an identifier for any environment value. The callback is invoked when the value match the identifier to modify its interpolation.

      Parameters

      • name: string

        The identifier name

      • callback: (value: string) => string | Promise<string>

        Callback function to process the identifier value

      Returns void

      use Env.defineIdentifier instead

    • Define an identifier for any environment value. The callback is invoked when the value match the identifier to modify its interpolation.

      Parameters

      • name: string

        The identifier name

      • callback: (value: string) => string | Promise<string>

        Callback function to process the identifier value

      Returns void

    • Define an identifier for any environment value, if it's not already defined. The callback is invoked when the value match the identifier to modify its interpolation.

      Parameters

      • name: string

        The identifier name

      • callback: (value: string) => string | Promise<string>

        Callback function to process the identifier value

      Returns void

    • Remove an identifier

      Parameters

      • name: string

        The identifier name to remove

      Returns void

    • Define the validation rules for validating environment variables. The return value is an instance of the env validator

      Type Parameters

      • T extends { [key: string]: ValidateFn<unknown> }

      Parameters

      • schema: T

        Validation schema object

      Returns EnvValidator<T>

      EnvValidator instance

    • Get the value of an environment variable by key. The values are looked up inside the validated environment and "process.env" is used as a fallback.

      The second param is the default value, which is returned when the environment variable does not exist.

      Env.get('PORT')

      // With default value
      Env.get('PORT', 3000)

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • key: K

        The environment variable key

      Returns EnvValues[K]

      The environment variable value or default

    • Get the value of an environment variable by key. The values are looked up inside the validated environment and "process.env" is used as a fallback.

      The second param is the default value, which is returned when the environment variable does not exist.

      Env.get('PORT')

      // With default value
      Env.get('PORT', 3000)

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • key: K

        The environment variable key

      • defaultValue: Exclude<EnvValues[K], undefined>

        Default value if key is not found

      Returns Exclude<EnvValues[K], undefined>

      The environment variable value or default

    • Get the value of an environment variable by key. The values are looked up inside the validated environment and "process.env" is used as a fallback.

      The second param is the default value, which is returned when the environment variable does not exist.

      Env.get('PORT')

      // With default value
      Env.get('PORT', 3000)

      Parameters

      • key: string

        The environment variable key

      Returns undefined | string

      The environment variable value or default

    • Get the value of an environment variable by key. The values are looked up inside the validated environment and "process.env" is used as a fallback.

      The second param is the default value, which is returned when the environment variable does not exist.

      Env.get('PORT')

      // With default value
      Env.get('PORT', 3000)

      Parameters

      • key: string

        The environment variable key

      • defaultValue: string

        Default value if key is not found

      Returns string

      The environment variable value or default

    • Update/set value of an environment variable.

      The value is not casted/validated using the validator, so make sure to set the correct data type.

      Env.set('PORT', 3000)

      Env.get('PORT') === 3000 // true
      process.env.PORT === '3000' // true

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • key: K

        The environment variable key

      • value: EnvValues[K]

        The value to set

      Returns void

    • Update/set value of an environment variable.

      The value is not casted/validated using the validator, so make sure to set the correct data type.

      Env.set('PORT', 3000)

      Env.get('PORT') === 3000 // true
      process.env.PORT === '3000' // true

      Parameters

      • key: string

        The environment variable key

      • value: string

        The value to set

      Returns void