Repository navigation
docs(middleware): add middleware composition section - #905
Closed
Sarthak-Shreshtha01 wants to merge 1 commit into
Closed
Sarthak-Shreshtha01 wants to merge 1 commit into
Sarthak-Shreshtha01 wants to merge 1 commit into
Conversation
Add a section demonstrating middleware composition patterns including: - Combining logger, cors, and auth middleware on a single route - Execution order and early exit flow in composed chains - Real-world error handling middleware wrapping downstream execution - References to factory.createHandlers and hono/combine Resolves honojs/hono#5355
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved CORS, error exposure, and TypeScript correctness issues remain.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds middleware composition guidance to the Hono documentation.
Changes:
- Demonstrates middleware chaining and execution order.
- Covers early exits and error handling.
- References reusable composition utilities.
File summaries
| File | Summary |
|---|---|
docs/guides/middleware.md |
Adds middleware composition documentation and examples. |
Review details
Suppressed comments (4)
docs/guides/middleware.md:391
- This fragment does not match the heading in
docs/helpers/factory.md: the repository's heading anchors strip punctuation (createMiddleware()links as#createmiddleware), sofactory.createHandlers()resolves as#factorycreatehandlers, not#factory-createhandlers.
- **[`factory.createHandlers()`](/docs/helpers/factory#factory-createhandlers)**: Define a reusable array of middlewares and a handler with complete type safety.
docs/guides/middleware.md:314
cors()does not always continue tobearerAuth: for anOPTIONSrequest it returns a 204 response before callingnext(). Since this diagram explicitly mentions preflight handling but then shows auth executing, it misstates the chain for browser preflights; show theOPTIONSearly-exit branch or qualify the diagram to non-OPTIONSrequests.
[cors] ── (before next: checks origin and preflight headers)
│
▼
[bearerAuth] ── (before next: validates bearer token)
docs/guides/middleware.md:325
- The CORS middleware sets
Access-Control-Allow-*headers beforeawait next(); afternext()it only appendsVary: Originwhen configured with a non-wildcard origin. Calling this stage 'appends CORS headers' gives the wrong mental model for the response mutation, including on early auth exits.
[cors] ── (after next: appends CORS headers to response)
docs/guides/middleware.md:316
bearerAuthdoes not return a 401Responsedirectly on invalid credentials; it throws anHTTPException, which Hono's error handler converts to the response and records inc.error. Describe the throw/error-handler path so this section matches the actual early-exit behavior.
├─► [Invalid Token] ── early exit: returns 401 Response directly
- Files reviewed: 1/1 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+268
to
+272
| app.post( | ||
| '/api/posts', | ||
| logger(), | ||
| cors(), | ||
| bearerAuth({ token: 'secret-token' }), |
| c.res = c.json( | ||
| { | ||
| success: false, | ||
| error: c.error.message || 'Internal Server Error', |
| success: false, | ||
| error: c.error.message || 'Internal Server Error', | ||
| }, | ||
| c.res.status || 500 |
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.
Add a section demonstrating middleware composition patterns including:
Resolves honojs/hono#5355