Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import { useQuery } from "@octanejs/tanstack-query";import { useCustomerQuerySession } from "../customer-query-provider";import type { SettingsConnection } from "../owner-rpc";
type DomainInfo = { setupUrl: string; origin: string | null };
function safeSetupUrl(value: unknown) { try { if (typeof value !== "string") return null; const url = new URL(value); const local = ["localhost", "127.0.0.1"].includes(url.hostname); const params = url.searchParams; return !url.username && !url.password && url.pathname === "/connect" && params.size === 1 && /^[a-f0-9]{32}$/.test(params.get("configureDomain") ?? "") && !url.hash && (url.protocol === "https:" || (url.protocol === "http:" && local)) ? url.href : null; } catch { return null; }}function safeOrigin(value: unknown) { if (value === null) return null; try { const url = new URL(String(value)); const local = ["localhost", "127.0.0.1"].includes(url.hostname); return (url.protocol === "https:" || (url.protocol === "http:" && local)) && url.pathname === "/" && !url.search && !url.hash && !url.username && !url.password ? url.origin : null; } catch { return null; }}
export function useDomainInfoQuery(connection: SettingsConnection) { const cache = useCustomerQuerySession(); return useQuery({ queryKey: cache.key("settings", "domain"), enabled: !!cache.getSnapshot().scope && !!connection?.isReady(), queryFn: async ({ signal }): Promise<DomainInfo> => { if (!connection?.isReady()) throw new Error("Connection unavailable"); const bounded = AbortSignal.any([ signal, connection.signal, AbortSignal.timeout(10_000), ]); const response = await fetch("/api/domain", { credentials: "same-origin", cache: "no-store", signal: bounded, }); if (response.status === 401 || response.status === 403) { if (connection.isCurrent()) cache.revoke(cache.getSnapshot().epoch); throw new Error("Sign-in required"); } const data = await response.json(); bounded.throwIfAborted(); const setupUrl = safeSetupUrl(data.setupUrl); if ( !response.ok || !setupUrl || (data.origin !== null && !safeOrigin(data.origin)) ) throw new Error("Domain unavailable"); return { setupUrl, origin: safeOrigin(data.origin) }; }, });}