Files
The-transit-Project/admin_ui/src/api.js
T
Sven laptop 1154d1d658
Build and Push Docker Images / detect-changes (push) Successful in 15s
Build and Push Docker Images / build-auth-api (push) Successful in 1m2s
Build and Push Docker Images / build-employee-api (push) Successful in 1m2s
Build and Push Docker Images / build-transit-api (push) Successful in 1m2s
Build and Push Docker Images / build-error-log-api (push) Successful in 1m10s
Build and Push Docker Images / build-admin-ui (push) Successful in 1m22s
Build and Push Docker Images / deploy-test-node (push) Successful in 4s
chore: standardize service domains and error logging
2026-09-18 19:26:05 +02:00

107 lines
2.8 KiB
JavaScript

import axios from "axios";
const RUNTIME =
typeof window !== "undefined" && window.__ENV__ ? window.__ENV__ : {};
const normalizeApiBaseUrl = (value, fallbackPath) => {
const rawValue = value || "";
if (!rawValue) return fallbackPath;
if (
/^https?:\/\//i.test(rawValue) ||
rawValue.startsWith("//") ||
rawValue.startsWith("/")
) {
return rawValue;
}
return `${window.location.protocol}//${rawValue}`;
};
const AUTH_API = normalizeApiBaseUrl(
RUNTIME.AUTH_API || import.meta.env.VITE_AUTH_API,
"https://auth.xtransit.testinstance.nl",
);
const TRANSIT_API = normalizeApiBaseUrl(
RUNTIME.TRANSIT_API || import.meta.env.VITE_TRANSIT_API,
"https://transit.xtransit.testinstance.nl",
);
const ERROR_LOG_API = normalizeApiBaseUrl(
RUNTIME.ERROR_LOG_API || import.meta.env.VITE_ERROR_LOG_API,
"https://error.xtransit.testinstance.nl",
);
const reportClientError = async ({ service, partofservice, error }) => {
try {
await fetch(`${ERROR_LOG_API}/error_log`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
service,
partofservice,
error: typeof error === "string" ? error : JSON.stringify(error),
}),
});
} catch (e) {
// ignore logging failures
}
};
export const authClient = axios.create({ baseURL: AUTH_API });
authClient.interceptors.response.use(
(response) => response,
(error) => {
void reportClientError({
service: "admin_ui",
partofservice: "authClient",
error:
error?.response?.data?.error || error?.message || "authClient_error",
});
return Promise.reject(error);
},
);
export const transitClient = (token) => {
const client = axios.create({
baseURL: TRANSIT_API,
headers: { Authorization: `Bearer ${token}` },
});
// Auto-logout on unauthorized responses to avoid delayed failures
client.interceptors.response.use(
(response) => response,
(error) => {
try {
const status = error?.response?.status;
if (status === 401 || status === 403) {
localStorage.removeItem("token");
localStorage.removeItem("user");
// reload to let App show the Login screen
if (typeof window !== "undefined") window.location.reload();
}
void reportClientError({
service: "admin_ui",
partofservice: "transitClient",
error:
error?.response?.data?.error ||
error?.message ||
"transitClient_error",
});
} catch (e) {
// ignore
}
return Promise.reject(error);
},
);
return client;
};
export const errorLogClient = (token) =>
axios.create({
baseURL: ERROR_LOG_API,
headers: token ? { Authorization: `Bearer ${token}` } : {},
});
export { reportClientError };