my-settings tab update
This commit is contained in:
parent
3796a1afd7
commit
b41fbd98a0
3 changed files with 280 additions and 18 deletions
|
|
@ -1,22 +1,23 @@
|
|||
// app/api/dx/[...path]/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import { dxGET } from "@/lib/directus";
|
||||
import { dxGET, dxPOST, dxPATCH, dxDELETE } from "@/lib/directus";
|
||||
import { requireBearer } from "@/app/api/_lib/auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic"; // proxy to Directus; never static
|
||||
export const dynamic = "force-dynamic"; // these are true proxies, never prerender
|
||||
|
||||
// GET /api/dx/<anything...>?<query>
|
||||
// Proxies to Directus using the user's ma_at, no caching.
|
||||
function buildPath(req: Request, context: any) {
|
||||
const search = new URL(req.url).search || "";
|
||||
const params = (context?.params ?? {}) as { path?: string[] };
|
||||
const pathParts = Array.isArray(params.path) ? params.path : [];
|
||||
return `/${pathParts.join("/")}${search}`;
|
||||
}
|
||||
|
||||
// GET /api/dx/<anything>?<query>
|
||||
export async function GET(req: Request, context: any) {
|
||||
try {
|
||||
const bearer = requireBearer(req); // ← pulls ma_at from Cookie
|
||||
const search = new URL(req.url).search || "";
|
||||
const params = (context?.params ?? {}) as { path?: string[] };
|
||||
|
||||
const pathParts = Array.isArray(params.path) ? params.path : [];
|
||||
const p = `/${pathParts.join("/")}${search}`;
|
||||
|
||||
const bearer = requireBearer(req);
|
||||
const p = buildPath(req, context);
|
||||
const json = await dxGET<any>(p, bearer);
|
||||
return NextResponse.json(json, { status: 200 });
|
||||
} catch (e: any) {
|
||||
|
|
@ -27,3 +28,57 @@ export async function GET(req: Request, context: any) {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST JSON → /api/dx/<path>
|
||||
export async function POST(req: Request, context: any) {
|
||||
try {
|
||||
const bearer = requireBearer(req);
|
||||
const p = buildPath(req, context);
|
||||
const body = await req.json().catch(() => {
|
||||
throw new Error("Invalid JSON body");
|
||||
});
|
||||
const json = await dxPOST<any>(p, bearer, body);
|
||||
return NextResponse.json(json, { status: 200 });
|
||||
} catch (e: any) {
|
||||
const status = e?.status ?? 500;
|
||||
return NextResponse.json(
|
||||
{ errors: [{ message: e?.message || "Directus proxy error", detail: e?.detail }] },
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH JSON → /api/dx/<path>
|
||||
export async function PATCH(req: Request, context: any) {
|
||||
try {
|
||||
const bearer = requireBearer(req);
|
||||
const p = buildPath(req, context);
|
||||
const body = await req.json().catch(() => {
|
||||
throw new Error("Invalid JSON body");
|
||||
});
|
||||
const json = await dxPATCH<any>(p, bearer, body);
|
||||
return NextResponse.json(json, { status: 200 });
|
||||
} catch (e: any) {
|
||||
const status = e?.status ?? 500;
|
||||
return NextResponse.json(
|
||||
{ errors: [{ message: e?.message || "Directus proxy error", detail: e?.detail }] },
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE → /api/dx/<path>
|
||||
export async function DELETE(req: Request, context: any) {
|
||||
try {
|
||||
const bearer = requireBearer(req);
|
||||
const p = buildPath(req, context);
|
||||
const json = await dxDELETE<any>(p, bearer);
|
||||
return NextResponse.json(json, { status: 200 });
|
||||
} catch (e: any) {
|
||||
const status = e?.status ?? 500;
|
||||
return NextResponse.json(
|
||||
{ errors: [{ message: e?.message || "Directus proxy error", detail: e?.detail }] },
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue