Skip to content

Commit 9bccecc

Browse files
committed
refactor: make env validation be JS files so we can use them in our next config
1 parent 5c37505 commit 9bccecc

File tree

8 files changed

+21
-31
lines changed

8 files changed

+21
-31
lines changed

next.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { validateClientEnv } from "./src/lib/env/client.js";
2+
import { validateServerEnv } from "./src/lib/env/server.js";
3+
4+
validateServerEnv();
5+
validateClientEnv();
16
/** @type {import('next').NextConfig} */
27
const nextConfig = {};
38

src/app/layout.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import type { Metadata } from "next";
99
import localFont from "next/font/local";
1010
import { cookies } from "next/headers";
1111
import "./globals.css";
12-
import { validateClientEnv } from "@/lib/env/client";
13-
14-
validateClientEnv();
1512

1613
const geistSans = localFont({
1714
src: "./fonts/GeistVF.woff",
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { isBuildTime } from "./helpers";
2+
import { shouldSkipValidation } from "./helpers.js";
33

44
const clientEnvSchema = z.object({
55
NEXT_PUBLIC_REOWN_PROJECT_ID: z
@@ -11,13 +11,9 @@ const clientEnvSchema = z.object({
1111
NEXT_PUBLIC_CRYPTO_TO_FIAT_TRUSTED_ORIGINS: z.string().optional(),
1212
});
1313

14-
export type ClientEnv = z.infer<typeof clientEnvSchema>;
15-
1614
export function validateClientEnv() {
17-
if (isBuildTime()) {
18-
console.warn(
19-
"⚠️ Skipping client environment variable validation at build time.",
20-
);
15+
if (shouldSkipValidation()) {
16+
console.warn("⚠️ Skipping client environment variable validation.");
2117
return;
2218
}
2319

@@ -41,5 +37,3 @@ export function validateClientEnv() {
4137
throw new Error("Invalid client environment variables");
4238
}
4339
}
44-
45-
export const clientEnv = validateClientEnv();

src/lib/env/helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function shouldSkipValidation() {
2+
return process.env.SKIP_ENV_VALIDATION === "true";
3+
}

src/lib/env/helpers.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { isBuildTime } from "./helpers";
2+
import { shouldSkipValidation } from "./helpers.js";
33

44
const serverEnvSchema = z.object({
55
DATABASE_URL: z.string().min(1, "DATABASE_URL is required"),
@@ -17,16 +17,11 @@ const serverEnvSchema = z.object({
1717
FEE_ADDRESS_FOR_PAYMENT: z.string().optional(),
1818
REDIS_URL: z.string().url().optional(),
1919
INVOICE_PROCESSING_TTL: z.string().optional(),
20-
VERCEL_URL: z.string().optional(),
2120
});
2221

23-
export type ServerEnv = z.infer<typeof serverEnvSchema>;
24-
2522
export function validateServerEnv() {
26-
if (isBuildTime()) {
27-
console.warn(
28-
"⚠️ Skipping server environment variable validation at build time.",
29-
);
23+
if (shouldSkipValidation()) {
24+
console.warn("⚠️ Skipping server environment variable validation.");
3025
return;
3126
}
3227

@@ -56,5 +51,3 @@ export function validateServerEnv() {
5651
throw new Error("Invalid server environment variables");
5752
}
5853
}
59-
60-
export const serverEnv = validateServerEnv();

src/middleware.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { NextResponse } from "next/server";
22

33
import type { NextRequest } from "next/server";
4-
import { validateServerEnv } from "./lib/env/server";
5-
6-
validateServerEnv();
74

85
export async function middleware(request: NextRequest): Promise<NextResponse> {
96
if (request.method === "GET") {

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
"@/*": ["./src/*"]
2222
}
2323
},
24-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
24+
"include": [
25+
"next-env.d.ts",
26+
"**/*.ts",
27+
"**/*.tsx",
28+
".next/types/**/*.ts",
29+
"src/lib/env/*.js"
30+
],
2531
"exclude": ["node_modules"]
2632
}

0 commit comments

Comments
 (0)