import { z } from "zod"; /** * Specify your server-side environment variables schema here. This way you can ensure the app isn't * built with invalid env vars. */ const server = z.object({ DATABASE_URL: z.string().url(), UPSTASH_REDIS_REST_URL: z.string().url(), UPSTASH_REDIS_REST_TOKEN: z.string(), UPSTASH_REDIS_EXPIRY_SECONDS: z.string(), UPSTASH_RATELIMIT_REQUESTS: z.string(), UPSTASH_RATELIMIT_SECONDS: z.string(), NODE_ENV: z.enum(["development", "test", "production"]), ABLY_PRIVATE_KEY: z.string(), APP_ENV: z.string(), RESEND_API_KEY: z.string(), UNKEY_ROOT_KEY: z.string(), CLERK_SECRET_KEY: z.string(), }); /** * Specify your client-side environment variables schema here. This way you can ensure the app isn't * built with invalid env vars. To expose them to the client, prefix them with `NEXT_PUBLIC_`. */ const client = z.object({ NEXT_PUBLIC_ABLY_PUBLIC_KEY: z.string(), NEXT_PUBLIC_APP_ENV: z.string(), NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string(), }); /** * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. * middlewares) or client-side so we need to destruct manually. * * @type {Record | keyof z.infer, string | undefined>} */ const processEnv = { DATABASE_URL: process.env.DATABASE_URL, UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN, UPSTASH_REDIS_EXPIRY_SECONDS: process.env.UPSTASH_REDIS_EXPIRY_SECONDS, UPSTASH_RATELIMIT_REQUESTS: process.env.UPSTASH_RATELIMIT_REQUESTS, UPSTASH_RATELIMIT_SECONDS: process.env.UPSTASH_RATELIMIT_SECONDS, NODE_ENV: process.env.NODE_ENV, ABLY_PRIVATE_KEY: process.env.ABLY_PRIVATE_KEY, NEXT_PUBLIC_ABLY_PUBLIC_KEY: process.env.NEXT_PUBLIC_ABLY_PUBLIC_KEY, APP_ENV: process.env.APP_ENV, NEXT_PUBLIC_APP_ENV: process.env.NEXT_PUBLIC_APP_ENV, RESEND_API_KEY: process.env.RESEND_API_KEY, UNKEY_ROOT_KEY: process.env.UNKEY_ROOT_KEY, NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY: process.env.CLERK_SECRET_KEY, }; // Don't touch the part below // -------------------------- const merged = server.merge(client); /** @typedef {z.input} MergedInput */ /** @typedef {z.infer} MergedOutput */ /** @typedef {z.SafeParseReturnType} MergedSafeParseReturn */ let env = /** @type {MergedOutput} */ (process.env); if (!!process.env.SKIP_ENV_VALIDATION == false) { const isServer = typeof window === "undefined"; const parsed = /** @type {MergedSafeParseReturn} */ ( isServer ? merged.safeParse(processEnv) // on server we can validate all env vars : client.safeParse(processEnv) // on client we can only validate the ones that are exposed ); if (parsed.success === false) { console.error( "❌ Invalid environment variables:", parsed.error.flatten().fieldErrors ); throw new Error("Invalid environment variables"); } env = new Proxy(parsed.data, { get(target, prop) { if (typeof prop !== "string") return undefined; // Throw a descriptive error if a server-side env var is accessed on the client // Otherwise it would just be returning `undefined` and be annoying to debug if (!isServer && !prop.startsWith("NEXT_PUBLIC_")) throw new Error( process.env.NODE_ENV === "production" ? "❌ Attempted to access a server-side environment variable on the client" : `❌ Attempted to access server-side environment variable '${prop}' on the client` ); return target[/** @type {keyof typeof target} */ (prop)]; }, }); } export { env };