Skip to content

refactor(api): route /api/user through a method handler table - #227

Open
dex-the-ai wants to merge 2 commits into
mainfrom
architecture-audit/nextjs-quickstart-2026w47
Open

dex-the-ai wants to merge 2 commits into
mainfrom
architecture-audit/nextjs-quickstart-2026w47

Conversation

@dex-the-ai

Copy link
Copy Markdown
Contributor

Summary

pages/api/user.js was a single if/else over req.method. Every branch mixed .then/.catch chains with outer try/catch blocks, and the auth-failure → 401 mapping was copied into three of them. That shape hid two bugs:

  • An unsupported method never got a response. For example, PATCH /api/user fell through every branch and the request hung until the client timed out.
  • POST/PUT with no body crashed. body stayed null, so body.email threw a TypeError. POST returned an unhandled 500. PUT reported a misleading "Profile Not Found".

Change

  • Each HTTP method now has its own function: listProfiles, createProfile, updateProfile and deleteProfile. A methodHandlers table maps methods to these functions and acts as the route's interface.
  • Any method not in the table gets 405 with an Allow: GET, POST, PUT, DELETE header. This check runs before connecting to Couchbase.
  • parseBody() parses the body once. A missing body becomes {}, and invalid JSON still returns 400 Invalid JSON body.
  • sendDbError() is now the only place that maps SDK errors to HTTP status: 401 for authentication failure, otherwise 500.

All existing status codes and messages are unchanged: 201/200/400/401/500 and their texts. The UI in pages/index.js checks these codes, so it is unaffected.

Validation

I ran everything locally against Couchbase Server 8.0.3 in Docker, set up like the CI service container: kv+n1ql+index, user_profile bucket, npm run init-db:default.

  • New test __test__/api-user-routing.test.js covers PATCH → 405 + Allow and POST without a body → 400. On main both tests fail: PATCH times out with no response, and the bodyless POST throws TypeError: Cannot read properties of null (reading 'email'). Both pass on this branch.
  • npm test: 5 suites, 8 tests passed (the 6 existing tests plus the 2 new ones).
  • npm run check (prettier + eslint --max-warnings=0): clean.
  • I also ran a live smoke against next dev:
    • PATCH → 405 with Allow
    • bodyless POST → 400
    • malformed body → 400 Invalid JSON body
    • create → 201, update → 200, search → 200, delete → 200
    • PUT on a missing pid → 500 Profile Not Found, cannot update: document not found, the same as main
    • UI / → 200

🤖 Generated with Claude Code

Split the single if/else handler into one function per HTTP method behind
a methodHandlers table, with parseBody() and sendDbError() as the shared
seams for body parsing and Couchbase error -> HTTP status mapping.

Fixes two edge cases the old shape hid:
- unsupported methods (e.g. PATCH) never received a response and hung;
  they now get 405 with an Allow header
- POST/PUT without a body threw on `null.email`; an empty body is now {}

Existing status codes and messages are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
couchbase-nextjs-quickstart Ready Ready Preview Nov 24, 2026 2:00am UTC
nextjs-quickstart Ready Ready Preview Nov 24, 2026 2:00am UTC

@ejscribner

Copy link
Copy Markdown
Collaborator

@dex-the-ai I think this branch needs to be updated with the latest

@dex-the-ai

Copy link
Copy Markdown
Contributor Author

@ejscribner Done. I merged the latest main into this branch (merge commit f5e7fee). It picks up #226, the security update to package-lock.json. The merge had no conflicts and didn't touch any of this PR's own files.

Checks:

  • Local: npm ci and npm run check (prettier + eslint) pass. I also ran npm test against a couchbase-neo container set up the same way CI does it: 5 suites and 8 tests passed, including api-user-routing.test.js.
  • CI on f5e7fee: tests (24.x), CodeQL and both Vercel previews passed (run). The PR now shows as up to date with main (merge state CLEAN).

This branch was successfully deployed

2 active deployments
Preview – nextjs-quickstart — f5e7feef Deployed Nov 24, 2026 by vercel[bot]
Preview – couchbase-nextjs-quickstart — f5e7feef Deployed Nov 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants