Refactor API URL handling in echo.js to improve path normalization and enhance auth endpoint configuration. Updated apiOrigin to apiUrl for better flexibility and added support for an optional API token in headers.

This commit is contained in:
Mekan1206
2026-08-16 17:18:11 +05:00
parent 347ec3536d
commit d6d5dd785d

View File

@@ -20,9 +20,11 @@ function getToken() {
return null;
}
function apiOrigin() {
const url = import.meta.env.VITE_API_URL || "https://mm.com.tm/api/v1/";
return url.replace(/\/?api\/v1\/?$/, "");
function apiUrl(path) {
const base = import.meta.env.VITE_API_URL || "https://mm.com.tm/api/v1/";
const normalized = base.endsWith("/") ? base : `${base}/`;
return `${normalized}${String(path).replace(/^\//, "")}`;
}
function isLoopbackHost(host) {
@@ -83,11 +85,12 @@ export function getChatEcho() {
enabledTransports: ["ws", "wss"],
disableStats: true,
Pusher,
authEndpoint: `${apiOrigin()}/broadcasting/auth`,
authEndpoint: apiUrl("broadcasting/auth"),
auth: {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
"Api-Token": import.meta.env.VITE_API_TOKEN || "hello-mf-s",
},
},
});