routing fixes
This commit is contained in:
parent
7f75ad7856
commit
a45038241f
4 changed files with 861 additions and 512 deletions
|
|
@ -1,19 +1,16 @@
|
|||
// app/api/me/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
/** Read a cookie value from a raw Cookie header string */
|
||||
function readCookie(name: string, cookieHeader: string) {
|
||||
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
||||
return m?.[1] ?? null;
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
// Prefer DIRECTUS_URL if present; fall back to NEXT_PUBLIC_API_BASE_URL
|
||||
const base =
|
||||
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || "";
|
||||
const url = `${base.replace(/\/$/, "")}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
||||
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
|
||||
// NOTE: include username explicitly
|
||||
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
||||
|
||||
// Forward the incoming cookies (session), and also ma_at as Bearer (token setups)
|
||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
||||
const ma_at = readCookie("ma_at", cookieHeader);
|
||||
|
||||
|
|
@ -21,7 +18,7 @@ export async function GET(req: Request) {
|
|||
if (cookieHeader) headers.cookie = cookieHeader;
|
||||
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
|
||||
|
||||
const res = await fetch(url, { headers, cache: "no-store" });
|
||||
const res = await fetch(url, { headers, cache: "no-store" });
|
||||
const body = await res.json().catch(() => ({}));
|
||||
|
||||
return new NextResponse(JSON.stringify(body), {
|
||||
|
|
|
|||
|
|
@ -2,61 +2,56 @@
|
|||
export const dynamic = "force-dynamic";
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
||||
|
||||
const BASE = (
|
||||
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || ""
|
||||
).replace(/\/$/, "");
|
||||
|
||||
function buildPath() {
|
||||
// This collection uses make/model (no `name` field)
|
||||
const url = new URL(`${BASE}/items/laser_source`);
|
||||
url.searchParams.set("fields", "id,make,model");
|
||||
url.searchParams.set("sort", "make,model");
|
||||
return String(url);
|
||||
}
|
||||
|
||||
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 {}
|
||||
return { res, json, text };
|
||||
function nmForTarget(target?: string | null) {
|
||||
// Optional: narrow list per tab; adjust if your data differs.
|
||||
switch (target) {
|
||||
case "fiber": return 1064;
|
||||
case "uv": return 355;
|
||||
case "co2-galvo":
|
||||
case "co2-gantry": return 10600;
|
||||
default: return 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 ma_at = req.cookies.get("ma_at")?.value;
|
||||
if (!ma_at) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||
|
||||
const target = req.nextUrl.searchParams.get("target"); // "fiber" | "uv" | "co2-galvo" | "co2-gantry"
|
||||
const url = new URL(`${BASE}/items/laser_source`);
|
||||
// Your collection has make/model (no single "name")
|
||||
url.searchParams.set("fields", "id,make,model,nm");
|
||||
url.searchParams.set("sort", "make,model");
|
||||
|
||||
const nm = nmForTarget(target);
|
||||
if (nm != null) {
|
||||
url.searchParams.set("filter[nm][_eq]", String(nm));
|
||||
}
|
||||
|
||||
const r = await dFetch(userAt);
|
||||
if (!r.res.ok) {
|
||||
const res = await fetch(String(url), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const text = await res.text().catch(() => "");
|
||||
if (!res.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: `Directus ${r.res.status}: ${r.text || r.res.statusText}` },
|
||||
{ status: r.res.status }
|
||||
{ error: `Directus ${res.status}: ${text || res.statusText}` },
|
||||
{ status: res.status }
|
||||
);
|
||||
}
|
||||
|
||||
const rows: Array<{ id: string | number; make?: string; model?: string }> =
|
||||
r.json?.data ?? [];
|
||||
|
||||
// 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 };
|
||||
});
|
||||
const json = text ? JSON.parse(text) : { data: [] };
|
||||
const data = (json?.data ?? []).map((r: any) => ({
|
||||
id: r.id,
|
||||
label: [r.make, r.model].filter(Boolean).join(" ").trim() || String(r.id),
|
||||
}));
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
app/api/options/material_opacity/route.ts
Normal file
39
app/api/options/material_opacity/route.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// app/api/options/material_opacity/route.ts
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const ma_at = req.cookies.get("ma_at")?.value;
|
||||
if (!ma_at) 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", "sort,opacity");
|
||||
|
||||
const res = await fetch(String(url), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const text = await res.text().catch(() => "");
|
||||
if (!res.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: `Directus ${res.status}: ${text || res.statusText}` },
|
||||
{ status: res.status }
|
||||
);
|
||||
}
|
||||
|
||||
const json = text ? JSON.parse(text) : { data: [] };
|
||||
const data = (json?.data ?? []).map((r: any) => ({
|
||||
id: r.id,
|
||||
label: r.opacity ?? String(r.id),
|
||||
}));
|
||||
|
||||
return NextResponse.json({ data });
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e?.message || "Failed to load opacity options" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue