Spaces:
Sleeping
Sleeping
File size: 1,542 Bytes
b63786a 4a22ca2 b63786a 4a22ca2 b63786a 4a22ca2 b63786a aa58194 b221019 b63786a | 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 | const trimTrailingSlash = (value = '') => value.replace(/\/$/, '');
const getDefaultBaseUrl = () => {
if (typeof window === 'undefined') {
return 'http://localhost:5000';
}
const { hostname } = window.location;
const isLocalDevelopment = hostname === 'localhost' || hostname === '127.0.0.1';
return isLocalDevelopment ? 'http://localhost:5000' : window.location.origin;
};
const rawBaseUrl = trimTrailingSlash(process.env.REACT_APP_API_URL || getDefaultBaseUrl());
const API_BASE_URL = rawBaseUrl.endsWith('/api') ? rawBaseUrl.slice(0, -4) : rawBaseUrl;
export const apiUrl = (path) => `${API_BASE_URL}${path}`;
export const getJson = async (path, options = {}) => {
try {
const response = await fetch(apiUrl(path), options);
if (!response.ok) {
const data = await response.json().catch(() => ({}));
const err = new Error(data.error || 'Request failed');
err.status = response.status;
throw err;
}
return await response.json();
} catch (err) {
if (err.message === 'Failed to fetch' || err.name === 'TypeError') {
const serverInfo = process.env.REACT_APP_API_URL
? `API URL: ${process.env.REACT_APP_API_URL}`
: `Expected backend at: ${apiUrl('')}`;
const message = `Cannot connect to server.\n\nPlease ensure the backend server is running.\n\n${serverInfo}`;
const friendlyErr = new Error(message);
friendlyErr.status = err.status || 0;
throw friendlyErr;
}
throw err;
}
};
|