2023-09-12 18:51:02 +00:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
async function handle(
|
|
|
|
req: NextRequest,
|
|
|
|
{ params }: { params: { path: string[] } },
|
|
|
|
) {
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const [protocol, ...subpath] = params.path;
|
|
|
|
const targetUrl = `${protocol}://${subpath.join("/")}`;
|
|
|
|
|
|
|
|
const method = req.headers.get("method") ?? undefined;
|
|
|
|
const shouldNotHaveBody = ["get", "head"].includes(
|
|
|
|
method?.toLowerCase() ?? "",
|
|
|
|
);
|
|
|
|
|
|
|
|
const fetchOptions: RequestInit = {
|
|
|
|
headers: {
|
|
|
|
authorization: req.headers.get("authorization") ?? "",
|
|
|
|
},
|
|
|
|
body: shouldNotHaveBody ? null : req.body,
|
|
|
|
method,
|
|
|
|
// @ts-ignore
|
|
|
|
duplex: "half",
|
|
|
|
};
|
|
|
|
|
2023-09-18 18:12:43 +00:00
|
|
|
const fetchResult = await fetch(targetUrl, fetchOptions);
|
2023-09-12 18:51:02 +00:00
|
|
|
|
2023-09-18 18:12:43 +00:00
|
|
|
console.log("[Any Proxy]", targetUrl, {
|
|
|
|
status: fetchResult.status,
|
|
|
|
statusText: fetchResult.statusText,
|
|
|
|
});
|
2023-09-12 18:51:02 +00:00
|
|
|
|
|
|
|
return fetchResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const POST = handle;
|
2023-09-18 18:12:43 +00:00
|
|
|
export const GET = handle;
|
|
|
|
export const OPTIONS = handle;
|
2023-09-12 18:51:02 +00:00
|
|
|
|
2023-09-18 18:21:31 +00:00
|
|
|
export const runtime = "nodejs";
|