Spaces:
Paused
Paused
| /** | |
| * 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<string> { | |
| 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<User> { | |
| const { data } = await api.post<User>("/auth/register", { | |
| email, | |
| password, | |
| display_name: displayName, | |
| role, | |
| }); | |
| return data; | |
| } | |
| export async function getMe(): Promise<User> { | |
| const { data } = await api.get<User>("/auth/me"); | |
| return data; | |
| } | |
| export function logout(): void { | |
| localStorage.removeItem("token"); | |
| } | |
| // ββ Query βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export async function submitQuery( | |
| query: string, | |
| includeVisualization = false, | |
| ): Promise<QueryResponse> { | |
| const { data } = await api.post<QueryResponse>("/query", { | |
| query, | |
| include_visualization: includeVisualization, | |
| }); | |
| return data; | |
| } | |
| export async function getQueryHistory( | |
| limit = 20, | |
| offset = 0 | |
| ): Promise<QueryHistoryItem[]> { | |
| const { data } = await api.get<QueryHistoryItem[]>("/query/history", { | |
| params: { limit, offset }, | |
| }); | |
| return data; | |
| } | |
| export default api; | |