hopefully final username and source/lens submission fix
This commit is contained in:
parent
28d363cdfe
commit
23ede9c872
5 changed files with 736 additions and 195 deletions
|
|
@ -3,41 +3,39 @@ export const dynamic = "force-dynamic";
|
|||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const BASE =
|
||||
(process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || "").replace(/\/$/, "");
|
||||
const BASE = (
|
||||
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || ""
|
||||
).replace(/\/$/, "");
|
||||
|
||||
function buildPath(target?: string | null) {
|
||||
function buildPath() {
|
||||
// This collection uses make/model (no `name` field)
|
||||
const url = new URL(`${BASE}/items/laser_source`);
|
||||
url.searchParams.set("fields", "id,name");
|
||||
url.searchParams.set("sort", "name");
|
||||
// if (target) url.searchParams.set("filter[target][_eq]", target);
|
||||
url.searchParams.set("fields", "id,make,model");
|
||||
url.searchParams.set("sort", "make,model");
|
||||
return String(url);
|
||||
}
|
||||
|
||||
function readCookieFromHeader(name: string, cookieHeader: string) {
|
||||
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
||||
return m?.[1] ?? null;
|
||||
}
|
||||
|
||||
async function dFetch(token: string, target?: string | null) {
|
||||
const res = await fetch(buildPath(target), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${token}` },
|
||||
async function dFetch(bearer: string) {
|
||||
const res = await fetch(buildPath(), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${bearer}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
const text = await res.text().catch(() => "");
|
||||
let json: any = null;
|
||||
try { json = text ? JSON.parse(text) : null; } catch {}
|
||||
try {
|
||||
json = text ? JSON.parse(text) : null;
|
||||
} catch {}
|
||||
return { res, json, text };
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
||||
const userAt = req.cookies.get("ma_at")?.value || readCookieFromHeader("ma_at", cookieHeader);
|
||||
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||
const userAt = req.cookies.get("ma_at")?.value;
|
||||
if (!userAt) {
|
||||
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||
}
|
||||
|
||||
const target = req.nextUrl.searchParams.get("target");
|
||||
const r = await dFetch(userAt, target);
|
||||
const r = await dFetch(userAt);
|
||||
if (!r.res.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: `Directus ${r.res.status}: ${r.text || r.res.statusText}` },
|
||||
|
|
@ -45,14 +43,20 @@ export async function GET(req: NextRequest) {
|
|||
);
|
||||
}
|
||||
|
||||
const rows: Array<{ id: number | string; name?: string; label?: string; title?: string }> =
|
||||
const rows: Array<{ id: string | number; make?: string; model?: string }> =
|
||||
r.json?.data ?? [];
|
||||
const data = rows.map(({ id, name, label, title }) => ({
|
||||
id,
|
||||
name: name || label || title || "",
|
||||
}));
|
||||
|
||||
// Compose human label from make + model
|
||||
const data = rows.map(({ id, make, model }) => {
|
||||
const label = [make, model].filter(Boolean).join(" ");
|
||||
return { id, name: label, label };
|
||||
});
|
||||
|
||||
return NextResponse.json({ data });
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e?.message || "Failed to load laser sources" }, { status: 500 });
|
||||
return NextResponse.json(
|
||||
{ error: e?.message || "Failed to load laser sources" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue