From 7fabc142bc0dd193592f241f1b294cd7e4012c39 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Fri, 29 Nov 2024 21:00:47 +0200 Subject: [PATCH] Add get_stats_for function to $lib/entities --- src/lib/entities.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib/entities.ts b/src/lib/entities.ts index 615e8b6..20a6f11 100644 --- a/src/lib/entities.ts +++ b/src/lib/entities.ts @@ -373,3 +373,31 @@ export async function filter(groupName: string, data: F, session?: string) return { message: error.message }; } } + +export async function get_stats_for(groupName: string, session?: string): Promise { + const url = `${BASE_API_URL}/statistics/${groupName}` + 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 }; + } +}