Add /api/settings/[entity]/ 'all' and 'update' proxy api routes
This commit is contained in:
parent
8d77d581f2
commit
a95dda907b
31
src/routes/api/settings/[entity]/all/+server.ts
Normal file
31
src/routes/api/settings/[entity]/all/+server.ts
Normal 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 });
|
||||
}
|
||||
|
28
src/routes/api/settings/[entity]/update/+server.ts
Normal file
28
src/routes/api/settings/[entity]/update/+server.ts
Normal 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 })
|
||||
}
|
Loading…
Reference in New Issue
Block a user