Files
2026-05-30 02:34:22 +05:30

71 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
const BFF_BASE = process.env.BFF_URL || "http://localhost:8080";
export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;
const q = searchParams.get("q") || "";
const type = searchParams.get("type") || "all";
const page = searchParams.get("page") || "1";
const limit = searchParams.get("limit") || "20";
const exact = searchParams.get("exact") === "1" ? "1" : "";
if (!q.trim()) {
return NextResponse.json({
query: "",
type,
results: [],
total: 0,
page: 1,
limit: 20,
total_pages: 0,
});
}
try {
const url = new URL("/api/v1/search", BFF_BASE);
url.searchParams.set("q", q);
url.searchParams.set("type", type);
url.searchParams.set("page", page);
url.searchParams.set("limit", limit);
if (exact) url.searchParams.set("exact", "1");
const res = await fetch(url.toString(), {
headers: { Accept: "application/json" },
});
if (!res.ok) {
return NextResponse.json(
{
query: q,
type,
results: [],
total: 0,
page: Number(page),
limit: Number(limit),
total_pages: 0,
},
{ status: res.status },
);
}
const data = await res.json();
// Unwrap BFF envelope: { data: { ... } } → { ... }
const payload = data.data ?? data;
return NextResponse.json(payload);
} catch {
return NextResponse.json(
{
query: q,
type,
results: [],
total: 0,
page: Number(page),
limit: Number(limit),
total_pages: 0,
},
{ status: 502 },
);
}
}