UNPKG

@qalisa/vike-envz

Version:

A type-safe environment variable management system for Vike applications that enables validation and transformation of environment variables using Zod schemas.

33 lines (32 loc) 1.42 kB
/** * @module lib/index * @description Core functionality for retrieving and validating environment variables from different sources. * This module provides the internal implementation for the public API. */ import { type z } from "zod"; import { type EnvZ } from "../types/public.js"; export declare const dotenvValuesAtKeyName: "z.serverOnly"; /** * Retrieves, validates and transforms server-side environment variables based on the provided configuration. * * @template T - Type extending EnvZ (a record of environment variable entries with their validation schemas) * @param {Record<string, any>} importMetaEnv - expects "import.meta.env" here * @param {T} envSchema - Configuration object defining environment variables and their validation schemas * @returns {Object} An object containing all validated and transformed environment variables * @throws {Error} When environment variables fail validation against their schemas * * @example * ```ts * import { getEnvZ } from 'vike-envz'; * import { z } from 'zod'; * * const env = getEnvZ(import.meta.env, { * PORT: [z.string().transform(Number), 'process'], * API_KEY: [z.string().min(1)] * }); * * // env.PORT is now a validated number * // env.API_KEY is a validated non-empty string * ``` */ export declare const parseEnvZ: <T extends EnvZ>(importMetaEnv: Record<string, any>, envSchema: T) => { [K in keyof T]: z.infer<T[K][0]>; };