0.11.2 • Published 2 years ago
@bevm0/trpc-sveltekit v0.11.2
@bevm0/trpc-sveltekit
Move fast and break nothing.
End-to-end typesafe APIs for your
SvelteKit applications.
Quickstart
Install the package and its dependencies:
# npm
npm install @bevm0/trpc-sveltekit @trpc/client @trpc/server
# Yarn
yarn add @bevm0/trpc-sveltekit @trpc/client @trpc/server
# pnpm
pnpm add @bevm0/trpc-sveltekit @trpc/client @trpc/serverCreate a tRPC context:
// src/lib/trpc/context.ts
import type { trpcSvelteContextOptions } from '@bevm0/trpc-sveltekit'
import type { inferAsyncReturnType } from '@trpc/server'
export const createContext = (options: trpcSvelteContextOptions) => (options)
export type Context = inferAsyncReturnType<typeof createContext>Create your tRPC router:
// src/lib/trpc/router.ts
import delay from 'delay';
import { initTRPC } from '@trpc/server';
import type { Context } from '$lib/trpc/context';
const t = initTRPC.context<Context>().create();
export const { router, procedure } = t
export const appRouter = router({
greeting: procedure.query(async () => {
await delay(500); // 👈 simulate an expensive operation
return `Hello tRPC v10 @ ${new Date().toLocaleTimeString()}`;
})
});
export type AppRouter = typeof appRouter;Add this handle to your SvelteKit hooks hooks:
// hooks.server.ts
import { createTRPCHandle } from '@bevm0/trpc-sveltekit';
import type { Handle } from '@sveltejs/kit';
import { createContext } from '$lib/trpc/context';
import { appRouter } from '$lib/trpc/router';
export const handle: Handle = createTRPCHandle({ router: appRouter, createContext });Or add it to a +server.ts file. More information is available on the official documentation.
Define a helper function to easily use the tRPC client in your pages:
// src/lib/trpc/client.ts
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client'
import type { AppRouter } from '$lib/trpc/router';
export const trpc = createTRPCProxyClient<AppRouter>({
links: [ httpBatchLink({ url: 'http://localhost:5173/trpc' }) ]
})Call the tRPC procedures in your pages:
<!-- routes/+page.svelte -->
<script lang="ts">
import trpc from '$lib/trpc/client';
let greeting = 'press the button to load data';
let loading = false;
async function loadData () {
loading = true;
greeting = await trpc.greeting.fetch();
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>Using the svelte-query + tRPC proxy client from @bevm0/trpc-svelte-query
Same steps as the other README
0.11.1
2 years ago
0.11.2
2 years ago
0.11.0
2 years ago
0.9.22
3 years ago
0.9.18
3 years ago
0.9.19
3 years ago
0.9.20
3 years ago
0.9.21
3 years ago
0.9.17
3 years ago
0.9.16
3 years ago
0.9.15
3 years ago
0.9.14
3 years ago
0.9.13
3 years ago
0.9.12
3 years ago
0.9.11
3 years ago
0.9.3
3 years ago
0.9.1
3 years ago
0.9.0
3 years ago
0.1.69
3 years ago
0.1.5
3 years ago
0.1.4
3 years ago
0.1.3
3 years ago
0.1.2
3 years ago
0.1.1
3 years ago
0.1.0
3 years ago
0.0.999
3 years ago
0.0.99
3 years ago
0.0.98
3 years ago
0.0.97
3 years ago
0.0.95
3 years ago
0.0.94
3 years ago
0.0.93
3 years ago
0.0.90
3 years ago
0.0.80
3 years ago
0.0.70
3 years ago
0.0.69
3 years ago