// Server-only example using Node 22 Web APIs. Wire these helpers to your // authenticated routes and your framework's CSRF and entitlement checks. const api = "https://api.paradym.tv/partner/v1/watch-party-launches"; export function watchInParadym({ siteOrigin, key, requireAccess }) { const origin = new URL(siteOrigin); if (origin.protocol !== "https:" || origin.origin !== siteOrigin || !key) { throw new Error("Configure the site's exact HTTPS origin and a server key"); } async function call(suffix, payload) { const response = await fetch(api + suffix, { method: "POST", headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }, body: JSON.stringify({ ...payload, origin: siteOrigin }), redirect: "error", signal: AbortSignal.timeout(7000), }); if (!response.ok) { const error = new Error("Paradym could not authorize this request"); error.status = response.status; error.requestId = response.headers.get("x-request-id"); throw error; } return response.json(); } function redirect(launch) { const destination = new URL(launch.launch_url); if (destination.origin !== "https://paradym.tv" || destination.username || destination.password || destination.pathname !== "/integrations/watch-party" || destination.search || !/^#launch=[A-Za-z0-9_-]+$/.test(destination.hash)) { throw new Error("Unexpected Paradym launch destination"); } return new Response(null, { status: 303, headers: { Location: destination.href, "Cache-Control": "private, no-store", "Referrer-Policy": "no-referrer", }, }); } const inspectGuest = (request) => call("/guest/inspect", { request }); return { inspectGuest, async startHost(account, assetId) { // Check the site's own signed-in account on every click. const viewerId = await requireAccess(account, assetId); return redirect(await call("", { asset_id: assetId, viewer_id: viewerId })); }, async authorizeGuest(account, request) { // Read the immutable target from Paradym with the server key. A browser // must never choose the asset that is authorized for this request. const asset = await inspectGuest(request); const viewerId = await requireAccess(account, asset.asset_id, asset.revision); return redirect(await call("/guest", { request, viewer_id: viewerId })); }, }; }