Skip to content

Quick Start

Follow these setps to quickly getting started with Sveltekit-Rest

Installation

Install sveltekit-rest on you sveltekit project.

  • npm
Install with npm
npm i sveltekit-rest
  • pnpm
Insall with pnpm
pnpm add sveltekit-rest

Setup

  • Create Router

src/lib/rest/router.ts
import { initSveltekitRest } from "sveltekit-rest";
const r = initSveltekitRest.create();
export const router = {
};
  • Add routes to your Router
src/lib/rest/router.ts
export const router = {
route1: r.get(() => {
return Math.random();
}),
};
  • Create Interface

InterfaceDescription
clientTypesafe client providing access to your APIs.
serverHookAutomatically generated hook by Sveltekit-Rest for server communication.
src/lib/rest/index.ts
import { createRESTInterface } from "sveltekit-rest";
import { router } from "./router";
export const { serverHook, client } =
createRESTInterface<typeof router>(router);
  • Setup your hook

src/hooks.server.ts
import { serverHook } from "$lib/rest";
export const handle = serverHook;

Enjoy Your Typesafe Api

+page.svelte
<script lang="ts">
import { client } from "$lib/rest";
const handleApi = async () => {
const value = await client.route1(); // value -> number
};
</script>