Spaces:
Running
Running
| import type { RequestHandler } from "@sveltejs/kit"; | |
| import { getApiEndpoint, ApiEndpoints } from "$lib/config"; | |
| export const POST: RequestHandler = async ({ request }) => { | |
| const authHeader = request.headers.get("authorization"); | |
| if (!authHeader || !authHeader.startsWith("Bearer ")) { | |
| return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }); | |
| } | |
| const accessToken = authHeader.substring("Bearer ".length); | |
| if (!accessToken || accessToken === "null" || accessToken === "undefined") { | |
| return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }); | |
| } | |
| const payload = await request.json(); | |
| payload.access_token = accessToken; | |
| const url = getApiEndpoint(ApiEndpoints.VOTE); | |
| try { | |
| const response = await fetch(url, { | |
| method: "POST", | |
| headers: { | |
| Authorization: `Bearer ${import.meta.env.VITE_HF_TOKEN}`, | |
| "Cache-Control": "no-cache", | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify(payload), | |
| }); | |
| if (response.ok) { | |
| const result = await response.json(); | |
| return new Response(JSON.stringify(result), { | |
| status: 200, | |
| }); | |
| } else { | |
| return new Response(JSON.stringify({ error: "Failed to process vote." }), { | |
| status: response.status, | |
| }); | |
| } | |
| } catch (error) { | |
| return new Response(JSON.stringify({ error: "Failed to process vote." }), { | |
| status: 500, | |
| }); | |
| } | |
| }; | |