Spaces:
Runtime error
Runtime error
Update frontend/src/lib/api.ts
Browse filesgit add frontend/src/lib/api.ts
git commit -m "Use same-origin API base for HF Space"
git push hf HEAD:main --force
- frontend/src/lib/api.ts +15 -23
frontend/src/lib/api.ts
CHANGED
|
@@ -1,31 +1,23 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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 |
}
|