mirror of
https://github.com/Xevion/icons.git
synced 2025-12-17 14:12:10 -06:00
Add VERCEL_URl to env with URL parsing & transformation
This commit is contained in:
75
src/env.mjs
75
src/env.mjs
@@ -1,12 +1,12 @@
|
|||||||
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
||||||
import { z } from "zod";
|
import {z} from "zod";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify your server-side environment variables schema here.
|
* Specify your server-side environment variables schema here.
|
||||||
* This way you can ensure the app isn't built with invalid env vars.
|
* This way you can ensure the app isn't built with invalid env vars.
|
||||||
*/
|
*/
|
||||||
const server = z.object({
|
const server = z.object({
|
||||||
NODE_ENV: z.enum(["development", "test", "production"]),
|
NODE_ENV: z.enum(["development", "test", "production"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,7 +15,13 @@ const server = z.object({
|
|||||||
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
|
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
|
||||||
*/
|
*/
|
||||||
const client = z.object({
|
const client = z.object({
|
||||||
// NEXT_PUBLIC_CLIENTVAR: z.string().min(1),
|
NEXT_PUBLIC_APP_URL: z.string().transform(url => {
|
||||||
|
// Add a HTTPS url prefix automatically, for vercel.
|
||||||
|
if (!(url.startsWith("http://") || url.startsWith("https://")))
|
||||||
|
return `https://${url}`;
|
||||||
|
return url;
|
||||||
|
})
|
||||||
|
// NEXT_PUBLIC_CLIENTVAR: z.string().min(1),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,8 +30,9 @@ const client = z.object({
|
|||||||
* @type {Record<keyof z.infer<typeof server> | keyof z.infer<typeof client>, string | undefined>}
|
* @type {Record<keyof z.infer<typeof server> | keyof z.infer<typeof client>, string | undefined>}
|
||||||
*/
|
*/
|
||||||
const processEnv = {
|
const processEnv = {
|
||||||
NODE_ENV: process.env.NODE_ENV,
|
NODE_ENV: process.env.NODE_ENV,
|
||||||
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
|
NEXT_PUBLIC_APP_URL: process.env.VERCEL_URL ?? process.env.APP_URL
|
||||||
|
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don't touch the part below
|
// Don't touch the part below
|
||||||
@@ -37,37 +44,37 @@ const merged = server.merge(client);
|
|||||||
let env = process.env;
|
let env = process.env;
|
||||||
|
|
||||||
if (!!process.env.SKIP_ENV_VALIDATION == false) {
|
if (!!process.env.SKIP_ENV_VALIDATION == false) {
|
||||||
const isServer = typeof window === "undefined";
|
const isServer = typeof window === "undefined";
|
||||||
|
|
||||||
const parsed = isServer
|
const parsed = isServer
|
||||||
? merged.safeParse(processEnv) // on server we can validate all env vars
|
? merged.safeParse(processEnv) // on server we can validate all env vars
|
||||||
: client.safeParse(processEnv); // on client we can only validate the ones that are exposed
|
: client.safeParse(processEnv); // on client we can only validate the ones that are exposed
|
||||||
|
|
||||||
if (parsed.success === false) {
|
if (parsed.success === false) {
|
||||||
console.error(
|
console.error(
|
||||||
"❌ Invalid environment variables:",
|
"❌ Invalid environment variables:",
|
||||||
parsed.error.flatten().fieldErrors,
|
parsed.error.flatten().fieldErrors,
|
||||||
);
|
|
||||||
throw new Error("Invalid environment variables");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type z.infer<merged>
|
|
||||||
* @ts-ignore - can't type this properly in jsdoc */
|
|
||||||
env = new Proxy(parsed.data, {
|
|
||||||
get(target, prop) {
|
|
||||||
if (typeof prop !== "string") return undefined;
|
|
||||||
// Throw a descriptive error if a server-side env var is accessed on the client
|
|
||||||
// Otherwise it would just be returning `undefined` and be annoying to debug
|
|
||||||
if (!isServer && !prop.startsWith("NEXT_PUBLIC_"))
|
|
||||||
throw new Error(
|
|
||||||
process.env.NODE_ENV === "production"
|
|
||||||
? "❌ Attempted to access a server-side environment variable on the client"
|
|
||||||
: `❌ Attempted to access server-side environment variable '${prop}' on the client`,
|
|
||||||
);
|
);
|
||||||
/* @ts-ignore - can't type this properly in jsdoc */
|
throw new Error("Invalid environment variables");
|
||||||
return target[prop];
|
}
|
||||||
},
|
|
||||||
});
|
/** @type z.infer<merged>
|
||||||
|
* @ts-ignore - can't type this properly in jsdoc */
|
||||||
|
env = new Proxy(parsed.data, {
|
||||||
|
get(target, prop) {
|
||||||
|
if (typeof prop !== "string") return undefined;
|
||||||
|
// Throw a descriptive error if a server-side env var is accessed on the client
|
||||||
|
// Otherwise it would just be returning `undefined` and be annoying to debug
|
||||||
|
if (!isServer && !prop.startsWith("NEXT_PUBLIC_"))
|
||||||
|
throw new Error(
|
||||||
|
process.env.NODE_ENV === "production"
|
||||||
|
? "❌ Attempted to access a server-side environment variable on the client"
|
||||||
|
: `❌ Attempted to access server-side environment variable '${prop}' on the client`,
|
||||||
|
);
|
||||||
|
/* @ts-ignore - can't type this properly in jsdoc */
|
||||||
|
return target[prop];
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export { env };
|
export {env};
|
||||||
|
|||||||
Reference in New Issue
Block a user