refactor: 提取 requireUserId/requireUser/requireMembership 校验工具

- 新增 requireUserId:统一 14 处 userId 非空校验,返回 401
- 新增 requireUser:统一 4 处用户存在性检查,返回 404
- validateMembership 升级为 requireMembership,直接抛 403
- 混合校验拆分为 auth(401) + 字段(400),状态码更准确
This commit is contained in:
2026-02-26 18:17:17 +08:00
parent 0595887480
commit 19edcaeeb5
15 changed files with 67 additions and 56 deletions
+4 -3
View File
@@ -1,14 +1,15 @@
import { NextResponse } from "next/server";
import { atomicUpdateRoom } from "@/lib/store";
import { notify } from "@/lib/roomEvents";
import { apiHandler, ApiError } from "@/lib/api";
import { apiHandler, ApiError, requireUserId } from "@/lib/api";
export const POST = apiHandler(async (req, { params }) => {
const { id } = await params;
const { userId, restaurantId, action } = await req.json();
if (!userId || restaurantId == null || !action) {
throw new ApiError("userId, restaurantId, and action are required");
requireUserId(userId);
if (restaurantId == null || !action) {
throw new ApiError("restaurantId and action are required");
}
const rid = String(restaurantId);