Refactored cards proxy requests as generic proxy params URI requests
This commit is contained in:
parent
19424266ae
commit
df3bd83f70
|
@ -20,6 +20,14 @@ export interface Card {
|
||||||
name: string;
|
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;
|
||||||
|
|
||||||
//
|
//
|
||||||
// }}}
|
// }}}
|
||||||
//
|
//
|
||||||
|
|
32
src/routes/api/[entity]/all/+server.ts
Normal file
32
src/routes/api/[entity]/all/+server.ts
Normal 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 });
|
||||||
|
}
|
29
src/routes/api/[entity]/create/+server.ts
Normal file
29
src/routes/api/[entity]/create/+server.ts
Normal 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 })
|
||||||
|
}
|
|
@ -1,18 +1,25 @@
|
||||||
import type { ErrorMessage } from "$lib/api";
|
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";
|
import type { RequestHandler } from "./$types";
|
||||||
|
|
||||||
function isErrorMessage(value: any): value is ErrorMessage {
|
function isErrorMessage(value: any): value is ErrorMessage {
|
||||||
return value && typeof value.message === 'string';
|
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 session = cookies.get('session')
|
||||||
|
const { entity } = params;
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
throw new Response(JSON.stringify("no cookies"), { status: 401 })
|
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 { 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)) {
|
if (isErrorMessage(result)) {
|
||||||
return new Response(JSON.stringify(result), { status: 500 })
|
return new Response(JSON.stringify(result), { status: 500 })
|
||||||
}
|
}
|
28
src/routes/api/[entity]/update/+server.ts
Normal file
28
src/routes/api/[entity]/update/+server.ts
Normal 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 })
|
||||||
|
}
|
|
@ -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 })
|
|
||||||
}
|
|
|
@ -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 })
|
|
||||||
}
|
|
|
@ -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 })
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user