submit 'type' fixes
This commit is contained in:
parent
2e6d85ce1d
commit
132c3b10d2
1 changed files with 40 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue