81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
const AUDITS_BASE_URL =
|
|
process.env.AUDITS_BASE_URL ||
|
|
process.env.BE_BASE_URL ||
|
|
"http://localhost:8583";
|
|
const COOKIE_NAME = "auth_token";
|
|
|
|
const DEFAULT_LIMIT = "25";
|
|
const DEFAULT_PAGE = "1";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { cookies } = await import("next/headers");
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get(COOKIE_NAME)?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ message: "Missing Authorization header" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const proxiedParams = new URLSearchParams();
|
|
|
|
// Forward provided params
|
|
searchParams.forEach((value, key) => {
|
|
if (value == null || value === "") return;
|
|
proxiedParams.append(key, value);
|
|
});
|
|
|
|
if (!proxiedParams.has("limit")) {
|
|
proxiedParams.set("limit", DEFAULT_LIMIT);
|
|
}
|
|
if (!proxiedParams.has("page")) {
|
|
proxiedParams.set("page", DEFAULT_PAGE);
|
|
}
|
|
|
|
const backendUrl = `${AUDITS_BASE_URL}/api/v1/audit${
|
|
proxiedParams.size ? `?${proxiedParams.toString()}` : ""
|
|
}`;
|
|
|
|
const response = await fetch(backendUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
cache: "no-store",
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response
|
|
.json()
|
|
.catch(() => ({ message: "Failed to fetch audits" }));
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: errorData?.message || "Failed to fetch audits",
|
|
},
|
|
{ status: response.status }
|
|
);
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log("[AUDITS] data:", data);
|
|
return NextResponse.json(data, { status: response.status });
|
|
} catch (err: unknown) {
|
|
console.log("[AUDITS] error:", err);
|
|
|
|
console.error("Proxy GET /api/v1/audits error:", err);
|
|
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
return NextResponse.json(
|
|
{ message: "Internal server error", error: errorMessage },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|