Repository navigation
feat(server): chatcopsNext() catch-all route adapter for Next.js (#574) - #580
Open
Saifjafri212213 wants to merge 1 commit into
Open
Saifjafri212213 wants to merge 1 commit into
Saifjafri212213 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A dedicated Next.js App Router adapter (
chatcopsNext()) that serves both the chat and session endpoints from a single catch-all route (app/api/chat/[[...path]]/route.ts), replacing the previous two-file requirement (route.tsandsession/route.ts).Closes #574
Why
Today, Next.js setups need two route files:
app/api/chat/route.tsandapp/api/chat/session/route.ts(which re-exportsPOSTandOPTIONS). Forgetting the second one quietly breaks session tokens with 404s when the chat tries to initialize.Next.js optional catch-all routes (
[[...path]]) let a single route file handle every subpath under/api/chat.fetchChatResponse()inpackages/server/src/http.tsalready routes paths ending in/sessionto the session endpoint viaisSessionPath(), so the catch-all works without needing internal routing of its own.What changed
@chatcops/server:packages/server/src/adapters/next.ts(new):chatcopsNext(config). It createscreateChatHandlerandoriginPolicyat module scope and returns{ POST, OPTIONS }, both(request: Request) => Promise<Response>. Client IP resolution and rate limits work across calls as inchatcopsVercelHandler."./next"toexportsinpackages/server/package.jsonalongside"./vercel", and re-exportedchatcopsNextfrompackages/server/src/index.ts.website/src/content/docs/server/fetch-runtimes.mdxto the single catch-all route, with a short note explaining the older two-file setup.chatcopsNexttoscripts/check-doc-samples.mjssopnpm docs:checkvalidates that Next doc samples configure session secrets or allow anonymous access.@chatcops/server, and committed the updated API snapshot inapi/server.api.md.Notes for review
chatcopsNextreturns{ POST, OPTIONS }. AGEThandler is left for later with visitor handoff (F-10), as noted in the issue.POSTandOPTIONSpoint to the single handler, so CORS preflight and chat/session POSTs are handled byfetchChatResponse().How I Tested This
packages/server/tests/adapters/next.test.ts(4 new tests with realRequestobjects):OPTIONSpreflight returning 204 with CORS headers.POSTto/api/chat/sessionminting a session token (200).POSTto/api/chatwithout a token returning 401 (session_required).POSTwith the session Bearer token streaming the SSE reply (200).src/adapters/next.ts.npx create-next-app@latest) withapp/api/chat/[[...path]]/route.tsusingchatcopsNext().pnpm docs:checkpasses (204 blocks checked, 3 skipped),pnpm api:checkis up to date,pnpm publintreports all good,pnpm linthas 0 errors (38 warnings, same asdev), andpnpm -r typecheckpasses cleanly.