Quick Start
Follow these setps to quickly getting started with Sveltekit-Rest
Installation
Install sveltekit-rest
on you sveltekit
project.
- npm
npm i sveltekit-rest
- pnpm
pnpm add sveltekit-rest
Setup
-
Create Router
import { initSveltekitRest } from "sveltekit-rest";
const r = initSveltekitRest.create();
export const router = {
};
- Add routes to your Router
export const router = { route1: r.get(() => { return Math.random(); }),};
-
Create Interface
Interface | Description |
---|---|
client | Typesafe client providing access to your APIs. |
serverHook | Automatically generated hook by Sveltekit-Rest for server communication. |
import { createRESTInterface } from "sveltekit-rest";import { router } from "./router";
export const { serverHook, client } = createRESTInterface<typeof router>(router);
-
Setup your hook
import { serverHook } from "$lib/rest";
export const handle = serverHook;
Enjoy Your Typesafe Api
<script lang="ts"> import { client } from "$lib/rest";
const handleApi = async () => { const value = await client.route1(); // value -> number };
</script>