Add proxy request route for bulk_create function
This commit is contained in:
parent
afc7f1b8ff
commit
2af0981a06
29
src/routes/api/[entity]/bulk_create/+server.ts
Normal file
29
src/routes/api/[entity]/bulk_create/+server.ts
Normal file
|
@ -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<Response> => {
|
||||
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<typeof entityName> = await request.json();
|
||||
|
||||
const result = await bulk_create<EntityType<typeof entityName>>(entityName, data, session);
|
||||
if (isErrorMessage(result)) {
|
||||
return new Response(JSON.stringify(result), { status: 500 })
|
||||
}
|
||||
return new Response(JSON.stringify(result), { status: 200 })
|
||||
}
|
Loading…
Reference in New Issue
Block a user