const BASE_URL = '' class ApiClient { async request(url: string, options: RequestInit = {}): Promise { const headers = new Headers(options.headers || {}) const isFormData = options.body instanceof FormData if (!isFormData && !headers.has('Content-Type')) { headers.set('Content-Type', 'application/json') } const response = await fetch(`${BASE_URL}${url}`, { ...options, credentials: 'include', headers, }) if (!response.ok) { const error = await response.json().catch(() => ({ detail: '请求失败' })) throw new Error(error.detail || '请求失败') } if (response.status === 204 || response.status === 205) { return undefined as T } const contentType = response.headers.get('content-type') || '' if (!contentType.includes('application/json')) { return undefined as T } return response.json() } get(url: string): Promise { return this.request(url) } post(url: string, data?: unknown): Promise { return this.request(url, { method: 'POST', body: data ? JSON.stringify(data) : undefined, }) } put(url: string, data?: unknown): Promise { return this.request(url, { method: 'PUT', body: data ? JSON.stringify(data) : undefined, }) } delete(url: string): Promise { return this.request(url, { method: 'DELETE' }) } } export const api = new ApiClient()