Proxy API through Next.js route handlers — eliminates CORS
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
const BACKEND = process.env.HERMES_BACKEND_URL || "http://127.0.0.1:8643";
|
||||||
|
|
||||||
|
export async function GET(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
|
||||||
|
const { path } = await params;
|
||||||
|
const target = path.join("/");
|
||||||
|
const qs = req.nextUrl.search;
|
||||||
|
const res = await fetch(`${BACKEND}/api/${target}${qs}`);
|
||||||
|
const data = await res.text();
|
||||||
|
return new NextResponse(data, {
|
||||||
|
status: res.status,
|
||||||
|
headers: { "content-type": res.headers.get("content-type") || "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
|
||||||
|
const { path } = await params;
|
||||||
|
const target = path.join("/");
|
||||||
|
const body = await req.text();
|
||||||
|
const res = await fetch(`${BACKEND}/api/${target}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
const data = await res.text();
|
||||||
|
return new NextResponse(data, {
|
||||||
|
status: res.status,
|
||||||
|
headers: { "content-type": res.headers.get("content-type") || "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
|
||||||
|
const { path } = await params;
|
||||||
|
const target = path.join("/");
|
||||||
|
const body = await req.text();
|
||||||
|
const res = await fetch(`${BACKEND}/api/${target}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
const data = await res.text();
|
||||||
|
return new NextResponse(data, {
|
||||||
|
status: res.status,
|
||||||
|
headers: { "content-type": res.headers.get("content-type") || "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
|
||||||
|
const { path } = await params;
|
||||||
|
const target = path.join("/");
|
||||||
|
const body = await req.text();
|
||||||
|
const res = await fetch(`${BACKEND}/api/${target}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: body || undefined,
|
||||||
|
});
|
||||||
|
const data = await res.text();
|
||||||
|
return new NextResponse(data, {
|
||||||
|
status: res.status,
|
||||||
|
headers: { "content-type": res.headers.get("content-type") || "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
const BACKEND = process.env.HERMES_BACKEND_URL || "http://127.0.0.1:8643";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${BACKEND}/health`);
|
||||||
|
const data = await res.text();
|
||||||
|
return new NextResponse(data, {
|
||||||
|
status: res.status,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return NextResponse.json({ status: "error" }, { status: 502 });
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
const BASE = process.env.NEXT_PUBLIC_HERMES_API || "http://localhost:8787";
|
const BASE = "";
|
||||||
|
|
||||||
async function get<T>(path: string): Promise<T> {
|
async function get<T>(path: string): Promise<T> {
|
||||||
const res = await fetch(`${BASE}${path}`);
|
const res = await fetch(`${BASE}${path}`);
|
||||||
@@ -85,4 +85,4 @@ export const fetchSkill = (name: string) =>
|
|||||||
export const fetchConfig = () => get<ConfigResponse>("/api/config");
|
export const fetchConfig = () => get<ConfigResponse>("/api/config");
|
||||||
|
|
||||||
// Health
|
// Health
|
||||||
export const fetchHealth = () => get<{ status: string }>("/api/health");
|
export const fetchHealth = () => get<{ status: string }>("/health");
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { NextConfig } from "next";
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
output: "standalone",
|
|
||||||
reactStrictMode: true,
|
reactStrictMode: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 546 B |
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
Reference in New Issue
Block a user