import { NextRequest, NextResponse } from "next/server"; export const dynamic = "force-dynamic"; const DROP = new Set(["host", "connection", "content-length", "transfer-encoding"]); async function proxy(request: NextRequest, context: { params: Promise<{ path: string[] }> }) { const base = process.env.API_INTERNAL_URL; if (!base) { return NextResponse.json({ detail: "API_INTERNAL_URL is not set" }, { status: 500 }); } const { path } = await context.params; const target = new URL(`${base.replace(/\/$/, "")}/${path.join("/")}`); target.search = request.nextUrl.search; const headers = new Headers(); request.headers.forEach((value, key) => { if (!DROP.has(key.toLowerCase())) { headers.set(key, value); } }); const hasBody = request.method !== "GET" && request.method !== "HEAD"; const upstream = await fetch(target, { method: request.method, headers, body: hasBody ? await request.arrayBuffer() : undefined, cache: "no-store", redirect: "manual", }); const responseHeaders = new Headers(); upstream.headers.forEach((value, key) => { const name = key.toLowerCase(); if (name === "content-encoding" || name === "content-length" || name === "transfer-encoding" || name === "set-cookie") { return; } responseHeaders.set(key, value); }); const cookies = typeof upstream.headers.getSetCookie === "function" ? upstream.headers.getSetCookie() : []; for (const cookie of cookies) { responseHeaders.append("set-cookie", cookie); } return new NextResponse(await upstream.arrayBuffer(), { status: upstream.status, headers: responseHeaders, }); } export const GET = proxy; export const POST = proxy; export const PUT = proxy; export const PATCH = proxy; export const DELETE = proxy;