-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.ts
25 lines (20 loc) · 772 Bytes
/
env.ts
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
import { type ZodFormattedError, z } from "zod";
import { logger } from "./logger.js";
export const serverSchema = z.object({
MONGODB_URI: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
CLIENT_ORIGIN: z.string().url(),
});
const formatErrors = (errors: ZodFormattedError<Map<string, string>, string>) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value) return `${name}: ${value._errors.join(", ")}\n`;
return "";
})
.filter(Boolean);
const _serverEnv = serverSchema.safeParse(process.env);
if (!_serverEnv.success) {
logger.error("❌ Invalid environment variables:\n", ...formatErrors(_serverEnv.error.format()));
process.exit(1);
}
export const env = _serverEnv.data;