Repository navigation
feat: The routeWith middleware builder function is now generic. - #78
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 📝 WalkthroughWalkthroughThe routing middleware was refactored to be generic over any handler type, not just Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Middleware
participant Router
participant Handler
Client->>Middleware: Send request
Middleware->>Router: Match route
Router-->>Middleware: Return matched value of type T
Middleware->>Middleware: Convert T to Handler (using toHandler or cast)
Middleware->>Handler: Invoke handler
Handler-->>Middleware: Return response
Middleware-->>Client: Return response
Possibly related PRs
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #78 +/- ##
==========================================
+ Coverage 82.42% 82.47% +0.05%
==========================================
Files 80 80
Lines 2429 2436 +7
Branches 1362 1367 +5
==========================================
+ Hits 2002 2009 +7
Misses 427 427 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
test/middleware/routing_middleware_test.dart (1)
25-28: 🛠️ Refactor suggestionAdd a test that exercises the new generic +
toHandlerpathAll updated tests still use
Router<Handler>where the automatic cast branch
is taken. To guard against regressions in the_toHandlerinitialisation
logic, add a test like:final strRouter = Router<String>()..add(Method.get, '/', (_) => 'ok'); final mw = routeWith<String>( strRouter, toHandler: (s) => respondWith((_) => Response.ok(s)), ); final ctx = _FakeRequest('/').toContext(Object()); final resCtx = await mw(respondWith((_) => Response.notFound()))(ctx) as ResponseContext; expect(resCtx.response.statusCode, 200); expect(await resCtx.response.readAsString(), 'ok');This will fail with the current bug and pass once the fix above is applied.
🧹 Nitpick comments (2)
lib/src/middleware/routing_middleware.dart (2)
32-33: Consider switching fromurltorequestUrito preserve full path & queryThe TODO comment hints at this; using
Request.urldrops the leading slash and
can mis-handle..segments or empty paths.Request.requestUri(or
Request.requestedUridepending on the framework version) preserves the raw
requested string, including query parameters, and avoids subtle mismatches
during router lookup.
56-57:_isSubtypehelper works but is brittle with NNBD future changesThe
(<S>[] is List<T>)trick is idiomatic but relies on list‐covariance and
can break if the language alters that rule. A safer (though slightly slower)
alternative istypeOf<S>() is Typechecks viadart:mirrorsor, when mirrors
are unavailable, simply defaulting to the cast path and letting it fail fast.
Not a blocker, just be aware of the limitation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
lib/src/middleware/routing_middleware.dart(2 hunks)test/middleware/routing_middleware_test.dart(1 hunks)
You can pass any Router<T> as long as you also pass a toHandler function that tells the middleware how to produce a handler from a stored route.
da239b1 to
98b957a
Compare
d2df95c to
439d6ce
Compare
Description
This pull request introduces a feature that makes the
routeWithmiddleware builder function generic.You can pass any Router as long as you also pass a toHandler function that tells the middleware how
to produce a handler from a stored route.
Key changes:
routeWithfunction inlib/src/middleware/routing_middleware.dartnow accepts a genericRouter<T>and an optionaltoHandlerfunction to convertTto aHandler.Tis already aHandler(or a subtype), and notoHandleris provided, it will be automatically cast.test/middleware/routing_middleware_test.dartwere updated to reflect this change, specifically by parameterizingRouterasRouter<Handler>().Related Issues
Pre-Launch Checklist
Please ensure that your PR meets the following requirements before submitting:
///), ensuring consistency with existing project documentation.Breaking Changes
Signature of
routeWithchanges.Summary by CodeRabbit
Refactor
Tests