/** * WorthDoing.ai * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: src/lib/env.ts * Description: Zod-validated server-side environment variables (never import from client code). */ import { z } from "zod"; const envSchema = z.object({ ANTHROPIC_API_KEY: z.string().min(10), FIRECRAWL_API_KEY: z.string().min(10), DATABASE_URL: z.string().url(), PUBLIC_BASE_URL: z.string().url().default("https://www.worthdoing.ai"), CLAUDE_AGENT_MODEL: z.string().default("claude-opus-5"), CLAUDE_SYNTHESIS_MODEL: z.string().default("claude-opus-5"), NODE_ENV: z.enum(["development", "test", "production"]).default("development"), }); let cached: z.infer | null = null; /** Validated environment. Throws a structured error at first server-side access if misconfigured. */ export function env(): z.infer { if (cached) return cached; const parsed = envSchema.safeParse(process.env); if (!parsed.success) { const missing = parsed.error.issues.map((i) => i.path.join(".")).join(", "); throw new Error(`Invalid environment configuration: ${missing}`); } cached = parsed.data; return cached; }