Skip to content

Commit 6c8ddaa

Browse files
committed
feat(api): add centralized API version config for frontend
- API_VERSION constant controls all frontend API paths - API_PREFIX auto-derived: /api/v1 - API_URL combines base URL + prefix - WS_URL for WebSocket connections (versioned) - Helper functions: buildApiUrl(), buildWsUrl() - LEGACY_API_URL for backward compatibility Mirrors backend/config/api.py pattern. Part of #55
1 parent e4c216e commit 6c8ddaa

1 file changed

Lines changed: 53 additions & 6 deletions

File tree

frontend/src/config/api.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
11
/**
2-
* API Configuration
2+
* API Configuration - Single Source of Truth for API Versioning
33
*
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/*
56
*/
67

7-
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000'
8+
// =============================================================================
9+
// BASE CONFIGURATION
10+
// =============================================================================
811

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'
1113

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

Comments
 (0)