build bug fix
This commit is contained in:
parent
1903762e3f
commit
aa25a41581
1 changed files with 16 additions and 18 deletions
|
|
@ -2,8 +2,10 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { directusFetch } from "@/lib/directus";
|
||||
|
||||
// Generic collections handled by this route.
|
||||
// (laser_source has its own route so it's NOT included here)
|
||||
/**
|
||||
* Generic option lists resolved by collection name.
|
||||
* NOTE: laser_source and lens have their own dedicated routes.
|
||||
*/
|
||||
const MAP: Record<
|
||||
string,
|
||||
{ path: string; fields: string; label: (x: any) => string }
|
||||
|
|
@ -15,37 +17,33 @@ string,
|
|||
laser_software: { path: "/items/laser_software", fields: "id,name", label: (x) => x.name },
|
||||
};
|
||||
|
||||
export async function GET(
|
||||
req: Request,
|
||||
{ params }: { params: { collection: string } }
|
||||
) {
|
||||
export async function GET(req: Request, ctx: any) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const q = searchParams.get("q")?.trim() || "";
|
||||
const limit = Number(searchParams.get("limit") || "400");
|
||||
const key = params.collection;
|
||||
|
||||
// Use a loose type for the 2nd arg to avoid Next 15's strict route signature check
|
||||
const key = String(ctx?.params?.collection ?? "");
|
||||
|
||||
const cfg = MAP[key];
|
||||
if (!cfg) {
|
||||
// Unknown collection → empty list (prevents 4xx spam in logs)
|
||||
// Unknown collection → return an empty list (avoids noisy 4xx)
|
||||
return NextResponse.json({ data: [] });
|
||||
}
|
||||
|
||||
// Build query (use a dummy base so URL can append params cleanly)
|
||||
const url = new URL("http://x" + cfg.path);
|
||||
url.searchParams.set("fields", cfg.fields);
|
||||
url.searchParams.set("limit", String(limit));
|
||||
if (q) url.searchParams.set("search", q);
|
||||
// Build Directus URL (dummy base to use URL.searchParams ergonomically)
|
||||
const u = new URL("http://x" + cfg.path);
|
||||
u.searchParams.set("fields", cfg.fields);
|
||||
u.searchParams.set("limit", String(limit));
|
||||
if (q) u.searchParams.set("search", q);
|
||||
|
||||
try {
|
||||
const { data } = await directusFetch<{ data: any[] }>(
|
||||
url.pathname + "?" + url.searchParams.toString()
|
||||
u.pathname + "?" + u.searchParams.toString()
|
||||
);
|
||||
|
||||
const out = (data || [])
|
||||
.map((it) => ({
|
||||
id: String(it.id),
|
||||
label: cfg.label(it) ?? String(it.id),
|
||||
}))
|
||||
.map((it) => ({ id: String(it.id), label: cfg.label(it) ?? String(it.id) }))
|
||||
.sort((a, b) => a.label.localeCompare(b.label));
|
||||
|
||||
return NextResponse.json({ data: out });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue