-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
useFetch Type Safety With Multiple HTTP Method API Routes #15280
Comments
Working on this one 👍 |
Hi @danielroe this was already fixed in nitro, right? What could I do to bring it to nuxt? |
Would you confirm whether it's fixed in the edge channel, which is using the latest nitro? https://nuxt.com/docs/guide/going-further/edge-channel/ |
Looks like the issue persists @danielroe It looks like the issue has changed:
Will create a new PR on Nitro to tackle this |
We may simply need to make a change in Nuxt to benefit from the incoming nitro change. |
Could you explain me a little bit the steps you will follow and I can try to look into it? |
Hi! I made the PR to Nitro. Haven't really had the chance to also make a PR to Nuxt yet, but it was my intention. You probably need to argument the types here:
The Nitro fetch type accepts a method somewhere as a generic, and you'll basically just need to link these two parts together. I'm on the road right now, so I can't really point out any specifics, but this should get you going. |
Looking into it thanks @dsluijk and @danielroe ❤️ |
Hi @dsluijk I couldn't get it to anywhere 😢 could you help me out a little bit. This is the main change I did but I am totally unsure if I am going on the good direction With this change it looks like is always trying to get the |
Alright, this was a little more tricky to get to work than I expected initially. I hope you don't mind me stealing your work, but I think I've made it to work now. This did require an additional change in Nitro to narrow the method generic correctly, so that needs to be merged first (unjs/nitro#883). I did get my change to work locally with that change applied. The corresponding PR can be found here: #18526 |
@dsluijk This may already be addressed in the above effort, but I want to point out that this issue also occurs when working with a e.g. const slug = params.slug
const { data } = useFetch(`/api/job/${slug}`) // const data: Ref<unknown> however, if I do this: const slug = <''>params.slug
const { data } = useFetch(`/api/job/${slug}`) // data is correctly inferred |
@genu I think that's an unrelated issue, so if you feel like I think you should open a new issue for that. But looking at your example I think that this is just the way TypeScript works. In the first example I assume |
* fix: workaround nuxt 3.1.2 useFetch problems, see nuxt/nuxt#15280 * fix: no extra semicolon
Environment
Windows_NT
v16.16.0
3.0.0-rc.12
0.6.0
npm@8.13.2
vite
-
-
-
Reproduction
https://github.com/Gigantor2131/nuxt-fetch-type-safety/
type safety error is in app.vue
https://github.com/Gigantor2131/nuxt-fetch-type-safety/blob/796da921643f4502472df02513c97a560d3c02aa/app.vue#L25
Describe the bug
The type safety of useFetch breaks when an API Route has multiple HTTP Method. Example a GET and POST both on the route
/api/example
Expected behavior is useFetch returns the type for the routes specified HTTP Method
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: