Add /api/settings/[entity]/ 'all' and 'update' proxy api routes

This commit is contained in:
qowevisa 2024-11-30 16:40:01 +02:00
parent 8d77d581f2
commit a95dda907b
2 changed files with 59 additions and 0 deletions

View File

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

View File

@ -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<Response> => {
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<typeof entityName> = await request.json();
const result = await update_settings_for<SettingsType<typeof entityName>>(entityName, data, session);
if (isErrorMessage(result)) {
return new Response(JSON.stringify(result), { status: 500 })
}
return new Response(JSON.stringify(result), { status: 200 })
}