routing fixes
This commit is contained in:
parent
0ee9686fb1
commit
44a690f4d2
20 changed files with 769 additions and 962 deletions
|
|
@ -1,36 +1,12 @@
|
|||
// app/api/options/material_opacity/route.ts
|
||||
export const dynamic = "force-dynamic";
|
||||
import { NextRequest } from "next/server";
|
||||
import { dFetchJSON, applyQFilter, json, Option } from "../_lib";
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
||||
type Row = { id: number | string; opacity?: string | null };
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const userAt = req.cookies.get("ma_at")?.value;
|
||||
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||
|
||||
const url = new URL(`${BASE}/items/material_opacity`);
|
||||
url.searchParams.set("fields", "id,opacity");
|
||||
url.searchParams.set("sort", "opacity");
|
||||
|
||||
const res = await fetch(String(url), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${userAt}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const text = await res.text().catch(() => "");
|
||||
const json = text ? JSON.parse(text) : {};
|
||||
if (!res.ok) {
|
||||
return NextResponse.json({ error: `Directus ${res.status}: ${text || res.statusText}` }, { status: res.status });
|
||||
}
|
||||
|
||||
const rows: Array<{ id: number | string; opacity?: string | number }> = json?.data ?? [];
|
||||
const data = rows
|
||||
.map(({ id, opacity }) => ({ id, label: String(opacity ?? "") }))
|
||||
.filter((x) => x.label);
|
||||
|
||||
return NextResponse.json({ data });
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e?.message || "Failed to load opacity options" }, { status: 500 });
|
||||
}
|
||||
const q = new URL(req.url).searchParams.get("q");
|
||||
// Ensure role can read id,opacity on this collection.
|
||||
const { data } = await dFetchJSON<{ data: Row[] }>(req, "/items/material_opacity?fields=id,opacity&limit=1000&sort=opacity");
|
||||
const options: Option[] = data.map((r) => ({ id: r.id, label: r.opacity ?? String(r.id) }));
|
||||
return json(applyQFilter(options, q, (o) => o.label));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue