New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable response type infer for API routes with params #222
feat: enable response type infer for API routes with params #222
Conversation
Updatethis is fixed with PR btw, I've asked in Nuxt's Dicord/typescript room about playgound/tsconfig.json setup for correctly inferring nitro's |
Would also be nice to support globs, such as |
on it! π |
@danielroe Hi! π I also added some type tests using the please check the updated PR description or the type tests code for details. π Thanks for your time and effort working on this and guiding me. |
This is looking great - just want to clarify what I meant by globs. If we have a key in export interface InternalApi {
'/api/user/:userId/post/**': SomeReturnType
} Then this should match |
Got it, I got it the wrong way around π€¦ββοΈ π |
Implemented globs π I think this test case @L84 should also include Api key: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. β¨
Side note: I'm cautious about potential type complexity in nitro but I'd be happy to merge and refactor type implementation down the line if needed. (For example, perhaps we could explicitly generate /api/${string}
rather than /api/:slug
as the key of InternalApi
if we find the current approach has negative perf implications.)
Codecov Report
@@ Coverage Diff @@
## main nuxt/framework#222 +/- ##
=========================================
+ Coverage 0 55.94% +55.94%
=========================================
Files 0 54 +54
Lines 0 3466 +3466
Branches 0 362 +362
=========================================
+ Hits 0 1939 +1939
- Misses 0 1178 +1178
- Partials 0 349 +349
Continue to review full report at Codecov.
|
Thanks @didavid61202 @danielroe <3 |
Co-authored-by: Daniel Roe <daniel@roe.dev>
π Linked issue
resolves nuxt/nuxt#10978, nuxt/nuxt#14187
β Type of change
π Description
Update
MatchedRoutes
helper type intypes/fetch.ts
to enable$fetch
to infer response type even for routes with params, (ex: users/[useId]/posts/[postId].ts)This will provide better DX for a wider range of complex string requests with variables in it for requesting server routes containing params
This would also further provide Nuxt 3's
useFetch
anduseLazyFetch
to be able to infer the response's data type as well. πUsage scenario
assume the project contains following API file structure
with each file export a default
h3
eventHandler, for example:and use in other file as
π Checklist