-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4b272e
commit 2fe3409
Showing
8 changed files
with
79 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export const runtime = "edge"; | ||
|
||
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.API_BASE_URL}/${path}${queryString}`, | ||
options, | ||
); | ||
|
||
return new NextResponse(res.body, { | ||
status: res.status, | ||
statusText: res.statusText, | ||
headers: { | ||
...res.headers, | ||
...getCorsHeaders(), | ||
}, | ||
}); | ||
} catch (e: any) { | ||
return NextResponse.json({ error: e.message }, { status: e.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"); | ||
|
||
// Add a new OPTIONS handler | ||
export const OPTIONS = () => { | ||
return new NextResponse(null, { | ||
status: 204, | ||
headers: { | ||
...getCorsHeaders(), | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
export const API_BASE_URL = | ||
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:8123"; | ||
export const LANGCHAIN_API_KEY = process.env.NEXT_PUBLIC_LANGCHAIN_API_KEY; | ||
|
||
export const RESPONSE_FEEDBACK_KEY = "user_score"; | ||
export const SOURCE_CLICK_KEY = "user_click"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters