Skip to main content

@rtorcato/js-common / env

env

Type Aliases​

RootApiEnvSchema​

RootApiEnvSchema = z.infer<typeof RootApiEnvSchema>

Defined in: env/index.ts:113

Schema for validating the root environment variables. It includes NODE_ENV, LOG_LEVEL, and PORT with their respective types and defaults.

Variables​

RootApiEnvSchema​

const RootApiEnvSchema: ZodObject<{ NODE_ENV: ZodDefault<ZodEnum<{ development: "development"; production: "production"; test: "test"; }>>; LOG_LEVEL: ZodDefault<ZodEnum<{ debug: "debug"; info: "info"; warn: "warn"; error: "error"; }>>; PORT: ZodDefault<ZodCoercedNumber<unknown>>; }, $strip>

Defined in: env/index.ts:113

Schema for validating the root environment variables. It includes NODE_ENV, LOG_LEVEL, and PORT with their respective types and defaults.

Functions​

getENV()​

getENV(key, defaultValue?): string

Defined in: env/index.ts:19

Retrieves the value of an environment variable by its key.

Example​

process.env.API_URL = 'https://api.example.com'
getENV('API_URL') // 'https://api.example.com'
getENV('MISSING', 'fallback') // 'fallback'
getENV('MISSING') // throws Error: Undefined ENV variable - MISSING

Parameters​

key​

string

The name of the environment variable to retrieve.

defaultValue?​

string

An optional default value to return if the environment variable is not defined.

Returns​

string

The value of the environment variable if it exists, otherwise the default value if provided.

Throws​

If the environment variable is undefined and no default value is provided.


isDev()​

isDev(): boolean

Defined in: env/index.ts:44

Determines if the current environment is set to development.

Example​

process.env.NODE_ENV = 'development'
isDev() // true
process.env.NODE_ENV = 'production'
isDev() // false

Returns​

boolean

true if process.env.NODE_ENV is 'development', otherwise false.


isProd()​

isProd(): boolean

Defined in: env/index.ts:61

Determines if the current environment is set to production.

Example​

process.env.NODE_ENV = 'production'
isProd() // true
process.env.NODE_ENV = 'test'
isProd() // false

Returns​

boolean

true if process.env.NODE_ENV is 'production', otherwise false.


isTest()​

isTest(): boolean

Defined in: env/index.ts:70

Determines if the current environment is set to test.

Returns​

boolean

true if process.env.NODE_ENV is 'test', otherwise false.


getNodeEnv()​

getNodeEnv(): string

Defined in: env/index.ts:79

Returns the current NODE_ENV value, or 'development' if not set.

Returns​

string


checkEnv()​

checkEnv<T>(EnvSchema, buildEnv?): $InferObjectOutput<T, { }>

Defined in: env/index.ts:91

Validates the environment variables against a Zod schema. Throws an error if any required variables are missing or invalid.

Type Parameters​

T​

T extends Readonly<{[k: string]: $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>; }>

Parameters​

EnvSchema​

ZodObject<T>

The Zod schema to validate against.

buildEnv?​

Record<string, string | undefined> = process.env

The environment variables to validate, defaults to process.env.

Returns​

$InferObjectOutput<T, { }>

Throws​

If validation fails, an error with a message listing missing variables is thrown.