infoalloggi-monorepo/apps/infoalloggi/src/middlewares/apis_middleware.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { type NextRequest, NextResponse } from "next/server";
import { env } from "~/env";
2025-08-04 17:45:44 +02:00
export const apisMiddleware = async (req: NextRequest) => {
2025-08-28 18:27:07 +02:00
const path = req.nextUrl.pathname;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const rewrite_paths = ["/go-api/images/", "/go-api/storage/"];
if (!rewrite_paths.some((p) => path.startsWith(p))) {
return;
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (
path.startsWith("/go-api/images/") ||
path.startsWith("/go-api/storage/")
) {
// Handle image and storage requests
const newPath = path
.replace("/go-api/images/", "/images/")
.replace("/go-api/storage/", "/storage/");
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const url = new URL(`${env.BACKENDSERVER_URL}${newPath}`, req.url);
const res = NextResponse.rewrite(url);
res.headers.set("Access-Control-Allow-Origin", env.NEXT_PUBLIC_BASE_URL);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
res.headers.set(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS",
);
res.headers.set(
"Access-Control-Allow-Headers",
"Content-Type, Authorization",
);
return res;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
// Fetch the content from the backend and serve it through the middleware
/*
2025-08-04 17:45:44 +02:00
const response = await fetch(url.toString(), {
headers: req.headers,
});
return new NextResponse(response.body, {
headers: response.headers,
status: response.status,
2025-08-28 18:27:07 +02:00
});*/
}
2025-08-04 17:45:44 +02:00
};