// app/api/directus/fields/route.ts import { NextRequest, NextResponse } from "next/server"; 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 async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const collection = searchParams.get("collection"); if (!collection) { return NextResponse.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" }); } 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); }