From 2af0981a063b5d8aff155e4c03d7b3ece50f8afc Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 27 Nov 2024 07:50:51 +0200 Subject: [PATCH] Add proxy request route for bulk_create function --- .../api/[entity]/bulk_create/+server.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/routes/api/[entity]/bulk_create/+server.ts diff --git a/src/routes/api/[entity]/bulk_create/+server.ts b/src/routes/api/[entity]/bulk_create/+server.ts new file mode 100644 index 0000000..751db82 --- /dev/null +++ b/src/routes/api/[entity]/bulk_create/+server.ts @@ -0,0 +1,29 @@ +import type { ErrorMessage } from "$lib/api"; +import { EntityTypes, type EntityType, type EntityName, bulk_create } 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, params }): Promise => { + const session = cookies.get('session') + const { entity } = params; + + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + + if (!(entity in EntityTypes)) { + return new Response(JSON.stringify("Invalid entity"), { status: 400 }); + } + + const entityName = entity as EntityName; + const data: EntityType = await request.json(); + + const result = await bulk_create>(entityName, data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +}