Skip to content

Commit d8c6f0a

Browse files
fix(api/jai/search): ensure CORS headers on all responses, add OPTION… (#232)
## Description This PR improves CORS handling in 'src/pages/api/jai/search.js'. Changes made: 1. Added an OPTIONS handler to properly respond to preflight requests with the required CORS headers. 2. Added early-return validation for invalid request bodies (messages missing / not an array) and ensured those responses also include CORS headers. 3. Ensured consistency so that all responses (including errors) return the defined CORS headers. ## Related Issue Fixes #218 ## Screenshots/Screencasts Early check for invalid request <img width="2725" height="470" alt="Screenshot 2025-10-02 152639" src="https://github.com/user-attachments/assets/16e0ee1d-136b-4147-98b7-e5ec79796548" /> OPTIONS handler to respond to preflight requests <img width="2731" height="618" alt="Screenshot 2025-10-02 152733" src="https://github.com/user-attachments/assets/a0c709c2-e398-4bc4-9e09-97e391ee8a1a" /> Result with normal free tier OpenAI API key <img width="1732" height="970" alt="image" src="https://github.com/user-attachments/assets/f7f2e478-0246-4a5f-91f0-4ea393a87104" /> Result using mock openAI response <img width="1738" height="1072" alt="image" src="https://github.com/user-attachments/assets/0504660e-fa4f-43a5-ba15-e85b0991978f" /> ## Notes to Reviewer Since this endpoint depends on the OpenAI API, running tests locally requires a paid API key. Without credits, requests return 429 Too Many Requests, so I’m unable to fully validate the end-to-end behavior. However, using mock OpenAI response the CORS headers are being included, as shown in screenshots. For now, I’ve focused on structural improvements: adding OPTIONS preflight support and early pre-checks for invalid input with proper CORS headers. Please advise on the best way to test these CORS updates under this limitation? Is there a recommended approach to mock the OpenAI response for testing? --------- Co-authored-by: Olabode Lawal-Shittabey <[email protected]>
1 parent 9a1c990 commit d8c6f0a

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/pages/api/jai/search.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,52 @@ import { RunnableSequence } from "@langchain/core/runnables";
33
import { jAIPrompts, model } from "../../../../apps/jai/index.js";
44
import { HttpResponseOutputParser } from "langchain/output_parsers";
55

6-
export async function POST({ request }) {
7-
const corsHeaders = {
8-
"Access-Control-Allow-Origin": "same-origin",
6+
const allowedOrigins = [
7+
"https://www.jargons.dev", // production
8+
"http://localhost:4321", // local dev (default Astro port)
9+
// add other allowed preview URLs if needed
10+
];
11+
12+
function getCorsHeaders(origin) {
13+
const headers = {
914
"Access-Control-Allow-Methods": "POST, OPTIONS",
1015
"Access-Control-Allow-Headers": "Content-Type, Authorization",
16+
"Access-Control-Allow-Credentials": "true",
1117
};
1218

19+
// Allow known origins and Vercel preview deployments
20+
if (
21+
allowedOrigins.includes(origin) ||
22+
(origin && origin.endsWith("-jargonsdev.vercel.app"))
23+
) {
24+
headers["Access-Control-Allow-Origin"] = origin;
25+
}
26+
27+
return headers;
28+
}
29+
30+
export async function OPTIONS({ request }) {
31+
const origin = request.headers.get("origin");
32+
const corsHeaders = getCorsHeaders(origin);
33+
34+
return new Response(null, { status: 204, headers: corsHeaders });
35+
}
36+
37+
export async function POST({ request }) {
38+
const origin = request.headers.get("origin");
39+
const corsHeaders = getCorsHeaders(origin);
40+
1341
try {
1442
// Extract the `messages` from the body of the request
1543
const { messages } = await request.json();
1644

45+
if (!messages || !Array.isArray(messages)) {
46+
return Response.json(
47+
{ error: "Invalid request body" },
48+
{ status: 400, headers: corsHeaders },
49+
);
50+
}
51+
1752
const currentMessageContent = messages[messages.length - 1].content;
1853

1954
// Create the parser - parses the response from the model into http-friendly format

0 commit comments

Comments
 (0)