rig_type route fix 2
This commit is contained in:
parent
77e30981c3
commit
e58f5aaff1
1 changed files with 36 additions and 15 deletions
|
|
@ -1,29 +1,50 @@
|
||||||
// app/api/options/rig_type/route.ts
|
// app/api/options/rig_type/route.ts
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { directusFetch } from "@/lib/directus";
|
|
||||||
|
|
||||||
/**
|
const BASE = process.env.DIRECTUS_URL!;
|
||||||
* Returns [{ id, label }] from the Directus collection `user_rig_type`,
|
const SUBMIT = process.env.DIRECTUS_TOKEN_SUBMIT || "";
|
||||||
* sorted by the `sort` field. Keeping this dedicated route avoids
|
|
||||||
* depending on the generic [collection] mapping.
|
const q = `/items/user_rig_type?fields=id,name&sort=sort`;
|
||||||
*/
|
const url = (BASE || "").replace(/\/$/, "") + q;
|
||||||
|
|
||||||
|
async function tryFetch(headers: HeadersInit) {
|
||||||
|
const res = await fetch(url, { headers });
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
let json: any = null;
|
||||||
|
try {
|
||||||
|
json = text ? JSON.parse(text) : null;
|
||||||
|
} catch {}
|
||||||
|
return { res, json, text };
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(_req: NextRequest) {
|
export async function GET(_req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const res = await directusFetch<{ data: { id: number | string; name: string }[] }>(
|
// 1) Try with submit token
|
||||||
`/items/user_rig_type?fields=id,name&sort=sort`
|
let { res, json, text } = await tryFetch({
|
||||||
);
|
Accept: "application/json",
|
||||||
|
...(SUBMIT ? { Authorization: `Bearer ${SUBMIT}` } : {}),
|
||||||
|
});
|
||||||
|
|
||||||
const items = (res?.data ?? []).map(({ id, name }) => ({
|
// 2) If forbidden, retry anonymously (Public role)
|
||||||
id,
|
if (res.status === 403) {
|
||||||
label: name,
|
({ res, json, text } = await tryFetch({ Accept: "application/json" }));
|
||||||
}));
|
}
|
||||||
|
|
||||||
return NextResponse.json({ data: items }, { status: 200 });
|
if (!res.ok) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: `Directus ${res.status}: ${text || res.statusText}` },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const items: Array<{ id: string | number; name: string }> = json?.data ?? [];
|
||||||
|
const data = items.map(({ id, name }) => ({ id, label: name }));
|
||||||
|
return NextResponse.json({ data }, { status: 200 });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
// Surface useful error text in case permissions are off
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: e?.message || "Failed to load rig types" },
|
{ error: e?.message || "Failed to load rig types" },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue