From 8d77d581f2beccec912c126976087b8434477e30 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 30 Nov 2024 16:38:46 +0200 Subject: [PATCH] Add get_settings_for and update_settings_for function to $lib/entities.ts --- src/lib/entities.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/lib/entities.ts b/src/lib/entities.ts index 3440c60..f8fd112 100644 --- a/src/lib/entities.ts +++ b/src/lib/entities.ts @@ -423,3 +423,64 @@ export async function get_stats_for(groupName: string, session?: string): Pro return { message: error.message }; } } + +// {{{ Settings functions + +export async function get_settings_for(groupName: string, session?: string, queryParams?: string): Promise { + const url = queryParams ? `${BASE_API_URL}/settings/${groupName}/all1${queryParams}` : `${BASE_API_URL}/settings/${groupName}/all` + const defaultHeaders = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + method: 'GET', + headers, + }; + + try { + const response = await fetch(url, config); + if (!response.ok) { + const body = await response.json() + throw new Error(`Failed to update ${groupName}: ${body.message}`); + } + return await response.json() as R; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + +export async function update_settings_for(groupName: string, data: T, session?: string): Promise { + const url = `${BASE_API_URL}/settings/${groupName}/update` + const defaultHeaders = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + method: 'PUT', + headers, + body: JSON.stringify(data) + }; + + try { + const response = await fetch(url, config); + if (!response.ok) { + const body = await response.json() + throw new Error(`Failed to update ${groupName}: ${body.message}`); + } + return await response.json() as T; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + +// }}}