mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-20 22:24:28 +01:00
32 lines
708 B
TypeScript
32 lines
708 B
TypeScript
import { z } from "zod"
|
|
|
|
const zodEnv = z.object({
|
|
AUTH_SECRET: z.string(),
|
|
NEXT_PUBLIC_DEMO: z.string().optional(),
|
|
})
|
|
|
|
declare global {
|
|
interface ProcessEnv extends z.TypeOf<typeof zodEnv> {
|
|
AUTH_SECRET: string;
|
|
NEXT_PUBLIC_DEMO?: string;
|
|
}
|
|
}
|
|
|
|
export function init() {
|
|
try {
|
|
zodEnv.parse(process.env)
|
|
} catch (err) {
|
|
if (err instanceof z.ZodError) {
|
|
const { fieldErrors } = err.flatten()
|
|
const errorMessage = Object.entries(fieldErrors)
|
|
.map(([field, errors]) =>
|
|
errors ? `${field}: ${errors.join(", ")}` : field,
|
|
)
|
|
.join("\n ")
|
|
|
|
throw new Error(
|
|
`Missing environment variables:\n ${errorMessage}`,
|
|
)
|
|
}
|
|
}
|
|
} |