Repository navigation
feat(router): Add asHandler extension to use Router<Handler> as Handler - #209
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughAdds a Router.asHandler extension, updates examples to use router.asHandler with router-level middleware and Cascade 404 fallback, adds comprehensive router tests, and applies minor CI workflow tweaks (temporary continue-on-error for pana on stable and formatting/step version updates). Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Client
participant H as Router.asHandler (Handler)
participant P as Pipeline
participant RW as routeWith(router)
participant R as Router
participant F as Fallback 404
Note over H,P: asHandler constructs Pipeline(routeWith(router)) with final 404 fallback
C->>H: HTTP Request
H->>P: handle(request)
P->>RW: apply routing middleware & lookup
RW->>R: perform route lookup
alt route matched & method allowed
R-->>RW: matched handler
RW->>P: forward to handler
P-->>C: 2xx/3xx/4xx response from handler
else path matched but method not allowed
R-->>RW: list of allowed methods
RW-->>C: 405 with Allow header
else no route match
RW->>F: fall through
F-->>C: 404 Not Found
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #209 +/- ##
==========================================
+ Coverage 91.90% 91.97% +0.07%
==========================================
Files 85 85
Lines 3174 3177 +3
Branches 1639 1641 +2
==========================================
+ Hits 2917 2922 +5
+ Misses 257 255 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
aaf8f99 to
f60405a
Compare
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Pull Request Overview
This PR introduces an asHandler extension method for Router<Handler> to simplify router usage by allowing routers to be used directly as handlers without requiring explicit wrapping with routeWith. The extension provides standard HTTP response behaviors (404 for unmatched routes, 405 for method mismatches with Allow headers).
Key changes:
- Adds
RouterHandlerextension withasHandlergetter that wraps the router in a pipeline - Updates examples to demonstrate the new simplified API pattern
- Adds comprehensive test coverage for the new functionality
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| lib/src/middleware/routing_middleware.dart | Implements the RouterHandler extension with asHandler getter |
| test/router/router_handler_test.dart | Comprehensive test suite covering routing scenarios, error responses, and middleware integration |
| example/example.dart | Updated to use router.asHandler with Cascade for fallback handling |
| example/middleware_example.dart | Simplified to use router.asHandler directly |
| example/multi_isolate.dart | Updated to use router-based approach with asHandler |
| .github/workflows/dart-tests.yaml | Added temporary workaround for pana score check |
Comments suppressed due to low confidence (1)
lib/src/middleware/routing_middleware.dart:1
- The documentation example references
router.callbut the actual implementation usesrouter.asHandler. The example should be updated to match the implementation.
import '../../relic.dart';
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
.github/workflows/dart-tests.yaml(2 hunks)example/example.dart(1 hunks)example/middleware_example.dart(1 hunks)example/multi_isolate.dart(1 hunks)lib/src/middleware/routing_middleware.dart(1 hunks)test/router/router_handler_test.dart(1 hunks)
🧰 Additional context used
🪛 actionlint (1.7.7)
.github/workflows/dart-tests.yaml
143-143: the runner of "actions/setup-node@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue
(action)
🔇 Additional comments (1)
example/example.dart (1)
16-24: Update comment to referenceasHandler.The comment still mentions the old
call()extension, but the code now relies onrouter.asHandler. Please align the comment so newcomers aren’t misled.- // Router<Handler> can be used directly as a Handler via the call() extension. + // Router<Handler> can be used directly as a Handler via the asHandler extension.Likely an incorrect or invalid review comment.
Adds a call() extension method convenience method to Router<Handler>, allowing it to be used "directly" as a Handler without wrapping with routeWith().
f60405a to
317644e
Compare
|
@CodeRabbit I know it is backwards, but please create matching issue for this PR |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 45 @nielsenko: I've created issue #214 to track this feature. The issue includes a description of the You can view the issue here: #214 |
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
example/example.dart (1)
9-24: Avoid doublelogRequests()middleware.
logRequests()is attached both on the router (Line 11) and on the surroundingPipeline(Line 18), so matched requests are logged twice. Pick either the router-level middleware or the pipeline-level middleware to keep logging once.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
.github/workflows/dart-tests.yaml(2 hunks)example/example.dart(1 hunks)example/middleware_example.dart(1 hunks)example/multi_isolate.dart(1 hunks)lib/src/middleware/routing_middleware.dart(1 hunks)test/router/router_handler_test.dart(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
- .github/workflows/dart-tests.yaml
- lib/src/middleware/routing_middleware.dart
- test/router/router_handler_test.dart
- example/middleware_example.dart
- example/multi_isolate.dart
SandPod
left a comment
There was a problem hiding this comment.
Changes look good! Have one example question and one for my own knowledge.
Description
Adds
asHandlerextension toRouter<Handler>to allow routers to be used directly as handlers without wrapping withrouteWith. This simplifies common usage patterns when a router is the final handler in a pipeline.The extension returns a handler that performs routing lookup and returns appropriate responses (404 for no match, 405 for method mismatch with Allow header).
Related Issues
Pre-Launch Checklist
///).Breaking Changes
Additional Notes
Examples updated to demonstrate the new API pattern using
router.asHandlerinstead of the more verbosePipeline().addMiddleware(routeWith(router)).addHandler(...)pattern.Summary by CodeRabbit
New Features
Refactor
Tests
Chores