import superjson from "superjson"; import type { OutputType } from "./spotify-download-status_GET.schema"; import { appendSetCookies } from "../helpers/spotifyServer"; import { getDownloadJob, jsonError, requireDownloadAccess } from "../helpers/downloadJobs"; export async function handle(request: Request) { try { const access = await requireDownloadAccess(request); if (access.error) return access.error; const jobId = new URL(request.url).searchParams.get("jobId") || ""; const job = getDownloadJob(jobId, access.userId); if (!job) return jsonError("This download has expired or does not exist.", 404); const output: OutputType = { status: job.status, done: job.done, total: job.total, failed: job.failed, currentTitle: job.currentTitle, error: job.error, fileName: job.fileName, savedPath: job.savedPath, }; const headers = new Headers({ "Content-Type": "application/json", "Cache-Control": "no-store" }); appendSetCookies(headers, access.setCookies); return new Response(superjson.stringify(output), { headers }); } catch (error) { return jsonError(error instanceof Error ? error.message : "Unable to check the download.", 500); } }