feat(middleware): add Method Not Allowed middleware#4751
Open
rokasta12 wants to merge 2 commits intohonojs:mainfrom
Open
feat(middleware): add Method Not Allowed middleware#4751rokasta12 wants to merge 2 commits intohonojs:mainfrom
rokasta12 wants to merge 2 commits intohonojs:mainfrom
Conversation
Add a new `methodNotAllowed` middleware that returns 405 with an Allow header when a request path matches a registered route but the HTTP method is not supported. This follows the implementation approach proposed by @usualoma in honojs#4633 and confirmed by @yusukebe. The middleware: - Takes the app instance as an argument (same pattern as methodOverride) - Lazily builds an internal TrieRouter from app.routes on first 404 - Checks if the path matches any registered route with a different method - Throws HTTPException(405) with Allow header listing valid methods - Does not interfere with app.all() routes or normal 404s Closes honojs#4633
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4751 +/- ##
==========================================
+ Coverage 91.48% 91.51% +0.03%
==========================================
Files 177 178 +1
Lines 11556 11599 +43
Branches 3357 3370 +13
==========================================
+ Hits 10572 10615 +43
Misses 983 983
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Clean implementation. One edge case: the Probably fine for typical use (routes defined at startup), but worth a note in the docs that this middleware assumes routes are registered before the first request. |
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
Add a new
methodNotAllowedmiddleware that returns405 Method Not Allowedwith anAllowheader when a request matches a registered route path but the HTTP method is not supported.This implements the approach proposed by @usualoma in #4633 (comment) and confirmed by @yusukebe (comment).
Closes #4633
Usage
Implementation Details
appinstance as an argument (same pattern asmethodOverride)TrieRouterfromapp.routeson first 404 encounterHTTPException(405)withAllowheader listing valid methods (per RFC 7231)app.all()/app.use()(METHOD_NAME_ALL) when collecting methodsAllowheaderTests (16 passing)
:id)app.use()middleware doesn't break 405 detectionapp.route('/sub', sub)routes are handled correctlyapp.all()routesChecklist
npx tsc --noEmit— zero errorsbun run format:fix— no changes needednpx eslint src/middleware/method-not-allowed/— zero warningspackage.json(exports + typesVersions) andjsr.jsonbuild/validate-exports.test.tspasses