Skip to content

Commit 3b9d4b7

Browse files
sweetmantechclaude
andauthored
fix: add CORS headers to streaming chat response (#138)
- Add CORS headers to createUIMessageStreamResponse call in handleChatStream - Add x-api-key to Access-Control-Allow-Headers in getCorsHeaders - Update handleChatStream test to expect CORS headers Fixes cross-origin requests to /api/chat endpoint being blocked. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 832df65 commit 3b9d4b7

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

lib/chat/__tests__/handleChatStream.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ describe("handleChatStream", () => {
162162
expect(mockCreateUIMessageStream).toHaveBeenCalled();
163163
expect(mockCreateUIMessageStreamResponse).toHaveBeenCalledWith({
164164
stream: mockStream,
165+
headers: {
166+
"Access-Control-Allow-Origin": "*",
167+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, PATCH",
168+
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With, x-api-key",
169+
},
165170
});
166171
expect(result).toBe(mockResponse);
167172
});

lib/chat/handleChatStream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function handleChatStream(request: NextRequest): Promise<Response>
5858
},
5959
});
6060

61-
return createUIMessageStreamResponse({ stream });
61+
return createUIMessageStreamResponse({ stream, headers: getCorsHeaders() });
6262
} catch (e) {
6363
console.error("/api/chat Global error:", e);
6464
return NextResponse.json(

lib/networking/getCorsHeaders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export function getCorsHeaders(): Record<string, string> {
77
return {
88
"Access-Control-Allow-Origin": "*",
99
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, PATCH",
10-
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",
10+
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With, x-api-key",
1111
};
1212
}

0 commit comments

Comments
 (0)