You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Something which kinda really sucks with creating APIs using SvelteKit +server.ts routes is that all the requests using fetch are untyped, as a result we don't receive typing on the front end, and have to manually constrain types
This can create a lot of surface area for bugs, as there can be a mismatch between the +server.ts and the fetch
My ideas are to somehow get SvelteKit to auto generate these types for use in the frontend
/api/hello/+server.ts
import{APIResponse,json}from"@svelte/kit"exportasyncfunctionGET(): APIResponse<{hello: "world"}>{returnjson({hello: "world"// these types are typechecked against the function response type to ensure correct data})}
import{api_fetch}from"@svelte/kit"// a new tool exported by sveltekit that contains autogenerated types for all of the `+server.ts` routesexportasyncfunctionload(){return{some_data: awaitapi_fetch("GET","/api/hello")// returns typed data}// the return type would look like { some_data: { hello: "world" } }}
We could imagine that api_fetch looks like this using typescript function overloading with auto generated types
Something which kinda really sucks with creating APIs using SvelteKit
+server.ts
routes is that all the requests usingfetch
are untyped, as a result we don't receive typing on the front end, and have to manually constrain typesThis can create a lot of surface area for bugs, as there can be a mismatch between the
+server.ts
and thefetch
My ideas are to somehow get SvelteKit to auto generate these types for use in the frontend
/api/hello/+server.ts
svelte code
/+page.ts
We could imagine that
api_fetch
looks like this using typescript function overloading with auto generated typesI think a lot of this should be possible with some typescript magic similar to how we're already doing with the load functions & page data
How should parameterized routes workOriginally posted by @AlbertMarashi in #11042
The text was updated successfully, but these errors were encountered: