// ============================================================ // HTTP API 客户端 // 职责:封装 REST API 请求(auth、conversations 等) // ============================================================ const API_BASE = "/api"; interface ApiResponse { data?: T; error?: { code: string; message: string }; status: number; } async function request( path: string, options: RequestInit = {} ): Promise> { const url = `${API_BASE}${path}`; const headers: Record = { "Content-Type": "application/json", ...(options.headers as Record), }; try { const res = await fetch(url, { ...options, headers }); const status = res.status; if (res.status === 204) { return { status }; } const body = await res.json(); if (!res.ok) { return { error: { code: body.error || "UNKNOWN", message: body.message || "请求失败" }, status, }; } return { data: body as T, status }; } catch (err) { return { error: { code: "NETWORK_ERROR", message: "网络连接失败,请检查网络" }, status: 0, }; } } function authHeaders(accessToken: string): Record { return { Authorization: `Bearer ${accessToken}` }; } // ---- Auth API ---- export interface AuthUser { id: string; username: string; created_at: string; } export interface AuthResponse { user: AuthUser; access_token: string; refresh_token: string; } export async function register( username: string, password: string ): Promise> { return request("/auth/register", { method: "POST", body: JSON.stringify({ username, password }), }); } export async function login( username: string, password: string ): Promise> { return request("/auth/login", { method: "POST", body: JSON.stringify({ username, password }), }); } export async function refreshToken( refresh_token: string ): Promise> { return request("/auth/refresh", { method: "POST", body: JSON.stringify({ refresh_token }), }); } export async function logout( accessToken: string, refreshToken: string ): Promise> { return request<{ message: string }>("/auth/logout", { method: "POST", headers: authHeaders(accessToken), body: JSON.stringify({ refresh_token: refreshToken }), }); }