-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
executable file
·45 lines (38 loc) · 1.25 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
/* eslint-disable node/no-process-env */
import { config } from "dotenv";
import { expand } from "dotenv-expand";
import path from "node:path";
import { z } from "zod";
expand(config({
path: path.resolve(
process.cwd(),
process.env.NODE_ENV === "test" ? ".env.test" : ".env",
),
}));
const EnvSchema = z.object({
NODE_ENV: z.string().default("development"),
PORT: z.coerce.number().default(9999),
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace", "silent"]),
DATABASE_URL: z.string().url(),
DATABASE_AUTH_TOKEN: z.string().optional(),
SESSION_COOKIE_SECRET: z.string().min(32),
}).superRefine((input, ctx) => {
if (input.NODE_ENV === "production" && !input.DATABASE_AUTH_TOKEN) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: "undefined",
path: ["DATABASE_AUTH_TOKEN"],
message: "Must be set when NODE_ENV is 'production'",
});
}
});
export type env = z.infer<typeof EnvSchema>;
// eslint-disable-next-line ts/no-redeclare
const { data: env, error } = EnvSchema.safeParse(process.env);
if (error) {
console.error("❌ Invalid env:");
console.error(JSON.stringify(error.flatten().fieldErrors, null, 2));
process.exit(1);
}
export default env!;