From df3bd83f700a0dc60c5dde6c5027861a02607ea3 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sun, 10 Nov 2024 11:22:03 +0200 Subject: [PATCH] Refactored cards proxy requests as generic proxy params URI requests --- src/lib/entities.ts | 8 +++++ src/routes/api/[entity]/all/+server.ts | 32 +++++++++++++++++++ src/routes/api/[entity]/create/+server.ts | 29 +++++++++++++++++ .../api/{card => [entity]}/delete/+server.ts | 13 ++++++-- src/routes/api/[entity]/update/+server.ts | 28 ++++++++++++++++ src/routes/api/card/all/+server.ts | 21 ------------ src/routes/api/card/create/+server.ts | 20 ------------ src/routes/api/card/update/+server.ts | 20 ------------ 8 files changed, 107 insertions(+), 64 deletions(-) create mode 100644 src/routes/api/[entity]/all/+server.ts create mode 100644 src/routes/api/[entity]/create/+server.ts rename src/routes/api/{card => [entity]}/delete/+server.ts (59%) create mode 100644 src/routes/api/[entity]/update/+server.ts delete mode 100644 src/routes/api/card/all/+server.ts delete mode 100644 src/routes/api/card/create/+server.ts delete mode 100644 src/routes/api/card/update/+server.ts diff --git a/src/lib/entities.ts b/src/lib/entities.ts index 5a89471..c3d583d 100644 --- a/src/lib/entities.ts +++ b/src/lib/entities.ts @@ -20,6 +20,14 @@ export interface Card { name: string; } +export const EntityTypes = { + card: "Card", + type: "Type" +} as const; + +export type EntityName = keyof typeof EntityTypes; +export type EntityType = T extends "card" ? Card : Type; + // // }}} // diff --git a/src/routes/api/[entity]/all/+server.ts b/src/routes/api/[entity]/all/+server.ts new file mode 100644 index 0000000..d3fae14 --- /dev/null +++ b/src/routes/api/[entity]/all/+server.ts @@ -0,0 +1,32 @@ +import type { ErrorMessage } from "$lib/api"; +import { getAll, EntityTypes, type EntityType, type EntityName } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const GET: RequestHandler = async ({ cookies, params }): Promise => { + const session = cookies.get('session'); + const { entity } = params; + + // Check if the entity is valid + if (!session) { + return new Response(JSON.stringify("no cookies"), { status: 401 }); + } + + if (!(entity in EntityTypes)) { + return new Response(JSON.stringify("Invalid entity"), { status: 400 }); + } + + // TypeScript type inference for entity + const entityName = entity as EntityName + const result = await getAll>(entityName, session); + + if (isErrorMessage(result)) { + console.log("ERROR"); + return new Response(JSON.stringify(result), { status: 500 }); + } + + return new Response(JSON.stringify(result), { status: 200 }); +} diff --git a/src/routes/api/[entity]/create/+server.ts b/src/routes/api/[entity]/create/+server.ts new file mode 100644 index 0000000..6eef6e5 --- /dev/null +++ b/src/routes/api/[entity]/create/+server.ts @@ -0,0 +1,29 @@ +import type { ErrorMessage } from "$lib/api"; +import { create, EntityTypes, type EntityType, type EntityName } 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 create>(entityName, data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/api/card/delete/+server.ts b/src/routes/api/[entity]/delete/+server.ts similarity index 59% rename from src/routes/api/card/delete/+server.ts rename to src/routes/api/[entity]/delete/+server.ts index f0babc1..936fbed 100644 --- a/src/routes/api/card/delete/+server.ts +++ b/src/routes/api/[entity]/delete/+server.ts @@ -1,18 +1,25 @@ import type { ErrorMessage } from "$lib/api"; -import { remove, type Card } from "$lib/entities"; +import { remove, EntityTypes, type EntityName } from "$lib/entities"; import type { RequestHandler } from "./$types"; function isErrorMessage(value: any): value is ErrorMessage { return value && typeof value.message === 'string'; } -export const DELETE: RequestHandler = async ({ request, cookies }): Promise => { +export const DELETE: 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 { id }: { id: number } = await request.json(); - const result = await remove("card", id, session); + const result = await remove(entity as EntityName, id, session); if (isErrorMessage(result)) { return new Response(JSON.stringify(result), { status: 500 }) } diff --git a/src/routes/api/[entity]/update/+server.ts b/src/routes/api/[entity]/update/+server.ts new file mode 100644 index 0000000..3719f40 --- /dev/null +++ b/src/routes/api/[entity]/update/+server.ts @@ -0,0 +1,28 @@ +import type { ErrorMessage } from "$lib/api"; +import { update, EntityTypes, type EntityType, type EntityName } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const PUT: 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 update>(entityName, data.id, data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/api/card/all/+server.ts b/src/routes/api/card/all/+server.ts deleted file mode 100644 index 1f3c7ff..0000000 --- a/src/routes/api/card/all/+server.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { ErrorMessage } from "$lib/api"; -import { getAll, type Card } from "$lib/entities"; -import type { RequestHandler } from "./$types"; - -function isErrorMessage(value: any): value is ErrorMessage { - return value && typeof value.message === 'string'; -} - -export const GET: RequestHandler = async ({ cookies }): Promise => { - const session = cookies.get('session') - if (!session) { - throw new Response(JSON.stringify("no cookies"), { status: 401 }) - } - - const result = await getAll("card", session); - if (isErrorMessage(result)) { - console.log("ERROR") - return new Response(JSON.stringify(result), { status: 500 }) - } - return new Response(JSON.stringify(result), { status: 200 }) -} diff --git a/src/routes/api/card/create/+server.ts b/src/routes/api/card/create/+server.ts deleted file mode 100644 index fdf1eae..0000000 --- a/src/routes/api/card/create/+server.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { ErrorMessage } from "$lib/api"; -import { create, type Card } 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: Card = await request.json(); - const result = await create("card", data, session); - if (isErrorMessage(result)) { - return new Response(JSON.stringify(result), { status: 500 }) - } - return new Response(JSON.stringify(result), { status: 200 }) -} diff --git a/src/routes/api/card/update/+server.ts b/src/routes/api/card/update/+server.ts deleted file mode 100644 index 1641295..0000000 --- a/src/routes/api/card/update/+server.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { ErrorMessage } from "$lib/api"; -import { update, type Card } from "$lib/entities"; -import type { RequestHandler } from "./$types"; - -function isErrorMessage(value: any): value is ErrorMessage { - return value && typeof value.message === 'string'; -} - -export const PUT: RequestHandler = async ({ request, cookies }): Promise => { - const session = cookies.get('session') - if (!session) { - throw new Response(JSON.stringify("no cookies"), { status: 401 }) - } - const data: Card = await request.json(); - const result = await update("card", data.id, data, session); - if (isErrorMessage(result)) { - return new Response(JSON.stringify(result), { status: 500 }) - } - return new Response(JSON.stringify(result), { status: 200 }) -}