Package Exports
- trpc-sveltekit
- trpc-sveltekit/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (trpc-sveltekit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
tRPC-SvelteKit
Documentation available at icflorescu.github.io/trpc-sveltekit.
Move fast and break nothing.
End-to-end typesafe APIs for your
SvelteKit applications.
Works with
β
@sveltejs/adapter-node
β
@sveltejs/adapter-vercel
β
@sveltejs/adapter-netlify
Important
tRPC-SvelteKit v3.x.x is compatible with tRPC v10.
If you're using tRPC v9, use tRPC-SvelteKit v2.x.x. The old source code is available in the trpc-v9 branch.
Quickstart
Install the package and its dependencies:
yarn add trpc-sveltekit @trpc/server @trpc/clientCreate your tRPC router:
// lib/trpc/router.ts
import type { Context } from '$lib/trpc/context';
import { initTRPC } from '@trpc/server';
import delay from 'delay';
export const t = initTRPC.context<Context>().create();
export const router = t.router({
greeting: t.procedure.query(async () => {
await delay(500); // π simulate an expensive operation
return `Hello tRPC v10 @ ${new Date().toLocaleTimeString()}`;
})
});
export type Router = typeof router;Create a tRPC context:
// lib/trpc/context.ts
import type { RequestEvent } from '@sveltejs/kit';
import type { inferAsyncReturnType } from '@trpc/server';
// we're not using the event parameter is this example,
// hence the eslint-disable rule
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function createContext(event: RequestEvent) {
return {
// context information
};
}
export type Context = inferAsyncReturnType<typeof createContext>;Add this handle to your SvelteKit app hooks:
// hooks.server.ts
import { createContext } from '$lib/trpc/context';
import { router } from '$lib/trpc/router';
import type { Handle } from '@sveltejs/kit';
import { createTRPCHandle } from 'trpc-sveltekit';
export const handle: Handle = createTRPCHandle({ router, createContext });Define a helper function to easily use the tRPC client in your pages:
// lib/trpc/client.ts
import type { Router } from '$lib/trpc/router';
import { createTRPCClient, type TRPCClientInit } from 'trpc-sveltekit';
let browserClient: ReturnType<typeof createTRPCClient<Router>>;
export function trpc(init?: TRPCClientInit) {
if (typeof window === 'undefined') return createTRPCClient<Router>({ init });
if (!browserClient) browserClient = createTRPCClient<Router>();
return browserClient;
}Call the tRPC procedures in your pages:
// routes/+page.svelte
<script lang="ts">
import { page } from '$app/stores';
import { trpc } from '$lib/trpc/client';
let greeting = 'press the button to load data';
let loading = false;
const loadData = async () => {
loading = true;
greeting = await trpc($page).greeting.query();
loading = false;
};
</script>
<h6>Loading data in<br /><code>+page.svelte</code></h6>
<a
href="#load"
role="button"
class="secondary"
aria-busy={loading}
on:click|preventDefault={loadData}>Load</a
>
<p>{greeting}</p>Examples
This repository contains a couple of examples:
Contributors
Acknowledgements
Huge thanks to Alex / KATT, the author of tRPC, for being the first sponsor of this project! π
Stand with Ukraine
On 24th of February 2022 Russia unlawfully invaded Ukraine. This is an unjustified, unprovoked attack on the sovereignty of a neighboring country, but also an open affront to international peace and stability that has the potential to degenerate into a nuclear event threatening the very existence of humanity. I am an EU (Romanian) citizen, but I am doing everything in my power to stop this madness. I stand with Ukraine. The entire Svelte community β€οΈπΊπ¦. Here's how you can show your support.
License
The ISC License.
