Conversation
0471273 to
a46b3c6
Compare
|
This is an awesome start. Wondering if we could pair it with |
Yea, i have already started working on it locally. But ran into some beahviour issues which i am trying to figure out first 👍🏻 Will include it in this PR when i am done. |
|
@pi0 This PR should be ready for a quick review when you have a moment - happy to adjust anything if needed 😊 I have tried cleaning the overloads from |
|
Sorry, it got delayed @luxass, we will try to review soon (also added @danielroe) |
* Updated the `H3EventContext` interface to accept a generic type parameter `TParams` for `params`. * This change enhances type safety and flexibility for router parameter handling.
* Updated the `context` property in `H3Event` to infer `routerParams` more effectively. * Added tests to verify the inference of router parameters from `EventHandlerRequest`. * Ensured default behavior for cases without specified `routerParams`.
This feels cleaner to have the optionality of the params in the context.
…nction signatures
The previous implementation in the pull request, removed access to the optional properties.
* Added `RouteParams` type to simplify route parameter inference. * Updated `on`, `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `connect`, and `trace` methods to utilize the new `RouteParams` type for better type safety. * Improved type definitions for event handlers to include inferred route parameters.
This will hopefully clean the types up a bit.
* Removed redundant `const` keyword from route type parameters in `on`, `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `connect`, and `trace` methods. * Improved type inference for route parameters.
This should make the h3 declared class not look too complex with the overloads. I couldn't get the `all` to use the H3HandlerInterface, some type errors in H3Core appeared 😅
537000c to
931281a
Compare
|
Dear @luxass you don't need to rebase PR on all commits. I can take care of rebase before merge 👍🏼 |
📝 WalkthroughWalkthroughThis PR implements type-safe route parameter inference for H3, enabling developers to access dynamically extracted route parameters (e.g., from Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (7)
🧰 Additional context used🧬 Code graph analysis (4)src/event.ts (1)
src/types/h3.ts (3)
src/h3.ts (6)
test/unit/types.test-d.ts (2)
🔇 Additional comments (11)
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 |
There was a problem hiding this comment.
Pull request overview
This PR adds automatic type inference for route parameters based on route patterns defined in H3 application methods. Previously, event.context.params was always typed as Record<string, string> | undefined. Now, TypeScript extracts parameter names from route patterns (e.g., :id, :userId) and provides specific types for them.
Key changes:
- Route parameters are now inferred from route patterns in
app.get(),app.post(), and other HTTP method handlers - Helper functions
getRouterParamsandgetRouterParamnow return properly typed parameters - Routes without parameters have
paramstyped asundefinedinstead of an optional record
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/unit/types.test-d.ts | Adds comprehensive type-level tests for router parameter inference across different route patterns and helper functions |
| src/utils/request.ts | Updates getRouterParams and getRouterParam with function overloads to support typed parameter inference |
| src/types/h3.ts | Adds H3HandlerInterface and updates HTTP method signatures to infer route parameters from route patterns |
| src/types/context.ts | Makes H3EventContext generic to accept custom parameter types instead of fixed Record<string, string> |
| src/types/_utils.ts | Introduces RouteParams type helper using InferRouteParams from rou3 for route pattern parsing |
| src/h3.ts | Implements runtime overloads for the on method to support typed route parameter handlers |
| src/event.ts | Updates H3Event.context to use the inferred router params type from the request |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This PR resolves #1053 by adding automatic type inference for route parameters. When you define a route with parameters using
app.get(),app.post(), or any other HTTP method, TypeScript now knows exactly what parameters are available inevent.context.params.Previously,
event.context.paramswas always typed asRecord<string, string> | undefined, even when the route pattern clearly defined specific parameters. Now the route pattern is parsed at the type level to extract parameter names and provide full type safety.Route parameters are now fully typed based on the route pattern you define:
This works with multiple parameters too:
The existing helper functions (
getRouterParamandgetRouterParams) also benefit from this:Type inference works across all route registration methods like
app.get(),app.post(),app.put(),app.delete(), andapp.on(). Routes without parameters haveparamstyped asundefined, so you'll know when there are no parameters available.When you use
defineHandlerdirectly (outside of a route), the route pattern isn't available yet, so params remain untyped asRecord<string, string> | undefined. You can still manually type them using therouterParamsfield inEventHandlerRequestif needed. However, when you inlinedefineHandlerwith a route, the params are fully typed automatically:The implementation leverages
InferRouteParamsfrom rou3 (h3js/rou3#168) for route pattern parsing. I have tried to make the types backward compatible, so if you catch something that doesn't work as before, just tell me and i'll fix it 😅Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.