MetiMiester commited on
Commit
a77382d
·
verified ·
1 Parent(s): aaef3a5

Update frontend/src/lib/api.ts

Browse files

git add frontend/src/lib/api.ts
git commit -m "Use same-origin API base for HF Space"
git push hf HEAD:main --force

Files changed (1) hide show
  1. frontend/src/lib/api.ts +15 -23
frontend/src/lib/api.ts CHANGED
@@ -1,31 +1,23 @@
1
- import axios from "axios";
 
2
 
3
- function resolveBase() {
4
- // Prefer Vite env; fallback to localhost:4000 for dev
5
- const env = import.meta.env?.VITE_API_BASE?.trim();
6
- if (env) return env.replace(/\/+$/, "");
7
- const proto = window.location.protocol === "https:" ? "https:" : "http:";
8
- return `${proto}//localhost:4000`;
9
- }
10
-
11
- export const API_BASE = resolveBase();
12
 
13
  export const api = axios.create({
14
  baseURL: `${API_BASE}/api`,
15
- headers: { "Content-Type": "application/json" },
16
  withCredentials: false,
 
17
  });
18
 
19
- api.interceptors.response.use(
20
- (r) => r,
21
- (err) => {
22
- console.error("API error:", err?.response?.data || err?.message || err);
23
- return Promise.reject(err);
24
- }
25
- );
26
-
27
- // Optional debug (comment out later)
28
- if (typeof window !== "undefined") {
29
- // eslint-disable-next-line no-console
30
- console.log("[api] API_BASE =", API_BASE);
31
  }
 
1
+ // frontend/src/lib/api.ts
2
+ import axios from "axios";
3
 
4
+ /**
5
+ * Compute API base. On HF Spaces we want same-origin (no host at all).
6
+ * Locally (vite dev) you can set VITE_API_BASE=http://localhost:4000
7
+ * but if it's empty we default to same-origin.
8
+ */
9
+ const envBase = (import.meta.env.VITE_API_BASE ?? "").trim();
10
+ export const API_BASE =
11
+ envBase !== "" ? envBase.replace(/\/$/, "") : ""; // "" = same origin
 
12
 
13
  export const api = axios.create({
14
  baseURL: `${API_BASE}/api`,
 
15
  withCredentials: false,
16
+ headers: { "Content-Type": "application/json" },
17
  });
18
 
19
+ // Optional helper
20
+ export async function health(): Promise<{ ok: boolean; db: string }> {
21
+ const res = await fetch(`${API_BASE}/health`, { cache: "no-store" });
22
+ return res.json();
 
 
 
 
 
 
 
 
23
  }