Skip to content

Commit 33dd782

Browse files
committed
chore: read base api url from index.html
1 parent 6a897e3 commit 33dd782

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
</head>
88
<body class="theme-minder">
99
<div id="root"></div>
10+
<script>
11+
window.APP_CONFIG = {
12+
BASE_API_URL: '${BASE_API_URL}',
13+
}
14+
</script>
1015
<script type="module" src="/src/main.tsx"></script>
1116
</body>
1217
</html>

src/global.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface AppConfig {
2+
BASE_API_URL?: string
3+
}
4+
5+
declare global {
6+
interface Window {
7+
APP_CONFIG: AppConfig
8+
}
9+
}
10+
11+
export {}

src/hooks/useSse.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import {
66
v1GetWorkspaceMessagesQueryKey,
77
} from '@/api/generated/@tanstack/react-query.gen'
88
import { invalidateQueries } from '@/lib/react-query-utils'
9+
import { getAppConfig } from '@/lib/utils'
910

10-
const BASE_URL = import.meta.env.VITE_BASE_API_URL
11+
const baseApiUrl = getAppConfig().BASE_API_URL
1112

1213
export function useSse() {
1314
const location = useLocation()
1415
const queryClient = useQueryClient()
1516

1617
useEffect(() => {
1718
const eventSource = new EventSource(
18-
`${BASE_URL}/api/v1/alerts_notification`
19+
`${baseApiUrl}/api/v1/alerts_notification`
1920
)
2021

2122
eventSource.onmessage = function (event) {

src/lib/__tests__/utils.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getAppConfig } from '../utils'
2+
import { AppConfig } from '@/global'
3+
4+
describe('getAppConfig', () => {
5+
const mockViteBaseApiUrl = 'https://api.mock.com'
6+
7+
it('default base api url if ${BASE_API_URL}" not configured', () => {
8+
const mockAppConfig: AppConfig = {
9+
BASE_API_URL: '${BASE_API_URL}',
10+
}
11+
12+
Object.defineProperty(window, 'APP_CONFIG', {
13+
value: mockAppConfig,
14+
writable: true,
15+
})
16+
17+
const expectedConfig: AppConfig = {
18+
...mockAppConfig,
19+
BASE_API_URL: 'https://mock.codegate.ai',
20+
}
21+
22+
expect(getAppConfig()).toEqual(expectedConfig)
23+
})
24+
25+
it('replace base api url if ${BASE_API_URL}" is configured', () => {
26+
const mockAppConfig: AppConfig = {
27+
BASE_API_URL: mockViteBaseApiUrl,
28+
}
29+
30+
Object.defineProperty(window, 'APP_CONFIG', {
31+
value: mockAppConfig,
32+
writable: true,
33+
})
34+
35+
const expectedConfig: AppConfig = {
36+
...mockAppConfig,
37+
BASE_API_URL: mockViteBaseApiUrl,
38+
}
39+
40+
expect(getAppConfig()).toEqual(expectedConfig)
41+
})
42+
})

src/lib/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { format } from 'date-fns'
2+
import { AppConfig } from '@/global'
23

34
const FILEPATH_REGEX = /(?:---FILEPATH|Path:|\/\/\s*filepath:)\s*([^\s]+)/g
45
const COMPARE_CODE_REGEX = /Compare this snippet[^:]*:/g
@@ -70,3 +71,19 @@ export function sanitizeQuestionPrompt({
7071
return question
7172
}
7273
}
74+
75+
export function getAppConfig(): AppConfig {
76+
const baseApiUrl = window.APP_CONFIG?.BASE_API_URL
77+
78+
if (!baseApiUrl || baseApiUrl === '${BASE_API_URL}') {
79+
return {
80+
...window.APP_CONFIG,
81+
BASE_API_URL: import.meta.env.VITE_BASE_API_URL,
82+
}
83+
}
84+
85+
return {
86+
...window.APP_CONFIG,
87+
BASE_API_URL: baseApiUrl,
88+
}
89+
}

src/main.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import './index.css'
44
import './code.css'
55
import '@stacklok/ui-kit/style'
66
import App from './App.tsx'
7-
87
import ErrorBoundary from './components/ErrorBoundary.tsx'
98
import { Error } from './components/Error.tsx'
109
import { DarkModeProvider, Toaster } from '@stacklok/ui-kit'
@@ -14,10 +13,11 @@ import { BrowserRouter } from 'react-router-dom'
1413
import { UiKitClientSideRoutingProvider } from './lib/ui-kit-client-side-routing.tsx'
1514
import { ConfirmProvider } from './context/confirm-context.tsx'
1615
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
16+
import { getAppConfig } from './lib/utils.ts'
1717

1818
// Initialize the API client
1919
client.setConfig({
20-
baseUrl: import.meta.env.VITE_BASE_API_URL,
20+
baseUrl: getAppConfig().BASE_API_URL,
2121
})
2222

2323
createRoot(document.getElementById('root')!).render(

tsconfig.app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"openapi-ts.config.ts",
3030
"tailwind.config.ts",
3131
"vitest.config.ts",
32-
"vitest.setup.ts"
32+
"vitest.setup.ts",
33+
"./global.d.ts"
3334
],
3435
"exclude": ["node_modules"]
3536
}

0 commit comments

Comments
 (0)