e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { type NextRequest, NextResponse } from "next/server";
|
|
|
|
function getCorsHeaders() {
|
|
return {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
|
"Access-Control-Allow-Headers": "*",
|
|
};
|
|
}
|
|
|
|
async function handleRequest(req: NextRequest, method: string) {
|
|
try {
|
|
const path = req.nextUrl.pathname.replace(/^\/?api\//, "");
|
|
const url = new URL(req.url);
|
|
const searchParams = new URLSearchParams(url.search);
|
|
searchParams.delete("_path");
|
|
searchParams.delete("nxtP_path");
|
|
const queryString = searchParams.toString()
|
|
? `?${searchParams.toString()}`
|
|
: "";
|
|
|
|
const options: RequestInit = {
|
|
method,
|
|
headers: {
|
|
"x-api-key": process.env.LANGCHAIN_API_KEY || "",
|
|
},
|
|
};
|
|
|
|
if (["POST", "PUT", "PATCH"].includes(method)) {
|
|
options.body = await req.text();
|
|
}
|
|
|
|
const res = await fetch(
|
|
`${process.env.LANGGRAPH_API_URL}/${path}${queryString}`,
|
|
options,
|
|
);
|
|
|
|
const headers = new Headers(res.headers);
|
|
headers.delete("content-encoding");
|
|
headers.delete("content-length");
|
|
headers.delete("transfer-encoding");
|
|
const corsHeaders = getCorsHeaders();
|
|
for (const [key, value] of Object.entries(corsHeaders)) {
|
|
headers.set(key, value);
|
|
}
|
|
|
|
return new NextResponse(res.body, {
|
|
status: res.status,
|
|
statusText: res.statusText,
|
|
headers,
|
|
});
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
const typedError = e as Error & { status?: number };
|
|
return NextResponse.json(
|
|
{ error: typedError.message },
|
|
{ status: typedError.status ?? 500 },
|
|
);
|
|
}
|
|
return NextResponse.json({ error: "Unknown error" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export const GET = (req: NextRequest) => handleRequest(req, "GET");
|
|
export const POST = (req: NextRequest) => handleRequest(req, "POST");
|
|
export const PUT = (req: NextRequest) => handleRequest(req, "PUT");
|
|
export const PATCH = (req: NextRequest) => handleRequest(req, "PATCH");
|
|
export const DELETE = (req: NextRequest) => handleRequest(req, "DELETE");
|
|
export const OPTIONS = () =>
|
|
new NextResponse(null, {
|
|
status: 204,
|
|
headers: getCorsHeaders(),
|
|
});
|