@@ -16,7 +16,18 @@ interface ClustersResponse {
16
16
}
17
17
class ApiService {
18
18
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
+ }
20
31
21
32
setCluster = ( cluster : string ) => {
22
33
this . currentCluster = cluster ;
@@ -28,14 +39,20 @@ class ApiService {
28
39
) : Promise < T > {
29
40
let response ;
30
41
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 } ` } ` ;
31
48
if ( this . currentCluster ) {
32
49
const headers = new Headers ( options ?. headers ) ;
33
50
if ( ! headers . has ( "X-Kubecontext" ) ) {
34
51
headers . set ( "X-Kubecontext" , this . currentCluster ) ;
35
52
}
36
- response = await fetch ( url , { ...options , headers } ) ;
53
+ response = await fetch ( fullUrl , { ...options , headers } ) ;
37
54
} else {
38
- response = await fetch ( url , options ) ;
55
+ response = await fetch ( fullUrl , options ) ;
39
56
}
40
57
41
58
if ( ! response . ok ) {
@@ -55,7 +72,7 @@ class ApiService {
55
72
}
56
73
57
74
getToolVersion = async ( ) => {
58
- const response = await fetch ( "/status" ) ;
75
+ const response = await this . fetchWithDefaults ( "/status" ) ;
59
76
const data = await response . json ( ) ;
60
77
return data ;
61
78
} ;
@@ -73,8 +90,9 @@ class ApiService {
73
90
} ;
74
91
75
92
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
+ ) ;
78
96
return data ;
79
97
} ;
80
98
0 commit comments