Skip to content
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

Typed API Routes #12645

Closed
bobbymannino opened this issue Sep 3, 2024 · 1 comment
Closed

Typed API Routes #12645

bobbymannino opened this issue Sep 3, 2024 · 1 comment

Comments

@bobbymannino
Copy link

Describe the problem

Trpc sucks, it takes way too much setup and SvelteKit is very clever and types a lot of things already. Parameters are typed already for pages and I don’t think it is too far fetched an idea to think api routes could be typed too, my self - I do not have the knowledge to create such a thing. I might just be dreaming and this is impossible but typed APIs out of the box would be an awesome headline feature of sveltekit

Describe the proposed solution

The syntax to use it would look something like this;

// /src/routes/api/test/+server.ts
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ request }) => {
 //Logic

  if (Math.random() > 0.5) return json({ message: "success" });
  return json({ message: 'failed' }, { status: 500 });
}
<!-- /src/routes/+page.svelte -->
<script>
  import { api } from '@sveltejs/kit/api';

  async function get() {
    const res = await api("/api/test").get();
    // res = { status: 500, body: { message: "failed" } } | { status: 200, body: { message: "success" } }


    // if there was a body or headers they can go in the method function
    // const res = await api("/api/test").get({ body: {} }); 
  }
</script>

<button onclick={get}>Get</button>

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@eltigerchino
Copy link
Member

duplicate of #647

@eltigerchino eltigerchino closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants