From d6d5dd785d21082657816effee1d13145932d82f Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sun, 16 Aug 2026 17:18:11 +0500 Subject: [PATCH] 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. --- src/utils/echo.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/utils/echo.js b/src/utils/echo.js index 298f42e..1677605 100644 --- a/src/utils/echo.js +++ b/src/utils/echo.js @@ -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", }, }, });