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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fastify-inject-trpc

v0.1.1

Published

Plugin for Fastify that allows to use the Fastify inject API with the full power of tRPC types

Readme

💉 Fastify inject tRPC

Plugin for Fastify that allows to use the Fastify inject API with the full power of tRPC types

Installation

Install with npm or yarn or pnpm

npm install fastify-inject-trpc
# or
yarn add fastify-inject-trpc
# or
pnpm add fastify-inject-trpc

Motivation

This library was created for testing tRPC procedures with Fastify as a server. This solution is better than tRPC caller, because this plugin allows to use Fastify ecosystem and don't create custom context for testing, it will be automatically generated by tRPC like in real request.

Features

  • Allows to inject tRPC procedures calls with fastify inject API
  • Fully type safe procedures calls
  • Very similar API to Fastify's inject

Limitations

  • Subscriptions are not supported at this time
  • Custom serializers are not supported at this time

Usage

import fastify from 'fastify'
import { z } from 'zod'
import { initTRPC } from '@trpc/server'
import {
  fastifyTRPCPlugin,
  FastifyTRPCPluginOptions,
} from '@trpc/server/adapters/fastify'
import { fastifyInjectTRPCPlugin } from 'fastify-inject-trpc'

// 1. Init TRPC
const t = initTRPC.create()

// 2. Create router
const appRouter = t.router({
  greeting: t.procedure
    .input(z.object({ name: z.string() }))
    .query(({ input: { name } }) => {
      return `Hello, ${name}!`
    }),
})

type AppRouter = typeof appRouter

// 3. Create Fastify builder
async function buildFastify() {
  // 3.1. Create Fastify instance
  const app = fastify()

  // 3.2. Register fastifyTRPCPlugin
  app.register(fastifyTRPCPlugin, {
    prefix: '/trpc',
    trpcOptions: {
      router: appRouter,
    },
  })

  // 3.3. Register fastifyTRPCInjectorPlugin and await it (https://fastify.dev/docs/latest/Reference/Plugins/#asyncawait)
  await app.regester(fastifyTRPCInjectorPlugin, {
    router: appRouter,
    prefix: '/trpc',
  })

  return app.withTypedTRPCInjector<AppRouter>()
}

const app = await buildFastify()

// 4. Inject a tRPC procedure call
const response = await app.injectTRPC((router) =>
  router.greeting({ name: 'World' }),
)
const json = response.json()

// Before using it you should check that response didn't return any error
if ('result' in json) {
  const greetingStr = json.result.data
  // ^?
  // Type of greetingStr would be a string
}