|
| 1 | +/** |
| 2 | + * Environment Variables Schema (t3-env) |
| 3 | + * |
| 4 | + * Type-safe environment variable validation using @t3-oss/env-nextjs. |
| 5 | + * Provides runtime validation and TypeScript types for all env vars. |
| 6 | + * |
| 7 | + * @description |
| 8 | + * - NEXT_PUBLIC_* vars are exposed to the browser (client section) |
| 9 | + * - Server vars are only available on the server (server section) |
| 10 | + * - APP_ENV distinguishes test from development (NODE_ENV is auto-set to 'production' after build) |
| 11 | + * - NEXT_PUBLIC_ENABLE_MSW_MOCK controls MSW activation |
| 12 | + */ |
| 13 | +import { createEnv } from '@t3-oss/env-nextjs' |
| 14 | +import { z } from 'zod' |
| 15 | + |
| 16 | +export const env = createEnv({ |
| 17 | + /** |
| 18 | + * Server-side environment variables schema. |
| 19 | + * These are only available on the server. |
| 20 | + */ |
| 21 | + server: { |
| 22 | + NODE_ENV: z |
| 23 | + .enum(['development', 'test', 'production']) |
| 24 | + .default('development'), |
| 25 | + // APP_ENV is separate from NODE_ENV because Next.js sets NODE_ENV to 'production' |
| 26 | + // after running `next build`, regardless of the actual deployment target. |
| 27 | + // This allows MSW mocking in production builds for E2E testing. |
| 28 | + APP_ENV: z |
| 29 | + .enum(['development', 'test', 'production']) |
| 30 | + .optional() |
| 31 | + .default('development'), |
| 32 | + }, |
| 33 | + |
| 34 | + /** |
| 35 | + * Client-side environment variables schema. |
| 36 | + * These are exposed to the browser via NEXT_PUBLIC_ prefix. |
| 37 | + */ |
| 38 | + client: { |
| 39 | + NEXT_PUBLIC_SUPABASE_URL: z.string().min(1), |
| 40 | + NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string().min(1), |
| 41 | + // MSW activation flag - only check this on client side |
| 42 | + // Server also requires APP_ENV=test for activation |
| 43 | + NEXT_PUBLIC_ENABLE_MSW_MOCK: z |
| 44 | + .string() |
| 45 | + .optional() |
| 46 | + .default('false') |
| 47 | + .transform((val) => val === 'true'), |
| 48 | + }, |
| 49 | + |
| 50 | + /** |
| 51 | + * Runtime environment variables. |
| 52 | + * For Next.js >= 13.4.4, you need to destructure manually. |
| 53 | + */ |
| 54 | + runtimeEnv: { |
| 55 | + NODE_ENV: process.env.NODE_ENV, |
| 56 | + APP_ENV: process.env.APP_ENV, |
| 57 | + NEXT_PUBLIC_SUPABASE_URL: process.env.NEXT_PUBLIC_SUPABASE_URL, |
| 58 | + NEXT_PUBLIC_SUPABASE_ANON_KEY: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, |
| 59 | + NEXT_PUBLIC_ENABLE_MSW_MOCK: process.env.NEXT_PUBLIC_ENABLE_MSW_MOCK, |
| 60 | + }, |
| 61 | + |
| 62 | + /** |
| 63 | + * Skip validation in certain environments. |
| 64 | + * Set SKIP_ENV_VALIDATION=1 to skip validation (useful for Docker builds). |
| 65 | + */ |
| 66 | + skipValidation: !!process.env.SKIP_ENV_VALIDATION, |
| 67 | + |
| 68 | + /** |
| 69 | + * Treat empty strings as undefined. |
| 70 | + * Useful for optional env vars that might be set to empty string. |
| 71 | + */ |
| 72 | + emptyStringAsUndefined: true, |
| 73 | +}) |
0 commit comments