|
| 1 | +/** |
| 2 | + * Console Application Configuration |
| 3 | + * |
| 4 | + * Supports two runtime modes: |
| 5 | + * - MSW Mode: Uses Mock Service Worker with in-browser ObjectStack kernel |
| 6 | + * - Server Mode: Connects to a real ObjectStack server |
| 7 | + */ |
| 8 | + |
| 9 | +export type RuntimeMode = 'msw' | 'server'; |
| 10 | + |
| 11 | +export interface ConsoleConfig { |
| 12 | + /** |
| 13 | + * Runtime mode |
| 14 | + * - 'msw': Mock Service Worker mode (browser-based kernel) |
| 15 | + * - 'server': Connect to real ObjectStack server |
| 16 | + */ |
| 17 | + mode: RuntimeMode; |
| 18 | + |
| 19 | + /** |
| 20 | + * Server base URL (used in 'server' mode) |
| 21 | + * @default 'http://localhost:5000/api/v1' |
| 22 | + */ |
| 23 | + serverUrl: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * MSW API base path (used in 'msw' mode) |
| 27 | + * @default '/api/v1' |
| 28 | + */ |
| 29 | + mswBasePath: string; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Get runtime mode from environment |
| 34 | + */ |
| 35 | +function getRuntimeMode(): RuntimeMode { |
| 36 | + const envMode = import.meta.env.VITE_RUNTIME_MODE; |
| 37 | + |
| 38 | + if (envMode === 'server') { |
| 39 | + return 'server'; |
| 40 | + } |
| 41 | + |
| 42 | + // Default to MSW mode for development |
| 43 | + return 'msw'; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Default configuration values |
| 48 | + */ |
| 49 | +const defaultConfig: ConsoleConfig = { |
| 50 | + mode: getRuntimeMode(), |
| 51 | + serverUrl: import.meta.env.VITE_SERVER_URL || 'http://localhost:5000/api/v1', |
| 52 | + mswBasePath: '/api/v1', |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Current application configuration |
| 57 | + */ |
| 58 | +export const config: ConsoleConfig = { |
| 59 | + ...defaultConfig, |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Check if running in MSW mode |
| 64 | + */ |
| 65 | +export function isMswMode(): boolean { |
| 66 | + return config.mode === 'msw'; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Check if running in Server mode |
| 71 | + */ |
| 72 | +export function isServerMode(): boolean { |
| 73 | + return config.mode === 'server'; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Get the API base URL based on current mode |
| 78 | + */ |
| 79 | +export function getApiBaseUrl(): string { |
| 80 | + if (isServerMode()) { |
| 81 | + return config.serverUrl; |
| 82 | + } |
| 83 | + return config.mswBasePath; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Update configuration at runtime |
| 88 | + * Useful for switching modes programmatically |
| 89 | + */ |
| 90 | +export function updateConfig(updates: Partial<ConsoleConfig>): void { |
| 91 | + Object.assign(config, updates); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Log current configuration (for debugging) |
| 96 | + */ |
| 97 | +export function logConfig(): void { |
| 98 | + console.log('[Console Config]', { |
| 99 | + mode: config.mode, |
| 100 | + apiBaseUrl: getApiBaseUrl(), |
| 101 | + serverUrl: config.serverUrl, |
| 102 | + }); |
| 103 | +} |
0 commit comments