From 132c3b10d2fad0c664b5a124692e6709aecd6546 Mon Sep 17 00:00:00 2001 From: makearmy Date: Sun, 28 Sep 2025 23:48:37 -0400 Subject: [PATCH] submit 'type' fixes --- app/api/directus/fields/route.ts | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/api/directus/fields/route.ts b/app/api/directus/fields/route.ts index e69de29b..0d0f5a83 100644 --- a/app/api/directus/fields/route.ts +++ b/app/api/directus/fields/route.ts @@ -0,0 +1,40 @@ +// 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); +}