N
Nuxtβ€’6mo ago
Nenad Novakovic

Get return type from api route?

Is it possible to get the return type (assuming it's generated by Nuxt) so that I can use it as a type of something else? Fetch utils know about the return type, but I want to access it directly. How?
4 Replies
Nenad Novakovic
Nenad NovakovicOPβ€’6mo ago
Ah took some time to find it. Leaving it here, as it works fine, but maybe someone from core contribs can collaborate if this is the way or there's another approach:
import type { InternalApi } from 'nitropack'
type ApiResponse = InternalApi['/api/example']['get']
import type { InternalApi } from 'nitropack'
type ApiResponse = InternalApi['/api/example']['get']
dwol
dwolβ€’6mo ago
if youre on vscode you can hover the util or composable and right click, then click go to definition and it'll show you the type inside nodemodules, not sure if thats helpful for you
Ragura
Raguraβ€’6mo ago
Interesting that you can get the types that way, thanks for following up on your own issue πŸ™‚ I wonder though if it wouldn't be better to create a type/interface inside the API route (or a shared types folder) and explicitly make sure the return value is of that type. Then you wouldn't have to look up the type anywhere else and could import it.
manniL
manniLβ€’6mo ago
That's usually how I do it too if necessary. But there might be a better way πŸ€” Might be a good topice to raise in the nitro repo
Want results from more Discord servers?
Add your server