67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { z } from "zod";
|
|
|
|
export const mintelEnvSchema = {
|
|
NODE_ENV: z
|
|
.enum(["development", "production", "test"])
|
|
.default("development"),
|
|
NEXT_PUBLIC_BASE_URL: z.string().url(),
|
|
|
|
// Analytics (Proxy Pattern)
|
|
UMAMI_WEBSITE_ID: z.string().optional(),
|
|
UMAMI_API_ENDPOINT: z
|
|
.string()
|
|
.url()
|
|
.default("https://analytics.infra.mintel.me"),
|
|
|
|
// Error Tracking
|
|
SENTRY_DSN: z.string().optional(),
|
|
|
|
// Notifications
|
|
GOTIFY_URL: z.string().url().optional(),
|
|
GOTIFY_TOKEN: z.string().optional(),
|
|
|
|
LOG_LEVEL: z
|
|
.enum(["trace", "debug", "info", "warn", "error", "fatal"])
|
|
.default("info"),
|
|
MAIL_HOST: z.string().optional(),
|
|
MAIL_PORT: z.coerce.number().default(587),
|
|
MAIL_USERNAME: z.string().optional(),
|
|
MAIL_PASSWORD: z.string().optional(),
|
|
MAIL_FROM: z.string().optional(),
|
|
MAIL_RECIPIENTS: z.preprocess(
|
|
(val) => (typeof val === "string" ? val.split(",").filter(Boolean) : val),
|
|
z.array(z.string()).default([]),
|
|
),
|
|
};
|
|
|
|
export function validateMintelEnv(schemaExtension = {}) {
|
|
const fullSchema = z.object({
|
|
...mintelEnvSchema,
|
|
...schemaExtension,
|
|
});
|
|
|
|
const isBuildTime =
|
|
process.env.NEXT_PHASE === "phase-production-build" ||
|
|
process.env.SKIP_ENV_VALIDATION === "true";
|
|
|
|
const result = fullSchema.safeParse(process.env);
|
|
|
|
if (!result.success) {
|
|
if (isBuildTime) {
|
|
console.warn(
|
|
"⚠️ Some environment variables are missing during build, but skipping strict validation.",
|
|
);
|
|
// Return partial data to allow build to continue
|
|
return process.env as unknown as z.infer<typeof fullSchema>;
|
|
}
|
|
|
|
console.error(
|
|
"❌ Invalid environment variables:",
|
|
result.error.flatten().fieldErrors,
|
|
);
|
|
throw new Error("Invalid environment variables");
|
|
}
|
|
|
|
return result.data;
|
|
}
|