forked from CaliCastle/cali.so
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.mjs
96 lines (84 loc) · 3.58 KB
/
env.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { z } from 'zod'
/**
* Specify server-side environment variables schema here.
*/
const server = z.object({
NODE_ENV: z.enum(['development', 'test', 'production']),
DATABASE_URL: z.string().min(1),
RESEND_API_KEY: z.string().min(1),
VERCEL_ENV: z
.enum(['development', 'preview', 'production'])
.default('development'),
UPSTASH_REDIS_REST_URL: z.string().min(1),
UPSTASH_REDIS_REST_TOKEN: z.string().min(1),
LINK_PREVIEW_API_BASE_URL: z.string().optional(),
SITE_NOTIFICATION_EMAIL_TO: z.string().optional(),
})
const client = z.object({
NEXT_PUBLIC_SANITY_PROJECT_ID: z.string().min(1),
NEXT_PUBLIC_SANITY_DATASET: z.string().min(1),
NEXT_PUBLIC_SANITY_USE_CDN: z.boolean(),
NEXT_PUBLIC_SITE_URL: z.string().min(1),
NEXT_PUBLIC_SITE_EMAIL_FROM: z.string().min(1),
NEXT_PUBLIC_SITE_LINK_PREVIEW_ENABLED: z.boolean().optional().default(false),
})
/**
* 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<typeof server> | keyof z.infer<typeof client>, string | undefined>}
*/
const processEnv = {
NODE_ENV: process.env.NODE_ENV,
DATABASE_URL: process.env.DATABASE_URL,
RESEND_API_KEY: process.env.RESEND_API_KEY,
VERCEL_ENV: process.env.VERCEL_ENV,
UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL,
UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN,
NEXT_PUBLIC_SANITY_PROJECT_ID: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
NEXT_PUBLIC_SANITY_DATASET: process.env.NEXT_PUBLIC_SANITY_DATASET,
NEXT_PUBLIC_SANITY_USE_CDN: process.env.NEXT_PUBLIC_SANITY_USE_CDN == 'true',
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
NEXT_PUBLIC_SITE_EMAIL_FROM: process.env.NEXT_PUBLIC_SITE_EMAIL_FROM,
LINK_PREVIEW_API_BASE_URL: process.env.LINK_PREVIEW_API_BASE_URL,
SITE_NOTIFICATION_EMAIL_TO: process.env.SITE_NOTIFICATION_EMAIL_TO,
NEXT_PUBLIC_SITE_LINK_PREVIEW_ENABLED:
process.env.NEXT_PUBLIC_SITE_LINK_PREVIEW_ENABLED == 'true',
}
// Don't touch the part below
// --------------------------
const merged = server.merge(client)
/** @typedef {z.input<typeof merged>} MergedInput */
/** @typedef {z.infer<typeof merged>} MergedOutput */
/** @typedef {z.SafeParseReturnType<MergedInput, MergedOutput>} 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 }