Files

22 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2026-09-25 23:20:57 -05:00
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);
}
}