UNPKG

envfortress

Version:

Turn your .env into a fortress—type-checked, secure, and effortless for React, Vite, and Node.

82 lines (79 loc) 3.31 kB
import { z } from 'zod'; export { z } from 'zod'; type EnvSchema = Record<string, z.ZodTypeAny>; interface EnvConfig<TClient extends EnvSchema, TServer extends EnvSchema> { client: TClient; server: TServer; } interface EnvOptions { exampleFile?: string; allowMissingInProduction?: boolean; onValidationError?: (error: z.ZodError) => void; strict?: boolean; debug?: boolean; } type InferClient<T extends EnvSchema> = { [K in keyof T]: z.infer<T[K]>; }; type InferServer<T extends EnvSchema> = { [K in keyof T]: z.infer<T[K]>; }; type InferEnv<TClient extends EnvSchema, TServer extends EnvSchema> = InferClient<TClient> & InferServer<TServer>; /** * Zod 4 utility functions for common environment variable patterns */ declare const envUtils: { /** * Creates a boolean environment variable parser with improved Zod 4 validation * Accepts 'true', 'false', '1', '0', 'yes', 'no' (case insensitive) */ boolean: () => z.ZodPipe<z.ZodString, z.ZodTransform<boolean, string>>; /** * Creates a number environment variable parser with Zod 4's improved validation */ number: () => z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>; /** * Creates an integer environment variable parser */ integer: () => z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>; /** * Creates a URL environment variable parser with Zod 4's improved URL validation */ url: () => z.ZodString; /** * Creates an email environment variable parser with Zod 4's improved email validation */ email: () => z.ZodString; /** * Creates a port number parser (1-65535) */ port: () => z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>; /** * Creates a JSON environment variable parser with Zod 4's improved error handling */ json: <T>(schema?: z.ZodType<T>) => z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>; /** * Creates a comma-separated list parser */ list: (separator?: string) => z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>; /** * Creates a secret key parser with minimum length validation */ secret: (minLength?: number) => z.ZodString; }; /** * Creates a type-safe environment variable validator with support for nested objects and transforms * @param config - Configuration object with client and server schemas * @param options - Optional configuration for validation behavior * @returns Validated environment variables with full type inference */ declare function createEnv<TClient extends EnvSchema, TServer extends EnvSchema>(config: EnvConfig<TClient, TServer>, options?: EnvOptions): InferEnv<TClient, TServer>; /** * Creates a public-only environment validator (for client-side only) * @param schema - Schema for public environment variables * @param options - Optional configuration * @returns Validated environment variables */ declare function createPublicEnv<TClient extends EnvSchema>(schema: TClient, options?: EnvOptions): InferClient<TClient>; declare function _clearEnvCacheForTests(): void; export { type EnvConfig, type EnvOptions, type EnvSchema, type InferClient, type InferEnv, type InferServer, _clearEnvCacheForTests, createEnv, createPublicEnv, createEnv as default, envUtils };