|
1 | 1 | /** |
2 | | - * API Configuration |
| 2 | + * API Configuration - Single Source of Truth for API Versioning |
3 | 3 | * |
4 | | - * Centralizes API URL configuration for all frontend components. |
| 4 | + * Change API_VERSION here to update all API calls across the frontend. |
| 5 | + * Example: "v1" -> "v2" will change /api/v1/* to /api/v2/* |
5 | 6 | */ |
6 | 7 |
|
7 | | -const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000' |
| 8 | +// ============================================================================= |
| 9 | +// BASE CONFIGURATION |
| 10 | +// ============================================================================= |
8 | 11 |
|
9 | | -// WebSocket URL - convert http(s) to ws(s) |
10 | | -const WS_URL = API_URL.replace(/^http/, 'ws') |
| 12 | +const BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000' |
11 | 13 |
|
12 | | -export { API_URL, WS_URL } |
| 14 | +// ============================================================================= |
| 15 | +// API VERSION CONFIGURATION |
| 16 | +// ============================================================================= |
| 17 | + |
| 18 | +export const API_VERSION = 'v1' |
| 19 | + |
| 20 | +// ============================================================================= |
| 21 | +// DERIVED URLs (auto-calculated from version) |
| 22 | +// ============================================================================= |
| 23 | + |
| 24 | +// API prefix: /api/v1 |
| 25 | +export const API_PREFIX = `/api/${API_VERSION}` |
| 26 | + |
| 27 | +// Full API URL: http://localhost:8000/api/v1 |
| 28 | +export const API_URL = `${BASE_URL}${API_PREFIX}` |
| 29 | + |
| 30 | +// WebSocket URL: ws://localhost:8000/api/v1 |
| 31 | +const WS_BASE = BASE_URL.replace(/^http/, 'ws') |
| 32 | +export const WS_URL = `${WS_BASE}${API_PREFIX}` |
| 33 | + |
| 34 | +// Legacy URL (for backward compatibility if needed) |
| 35 | +export const LEGACY_API_URL = `${BASE_URL}/api` |
| 36 | + |
| 37 | +// ============================================================================= |
| 38 | +// ENDPOINT HELPERS |
| 39 | +// ============================================================================= |
| 40 | + |
| 41 | +/** |
| 42 | + * Build a full API endpoint URL |
| 43 | + * @param path - Endpoint path (e.g., '/repos', '/search') |
| 44 | + * @returns Full URL (e.g., 'http://localhost:8000/api/v1/repos') |
| 45 | + */ |
| 46 | +export const buildApiUrl = (path: string): string => { |
| 47 | + const cleanPath = path.startsWith('/') ? path : `/${path}` |
| 48 | + return `${API_URL}${cleanPath}` |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Build a WebSocket endpoint URL |
| 53 | + * @param path - WebSocket path (e.g., '/ws/index/repo-123') |
| 54 | + * @returns Full WS URL (e.g., 'ws://localhost:8000/api/v1/ws/index/repo-123') |
| 55 | + */ |
| 56 | +export const buildWsUrl = (path: string): string => { |
| 57 | + const cleanPath = path.startsWith('/') ? path : `/${path}` |
| 58 | + return `${WS_URL}${cleanPath}` |
| 59 | +} |
0 commit comments