npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bevm0/trpc-sveltekit

v0.11.2

Published

simple tRPC integration with SvelteKit hooks via the fetch adapter

Downloads

120

Readme

@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/server

Create 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