File size: 1,364 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { buildBackendAuthHeaders } from "@/lib/backend-auth";
import { backendTraceHeaders } from "@/server/backend-api";
export async function POST(request: Request) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const payload = await request.text();
const apiUrl =
process.env.BACKEND_INTERNAL_URL ||
process.env.NEXT_PUBLIC_API_URL ||
"http://localhost:8000";
const idempotencyKey = request.headers.get("idempotency-key");
const upstream = await fetch(`${apiUrl}/tasks/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...buildBackendAuthHeaders(session.user.id),
...(await backendTraceHeaders()),
...(idempotencyKey ? { "idempotency-key": idempotencyKey } : {}),
},
body: payload,
});
const responseText = await upstream.text();
const traceId = upstream.headers.get("x-trace-id");
return new NextResponse(responseText, {
status: upstream.status,
headers: {
"Content-Type": upstream.headers.get("content-type") || "application/json",
...(traceId ? { "x-trace-id": traceId } : {}),
},
});
}
|