@@ -16,7 +16,18 @@ interface ClustersResponse {
1616}
1717class ApiService {
1818 currentCluster = "" ;
19- constructor ( protected readonly isMockMode : boolean = false ) { }
19+ private readonly basePath : string ;
20+ constructor ( protected readonly isMockMode : boolean = false ) {
21+ const fromEnv = ( import . meta as any ) . env ?. VITE_BASE_PATH as string | undefined ;
22+ // In production, Vite injects import.meta.env.BASE_URL. Prefer explicit VITE_BASE_PATH if provided.
23+ const viteBase = ( import . meta as any ) . env ?. BASE_URL as string | undefined ;
24+ const computed = ( fromEnv ?? viteBase ?? "/" ) . trim ( ) ;
25+ // Normalize to leading slash, no trailing slash (except root)
26+ let normalized = computed ;
27+ if ( ! normalized . startsWith ( "/" ) ) normalized = "/" + normalized ;
28+ if ( normalized !== "/" ) normalized = normalized . replace ( / \/ + $ / g, "" ) ;
29+ this . basePath = normalized ;
30+ }
2031
2132 setCluster = ( cluster : string ) => {
2233 this . currentCluster = cluster ;
@@ -28,14 +39,20 @@ class ApiService {
2839 ) : Promise < T > {
2940 let response ;
3041
42+ const isAbsolute = / ^ h t t p s ? : \/ \/ / . test ( url ) ;
43+ const fullUrl = isAbsolute
44+ ? url
45+ : this . basePath === "/"
46+ ? url
47+ : `${ this . basePath } ${ url . startsWith ( "/" ) ? url : `/${ url } ` } ` ;
3148 if ( this . currentCluster ) {
3249 const headers = new Headers ( options ?. headers ) ;
3350 if ( ! headers . has ( "X-Kubecontext" ) ) {
3451 headers . set ( "X-Kubecontext" , this . currentCluster ) ;
3552 }
36- response = await fetch ( url , { ...options , headers } ) ;
53+ response = await fetch ( fullUrl , { ...options , headers } ) ;
3754 } else {
38- response = await fetch ( url , options ) ;
55+ response = await fetch ( fullUrl , options ) ;
3956 }
4057
4158 if ( ! response . ok ) {
@@ -55,7 +72,7 @@ class ApiService {
5572 }
5673
5774 getToolVersion = async ( ) => {
58- const response = await fetch ( "/status" ) ;
75+ const response = await this . fetchWithDefaults ( "/status" ) ;
5976 const data = await response . json ( ) ;
6077 return data ;
6178 } ;
@@ -73,8 +90,9 @@ class ApiService {
7390 } ;
7491
7592 getClusters = async ( ) => {
76- const response = await fetch ( "/api/k8s/contexts" ) ;
77- const data = ( await response . json ( ) ) as ClustersResponse [ ] ;
93+ const data = await this . fetchWithDefaults < ClustersResponse [ ] > (
94+ "/api/k8s/contexts"
95+ ) ;
7896 return data ;
7997 } ;
8098
0 commit comments