From 54b2032348ac00fa78a92605899606a4d74b76c7 Mon Sep 17 00:00:00 2001 From: "Dennis (via Claude+Gemma)" Date: Sat, 23 May 2026 04:26:54 +0200 Subject: [PATCH] gemma: generate apps/web/src/lib/api.ts --- apps/web/src/lib/api.ts | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 apps/web/src/lib/api.ts diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts new file mode 100644 index 0000000..b39e51f --- /dev/null +++ b/apps/web/src/lib/api.ts @@ -0,0 +1,65 @@ +import { z } from "@rmpks/shared" + +const API_BASE = "/api" + +async function request(endpoint: string, options: RequestInit = {}): Promise { + const token = localStorage.getItem("auth_token") + const headers = new Headers(options.headers) + + if (token) { + headers.set("Authorization", `Bearer ${token}`) + } + + headers.set("Content-Type", "application/json") + + const response = await fetch(`${API_BASE}${endpoint}`, { + ...options, + headers, + credentials: "include" + }) + + if (!response.ok) { + const errorData = await response.json().catch(() => ({})) + throw new Error(errorData.message || `API Error: ${response.status}`) + } + + return response.json() +} + +export const api = { + async login(email: string, password: string) { + const data = await request<{ token: string }>("/auth/login", { + method: "POST", + body: JSON.stringify({ email, password }) + }) + localStorage.setItem("auth_token", data.token) + return data + }, + + logout() { + localStorage.removeItem("auth_token") + }, + + async getMe() { + return request("/auth/me") + }, + + async listTimeEntries(opts?: Record) { + const query = new URLSearchParams(opts).toString() + const endpoint = query ? `/time-entries?${query}` : "/time-entries" + return request(`/time-entries${query ? `?${query}` : ""}`) + }, + + async createTimeEntry(data: z.infer) { + return request("/time-entries", { + method: "POST", + body: JSON.stringify(data) + }) + }, + + async deleteTimeEntry(id: string) { + return request(`/time-entries/${id}`, { + method: "DELETE" + }) + } +} \ No newline at end of file