Commit d8c6f0a
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
1 file changed
+38
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
9 | 14 | | |
10 | 15 | | |
| 16 | + | |
11 | 17 | | |
12 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
13 | 41 | | |
14 | 42 | | |
15 | 43 | | |
16 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
17 | 52 | | |
18 | 53 | | |
19 | 54 | | |
| |||
0 commit comments