refactor: 引入 apiHandler + ApiError,消除 20 个路由的 try/catch 样板

- 新增 src/lib/api.ts:ApiError 错误类 + apiHandler 统一包装器
- 20 个 API 路由统一使用 apiHandler,删除重复的 try/catch 块
- 验证错误改用 throw new ApiError(),减少嵌套层级
- join/manage 路由的错误码映射改为直接抛出 ApiError
- 删除已无引用的 errorResponse 辅助函数
- 净减 273 行代码
This commit is contained in:
2026-02-26 18:08:47 +08:00
parent d4c6da57a1
commit 0595887480
22 changed files with 626 additions and 863 deletions
+16 -31
View File
@@ -1,17 +1,14 @@
import { NextRequest, NextResponse } from "next/server";
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import bcrypt from "bcryptjs";
import { apiHandler, ApiError } from "@/lib/api";
export async function GET(req: NextRequest) {
export const GET = apiHandler(async (req) => {
const userId = req.nextUrl.searchParams.get("id");
if (!userId) {
return NextResponse.json(null);
}
if (!userId) return NextResponse.json(null);
const user = await prisma.user.findUnique({ where: { id: userId } });
if (!user) {
return NextResponse.json(null);
}
if (!user) return NextResponse.json(null);
const decisionCount = await prisma.decision.count({ where: { userId } });
@@ -24,48 +21,36 @@ export async function GET(req: NextRequest) {
createdAt: user.createdAt.toISOString(),
decisionCount,
});
}
});
export async function PUT(req: NextRequest) {
export const PUT = apiHandler(async (req) => {
const body = await req.json();
const { userId } = body;
if (!userId) {
return NextResponse.json({ error: "缺少用户 ID" }, { status: 400 });
}
if (!userId) throw new ApiError("缺少用户 ID");
const existing = await prisma.user.findUnique({ where: { id: userId } });
if (!existing) {
return NextResponse.json({ error: "用户不存在" }, { status: 404 });
}
if (!existing) throw new ApiError("用户不存在", 404);
const updateData: Record<string, unknown> = {};
if (body.username !== undefined) {
const trimmed = body.username.trim();
if (trimmed.length < 2 || trimmed.length > 16) {
return NextResponse.json({ error: "用户名需要 2-16 个字符" }, { status: 400 });
throw new ApiError("用户名需要 2-16 个字符");
}
if (trimmed !== existing.username) {
const taken = await prisma.user.findUnique({ where: { username: trimmed } });
if (taken) {
return NextResponse.json({ error: "用户名已被占用" }, { status: 409 });
}
if (taken) throw new ApiError("用户名已被占用", 409);
}
updateData.username = trimmed;
}
if (body.newPassword !== undefined) {
if (!body.currentPassword) {
return NextResponse.json({ error: "请输入当前密码" }, { status: 400 });
}
if (!body.currentPassword) throw new ApiError("请输入当前密码");
const valid = await bcrypt.compare(body.currentPassword, existing.passwordHash);
if (!valid) {
return NextResponse.json({ error: "当前密码错误" }, { status: 403 });
}
if (body.newPassword.length < 6) {
return NextResponse.json({ error: "新密码至少 6 个字符" }, { status: 400 });
}
if (!valid) throw new ApiError("当前密码错误", 403);
if (body.newPassword.length < 6) throw new ApiError("新密码至少 6 个字符");
updateData.passwordHash = await bcrypt.hash(body.newPassword, 10);
}
@@ -75,7 +60,7 @@ export async function PUT(req: NextRequest) {
if (body.email !== undefined) {
if (body.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body.email)) {
return NextResponse.json({ error: "邮箱格式不正确" }, { status: 400 });
throw new ApiError("邮箱格式不正确");
}
updateData.email = body.email || null;
}
@@ -96,4 +81,4 @@ export async function PUT(req: NextRequest) {
email: user.email,
preferences: JSON.parse(user.preferences),
});
}
});