feat: update Directus API routes, add health endpoint
This commit is contained in:
parent
78f8d225ee
commit
79f0af51eb
7 changed files with 771 additions and 807 deletions
|
|
@ -2,39 +2,53 @@
|
|||
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 ?? "");
|
||||
/**
|
||||
* For fiber, co2-galvo, uv → f-theta scan lenses
|
||||
* For co2-gantry → focusing lenses
|
||||
*/
|
||||
function collectionForTarget(target?: string): { coll: string } | null {
|
||||
switch (target) {
|
||||
case "settings_fiber":
|
||||
case "settings_co2gal":
|
||||
case "settings_uv":
|
||||
return { coll: "laser_scan_lens" };
|
||||
case "settings_co2gan":
|
||||
return { coll: "laser_focusing_lens" };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function bestLensLabel(it: any): string {
|
||||
// Try common fields, then derive something readable
|
||||
if (it.name) return String(it.name);
|
||||
if (it.model) return String(it.model);
|
||||
if (it.focal_length_mm) return `F${it.focal_length_mm} mm`;
|
||||
return String(it.submission_id ?? it.id ?? "lens");
|
||||
}
|
||||
|
||||
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");
|
||||
try {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const q = searchParams.get("q")?.trim() || "";
|
||||
const limit = Number(searchParams.get("limit") || "500");
|
||||
const target = searchParams.get("target") || undefined;
|
||||
|
||||
// Fiber / CO2 Galvo / UV -> scan lens ; CO2 Gantry -> focus lens
|
||||
const isGantry = target === "settings_co2gan";
|
||||
const coll = isGantry ? "laser_focus_lens" : "laser_scan_lens";
|
||||
const cfg = collectionForTarget(target);
|
||||
if (!cfg) return NextResponse.json({ error: "missing/invalid target" }, { status: 400 });
|
||||
|
||||
// Avoid explicit fields -> prevents 403 on disallowed fields
|
||||
const res = await directusFetch<{ data: any[] }>(`/items/${coll}?limit=${limit}`);
|
||||
let items = res?.data ?? [];
|
||||
const url = `/items/${cfg.coll}?limit=${limit}${q ? `&search=${encodeURIComponent(q)}` : ""}`;
|
||||
const { data } = await directusFetch<{ data: any[] }>(url);
|
||||
|
||||
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);
|
||||
const out = (data || [])
|
||||
.map((it) => ({
|
||||
id: String(it.submission_id ?? it.id),
|
||||
label: bestLensLabel(it),
|
||||
}))
|
||||
.sort((a, b) => a.label.localeCompare(b.label));
|
||||
|
||||
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) });
|
||||
return NextResponse.json({ data: out });
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e?.message || "lens error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue