File size: 743 Bytes
a77382d
 
8b8b6f5
a77382d
 
 
 
 
 
 
 
8b8b6f5
 
 
 
a77382d
8b8b6f5
 
a77382d
 
 
 
8b8b6f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// frontend/src/lib/api.ts
import axios from "axios";

/**
 * Compute API base. On HF Spaces we want same-origin (no host at all).
 * Locally (vite dev) you can set VITE_API_BASE=http://localhost:4000
 * but if it's empty we default to same-origin.
 */
const envBase = (import.meta.env.VITE_API_BASE ?? "").trim();
export const API_BASE =
  envBase !== "" ? envBase.replace(/\/$/, "") : ""; // "" = same origin

export const api = axios.create({
  baseURL: `${API_BASE}/api`,
  withCredentials: false,
  headers: { "Content-Type": "application/json" },
});

// Optional helper
export async function health(): Promise<{ ok: boolean; db: string }> {
  const res = await fetch(`${API_BASE}/health`, { cache: "no-store" });
  return res.json();
}