Skip to main content

@rtorcato/js-common / env

env

Type Aliases

RootApiEnvSchema

RootApiEnvSchema = z.infer<typeof RootApiEnvSchema>

Defined in: env/index.ts:89

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:89

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:11

Retrieves the value of an environment variable by its key.

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:28

Determines if the current environment is set to development.

Returns

boolean

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


isProd()

isProd(): boolean

Defined in: env/index.ts:37

Determines if the current environment is set to production.

Returns

boolean

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


isTest()

isTest(): boolean

Defined in: env/index.ts:46

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:55

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:67

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.