Files
Calbook/lib/public-url.ts

32 lines
851 B
TypeScript

const DEFAULT_PUBLIC_URL = "http://localhost:3000";
function normalizeBaseUrl(value?: string | null) {
if (!value) return null;
const trimmed = value.trim();
if (!trimmed) return null;
try {
const parsed = new URL(trimmed);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
return null;
}
return parsed.toString().replace(/\/+$/g, "");
} catch {
return null;
}
}
export function getPublicBaseUrl() {
return (
normalizeBaseUrl(process.env.PUBLIC_URL) ??
normalizeBaseUrl(process.env.APP_BASE_URL) ??
normalizeBaseUrl(process.env.NEXTAUTH_URL) ??
DEFAULT_PUBLIC_URL
);
}
export function buildPublicUrl(pathname: string) {
const normalizedPath = pathname.startsWith("/") ? pathname : `/${pathname}`;
return new URL(normalizedPath, `${getPublicBaseUrl()}/`).toString();
}