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
+29 -44
View File
@@ -1,54 +1,39 @@
import { NextResponse } from "next/server";
import { apiHandler, ApiError } from "@/lib/api";
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const lat = searchParams.get("lat");
const lng = searchParams.get("lng");
export const GET = apiHandler(async (req) => {
const lat = req.nextUrl.searchParams.get("lat");
const lng = req.nextUrl.searchParams.get("lng");
if (!lat || !lng) {
return NextResponse.json(
{ error: "lat and lng are required" },
{ status: 400 },
);
}
if (!lat || !lng) throw new ApiError("lat and lng are required");
const apiKey = process.env.AMAP_API_KEY;
if (!apiKey) {
return NextResponse.json(
{ error: "AMAP_API_KEY not configured" },
{ status: 500 },
);
}
if (!apiKey) throw new ApiError("AMAP_API_KEY not configured", 500);
try {
const url = new URL("https://restapi.amap.com/v3/geocode/regeo");
url.searchParams.set("key", apiKey);
url.searchParams.set("location", `${lng},${lat}`);
url.searchParams.set("extensions", "base");
const url = new URL("https://restapi.amap.com/v3/geocode/regeo");
url.searchParams.set("key", apiKey);
url.searchParams.set("location", `${lng},${lat}`);
url.searchParams.set("extensions", "base");
const res = await fetch(url.toString());
const data = await res.json();
const res = await fetch(url.toString());
const data = await res.json();
if (data.status !== "1" || !data.regeocode) {
return NextResponse.json({ name: null });
}
const comp = data.regeocode.addressComponent;
const district = comp?.district || comp?.city || "";
const township = comp?.township || "";
const neighborhood = comp?.neighborhood?.name || "";
const name = [district, township, neighborhood]
.filter(Boolean)
.join(" ")
.trim();
return NextResponse.json({
name: name || data.regeocode.formatted_address || null,
formatted: data.regeocode.formatted_address || null,
});
} catch (e) {
console.error("Regeo error:", e);
if (data.status !== "1" || !data.regeocode) {
return NextResponse.json({ name: null });
}
}
const comp = data.regeocode.addressComponent;
const district = comp?.district || comp?.city || "";
const township = comp?.township || "";
const neighborhood = comp?.neighborhood?.name || "";
const name = [district, township, neighborhood]
.filter(Boolean)
.join(" ")
.trim();
return NextResponse.json({
name: name || data.regeocode.formatted_address || null,
formatted: data.regeocode.formatted_address || null,
});
});
+28 -43
View File
@@ -1,52 +1,37 @@
import { NextResponse } from "next/server";
import { apiHandler, ApiError } from "@/lib/api";
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const keywords = searchParams.get("keywords")?.trim();
if (!keywords) {
return NextResponse.json([]);
}
export const GET = apiHandler(async (req) => {
const keywords = req.nextUrl.searchParams.get("keywords")?.trim();
if (!keywords) return NextResponse.json([]);
const apiKey = process.env.AMAP_API_KEY;
if (!apiKey) {
return NextResponse.json(
{ error: "AMAP_API_KEY not configured" },
{ status: 500 },
);
}
if (!apiKey) throw new ApiError("AMAP_API_KEY not configured", 500);
try {
const url = new URL("https://restapi.amap.com/v3/assistant/inputtips");
url.searchParams.set("key", apiKey);
url.searchParams.set("keywords", keywords);
url.searchParams.set("datatype", "poi");
const url = new URL("https://restapi.amap.com/v3/assistant/inputtips");
url.searchParams.set("key", apiKey);
url.searchParams.set("keywords", keywords);
url.searchParams.set("datatype", "poi");
const res = await fetch(url.toString());
const data = await res.json();
const res = await fetch(url.toString());
const data = await res.json();
if (data.status !== "1" || !data.tips) {
return NextResponse.json([]);
}
if (data.status !== "1" || !data.tips) return NextResponse.json([]);
const suggestions = data.tips
.filter((t: { location?: string }) => t.location && t.location !== "")
.slice(0, 8)
.map((t: { id: string; name: string; district?: string; address?: string; location: string }) => {
const [lng, lat] = t.location.split(",").map(Number);
return {
id: t.id,
name: t.name,
district: t.district || "",
address: t.address || "",
lat,
lng,
};
});
const suggestions = data.tips
.filter((t: { location?: string }) => t.location && t.location !== "")
.slice(0, 8)
.map((t: { id: string; name: string; district?: string; address?: string; location: string }) => {
const [lng, lat] = t.location.split(",").map(Number);
return {
id: t.id,
name: t.name,
district: t.district || "",
address: t.address || "",
lat,
lng,
};
});
return NextResponse.json(suggestions);
} catch (e) {
console.error("Location suggest error:", e);
return NextResponse.json([]);
}
}
return NextResponse.json(suggestions);
});