sitegen/framework/meta/nextjs/resolvers/resolve-url.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import path from "path";
import process from "node:process";
function isStringOrURL(icon: any): icon is string | URL {
return typeof icon === "string" || icon instanceof URL;
}
function resolveUrl(url: null | undefined, metadataBase: URL | null): null;
function resolveUrl(url: string | URL, metadataBase: URL | null): URL;
function resolveUrl(
url: string | URL | null | undefined,
metadataBase: URL | null,
): URL | null;
function resolveUrl(
url: string | URL | null | undefined,
metadataBase: URL | null,
): URL | null {
if (url instanceof URL) return url;
if (!url) return null;
try {
// If we can construct a URL instance from url, ignore metadataBase
const parsedUrl = new URL(url);
return parsedUrl;
} catch (_) {}
if (!metadataBase) {
metadataBase = new URL(`http://localhost:${process.env.PORT || 3000}`);
}
// Handle relative or absolute paths
const basePath = metadataBase.pathname || "";
const joinedPath = path.join(basePath, url);
return new URL(joinedPath, metadataBase);
}
export { isStringOrURL, resolveUrl };