Branchwell

Lumen & Gesso / website · lumengesso.com / app / lib / env.ts

1// Centralized, lazy env access. Throws a helpful error if a required
2// var is missing — keeps "is this set?" out of every callsite.
3
4function required(name: string): string {
5 const v = process.env[name];
6 if (!v) throw new Error(`Missing required env var: ${name}`);
7 return v;
8}
9
10function optional(name: string, fallback = ''): string {
11 return process.env[name] ?? fallback;
12}
13
14export const env = {
15 cf: {
16 accountId: () => required('CF_ACCOUNT_ID'),
17 imagesHash: () => required('CF_IMAGES_HASH'),
18 imagesApiToken: () => required('CF_IMAGES_API_TOKEN'),
19 },
20 stripe: {
21 secretKey: () => required('STRIPE_SECRET_KEY'),
22 publishableKey: () => required('STRIPE_PUBLISHABLE_KEY'),
23 webhookSecret: () => required('STRIPE_WEBHOOK_SECRET'),
24 },
25 email: {
26 from: () => optional('EMAIL_FROM', 'Lumen & Gesso <orders@lumengesso.com>'),
27 },
28 pinterest: {
29 // Two supported wiring modes:
30 //
31 // 1. **Trial / direct-bearer** (simplest): paste the access token from
32 // Pinterest's dev console into PINTEREST_ACCESS_TOKEN. No OAuth flow
33 // needed; works as long as the token is valid (Pinterest's trial
34 // tokens are long-lived but not refreshable).
35 //
36 // 2. **OAuth refresh flow** (long-term): set CLIENT_ID + CLIENT_SECRET,
37 // run /admin/pinterest → Connect, paste the captured refresh token
38 // into PINTEREST_REFRESH_TOKEN. Auto-mints a new access token every
39 // ~25 minutes; the refresh token lasts ~1 year.
40 //
41 // pinterest.ts prefers mode 1 if ACCESS_TOKEN is set; otherwise mode 2.
42 accessTokenMaybe: () => optional('PINTEREST_ACCESS_TOKEN'),
43 clientId: () => required('PINTEREST_CLIENT_ID'),
44 clientSecret: () => required('PINTEREST_CLIENT_SECRET'),
45 refreshToken: () => required('PINTEREST_REFRESH_TOKEN'),
46 // Read without throwing — used by the setup page to render status.
47 clientIdMaybe: () => optional('PINTEREST_CLIENT_ID'),
48 refreshTokenMaybe: () => optional('PINTEREST_REFRESH_TOKEN'),
49 },
50 cron: {
51 // Shared secret Vercel Cron sends as `Authorization: Bearer <secret>`.
52 // Guards /api/cron/* so only the scheduler can trigger those routes.
53 secret: () => required('CRON_SECRET'),
54 },
55 site: {
56 url: () => optional('NEXT_PUBLIC_SITE_URL', 'http://localhost:3000'),
57 // Allowlist for /admin access. Anyone whose verified email is
58 // in this set can reach the admin UI; everyone else is bounced to
59 // /orders. Defaults to admin@lumengesso.com (a Cloudflare Email
60 // Routing alias that forwards to the owner's personal gmail).
61 adminEmails: () => optional('ADMIN_EMAILS', 'admin@lumengesso.com')
62 .split(',')
63 .map(s => s.trim().toLowerCase())
64 .filter(Boolean),
65 // Where internal studio notifications go (new-commission emails,
66 // revision requests, etc.). Separate from adminEmails so the
67 // "where it goes" inbox can differ from the "who can log in"
68 // allowlist. Defaults to orders@lumengesso.com.
69 studioEmail: () => optional('STUDIO_EMAIL', 'orders@lumengesso.com'),
70 },
71};