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 }) +} +