basePath wird jetzt in config.json gepflegt statt als NEXT_PUBLIC_BASE_PATH in .env.* Alle relevanten Code-Stellen lesen basePath dynamisch aus config.json Dokumentation und Beispiele in Markdown-Dateien entsprechend angepasst Erhöhte Flexibilität für Deployments ohne Rebuild
68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
/**
|
||
* Holt Statusinformationen der GIS-Bezirksstationen.
|
||
* Unterstützt dynamische Umschaltung zwischen echten und Mock-Daten.
|
||
*
|
||
* @returns {Promise<Array>} Liste mit Statis[]
|
||
* @throws {Error} bei Fehler oder ungültiger Antwortstruktur
|
||
*/
|
||
let __configCache;
|
||
async function getConfig() {
|
||
if (__configCache) return __configCache;
|
||
const res = await fetch("/config.json");
|
||
if (!res.ok) throw new Error("config.json konnte nicht geladen werden");
|
||
__configCache = await res.json();
|
||
return __configCache;
|
||
}
|
||
|
||
export const fetchGisStationsStatusDistrictService = async () => {
|
||
const useMocks = process.env.NEXT_PUBLIC_USE_MOCKS === "true";
|
||
const config = await getConfig();
|
||
const basePath = config.basePath || "";
|
||
|
||
if (useMocks) {
|
||
const mockBasePath = "/api/mocks/webservice/gisStationsStatusDistrict";
|
||
const mockURL = `${window.location.origin}${mockBasePath}`;
|
||
if (process.env.NEXT_PUBLIC_DEBUG_LOG === "true") {
|
||
console.log("🧪 Mock-Modus aktiviert: fetchGisStationsStatusDistrictService ", mockURL);
|
||
}
|
||
|
||
const response = await fetch("/api/mocks/webservice/gisStationsStatusDistrict");
|
||
if (!response.ok) {
|
||
throw new Error("Mockdaten konnten nicht geladen werden");
|
||
}
|
||
|
||
const mockData = await response.json();
|
||
if (!Array.isArray(mockData.Statis)) {
|
||
throw new Error("Ungültige Struktur: 'Statis' fehlt im Mock");
|
||
}
|
||
|
||
return mockData.Statis;
|
||
} else {
|
||
const baseUrl = `${window.location.protocol}//${window.location.hostname}:80${basePath}/ClientData/WebServiceMap.asmx`;
|
||
|
||
const params = new URLSearchParams(window.location.search);
|
||
const idMap = params.get("m");
|
||
const idUser = params.get("u");
|
||
|
||
const url = `${baseUrl}/GisStationsStatusDistrict?idMap=${idMap}&idUser=${idUser}`;
|
||
if (process.env.NEXT_PUBLIC_DEBUG_LOG === "true") {
|
||
console.log("📡 fetchGisStationsStatusDistrictService URL:", url);
|
||
}
|
||
|
||
const response = await fetch(url);
|
||
if (!response.ok) {
|
||
const message = `❌ Fehler: ${response.status} ${response.statusText}`;
|
||
console.error(message);
|
||
throw new Error(message);
|
||
}
|
||
|
||
const jsonResponse = await response.json();
|
||
|
||
if (!Array.isArray(jsonResponse.Statis)) {
|
||
throw new Error("Antwortstruktur ungültig – 'Statis' fehlt");
|
||
}
|
||
|
||
return jsonResponse.Statis;
|
||
}
|
||
};
|