22 lines
612 B
TypeScript
22 lines
612 B
TypeScript
export function validateChatGptShareUrl(raw: string): string {
|
|
let url: URL;
|
|
try {
|
|
url = new URL(raw);
|
|
} catch {
|
|
throw new Error("Enter a valid public ChatGPT share URL.");
|
|
}
|
|
|
|
if (url.protocol !== "https:") {
|
|
throw new Error("Only HTTPS ChatGPT share URLs are accepted.");
|
|
}
|
|
|
|
const host = url.hostname.toLowerCase();
|
|
const allowedHost = host === "chatgpt.com" || host === "chat.openai.com";
|
|
if (!allowedHost || !url.pathname.startsWith("/share/")) {
|
|
throw new Error("Only public ChatGPT share URLs under /share/ are accepted.");
|
|
}
|
|
|
|
url.hash = "";
|
|
return url.toString();
|
|
}
|