refactor: 代码审查修复 — 错误边界、DRY抽取、测试修复、合规性改进
- P1: server.ts 统一错误边界 (withErrorHandler + AppError),修复 3 个失败/卡死测试 - P2: db 层 wrap/paginateQuery 抽取,前端 handleResponse 抽取,parseIdFromUrl 抽取 - P3: middleware 验证消息中文化,Flex→Space 替换 - P0: docs/development/README.md 新增已知设计决策章节 - P3-11 setup 拆分已尝试回退(@testing-library/react preload 依赖无法拆分) - P3-13 config 层测试从本次变更移除
This commit is contained in:
@@ -11,6 +11,8 @@ import type {
|
||||
UpdateModelRequest,
|
||||
} from "../../shared/api";
|
||||
|
||||
import { handleResponse, handleVoidResponse } from "../utils/api";
|
||||
|
||||
const MODELS_KEY = ["models"] as const;
|
||||
|
||||
export async function createModel(data: CreateModelRequest): Promise<Model> {
|
||||
@@ -19,20 +21,17 @@ export async function createModel(data: CreateModelRequest): Promise<Model> {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
return handleResponse(response);
|
||||
return handleResponse(response, (data) => (data as ModelResponse).model);
|
||||
}
|
||||
|
||||
export async function deleteModel(id: string): Promise<void> {
|
||||
const response = await fetch(`/api/models/${id}`, { method: "DELETE" });
|
||||
if (!response.ok) {
|
||||
const body = (await response.json().catch(() => null)) as null | { error?: string };
|
||||
throw new Error(body?.error ?? `HTTP ${response.status}`);
|
||||
}
|
||||
return handleVoidResponse(response);
|
||||
}
|
||||
|
||||
export async function fetchModel(id: string): Promise<Model> {
|
||||
const response = await fetch(`/api/models/${id}`);
|
||||
return handleResponse(response);
|
||||
return handleResponse(response, (data) => (data as ModelResponse).model);
|
||||
}
|
||||
|
||||
export async function fetchModelList(params: {
|
||||
@@ -76,7 +75,7 @@ export async function updateModel(id: string, data: UpdateModelRequest): Promise
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "PATCH",
|
||||
});
|
||||
return handleResponse(response);
|
||||
return handleResponse(response, (data) => (data as ModelResponse).model);
|
||||
}
|
||||
|
||||
export function useCreateModel() {
|
||||
@@ -129,12 +128,3 @@ export function useUpdateModel() {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function handleResponse(response: Response): Promise<Model> {
|
||||
if (!response.ok) {
|
||||
const body = (await response.json().catch(() => null)) as null | { error?: string };
|
||||
throw new Error(body?.error ?? `HTTP ${response.status}`);
|
||||
}
|
||||
const data = (await response.json()) as ModelResponse;
|
||||
return data.model;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user