Response constructor rejects bodies on these statuses, so DELETE returning 204 was triggering a 500 in the proxy. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { auth } from "@/auth";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
const BACKEND_URL = process.env.SERVER_API_URL || "http://backend:8080";
|
|
|
|
async function handler(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ path?: string[] }> }
|
|
) {
|
|
const session = await auth();
|
|
const { path } = await params;
|
|
const pathStr = path?.join("/") || "";
|
|
const url = `${BACKEND_URL}/api/${pathStr}${req.nextUrl.search}`;
|
|
|
|
const headers = new Headers(req.headers);
|
|
headers.delete("host");
|
|
|
|
if (session?.user) {
|
|
headers.set("X-User-Id", (session.user as any).id || "");
|
|
headers.set("X-User-Role", (session.user as any).role || "");
|
|
}
|
|
|
|
const body =
|
|
req.method === "GET" || req.method === "HEAD"
|
|
? undefined
|
|
: await req.arrayBuffer();
|
|
|
|
const res = await fetch(url, {
|
|
method: req.method,
|
|
headers,
|
|
body,
|
|
});
|
|
|
|
// Per Fetch spec, Response with 204/205/304 must not have a body.
|
|
const hasBody = res.status !== 204 && res.status !== 205 && res.status !== 304;
|
|
const data = hasBody ? await res.arrayBuffer() : null;
|
|
|
|
return new NextResponse(data, {
|
|
status: res.status,
|
|
statusText: res.statusText,
|
|
headers: res.headers,
|
|
});
|
|
}
|
|
|
|
export const GET = handler;
|
|
export const POST = handler;
|
|
export const PUT = handler;
|
|
export const DELETE = handler;
|
|
export const PATCH = handler;
|
|
export const OPTIONS = handler;
|