Updated API for production

This commit is contained in:
2026-02-22 21:33:09 +01:00 Verified
parent 76c498517f
commit 1377a97398
+8 -8
View File
@@ -2,14 +2,11 @@
const getBackendHost = () => { const getBackendHost = () => {
const host = window.location.hostname || 'localhost'; const host = window.location.hostname || 'localhost';
return host; return window.location.protocol === 'https:' ? host : `${host}:8000`;
}; };
const port = '8000'; export const API_BASE = `${window.location.protocol}//${getBackendHost()}/api`;
export const WS_URL = `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${getBackendHost()}/api/ws`;
export const API_BASE = `${window.location.protocol}//${getBackendHost()}:${port}/api`;
export const WS_URL = `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${getBackendHost()}:${port}/api/ws`;
export const getToken = () => localStorage.getItem('volleyToken'); export const getToken = () => localStorage.getItem('volleyToken');
@@ -24,16 +21,19 @@ const api = {
if (data) opts.body = isFormData ? data : JSON.stringify(data); if (data) opts.body = isFormData ? data : JSON.stringify(data);
// Ensure clean URL concatenation // Ensure clean URL concatenation
const baseUrl = API_BASE.endsWith('/') ? API_BASE.slice(0, -1) : API_BASE; const baseUrl = API_BASE.replace(/\/$/, '');
const endpoint = url.startsWith('/') ? url : `/${url}`; const endpoint = url.startsWith('/') ? url : `/${url}`;
const res = await fetch(`${baseUrl}${endpoint}`, opts); const res = await fetch(`${baseUrl}${endpoint}`, opts);
if (!res.ok) { if (!res.ok) {
if (res.status === 401) { if (res.status === 401) {
localStorage.removeItem('volleyToken'); localStorage.removeItem('volleyToken');
if (window.location.pathname !== '/login') {
window.location.href = '/login'; window.location.href = '/login';
} }
throw await res.json(); }
const errorData = await res.json().catch(() => ({ detail: 'An error occurred' }));
throw errorData;
} }
return res.json(); return res.json();
}, },