N
Nuxt3mo ago
Ne0n

useFetch loses type from /server/api after using getCachedData

Hi, I'm using /server endpoints with Prisma ORM, which automatically share data types with the frontend, which is awesome. The problem arises when I try to use another function to cache data inside a nuxtApp instance called getCachedData. Adding this option to useFetch converts the types from the Prisma response to any type. Is there any workaround for this problem to use getCachedData and have automatic typing from the backend?
No description
No description
2 Replies
Ne0n
Ne0n3mo ago
Well, i found solution but would be great if Nuxt can implement by self. There is available import from nitropack called { type InternalApi} that returns type from server, we can set type of returned content and now it works.
pano9000
pano90003mo ago
Hi @Ne0n I am facing a similar issue, where with useFetch inside a component, I am not able to get proper typings - and I also stumbled across this InternalApi type (I think it was mentioned on StackOverflow?) -> but I am still running into issues: Can I ask you, how you used the InternalApi successfully? IWhat I am doing is:
type ApiResponse = InternalApi["/api/v1/myroute"]["get"]
const { data: data } = await useFetch<ApiResponse>("/api/v1/myroute");
type ApiResponse = InternalApi["/api/v1/myroute"]["get"]
const { data: data } = await useFetch<ApiResponse>("/api/v1/myroute");
however Typescript then infers the "data" variable to be of type "globalThis.Ref<any>" 😦 (sorry for threadjacking! but I seem to be in the same boat as you :-D)
Want results from more Discord servers?
Add your server