57 lines
2 KiB
TypeScript
57 lines
2 KiB
TypeScript
// app/api/options/laser_source/route.ts
|
|
export const dynamic = "force-dynamic";
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
|
|
|
function nmForTarget(target?: string | null) {
|
|
// Optional: narrow list per tab; adjust if your data differs.
|
|
switch (target) {
|
|
case "fiber": return 1064;
|
|
case "uv": return 355;
|
|
case "co2-galvo":
|
|
case "co2-gantry": return 10600;
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const ma_at = req.cookies.get("ma_at")?.value;
|
|
if (!ma_at) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
|
|
|
const target = req.nextUrl.searchParams.get("target"); // "fiber" | "uv" | "co2-galvo" | "co2-gantry"
|
|
const url = new URL(`${BASE}/items/laser_source`);
|
|
// Your collection has make/model (no single "name")
|
|
url.searchParams.set("fields", "id,make,model,nm");
|
|
url.searchParams.set("sort", "make,model");
|
|
|
|
const nm = nmForTarget(target);
|
|
if (nm != null) {
|
|
url.searchParams.set("filter[nm][_eq]", String(nm));
|
|
}
|
|
|
|
const res = await fetch(String(url), {
|
|
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
|
|
cache: "no-store",
|
|
});
|
|
|
|
const text = await res.text().catch(() => "");
|
|
if (!res.ok) {
|
|
return NextResponse.json(
|
|
{ error: `Directus ${res.status}: ${text || res.statusText}` },
|
|
{ status: res.status }
|
|
);
|
|
}
|
|
|
|
const json = text ? JSON.parse(text) : { data: [] };
|
|
const data = (json?.data ?? []).map((r: any) => ({
|
|
id: r.id,
|
|
label: [r.make, r.model].filter(Boolean).join(" ").trim() || String(r.id),
|
|
}));
|
|
|
|
return NextResponse.json({ data });
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: e?.message || "Failed to load laser sources" }, { status: 500 });
|
|
}
|
|
}
|