import type { FileResponse } from '../types' const API_BASE = '/api' async function handleResponse(response: Response): Promise { if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Unknown error' })) throw new Error(error.error || `HTTP ${response.status}`) } if (response.status === 204) { return null as T } return response.json() } export const api = { async listFiles(): Promise { const response = await fetch(`${API_BASE}/files`) return handleResponse(response) }, async getFile(name: string): Promise { const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`) return handleResponse(response) }, async createFile(name: string, content: string): Promise { const response = await fetch(`${API_BASE}/files`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, content }), }) return handleResponse(response) }, async updateFile(name: string, content: string): Promise { const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content }), }) return handleResponse(response) }, async deleteFile(name: string): Promise { const response = await fetch(`${API_BASE}/files/${encodeURIComponent(name)}`, { method: 'DELETE', }) return handleResponse(response) }, }