From a1560304d46ea2437059926542b20d6de9adf4b9 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 20 Nov 2024 18:23:23 +0200 Subject: [PATCH] Add api/item/filter proxy request --- src/routes/api/item/filter/+server.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/routes/api/item/filter/+server.ts diff --git a/src/routes/api/item/filter/+server.ts b/src/routes/api/item/filter/+server.ts new file mode 100644 index 0000000..e20fdd1 --- /dev/null +++ b/src/routes/api/item/filter/+server.ts @@ -0,0 +1,23 @@ +import type { ErrorMessage } from "$lib/api"; +import { filter, type ItemFilter, type Item } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const POST: RequestHandler = async ({ request, cookies }): Promise => { + const session = cookies.get('session') + + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + + const data: ItemFilter = await request.json(); + const result = await filter("item", data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} +