import { userIdentity } from "$lib/Data/AniList/identity"; import { decodeAuthCookieOrThrow } from "$lib/Effect/authCookie"; import { decodeRequestJsonOrThrow } from "$lib/Effect/requestBody"; import { deleteUserConfiguration, getUserConfiguration, setUserConfiguration, } from "$lib/Database/SB/User/configuration"; import { Schema } from "effect"; const unauthorised = new Response("Unauthorised", { status: 401 }); export const GET = async ({ url }) => Response.json( await getUserConfiguration(Number(url.searchParams.get("id") || 0)), { headers: { "Access-Control-Allow-Origin": "https://due.moe", }, }, ); export const PUT = async ({ cookies, request }) => { const userCookie = cookies.get("user"); if (!userCookie) return unauthorised; const user = decodeAuthCookieOrThrow(userCookie); return Response.json( await setUserConfiguration((await userIdentity(user)).id, { configuration: await decodeRequestJsonOrThrow( request, Schema.Record(Schema.String, Schema.Unknown), ), }), { headers: { "Access-Control-Allow-Origin": "https://due.moe", }, }, ); }; export const DELETE = async ({ cookies }) => { const userCookie = cookies.get("user"); if (!userCookie) return unauthorised; const user = decodeAuthCookieOrThrow(userCookie); return Response.json( await deleteUserConfiguration((await userIdentity(user)).id), { headers: { "Access-Control-Allow-Origin": "https://due.moe", }, }, ); };