Typesafety, Minus the typing
š Check it out @ (https://svetch-dev.vercel.app/) Effortlessly generate a typesafe Fetch client for your Svelte/Sveltekit applications, complete with automatic input/output zod validation and autogenerated types & API Docs.
Svetch automatically scans your +server.ts
files in /src/routes/api (or whatever directory you specify) and generates a typesafe Fetch client that has intellisense for path, query, body parameters & all the possible responses (and errors)
url.searchParams.get
const payload: X = await request.json
or as X
json(X) (sveltekit utility)
or new Response(X)
throw error()
or throw new Error
or return error(HTTP_CODE, error_message)
$ npx svetch.ts@latest
svelte.config.js
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter(),
+ alias: {
+ "src": "./src",
},
}
};
export default config;
The library will generate an OpenAPI compatible schema, alongside with swagger docs, by default in /docs/+page.svelte
.svetchrc
{
"framework": "sveltekit", // currently the only option
"input": "src/routes/api", // the directory you want the generator to traverse
"out": "src/lib/api", // the directory to output the types & the client to
"docs": "src/routes/docs", // where to output docs
"tsconfig": "tsconfig.json" // your tsconfig directory
}
user/[user_id].ts
will have a path parameter of user_id
url.searchParams.get('param')
Must be assigned to a const called
payload ā IMPORTANTimport { Svetch } from "src/lib/api/client"; // or wherever you chose to generate the client
const svetch = new Svetch({
baseUrl: "/api", // default is '/api'
fetch, // default is window.fetch, pass the global fetch to it in node, etc...
validate: false, // default is false, uses Zod to validate payload + response (ON CLIENT THIS CAN MAKE THE IMPORT SIZE HUGE)
});
await svetch
.post("user/[user_id]", {
path: {
user_id: 1,
},
body: {
text: foodInput,
journalId: $journal.id,
today: new Date(),
},
})
.then((response) => {
if (response.error) throw new Error(response.error);
food = response.data;
loading = false;
})
.catch((error) => {
throw new Error(error);
});
import { Svetch } from "src/lib/api/client"; // or wherever you chose to generate the client
const svetch = new Svetch({
baseUrl: "/api", // default is '/api'
fetch, // pass the load function's fetch to it
});
export async function load({ fetch, session }) {
const user = await svetch.get("user").then((response) => {
if (response.error) throw new Error(response.error);
return response.data;
});
return {
props: {
user,
},
};
}
This library is Free for personal use, If it's useful to you, please consider purchasing a license @ https://petrasolutions.lemonsqueezy.com/checkout/buy/19210e05-ae3c-41a0-920c-324e3083618d Redistribution/Forking is Not Allowed.