Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

NotificationsYou must be signed in to change notification settings

Falk33n/sveltekit-rpc

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.


📚 Table of Contents


🧠 How It Works

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.

🚀 Getting Started

1. Clone the repository

git clone https://github.com/Falk33n/sveltekit-rpc.gitcd sveltekit-rpc

2. Install dependencies

# (or use `bun install` / `pnpm install` / `yarn install` if you prefer).npm install

3. Start the dev server

# (or use `bun run dev` / `pnpm run dev` / `yarn run dev` if you prefer).npm run dev

Your app should now be running athttp://localhost:5173.

🛠️ How To Use

1. Define the routes

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',};}),},});

2. Create the app router

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,});

3. Calling the API

<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>

4. Creating an middleware

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()));

License

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.

🐛 Found a Bug?

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

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp