42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
/**
|
|
* 統一的API配置管理
|
|
* 中央化管理所有API端點和配置
|
|
*/
|
|
|
|
// API基礎配置
|
|
export const API_CONFIG = {
|
|
BASE_URL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5008',
|
|
ENDPOINTS: {
|
|
AUTH: '/api/auth',
|
|
FLASHCARDS: '/api',
|
|
REVIEW: '/api',
|
|
IMAGE: '/api'
|
|
},
|
|
TIMEOUT: 30000,
|
|
RETRY_ATTEMPTS: 3
|
|
} as const
|
|
|
|
// 構建完整的API URL
|
|
export const buildApiUrl = (endpoint: keyof typeof API_CONFIG.ENDPOINTS, path: string = ''): string => {
|
|
const baseEndpoint = API_CONFIG.ENDPOINTS[endpoint]
|
|
const fullPath = path ? `${baseEndpoint}${path}` : baseEndpoint
|
|
return `${API_CONFIG.BASE_URL}${fullPath}`
|
|
}
|
|
|
|
// 常用的API URL生成器
|
|
export const API_URLS = {
|
|
// 認證相關
|
|
auth: (path: string = '') => buildApiUrl('AUTH', path),
|
|
|
|
// 詞卡相關
|
|
flashcards: (path: string = '') => buildApiUrl('FLASHCARDS', path),
|
|
|
|
// 複習相關
|
|
review: (path: string = '') => buildApiUrl('REVIEW', path),
|
|
|
|
// 圖片相關
|
|
image: (path: string = '') => buildApiUrl('IMAGE', path)
|
|
} as const
|
|
|
|
// 導出基礎URL以便直接使用
|
|
export const { BASE_URL, TIMEOUT, RETRY_ATTEMPTS } = API_CONFIG |