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 { NextResponse } from "next/server";
|
||||||
import { directusFetch } from "@/lib/directus";
|
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<
|
const MAP: Record<
|
||||||
string,
|
string,
|
||||||
{ path: string; fields: string; label: (x: any) => 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 },
|
laser_software: { path: "/items/laser_software", fields: "id,name", label: (x) => x.name },
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(req: Request, ctx: any) {
|
||||||
req: Request,
|
|
||||||
{ params }: { params: { collection: string } }
|
|
||||||
) {
|
|
||||||
const { searchParams } = new URL(req.url);
|
const { searchParams } = new URL(req.url);
|
||||||
const q = searchParams.get("q")?.trim() || "";
|
const q = searchParams.get("q")?.trim() || "";
|
||||||
const limit = Number(searchParams.get("limit") || "400");
|
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];
|
const cfg = MAP[key];
|
||||||
if (!cfg) {
|
if (!cfg) {
|
||||||
// Unknown collection → empty list (prevents 4xx spam in logs)
|
// Unknown collection → return an empty list (avoids noisy 4xx)
|
||||||
return NextResponse.json({ data: [] });
|
return NextResponse.json({ data: [] });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build query (use a dummy base so URL can append params cleanly)
|
// Build Directus URL (dummy base to use URL.searchParams ergonomically)
|
||||||
const url = new URL("http://x" + cfg.path);
|
const u = new URL("http://x" + cfg.path);
|
||||||
url.searchParams.set("fields", cfg.fields);
|
u.searchParams.set("fields", cfg.fields);
|
||||||
url.searchParams.set("limit", String(limit));
|
u.searchParams.set("limit", String(limit));
|
||||||
if (q) url.searchParams.set("search", q);
|
if (q) u.searchParams.set("search", q);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await directusFetch<{ data: any[] }>(
|
const { data } = await directusFetch<{ data: any[] }>(
|
||||||
url.pathname + "?" + url.searchParams.toString()
|
u.pathname + "?" + u.searchParams.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
const out = (data || [])
|
const out = (data || [])
|
||||||
.map((it) => ({
|
.map((it) => ({ id: String(it.id), label: cfg.label(it) ?? String(it.id) }))
|
||||||
id: String(it.id),
|
|
||||||
label: cfg.label(it) ?? String(it.id),
|
|
||||||
}))
|
|
||||||
.sort((a, b) => a.label.localeCompare(b.label));
|
.sort((a, b) => a.label.localeCompare(b.label));
|
||||||
|
|
||||||
return NextResponse.json({ data: out });
|
return NextResponse.json({ data: out });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue