- Notifications
You must be signed in to change notification settings - Fork0
A simple and type-safe RPC system for SvelteKit Inspired by tRPC, built specifically for SvelteKit. Define your backend procedures and call them from the client with full end-to-end type safety — no API routes, no schemas, just functions.
License
Falk33n/sveltekit-rpc
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a simple and type-safeRemote Procedure Call (RPC) implementation in aSvelteKit project.
It demonstrates how to call server-side functionsdirectly andtypesafely from the client using a minimal setup.
Server-side functions are defined in$lib/server/routers/*.ts and combined inside theappRouter in$lib/server/root.ts.
The/api/[...endpoints] route catchesRPC requests and runs the matching function using thecallRouter function in$lib/server/caller.ts.
The client makes a fetch call to that route using thecallApi function in$lib/client/caller.ts. Which provides full type safety andIntelliSense for the defined route.
Zod is used to make validations of the input before the actual function runs but also validates the output before it reaches its end destination. This functionality is wrapped inside the base middlewarepublicProcedure in$lib/server/setup.ts.
TypeScript ensures type safety for arguments and return values but alsoIntelliSense for the developer.
git clone https://github.com/Falk33n/sveltekit-rpc.gitcd sveltekit-rpc# (or use `bun install` / `pnpm install` / `yarn install` if you prefer).npm install# (or use `bun run dev` / `pnpm run dev` / `yarn run dev` if you prefer).npm run devYour app should now be running athttp://localhost:5173.
import{exampleInputSchema,exampleOutputSchema}from'$lib/schemas/example';import{createRouter,publicProcedure}from'../setup';exportconstexampleRouter=createRouter({example:{method:'post',handler:publicProcedure.input(exampleInputSchema).output(exampleOutputSchema).resolve(async(event,input)=>{// The input is directly parsed from the input schema.const{ id}=input;// The event is the same as the event that comes from SvelteKit backend.console.log(event.params);// The output is directly parsed from the output schema.return{status:200,data:{ id,name:'hanna'},message:'OK',};}),},});
import{exampleRouter}from'./routers';import{createAppRouter}from'./setup';// This is the root of all the endpoints, we define this to get a better structure of different// categories of routers.exportconstappRouter=createAppRouter({example:exampleRouter,});
<scriptlang="ts">import {callApi }from'../lib/client/caller';asyncfunction onclick() {// Fully typesafe api caller, that will give you the correct input,// output and method based on the endpoint string.const api=awaitcallApi('example.example', {input: { id:'hoho' },method:'post',});// Logs the return object of the api.console.log(api);}</script><button {onclick}>test</button>
import{publicProcedure}from'../setup';// This is how we define an middleware to be used and called inbefore each endpoint runs.constexampleProcedure=publicProcedure.use((event)=>console.log(event.cookies.getAll()));
Distributed under theMIT License. This project is open source and free to use, modify, and distribute under the terms of theMIT License.
You can find the full license text in theLICENSE.md file.
Open an issue or create a pull request. Contributions are always welcome!
If you discover asecurity vulnerability, pleasedo not open an issue or create a pull request.Instead, report itprivately by emailingtim.falk00@gmail.comornils-pettsson@outlook.com. Thank you for being responsible!
About
A simple and type-safe RPC system for SvelteKit Inspired by tRPC, built specifically for SvelteKit. Define your backend procedures and call them from the client with full end-to-end type safety — no API routes, no schemas, just functions.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.