|
| 1 | +/** |
| 2 | + * Typed, validated environment configuration. |
| 3 | + * |
| 4 | + * All NEXT_PUBLIC_* variables are validated at module load time using zod. |
| 5 | + * A missing or malformed required variable throws at build time (or on first |
| 6 | + * import in dev), preventing silent undefined values from reaching production. |
| 7 | + * |
| 8 | + * Variable ownership: |
| 9 | + * NEXT_PUBLIC_API_URL — backend team, updated on each deployment |
| 10 | + * NEXT_PUBLIC_SOROBAN_RPC_URL — infra team, per-network |
| 11 | + * NEXT_PUBLIC_HORIZON_URL — infra team, per-network |
| 12 | + * NEXT_PUBLIC_CONTRACT_ID — contracts team, updated after each deploy |
| 13 | + * NEXT_PUBLIC_IPFS_GATEWAY — infra team |
| 14 | + * NEXT_PUBLIC_NETWORK — set per environment (testnet | public) |
| 15 | + * NEXT_PUBLIC_CAPTCHA_SITE_KEY — security team |
| 16 | + * NEXT_PUBLIC_CAPTCHA_PROVIDER — security team (turnstile | hcaptcha) |
| 17 | + * |
| 18 | + * Rotation: when backend deployment-registry.json changes contract addresses, |
| 19 | + * update NEXT_PUBLIC_CONTRACT_ID and redeploy the frontend. |
| 20 | + */ |
| 21 | + |
| 22 | +import { z } from 'zod' |
| 23 | + |
| 24 | +const envSchema = z.object({ |
| 25 | + /** Backend REST API base URL — no trailing slash */ |
| 26 | + NEXT_PUBLIC_API_URL: z.string().url('NEXT_PUBLIC_API_URL must be a valid URL'), |
| 27 | + |
| 28 | + /** Soroban RPC endpoint for the active network */ |
| 29 | + NEXT_PUBLIC_SOROBAN_RPC_URL: z |
| 30 | + .string() |
| 31 | + .url('NEXT_PUBLIC_SOROBAN_RPC_URL must be a valid URL') |
| 32 | + .default('https://soroban-testnet.stellar.org'), |
| 33 | + |
| 34 | + /** Horizon REST endpoint for the active network */ |
| 35 | + NEXT_PUBLIC_HORIZON_URL: z |
| 36 | + .string() |
| 37 | + .url('NEXT_PUBLIC_HORIZON_URL must be a valid URL') |
| 38 | + .default('https://horizon-testnet.stellar.org'), |
| 39 | + |
| 40 | + /** Deployed niffyinsure contract ID for the active network */ |
| 41 | + NEXT_PUBLIC_CONTRACT_ID: z |
| 42 | + .string() |
| 43 | + .min(1, 'NEXT_PUBLIC_CONTRACT_ID must not be empty') |
| 44 | + .default(''), |
| 45 | + |
| 46 | + /** IPFS gateway base URL used to resolve CIDs — no trailing slash */ |
| 47 | + NEXT_PUBLIC_IPFS_GATEWAY: z |
| 48 | + .string() |
| 49 | + .url('NEXT_PUBLIC_IPFS_GATEWAY must be a valid URL') |
| 50 | + .default('https://ipfs.io/ipfs'), |
| 51 | + |
| 52 | + /** |
| 53 | + * Active Stellar network. |
| 54 | + * - "testnet" → testnet RPC / Horizon / explorer |
| 55 | + * - "public" → mainnet RPC / Horizon / explorer |
| 56 | + */ |
| 57 | + NEXT_PUBLIC_NETWORK: z.enum(['testnet', 'public']).default('testnet'), |
| 58 | + |
| 59 | + /** Captcha site key (public, safe to expose) */ |
| 60 | + NEXT_PUBLIC_CAPTCHA_SITE_KEY: z.string().default(''), |
| 61 | + |
| 62 | + /** Captcha provider */ |
| 63 | + NEXT_PUBLIC_CAPTCHA_PROVIDER: z.enum(['turnstile', 'hcaptcha']).default('turnstile'), |
| 64 | +}) |
| 65 | + |
| 66 | +type Env = z.infer<typeof envSchema> |
| 67 | + |
| 68 | +function parseEnv(): Env { |
| 69 | + const result = envSchema.safeParse({ |
| 70 | + NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL, |
| 71 | + NEXT_PUBLIC_SOROBAN_RPC_URL: process.env.NEXT_PUBLIC_SOROBAN_RPC_URL, |
| 72 | + NEXT_PUBLIC_HORIZON_URL: process.env.NEXT_PUBLIC_HORIZON_URL, |
| 73 | + NEXT_PUBLIC_CONTRACT_ID: process.env.NEXT_PUBLIC_CONTRACT_ID, |
| 74 | + NEXT_PUBLIC_IPFS_GATEWAY: process.env.NEXT_PUBLIC_IPFS_GATEWAY, |
| 75 | + NEXT_PUBLIC_NETWORK: process.env.NEXT_PUBLIC_NETWORK, |
| 76 | + NEXT_PUBLIC_CAPTCHA_SITE_KEY: process.env.NEXT_PUBLIC_CAPTCHA_SITE_KEY, |
| 77 | + NEXT_PUBLIC_CAPTCHA_PROVIDER: process.env.NEXT_PUBLIC_CAPTCHA_PROVIDER, |
| 78 | + }) |
| 79 | + |
| 80 | + if (!result.success) { |
| 81 | + const messages = result.error.errors |
| 82 | + .map((e) => ` ${e.path.join('.')}: ${e.message}`) |
| 83 | + .join('\n') |
| 84 | + throw new Error(`Invalid environment configuration:\n${messages}`) |
| 85 | + } |
| 86 | + |
| 87 | + // Dev-time consistency warning: mainnet contract ID looks like testnet config |
| 88 | + if ( |
| 89 | + process.env.NODE_ENV === 'development' && |
| 90 | + result.data.NEXT_PUBLIC_NETWORK === 'public' && |
| 91 | + result.data.NEXT_PUBLIC_SOROBAN_RPC_URL.includes('testnet') |
| 92 | + ) { |
| 93 | + console.warn( |
| 94 | + '[env] Warning: NEXT_PUBLIC_NETWORK=public but NEXT_PUBLIC_SOROBAN_RPC_URL points at testnet.', |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + return result.data |
| 99 | +} |
| 100 | + |
| 101 | +const env = parseEnv() |
| 102 | + |
| 103 | +/** |
| 104 | + * Returns the validated, typed environment configuration. |
| 105 | + * Use this instead of accessing process.env directly in hooks and API clients. |
| 106 | + * |
| 107 | + * @example |
| 108 | + * import { getConfig } from '@/config/env' |
| 109 | + * const { apiUrl, network } = getConfig() |
| 110 | + */ |
| 111 | +export function getConfig() { |
| 112 | + return { |
| 113 | + /** Backend REST API base URL */ |
| 114 | + apiUrl: env.NEXT_PUBLIC_API_URL, |
| 115 | + |
| 116 | + /** Soroban RPC URL for the active network */ |
| 117 | + sorobanRpcUrl: env.NEXT_PUBLIC_SOROBAN_RPC_URL, |
| 118 | + |
| 119 | + /** Horizon REST URL for the active network */ |
| 120 | + horizonUrl: env.NEXT_PUBLIC_HORIZON_URL, |
| 121 | + |
| 122 | + /** Deployed contract ID for the active network */ |
| 123 | + contractId: env.NEXT_PUBLIC_CONTRACT_ID, |
| 124 | + |
| 125 | + /** IPFS gateway base URL */ |
| 126 | + ipfsGateway: env.NEXT_PUBLIC_IPFS_GATEWAY, |
| 127 | + |
| 128 | + /** Active Stellar network */ |
| 129 | + network: env.NEXT_PUBLIC_NETWORK, |
| 130 | + |
| 131 | + /** Captcha site key */ |
| 132 | + captchaSiteKey: env.NEXT_PUBLIC_CAPTCHA_SITE_KEY, |
| 133 | + |
| 134 | + /** Captcha provider */ |
| 135 | + captchaProvider: env.NEXT_PUBLIC_CAPTCHA_PROVIDER, |
| 136 | + |
| 137 | + /** Stellar block explorer base URL for the active network */ |
| 138 | + explorerBase: |
| 139 | + env.NEXT_PUBLIC_NETWORK === 'public' |
| 140 | + ? 'https://stellar.expert/explorer/public/tx' |
| 141 | + : 'https://stellar.expert/explorer/testnet/tx', |
| 142 | + } as const |
| 143 | +} |
0 commit comments