-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenv.ts
48 lines (38 loc) · 1.17 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import dotenv from "dotenv";
import { z } from "zod";
export type Schema = z.infer<typeof schema>;
export const schema = z.object({
NODE_ENV: z
.union([
z.literal("development"),
z.literal("test"),
z.literal("production"),
])
.default("development"),
// for client and server
NEXT_PUBLIC_SITE_URL: z.string().url(),
// for server
DATABASE_USER: z.string().min(1),
DATABASE_PASSWORD: z.string().min(1),
DATABASE_DB: z.string().min(1),
DATABASE_HOST: z.string().min(1),
DATABASE_PORT: z.coerce.number().min(1),
DATABASE_SCHEMA: z.string().min(1),
DATABASE_URL: z.string().min(1),
GOOGLE_CLIENT_ID: z.string().min(1),
GOOGLE_CLIENT_SECRET: z.string().min(1),
NEXTAUTH_URL: z.string().min(1),
NEXTAUTH_SECRET: z.string().min(1),
TRACE_EXPORTER_URL: z.string().url().optional().or(z.literal("")),
});
export function config(file?: ".env" | ".env.test") {
if (file) {
dotenv.config({ path: file });
}
const res = schema.safeParse(process.env);
if (res.error) {
console.error("\x1b[31m%s\x1b[0m", "[Errors] environment variables");
console.error(JSON.stringify(res.error.errors, null, 2));
process.exit(1);
}
}