fix: 修复 Admin 后台多个 bug
- api.ts: 修复 URL 拼接 bug(缺少 API_BASE) 和 204 判断逻辑 - dal.ts: 修复 fetchDashboard 类型错乱,修正 API 端点 - types.ts: 对齐后端 Pydantic 模型 - 所有页面: Badge variant→status, CardHeader children→action - 移除不存在的后端端点调用
This commit is contained in:
+10
-20
@@ -1,17 +1,10 @@
|
||||
/**
|
||||
* Admin 后台管理系统 - 统一 API 客户端
|
||||
*
|
||||
* 封装 fetch 调用,提供:
|
||||
* - 统一错误处理
|
||||
* - 请求/响应日志
|
||||
* - 超时控制
|
||||
* - 类型安全的响应解析
|
||||
*/
|
||||
|
||||
const API_BASE = '/api/v1'
|
||||
const TIMEOUT_MS = 30_000
|
||||
|
||||
/** 通用 API 错误类型 */
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
@@ -23,12 +16,14 @@ export class ApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/** 基础 fetch 封装,带超时和错误处理 */
|
||||
async function request<T>(
|
||||
path: string,
|
||||
options: RequestInit = {},
|
||||
): Promise<T> {
|
||||
const url = path.startsWith('http') ? path : `${path}`
|
||||
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
// 修复: 正确拼接 API_BASE
|
||||
const url = path.startsWith('http')
|
||||
? path
|
||||
: path.startsWith('/')
|
||||
? path // 已经是绝对路径(如 /health)
|
||||
: `${API_BASE}${path}`
|
||||
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS)
|
||||
|
||||
@@ -58,8 +53,8 @@ async function request<T>(
|
||||
)
|
||||
}
|
||||
|
||||
// 204 No Content
|
||||
if (res.status === 200 && res.headers.get('content-length') === '0') {
|
||||
// 修复: 正确判断 204 No Content
|
||||
if (res.status === 204) {
|
||||
return undefined as T
|
||||
}
|
||||
|
||||
@@ -78,17 +73,12 @@ async function request<T>(
|
||||
}
|
||||
}
|
||||
|
||||
// ── 通用 CRUD 快捷方法 ──────────────────────────────────────────
|
||||
|
||||
export const api = {
|
||||
get: <T>(path: string) => request<T>(path),
|
||||
|
||||
post: <T>(path: string, body?: unknown) =>
|
||||
request<T>(path, { method: 'POST', body: body ? JSON.stringify(body) : undefined }),
|
||||
|
||||
put: <T>(path: string, body?: unknown) =>
|
||||
request<T>(path, { method: 'PUT', body: body ? JSON.stringify(body) : undefined }),
|
||||
|
||||
delete: <T>(path: string) => request<T>(path, { method: 'DELETE' }),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user