switching fields and users calls to API proxy

This commit is contained in:
makearmy 2025-09-29 10:43:53 -04:00
parent b7b3fb53f9
commit fda015531c
4 changed files with 132 additions and 62 deletions

View file

@ -0,0 +1,39 @@
// 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 });
}

View file

@ -1,40 +1,26 @@
// app/api/directus/fields/route.ts
import { NextRequest, NextResponse } from "next/server";
import { NextRequest } from "next/server";
import { directusAdminFetch } from "@/lib/directus";
const DIRECTUS_URL = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
const AUTH_HEADER = process.env.DIRECTUS_TOKEN_SUBMIT
? { Authorization: `Bearer ${process.env.DIRECTUS_TOKEN_SUBMIT}` }
: {};
export const dynamic = "force-dynamic"; // no caching
export const revalidate = 0;
export const dynamic = "force-dynamic";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const collection = searchParams.get("collection");
const collection = searchParams.get("collection")?.trim();
if (!collection) {
return NextResponse.json({ error: "Missing `collection`" }, { status: 400 });
return Response.json({ error: "Missing ?collection" }, { status: 400 });
}
// 1) Preferred: /fields/{collection}
const url1 = `${DIRECTUS_URL}/fields/${encodeURIComponent(collection)}`;
let res = await fetch(url1, { headers: AUTH_HEADER, cache: "no-store" });
// 2) Fallback: /fields?filter[collection][_eq]=...
if (!res.ok) {
const url2 = `${DIRECTUS_URL}/fields?filter[collection][_eq]=${encodeURIComponent(collection)}`;
res = await fetch(url2, { headers: AUTH_HEADER, cache: "no-store" });
try {
// Preferred endpoint
const res = await directusAdminFetch<any>(`/fields/${encodeURIComponent(collection)}`);
const data = Array.isArray(res?.data) ? res.data : Array.isArray(res) ? res : [];
return Response.json({ data });
} catch {
// Fallback (some Directus setups restrict the path variant)
const qs = new URLSearchParams({ "filter[collection][_eq]": collection });
const fb = await directusAdminFetch<any>(`/fields?${qs.toString()}`);
const data = Array.isArray(fb?.data) ? fb.data : Array.isArray(fb) ? fb : [];
return Response.json({ data });
}
if (!res.ok) {
const body = await res.text().catch(() => "");
return NextResponse.json(
{ error: "Directus request failed", status: res.status, body },
{ status: 502 },
);
}
const json = await res.json().catch(() => ({}));
return NextResponse.json(json);
}