diff --git a/src/routes/api/statistics/type/+server.ts b/src/routes/api/statistics/type/+server.ts new file mode 100644 index 0000000..80cb07c --- /dev/null +++ b/src/routes/api/statistics/type/+server.ts @@ -0,0 +1,27 @@ +import type { ErrorMessage } from "$lib/api"; +import { get_stats_for, type StatsTypeCurrencyChart } 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 }): Promise => { + const session = cookies.get('session'); + + // const queryParams = url.searchParams.toString(); + // Check if the entity is valid + if (!session) { + return new Response(JSON.stringify("no cookies"), { status: 401 }); + } + + // TypeScript type inference for entity + const result = await get_stats_for("type", session); + + if (isErrorMessage(result)) { + console.log("ERROR"); + return new Response(JSON.stringify(result), { status: 500 }); + } + + return new Response(JSON.stringify(result), { status: 200 }); +}