/** * API client for integration tests. * Used for direct backend calls: DB verification, test reset, data assertions. */ const API_URL = process.env.API_URL || "http://localhost:8080" export class ApiClient { private token: string | null = null async login(email: string, password: string): Promise { const res = await fetch(`${API_URL}/api/v1/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }), }) if (!res.ok) throw new Error(`Login failed: ${res.status}`) const data = await res.json() this.token = data.token } async resetDb(): Promise { const res = await fetch(`${API_URL}/api/v1/test/reset-db`, { method: "POST", headers: this.authHeaders(), }) if (!res.ok) throw new Error(`DB reset failed: ${res.status}`) } async getMembers(): Promise { return this.get("/api/v1/members") } async getDocuments(): Promise { return this.get("/api/v1/documents") } async getBatches(): Promise { return this.get("/api/v1/batches") } async getDistributions(): Promise { return this.get("/api/v1/distributions") } async getBoardPositions(): Promise { return this.get("/api/v1/board") } private authHeaders(): Record { const headers: Record = { "Content-Type": "application/json", } if (this.token) headers["Authorization"] = `Bearer ${this.token}` return headers } private async get(path: string): Promise { const res = await fetch(`${API_URL}${path}`, { headers: this.authHeaders(), }) if (!res.ok) throw new Error(`GET ${path} failed: ${res.status}`) return res.json() } private async post(path: string, body?: unknown): Promise { const res = await fetch(`${API_URL}${path}`, { method: "POST", headers: this.authHeaders(), body: body ? JSON.stringify(body) : undefined, }) if (!res.ok) throw new Error(`POST ${path} failed: ${res.status}`) return res.json() } }