UNPKG

@darkobits/env

Version:

Functional environment variable getter/parser.

38 lines (37 loc) 1.64 kB
type Primitive = string | number | boolean | null | undefined; type PlainObject = Record<any, Primitive>; type EnvironmentVariable = Primitive | PlainObject | Array<Primitive | PlainObject>; export interface Env { /** * Accepts an environment variable name. Returns the parsed value of the * variable, if it is set. * * Additionally accepts an optional `strict` argument that, when `true`, will * cause env to throw if the provided variable does not exist in process.env. */ <T extends EnvironmentVariable = string>(variableName: string, strict: true): T; <T extends EnvironmentVariable = string>(variableName: string, strict?: boolean): T | undefined; /** * Returns `true` if the provided variable name is set and `false` otherwise. * * Shorthand for `Object.keys(process.env).includes(variableName)`. */ has(variableName: string): boolean; /** * Accepts a variable name and a value and returns `true` if the variable name * is strictly equal to the provided value. * * Additionally accepts an optional `strict` argument that, when `true`, will * cause env.eq to throw if the provided variable does not exist in * process.env. * * Note: When comparing against non-primitives (objects, arrays), env.eq will * serialize the provided `value` and compare it against the serialized (re: * string) form of the environment variable. * * Shorthand for `env('SOME_VAR') === testValue`. */ eq(variableName: string, value: any, strict?: boolean): boolean; } declare const env: Env; export default env;