fix(frontend): Fix hard-coded API base in checkAndSetApiUrl.ts

This commit is contained in:
MidoriKurage 2026-03-21 20:22:19 +08:00 committed by kolaente
parent e31c45c44e
commit 7710e2549e
1 changed files with 27 additions and 18 deletions

View File

@ -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<string> => {
let url = pUrl
if (url === '' || url === null || typeof url === 'undefined') {
@ -57,25 +75,19 @@ export const checkAndSetApiUrl = (pUrl: string | undefined | null): Promise<stri
return configStore.update()
.catch(e => {
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<stri
throw e
})
.catch(e => {
// 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()
}