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

26 lines
1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { directusFetch } from "@/lib/directus";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const target = searchParams.get("target") || "";
const group = searchParams.get("group") || "";
const field = searchParams.get("field") || "type";
if (!target || !group) return NextResponse.json({ error: "missing target/group" }, { status: 400 });
const meta = await directusFetch<any>(`/fields/${target}/${group}?fields=meta`);
const fields = meta?.data?.meta?.options?.fields ?? [];
const nested = fields.find((f: any) => (f?.field ?? f?.key) === field);
const choices = nested?.options?.choices ?? nested?.meta?.options?.choices ?? [];
const out = (choices as any[])
.map((c) => ({
id: String(c.value ?? c.text ?? c.label ?? ""),
label: String(c.text ?? c.label ?? c.value ?? ""),
}))
.filter((o) => o.id)
.sort((a, b) => a.label.localeCompare(b.label));
return NextResponse.json({ data: out });
}