-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
30 lines (25 loc) · 896 Bytes
/
Copy pathenv.ts
File metadata and controls
30 lines (25 loc) · 896 Bytes
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
import { z } from "zod";
/**
* Typed, validated access to environment variables. Import `env` from here
* instead of reading process.env directly. Server-only keys are optional until
* the features that consume them require them (checked at the call site).
*/
const envSchema = z.object({
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),
/** Canonical site origin, for absolute URLs in metadata/OG tags. */
NEXT_PUBLIC_SITE_URL: z.string().url().optional(),
});
export type Env = z.infer<typeof envSchema>;
const parsed = envSchema.safeParse({
NODE_ENV: process.env.NODE_ENV,
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
});
if (!parsed.success) {
throw new Error(
`Invalid environment variables: ${JSON.stringify(parsed.error.flatten().fieldErrors)}`,
);
}
export const env: Env = parsed.data;
export { envSchema };