Auto Generated +server.ts
types for API calling
#11042
Closed
AlbertMarashi
started this conversation in
Ideas
Replies: 3 comments 1 reply
-
We could probably make param routes work using typescript template literal types but I don't know how far we could take it async function api_fetch(method: "POST", url: `/api/hello`): Promise<{ hello: "world" }>;
async function api_fetch(method: "GET", url: `/api/${string}/${string}`): Promise<{ post_title: "world"}>;
async function api_fetch(method: "GET" | "POST" | ".." , url: string): Promise<unknown> {
// ...
}
let x = await api_fetch("GET", "/api/foo/bar") satisfies { post_title: "world" } // no errors
let y = await api_fetch("POST", "/api/hello") satisfies { hello: "world" } // no errors |
Beta Was this translation helpful? Give feedback.
1 reply
-
This is a great idea. I've wanted this badly. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Closing this because it's already been raised here by @Rich-Harris #647 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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 workBeta Was this translation helpful? Give feedback.
All reactions