// app/api/directus/choices/route.ts import { NextRequest } from "next/server"; import { directusAdminFetch } from "@/lib/directus"; export const dynamic = "force-dynamic"; export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const collection = String(searchParams.get("collection") || ""); const group = String(searchParams.get("group") || ""); const field = String(searchParams.get("field") || ""); if (!collection || !group || !field) { return Response.json({ error: "collection, group, and field are required" }, { status: 400 }); } // Pull field metadata server-side (admin token), then extract repeater child choices const meta = await directusAdminFetch<{ data: any[] }>( `/fields?filter[collection][_eq]=${encodeURIComponent(collection)}&limit=500` ); const rows: any[] = Array.isArray(meta?.data) ? meta.data : []; const parent = rows.find((r: any) => r?.field === group); const nestedChildren: any[] = parent?.meta?.options?.fields || []; const child = nestedChildren.find((f: any) => f?.field === field) || rows.find((r: any) => r?.field === `${group}.${field}`); const choices: any[] = (child?.options?.choices as any[]) ?? (child?.meta?.options?.choices as any[]) ?? []; const data = choices.map((c: any) => ({ id: String(c.value ?? c.key ?? c.id), label: String(c.text ?? c.label ?? c.name ?? c.value ?? c.id), })); return Response.json({ data }); }