makearmy-app/app/api/options/lens/route.ts
2025-09-22 10:37:53 -04:00

40 lines
1.5 KiB
TypeScript

// app/api/options/lens/route.ts
import { NextResponse } from "next/server";
import { directusFetch } from "@/lib/directus";
/** pick a decent label from whatever fields are readable */
function pickLabel(it: any) {
const mm = [it?.make, it?.model].filter(Boolean).join(" ").trim();
if (mm) return mm;
if (it?.name) return String(it.name);
const f = it?.focal_length ?? it?.f ?? it?.fl;
if (f != null) return `${mm ? mm + " " : ""}${f} mm`.trim();
return String(it?.label ?? it?.title ?? it?.id ?? "");
}
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const target = searchParams.get("target") || ""; // required
const q = (searchParams.get("q") || "").toLowerCase();
const limit = Number(searchParams.get("limit") || "500");
// Fiber / CO2 Galvo / UV -> scan lens ; CO2 Gantry -> focus lens
const isGantry = target === "settings_co2gan";
const coll = isGantry ? "laser_focus_lens" : "laser_scan_lens";
// Avoid explicit fields -> prevents 403 on disallowed fields
const res = await directusFetch<{ data: any[] }>(`/items/${coll}?limit=${limit}`);
let items = res?.data ?? [];
let rows = items.map((it) => {
const label = pickLabel(it);
const search = Object.values(it ?? {}).join(" ").toLowerCase();
return { id: String(it?.id ?? ""), label, _search: search };
}).filter((r) => r.id);
if (q) rows = rows.filter((r) => r._search.includes(q));
rows.sort((a, b) => a.label.localeCompare(b.label));
return NextResponse.json({ data: rows.map(({ _search, ...r }) => r) });
}