Spaces:
Sleeping
Sleeping
File size: 1,147 Bytes
b64de39 fb6ccf7 b64de39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /**
* Axios instance for FarmHelp API.
*
* In development, requests to /api/* are proxied to the backend by Vite.
* Uses relative URLs so the same origin gets the proxy.
*/
import axios from "axios";
import { API_BASE_URL, API_TIMEOUT_MS } from "@utils/constants";
const isDev = import.meta.env.DEV;
const baseURL = isDev ? "" : API_BASE_URL;
const api = axios.create({
baseURL,
timeout: API_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
api.interceptors.response.use(
(response) => response,
(error) => {
const message =
error.response?.data?.detail ||
error.message ||
"Request failed";
return Promise.reject(new Error(typeof message === "string" ? message : JSON.stringify(message)));
}
);
/** Base path for v1 API (used when not using proxy, e.g. in production). */
export const API_V1 = "/api/v1";
/** @param {string} path - Path starting with /api/v1/... */
export function apiUrl(path) {
if (path.startsWith("http")) return path;
if (isDev || !API_BASE_URL) return path;
return `${API_BASE_URL}${path}`;
}
export default api;
|