This commit is contained in:
jhartworks
2026-06-23 10:59:58 +02:00
parent 9b651f1cd6
commit a445fef521
21 changed files with 3161 additions and 664 deletions
+69 -33
View File
@@ -1,50 +1,86 @@
const API_URL = import.meta.env.VITE_API_URL || "http://localhost:18080/api";
function resolveServiceUrl(explicitEnv, fallbackPort, suffix = "") {
const explicit = String(explicitEnv || "").trim();
if (explicit) {
return explicit.replace(/\/$/, "") + suffix;
}
async function request(path, options = {}) {
const response = await fetch(API_URL + path, {
headers: {
"Content-Type": "application/json",
...(options.token ? { Authorization: "Bearer " + options.token } : {}),
...options.headers,
},
if (typeof window !== "undefined") {
const { protocol, hostname } = window.location;
return `${protocol}//${hostname}:${fallbackPort}${suffix}`;
}
return `http://localhost:${fallbackPort}${suffix}`;
}
const API_URL = resolveServiceUrl(import.meta.env.VITE_API_URL, import.meta.env.VITE_API_PORT || "18080", "/api");
const COLLECTOR_URL = resolveServiceUrl(import.meta.env.VITE_COLLECTOR_URL, import.meta.env.VITE_COLLECTOR_PORT || "18110");
async function request(baseUrl, path, options = {}) {
const headers = {
...(options.token ? { Authorization: `Bearer ${options.token}` } : {}),
...options.headers,
};
const isJsonBody = options.body !== undefined && !(options.body instanceof FormData);
if (isJsonBody) {
headers["Content-Type"] = "application/json";
}
const response = await fetch(baseUrl + path, {
headers,
method: options.method || "GET",
body: options.body ? JSON.stringify(options.body) : undefined,
body: options.body === undefined ? undefined : isJsonBody ? JSON.stringify(options.body) : options.body,
});
if (response.status === 204) {
return null;
}
const data = await response.json();
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || "Request failed.");
throw new Error(data.error || "Anfrage fehlgeschlagen.");
}
return data;
}
const apiRequest = (path, options) => request(API_URL, path, options);
const collectorRequest = (path, options) => request(COLLECTOR_URL, path, options);
export const api = {
login: (body) => request("/auth/login", { method: "POST", body }),
me: (token) => request("/auth/me", { token }),
health: () => request("/health"),
getIsps: (token) => request("/isps", { token }),
getAliases: (token, isp) => request("/isps/" + isp + "/aliases", { token }),
saveAliases: (token, isp, body) =>
request("/isps/" + isp + "/aliases", { method: "PUT", token, body }),
importSql: (token, body) => request("/admin/import-sql", { method: "POST", token, body }),
getUsers: (token) => request("/users", { token }),
createUser: (token, body) => request("/users", { method: "POST", token, body }),
updateUser: (token, id, body) => request("/users/" + id, { method: "PATCH", token, body }),
getSelections: (token) => request("/selections", { token }),
createSelection: (token, body) => request("/selections", { method: "POST", token, body }),
updateSelection: (token, id, body) => request("/selections/" + id, { method: "PUT", token, body }),
deleteSelection: (token, id) => request("/selections/" + id, { method: "DELETE", token }),
queryTrends: (token, body) => request("/trends/query", { method: "POST", token, body }),
latestValues: (token, body) => request("/points/latest", { method: "POST", token, body }),
getDashboard: (token) => request("/dashboard", { token }),
saveDashboard: (token, body) => request("/dashboard", { method: "PUT", token, body }),
loadDashboardData: (token, body) => request("/dashboard/data", { method: "POST", token, body }),
getPreferences: (token) => request("/preferences", { token }),
savePreferences: (token, body) => request("/preferences", { method: "PUT", token, body }),
login: (body) => apiRequest("/auth/login", { method: "POST", body }),
me: (token) => apiRequest("/auth/me", { token }),
changePassword: (token, body) => apiRequest("/auth/change-password", { method: "POST", token, body }),
health: () => apiRequest("/health"),
getIsps: (token) => apiRequest("/isps", { token }),
getAliases: (token, isp) => apiRequest(`/isps/${isp}/aliases`, { token }),
saveAliases: (token, isp, body) => apiRequest(`/isps/${isp}/aliases`, { method: "PUT", token, body }),
createTrendTable: (token, body) => apiRequest("/trend-tables", { method: "POST", token, body }),
normalizeAliasMetadata: (token, body) => apiRequest("/admin/normalize-alias-metadata", { method: "POST", token, body }),
importSql: (token, body) => apiRequest("/admin/import-sql", { method: "POST", token, body }),
getUsers: (token) => apiRequest("/users", { token }),
createUser: (token, body) => apiRequest("/users", { method: "POST", token, body }),
updateUser: (token, id, body) => apiRequest(`/users/${id}`, { method: "PATCH", token, body }),
getSelections: (token) => apiRequest("/selections", { token }),
createSelection: (token, body) => apiRequest("/selections", { method: "POST", token, body }),
updateSelection: (token, id, body) => apiRequest(`/selections/${id}`, { method: "PUT", token, body }),
deleteSelection: (token, id) => apiRequest(`/selections/${id}`, { method: "DELETE", token }),
queryTrends: (token, body) => apiRequest("/trends/query", { method: "POST", token, body }),
latestValues: (token, body) => apiRequest("/points/latest", { method: "POST", token, body }),
getDashboard: (token) => apiRequest("/dashboard", { token }),
saveDashboard: (token, body) => apiRequest("/dashboard", { method: "PUT", token, body }),
loadDashboardData: (token, body) => apiRequest("/dashboard/data", { method: "POST", token, body }),
getPreferences: (token) => apiRequest("/preferences", { token }),
savePreferences: (token, body) => apiRequest("/preferences", { method: "PUT", token, body }),
getCollectorCapabilities: () => collectorRequest("/capabilities"),
getCollectorSources: () => collectorRequest("/sources"),
getCollectorDatapoints: (sourceId = "") => collectorRequest(sourceId ? `/datapoints?sourceId=${encodeURIComponent(sourceId)}` : "/datapoints"),
createCollectorSource: (body) => collectorRequest("/sources", { method: "POST", body }),
updateCollectorSource: (id, body) => collectorRequest(`/sources/${id}`, { method: "PUT", body }),
deleteCollectorSource: (id) => collectorRequest(`/sources/${id}`, { method: "DELETE" }),
importModbusCsv: (body) => collectorRequest("/imports/modbus-csv", { method: "POST", body }),
importKnxXml: (body) => collectorRequest("/imports/knx-xml", { method: "POST", body }),
scanOpcua: (body) => collectorRequest("/scan/opcua", { method: "POST", body }),
scanBacnet: (body) => collectorRequest("/scan/bacnet", { method: "POST", body }),
};