/** * Thin Axios wrapper around the Agentic-RAG API. * The JWT token is stored in localStorage and injected via interceptor. */ import axios from "axios"; import type { QueryHistoryItem, QueryResponse, User } from "../types"; const api = axios.create({ baseURL: "/api/v1", headers: { "Content-Type": "application/json" }, }); // Inject auth token on every request api.interceptors.request.use((config) => { const token = localStorage.getItem("token"); if (token) config.headers.Authorization = `Bearer ${token}`; return config; }); // ── Auth ────────────────────────────────────────────────────────────────────── export async function login(email: string, password: string): Promise { const form = new URLSearchParams({ username: email, password }); const { data } = await api.post<{ access_token: string }>("/auth/login", form, { headers: { "Content-Type": "application/x-www-form-urlencoded" }, }); localStorage.setItem("token", data.access_token); return data.access_token; } export async function register( email: string, password: string, displayName?: string, role = "guest" ): Promise { const { data } = await api.post("/auth/register", { email, password, display_name: displayName, role, }); return data; } export async function getMe(): Promise { const { data } = await api.get("/auth/me"); return data; } export function logout(): void { localStorage.removeItem("token"); } // ── Query ───────────────────────────────────────────────────────────────────── export async function submitQuery( query: string, includeVisualization = false, ): Promise { const { data } = await api.post("/query", { query, include_visualization: includeVisualization, }); return data; } export async function getQueryHistory( limit = 20, offset = 0 ): Promise { const { data } = await api.get("/query/history", { params: { limit, offset }, }); return data; } export default api;