|
|
import type { RuntimeConfig } from '../types/runtime'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getRuntimeConfig = (): RuntimeConfig => { |
|
|
return ( |
|
|
window.__MCPHUB_CONFIG__ || { |
|
|
basePath: '', |
|
|
version: 'dev', |
|
|
name: 'mcphub', |
|
|
} |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getBasePath = (): string => { |
|
|
const config = getRuntimeConfig(); |
|
|
const basePath = config.basePath || ''; |
|
|
|
|
|
|
|
|
if (basePath && !basePath.startsWith('/')) { |
|
|
return '/' + basePath; |
|
|
} |
|
|
return basePath; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getApiBaseUrl = (): string => { |
|
|
const basePath = getBasePath(); |
|
|
|
|
|
return basePath + '/api'; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getApiUrl = (endpoint: string): string => { |
|
|
const baseUrl = getApiBaseUrl(); |
|
|
|
|
|
const normalizedEndpoint = endpoint.startsWith('/') ? endpoint : '/' + endpoint; |
|
|
return baseUrl + normalizedEndpoint; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const loadRuntimeConfig = async (): Promise<RuntimeConfig> => { |
|
|
try { |
|
|
|
|
|
|
|
|
const currentPath = window.location.pathname; |
|
|
const possibleConfigPaths = [ |
|
|
|
|
|
currentPath.replace(/\/[^\/]*$/, '') + '/config', |
|
|
|
|
|
'/config', |
|
|
|
|
|
...(currentPath.includes('/') |
|
|
? [currentPath.split('/')[1] ? `/${currentPath.split('/')[1]}/config` : '/config'] |
|
|
: ['/config']), |
|
|
]; |
|
|
|
|
|
for (const configPath of possibleConfigPaths) { |
|
|
try { |
|
|
const response = await fetch(configPath, { |
|
|
method: 'GET', |
|
|
headers: { |
|
|
Accept: 'application/json', |
|
|
'Cache-Control': 'no-cache', |
|
|
}, |
|
|
}); |
|
|
|
|
|
if (response.ok) { |
|
|
const data = await response.json(); |
|
|
if (data.success && data.data) { |
|
|
return data.data; |
|
|
} |
|
|
} |
|
|
} catch (error) { |
|
|
|
|
|
console.debug(`Failed to load config from ${configPath}:`, error); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.warn('Could not load runtime config from server, using defaults'); |
|
|
return { |
|
|
basePath: '', |
|
|
version: 'dev', |
|
|
name: 'mcphub', |
|
|
}; |
|
|
} catch (error) { |
|
|
console.error('Error loading runtime config:', error); |
|
|
return { |
|
|
basePath: '', |
|
|
version: 'dev', |
|
|
name: 'mcphub', |
|
|
}; |
|
|
} |
|
|
}; |
|
|
|