Skip to main content

functions/env

Functions

getEnv()

function getEnv<A, B, C>(envSchema): ReturnType<typeof envSchema.parse>

Helper function to get environment variables from a ".env" file that is located next to the project's "package.json" file (i.e. at the root of the project repository).

You must provide the Zod schema of the ".env" file. The contents of the file will be parsed and validated with Zod. For example:

const envSchema = z.object({
DOMAIN: z.string().min(1).default("localhost"),
});

export const env = getEnv(envSchema);

This function contains logic to convert empty strings to undefined so that Zod default values can work correctly.

Under the hood, this uses the dotenv library to get the environment variables from the ".env" file.

Type Parameters

Type Parameter
A extends ZodRawShape
B extends UnknownKeysParam
C extends ZodTypeAny

Parameters

ParameterType
envSchemaZodObject<A, B, C, objectOutputType<A, C, B>, objectInputType<A, C, B>>

Returns

ReturnType<typeof envSchema.parse>

Defined in

functions/env.ts:29