Refactored cards proxy requests as generic proxy params URI requests

This commit is contained in:
qowevisa 2024-11-10 11:22:03 +02:00
parent 19424266ae
commit df3bd83f70
8 changed files with 107 additions and 64 deletions

View File

@ -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 EntityName> = T extends "card" ? Card : Type;
//
// }}}
//

View File

@ -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<Response> => {
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<EntityType<typeof entityName>>(entityName, session);
if (isErrorMessage(result)) {
console.log("ERROR");
return new Response(JSON.stringify(result), { status: 500 });
}
return new Response(JSON.stringify(result), { status: 200 });
}

View File

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

View File

@ -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<Response> => {
export const DELETE: 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 { id }: { id: number } = await request.json();
const result = await remove<Card>("card", id, session);
const result = await remove(entity as EntityName, id, session);
if (isErrorMessage(result)) {
return new Response(JSON.stringify(result), { status: 500 })
}

View File

@ -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<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 update<EntityType<typeof entityName>>(entityName, data.id, data, session);
if (isErrorMessage(result)) {
return new Response(JSON.stringify(result), { status: 500 })
}
return new Response(JSON.stringify(result), { status: 200 })
}

View File

@ -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<Response> => {
const session = cookies.get('session')
if (!session) {
throw new Response(JSON.stringify("no cookies"), { status: 401 })
}
const result = await getAll<Card>("card", session);
if (isErrorMessage(result)) {
console.log("ERROR")
return new Response(JSON.stringify(result), { status: 500 })
}
return new Response(JSON.stringify(result), { status: 200 })
}

View File

@ -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<Response> => {
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>("card", data, session);
if (isErrorMessage(result)) {
return new Response(JSON.stringify(result), { status: 500 })
}
return new Response(JSON.stringify(result), { status: 200 })
}

View File

@ -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<Response> => {
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>("card", data.id, data, session);
if (isErrorMessage(result)) {
return new Response(JSON.stringify(result), { status: 500 })
}
return new Response(JSON.stringify(result), { status: 200 })
}