import superjson from "superjson"; import { schema, type OutputType } from "./spotify-download_POST.schema"; import { appendSetCookies } from "../helpers/spotifyServer"; import { jsonError, requireDownloadAccess, startDownloadJob } from "../helpers/downloadJobs"; export async function handle(request: Request) { try { const access = await requireDownloadAccess(request); if (access.error) return access.error; const parsed = schema.safeParse(superjson.parse(await request.text())); if (!parsed.success) return jsonError(parsed.error.issues[0]?.message || "Invalid download request.", 400); const job = startDownloadJob({ ownerId: access.userId, ...parsed.data }); const headers = new Headers({ "Content-Type": "application/json", "Cache-Control": "no-store" }); appendSetCookies(headers, access.setCookies); return new Response(superjson.stringify({ jobId: job.id } satisfies OutputType), { headers }); } catch (error) { return jsonError(error instanceof Error ? error.message : "Unable to start the download.", 503); } }