/api/mek/list, /api/mek/register Endpoint 구현

This commit is contained in:
static
2024-12-29 21:52:33 +09:00
parent 3664ad66ac
commit 97f6e1e32f
6 changed files with 186 additions and 24 deletions

View File

@@ -0,0 +1,14 @@
import { error, json } from "@sveltejs/kit";
import { authenticate } from "$lib/server/modules/auth";
import { getClientMekList } from "$lib/server/services/mek";
import type { RequestHandler } from "@sveltejs/kit";
export const GET: RequestHandler = async ({ cookies }) => {
const { userId, clientId } = authenticate(cookies);
if (!clientId) {
error(403, "Forbidden");
}
const { meks } = await getClientMekList(userId, clientId);
return json({ meks });
};

View File

@@ -0,0 +1,35 @@
import { error, text } from "@sveltejs/kit";
import { z } from "zod";
import { authenticate } from "$lib/server/modules/auth";
import { registerNewActiveMek } from "$lib/server/services/mek";
import type { RequestHandler } from "@sveltejs/kit";
export const POST: RequestHandler = async ({ request, cookies }) => {
const zodRes = z
.object({
meks: z.array(
z.object({
clientId: z.number(),
mek: z.string().base64().nonempty(),
}),
),
})
.safeParse(await request.json());
if (!zodRes.success) error(400, "Invalid request body");
const { userId, clientId } = authenticate(cookies);
if (!clientId) {
error(403, "Forbidden");
}
const { meks } = zodRes.data;
await registerNewActiveMek(
userId,
clientId,
meks.map(({ clientId, mek }) => ({
clientId,
encMek: mek.trim(),
})),
);
return text("MEK registered", { headers: { "Content-Type": "text/plain" } });
};