sveltekit-typesafe-api
v0.1.2
Published
Adding type safety to SvelteKit's server endpoints and fetches.
Downloads
27
Maintainers
Readme
SvelteKit TypeSafe API Fetch 🔗🌐
Making SvelteKit fetch and validation of server endpoints easier than ever!
Feature list
- Type safe
fetch
-like functions to create a better coding experience. - Usage of the powerful
zod
library to parse the incomming data. - Plug and play and opt-in structure.
Installation
Install the package with your favorite NodeJs package manager.
npm i sveltekit-typesafe-api zod
Get started
Follow these 3 simple steps to harnest the power of zod
and TypeScript
in your API endpoints:
Simply add the vite plugin :
// vite.config.ts import { sveltekit } from "@sveltejs/kit/vite"; import { defineConfig } from "vite"; import { typesafeApi } from "sveltekit-typesafe-api/vite"; export default defineConfig({ plugins: [sveltekit(), typesafeApi()], });
Create a
zod
object to validate the endpoint's request body, and pass it to thevalidate
function.// src/routes/api/+server.ts import { json } from "@sveltejs/kit"; import { z } from "zod"; import { validate } from "sveltekit-typesafe-api/server"; import type { RequestHandler } from "./$types"; export const POST = (async ({ request }) => { const { data } = await validate(request, { email: z.string().email(), password: z.string().min(8), }); return json({ success: true, jwt: db.createJWT({ email: data.email, password: data.password }), }); }) satisfies RequestHandler;
All done, you can finally enjoy the new type safe
api
!<script> import { api } from "sveltekit-typesafe-api"; let res: Promise<Response> | undefined; const onClick = () => res = api.POST("/api", { body: { email: "[email protected]", password: "******" } }); </sricpt>
Contributing
This package is still in beta. Do not hesitate to contact me if you have feedback of any kind! :)
Ideas, bug reports, PRs and the likes are welcome as a Github issue or as a discussion!