Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: dashboard api base url env #374

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div id="root"></div>
<script>
window.APP_CONFIG = {
BASE_API_URL: '${BASE_API_URL}',
DASHBOARD_API_BASE_URL: '${DASHBOARD_API_BASE_URL}',
}
</script>
<script type="module" src="/src/main.tsx"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface AppConfig {
BASE_API_URL?: string
DASHBOARD_API_BASE_URL?: string
}

declare global {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { invalidateQueries } from '@/lib/react-query-utils'
import { getAppConfig } from '@/lib/utils'

const baseApiUrl = getAppConfig().BASE_API_URL
const baseApiUrl = getAppConfig().DASHBOARD_API_BASE_URL

export function useSse() {
const location = useLocation()
Expand Down
12 changes: 6 additions & 6 deletions src/lib/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { AppConfig } from '@/global'
describe('getAppConfig', () => {
const mockViteBaseApiUrl = 'https://api.mock.com'

it('default base api url if ${BASE_API_URL}" not configured', () => {
it('default base api url if ${DASHBOARD_API_BASE_URL}" not configured', () => {
const mockAppConfig: AppConfig = {
BASE_API_URL: '${BASE_API_URL}',
DASHBOARD_API_BASE_URL: '${DASHBOARD_API_BASE_URL}',
}

Object.defineProperty(window, 'APP_CONFIG', {
Expand All @@ -16,15 +16,15 @@ describe('getAppConfig', () => {

const expectedConfig: AppConfig = {
...mockAppConfig,
BASE_API_URL: 'https://mock.codegate.ai',
DASHBOARD_API_BASE_URL: 'https://mock.codegate.ai',
}

expect(getAppConfig()).toEqual(expectedConfig)
})

it('replace base api url if ${BASE_API_URL}" is configured', () => {
it('replace base api url if ${DASHBOARD_API_BASE_URL}" is configured', () => {
const mockAppConfig: AppConfig = {
BASE_API_URL: mockViteBaseApiUrl,
DASHBOARD_API_BASE_URL: mockViteBaseApiUrl,
}

Object.defineProperty(window, 'APP_CONFIG', {
Expand All @@ -34,7 +34,7 @@ describe('getAppConfig', () => {

const expectedConfig: AppConfig = {
...mockAppConfig,
BASE_API_URL: mockViteBaseApiUrl,
DASHBOARD_API_BASE_URL: mockViteBaseApiUrl,
}

expect(getAppConfig()).toEqual(expectedConfig)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ export function sanitizeQuestionPrompt({
}

export function getAppConfig(): AppConfig {
const baseApiUrl = window.APP_CONFIG?.BASE_API_URL
const baseApiUrl = window.APP_CONFIG?.DASHBOARD_API_BASE_URL

if (!baseApiUrl || baseApiUrl === '${BASE_API_URL}') {
if (!baseApiUrl || baseApiUrl === '${DASHBOARD_API_BASE_URL}') {
return {
...window.APP_CONFIG,
BASE_API_URL: import.meta.env.VITE_BASE_API_URL,
DASHBOARD_API_BASE_URL: import.meta.env.VITE_BASE_API_URL,
}
}

return {
...window.APP_CONFIG,
BASE_API_URL: baseApiUrl,
DASHBOARD_API_BASE_URL: baseApiUrl,
}
}
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getAppConfig } from './lib/utils.ts'

// Initialize the API client
client.setConfig({
baseUrl: getAppConfig().BASE_API_URL,
baseUrl: getAppConfig().DASHBOARD_API_BASE_URL,
})

createRoot(document.getElementById('root')!).render(
Expand Down
2 changes: 1 addition & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="vite/client" />
interface ImportBaseApiEnv {
readonly BASE_API_URL: string
readonly DASHBOARD_API_BASE_URL: string
readonly VITE_BASE_API_URL: string
}
Loading