From a95dda907b256c820f49f3bf6b78638f343e4908 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 30 Nov 2024 16:40:01 +0200 Subject: [PATCH] Add /api/settings/[entity]/ 'all' and 'update' proxy api routes --- .../api/settings/[entity]/all/+server.ts | 31 +++++++++++++++++++ .../api/settings/[entity]/update/+server.ts | 28 +++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/routes/api/settings/[entity]/all/+server.ts create mode 100644 src/routes/api/settings/[entity]/update/+server.ts diff --git a/src/routes/api/settings/[entity]/all/+server.ts b/src/routes/api/settings/[entity]/all/+server.ts new file mode 100644 index 0000000..90fe1bd --- /dev/null +++ b/src/routes/api/settings/[entity]/all/+server.ts @@ -0,0 +1,31 @@ +import type { ErrorMessage } from "$lib/api"; +import { get_settings_for, type SettingsType, type SettingsName, SettingsTypes } 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, url }): Promise => { + const session = cookies.get('session'); + const { entity } = params; + + const queryParams = url.searchParams.toString(); + if (!session) { + return new Response(JSON.stringify("no cookies"), { status: 401 }); + } + + if (!(entity in SettingsTypes)) { + return new Response(JSON.stringify("Invalid entity"), { status: 400 }); + } + + const entityName = entity as SettingsName + const result = await get_settings_for>(entityName, session, queryParams); + + 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/settings/[entity]/update/+server.ts b/src/routes/api/settings/[entity]/update/+server.ts new file mode 100644 index 0000000..c7f05c4 --- /dev/null +++ b/src/routes/api/settings/[entity]/update/+server.ts @@ -0,0 +1,28 @@ +import type { ErrorMessage } from "$lib/api"; +import { SettingsTypes, type SettingsName, type SettingsType, update_settings_for } 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 SettingsTypes)) { + return new Response(JSON.stringify("Invalid entity"), { status: 400 }); + } + + const entityName = entity as SettingsName; + const data: SettingsType = await request.json(); + const result = await update_settings_for>(entityName, data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +}