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:
@@ -1,11 +1,10 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
export const GET = apiHandler(async (req) => {
|
||||
const userId = req.nextUrl.searchParams.get("userId");
|
||||
if (!userId) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
if (!userId) return NextResponse.json([]);
|
||||
|
||||
const favorites = await prisma.favorite.findMany({
|
||||
where: { userId },
|
||||
@@ -20,19 +19,15 @@ export async function GET(req: NextRequest) {
|
||||
createdAt: f.createdAt.toISOString(),
|
||||
})),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { userId, restaurant } = await req.json();
|
||||
|
||||
if (!userId || !restaurant) {
|
||||
return NextResponse.json({ error: "缺少必要字段" }, { status: 400 });
|
||||
}
|
||||
if (!userId || !restaurant) throw new ApiError("缺少必要字段");
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "请先设置个人资料" }, { status: 404 });
|
||||
}
|
||||
if (!user) throw new ApiError("请先设置个人资料", 404);
|
||||
|
||||
const existing = await prisma.favorite.findFirst({
|
||||
where: {
|
||||
@@ -53,20 +48,16 @@ export async function POST(req: NextRequest) {
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: fav.id });
|
||||
}
|
||||
});
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
export const DELETE = apiHandler(async (req) => {
|
||||
const { userId, favoriteId } = await req.json();
|
||||
|
||||
if (!userId || !favoriteId) {
|
||||
return NextResponse.json({ error: "缺少必要字段" }, { status: 400 });
|
||||
}
|
||||
if (!userId || !favoriteId) throw new ApiError("缺少必要字段");
|
||||
|
||||
const fav = await prisma.favorite.findUnique({ where: { id: favoriteId } });
|
||||
if (!fav || fav.userId !== userId) {
|
||||
return NextResponse.json({ error: "收藏不存在" }, { status: 404 });
|
||||
}
|
||||
if (!fav || fav.userId !== userId) throw new ApiError("收藏不存在", 404);
|
||||
|
||||
await prisma.favorite.delete({ where: { id: favoriteId } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
const MAX_HISTORY = 50;
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
export const GET = apiHandler(async (req) => {
|
||||
const userId = req.nextUrl.searchParams.get("userId");
|
||||
if (!userId) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
if (!userId) return NextResponse.json([]);
|
||||
|
||||
const decisions = await prisma.decision.findMany({
|
||||
where: { userId },
|
||||
@@ -26,20 +25,18 @@ export async function GET(req: NextRequest) {
|
||||
createdAt: d.createdAt.toISOString(),
|
||||
})),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { userId, roomId, restaurant, matchType, participants } =
|
||||
await req.json();
|
||||
|
||||
if (!userId || !roomId || !restaurant || !matchType) {
|
||||
return NextResponse.json({ error: "缺少必要字段" }, { status: 400 });
|
||||
throw new ApiError("缺少必要字段");
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "用户未注册" }, { status: 404 });
|
||||
}
|
||||
if (!user) throw new ApiError("用户未注册", 404);
|
||||
|
||||
const existing = await prisma.decision.findFirst({
|
||||
where: { userId, roomId },
|
||||
@@ -73,4 +70,4 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
return NextResponse.json({ id: decision.id });
|
||||
}
|
||||
});
|
||||
|
||||
+16
-31
@@ -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),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user