Build [v1.8.0]
This commit is contained in:
@@ -1,20 +1,48 @@
|
||||
import axios from 'axios';
|
||||
import { getToken, clearAuth } from './auth';
|
||||
|
||||
export const getBackendUrl = () => {
|
||||
if (typeof window === 'undefined') return 'http://localhost:8000';
|
||||
// Cached config to avoid repeated fetches
|
||||
let cachedConfig: any = null;
|
||||
|
||||
/**
|
||||
* Fetches the network configuration from the public/network.json file.
|
||||
* This file is generated at startup by start_server.sh or docker-compose.
|
||||
*/
|
||||
export const getNetworkConfig = async () => {
|
||||
if (cachedConfig) return cachedConfig;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return { SERVER_IP: 'localhost', BACKEND_PORT: 8000, BACKEND_SSL_PORT: 8908 };
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/network.json');
|
||||
if (!response.ok) throw new Error("Config not found");
|
||||
cachedConfig = await response.json();
|
||||
return cachedConfig;
|
||||
} catch (e) {
|
||||
console.warn("Network config not found, using compiled defaults.");
|
||||
// Defaults matching the initial reserve ports in case network.json is missing
|
||||
return { SERVER_IP: 'localhost', BACKEND_PORT: 8906, BACKEND_SSL_PORT: 8908 };
|
||||
}
|
||||
};
|
||||
|
||||
export const getBackendUrl = async () => {
|
||||
const config = await getNetworkConfig();
|
||||
|
||||
if (typeof window === 'undefined') return `http://localhost:${config.BACKEND_PORT}`;
|
||||
|
||||
const host = window.location.hostname;
|
||||
|
||||
// If we are on HTTPS (Proxy/Mobile mode), we use port 3002 for the backend
|
||||
// If we are on HTTPS (Proxy/Mobile mode), we use the SSL port for the backend
|
||||
if (window.location.protocol === 'https:') {
|
||||
if (host.includes('.loca.lt')) {
|
||||
return 'https://inventory-ai-api.loca.lt';
|
||||
}
|
||||
return `https://${host}:3002`;
|
||||
return `https://${host}:${config.BACKEND_SSL_PORT}`;
|
||||
}
|
||||
|
||||
return `http://${host}:8000`;
|
||||
return `http://${host}:${config.BACKEND_PORT}`;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -23,9 +51,9 @@ export const getBackendUrl = () => {
|
||||
*/
|
||||
const axiosInstance = axios.create({});
|
||||
|
||||
axiosInstance.interceptors.request.use((config) => {
|
||||
axiosInstance.interceptors.request.use(async (config) => {
|
||||
if (!config.baseURL) {
|
||||
config.baseURL = getBackendUrl(); // called at request time — always correct
|
||||
config.baseURL = await getBackendUrl(); // called at request time — always correct
|
||||
}
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
@@ -110,7 +138,8 @@ export const inventoryApi = {
|
||||
// Users
|
||||
getUsers: async () => {
|
||||
// [C-01] Public endpoint — use plain axios to avoid JWT interceptor
|
||||
const res = await axios.get(`${getBackendUrl()}/users/`);
|
||||
const baseUrl = await getBackendUrl();
|
||||
const res = await axios.get(`${baseUrl}/users/`);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
@@ -121,7 +150,8 @@ export const inventoryApi = {
|
||||
|
||||
login: async (credentials: any) => {
|
||||
// [C-01] Login endpoint — NU adaug token header (login e public)
|
||||
const res = await axios.post(`${getBackendUrl()}/users/login`, credentials);
|
||||
const baseUrl = await getBackendUrl();
|
||||
const res = await axios.post(`${baseUrl}/users/login`, credentials);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user