From 7710e2549e1d4292d7321e7817ae5db500f0c803 Mon Sep 17 00:00:00 2001 From: MidoriKurage Date: Sat, 21 Mar 2026 20:22:19 +0800 Subject: [PATCH] fix(frontend): Fix hard-coded API base in checkAndSetApiUrl.ts --- frontend/src/helpers/checkAndSetApiUrl.ts | 45 ++++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/frontend/src/helpers/checkAndSetApiUrl.ts b/frontend/src/helpers/checkAndSetApiUrl.ts index 278f97a08..892ed6e24 100644 --- a/frontend/src/helpers/checkAndSetApiUrl.ts +++ b/frontend/src/helpers/checkAndSetApiUrl.ts @@ -1,6 +1,7 @@ import {useConfigStore} from '@/stores/config' const API_DEFAULT_PORT = '3456' +const API_PATH_SUFFIX = '/api/v1' export const ERROR_NO_API_URL = 'noApiUrlProvided' @@ -20,6 +21,23 @@ export class InvalidApiUrlProvidedError extends Error { } } +/** + * Join a base pathname with the API_DEFAULT_PATH, normalizing slashes between them. + */ +function joinPath(base: string, suffix: string): string { + const normalizedBase = base.endsWith('/') ? base.slice(0, -1) : base + return normalizedBase + suffix +} + +/** + * Check whether a pathname already ends with the API default path, + * with or without a trailing slash. + */ +function hasApiPath(pathname: string): boolean { + const clean = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname + return clean.endsWith(API_PATH_SUFFIX) +} + export const checkAndSetApiUrl = (pUrl: string | undefined | null): Promise => { let url = pUrl if (url === '' || url === null || typeof url === 'undefined') { @@ -57,25 +75,19 @@ export const checkAndSetApiUrl = (pUrl: string | undefined | null): Promise { console.warn(`Could not fetch 'info' from the provided endpoint ${pUrl} on ${window.API_URL}/info. Some automatic fallback will be tried.`) - // Check if it is reachable at /api/v1 and http - if ( - !urlToCheck.pathname.endsWith('/api/v1') && - !urlToCheck.pathname.endsWith('/api/v1/') - ) { - urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + // Check if it is reachable at the base path + /api/v1 via http + if (!hasApiPath(urlToCheck.pathname)) { + urlToCheck.pathname = joinPath(urlToCheck.pathname, API_PATH_SUFFIX) window.API_URL = urlToCheck.toString() return configStore.update() } throw e }) .catch(e => { - // Check if it is reachable at /api/v1 and https + // Check if it is reachable at the base path + /api/v1 via https urlToCheck.pathname = origPathname - if ( - !urlToCheck.pathname.endsWith('/api/v1') && - !urlToCheck.pathname.endsWith('/api/v1/') - ) { - urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + if (!hasApiPath(urlToCheck.pathname)) { + urlToCheck.pathname = joinPath(urlToCheck.pathname, API_PATH_SUFFIX) window.API_URL = urlToCheck.toString() return configStore.update() } @@ -91,13 +103,10 @@ export const checkAndSetApiUrl = (pUrl: string | undefined | null): Promise { - // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 + // Check if it is reachable at :API_DEFAULT_PORT with base path + /api/v1 urlToCheck.pathname = origPathname - if ( - !urlToCheck.pathname.endsWith('/api/v1') && - !urlToCheck.pathname.endsWith('/api/v1/') - ) { - urlToCheck.pathname = `${urlToCheck.pathname}api/v1` + if (!hasApiPath(urlToCheck.pathname)) { + urlToCheck.pathname = joinPath(urlToCheck.pathname, API_PATH_SUFFIX) window.API_URL = urlToCheck.toString() return configStore.update() }