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 🙏

© 2026 – Pkg Stats / Ryan Hefner

trpc-controllers

v1.1.0

Published

Decorators and class-based routing for tRPC v11

Readme

trpc-controllers

Decorators and class-based routing for tRPC v11 with support for standard and legacy decorators.

Installation

Requires Node.js >=18.

npm install trpc-controllers

Quick Start

Class-based routers

import { z } from 'zod';
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
import {
  Router,
  Query,
  Mutation,
  UseZod,
  UseMiddlewares,
  Auth,
  RateLimit,
  Ctx,
  Input,
  createClassRouter,
} from 'trpc-controllers';

interface AppContext {
  user?: { id: string; role: 'user' | 'admin' };
}

const t = initTRPC.context<AppContext>().create({ transformer: superjson });

const logger = async ({ path, type, next }) => {
  console.log(`[${type}] ${path}`);
  return next();
};

@Router('users')
@UseMiddlewares(logger)
export class UsersController {
  @Query('getById')
  @UseZod(z.object({ id: z.string() }))
  getById(@Ctx() _ctx: AppContext, @Input() input: { id: string }) {
    return { id: input.id };
  }

  @Mutation('create')
  @UseZod(z.object({ name: z.string() }))
  @Auth((ctx: AppContext) => (ctx.user?.role === 'admin' ? true : 'FORBIDDEN'))
  @RateLimit({ points: 5, durationSec: 60 })
  create(@Input() input: { name: string }) {
    return { id: '1', name: input.name };
  }
}

const { router: appRouter } = createClassRouter({
  t,
  controllers: { users: new UsersController() },
});

export type AppRouter = typeof appRouter;

Tip: use an object for controllers to preserve route keys in the inferred types, especially when you register multiple controllers.

Low-level API

import { z } from 'zod';
import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson';
import { makeDecorators } from 'trpc-controllers';

const t = initTRPC.context().create({ transformer: superjson });
const { query, mutation, input, controllerToRouter } = makeDecorators(t);

class UsersController {
  @query()
  @input(z.object({ id: z.string() }))
  getById({ input }: { input: { id: string } }) {
    return { id: input.id };
  }

  @mutation()
  @input(z.object({ name: z.string() }))
  create({ input }: { input: { name: string } }) {
    return { id: '1', name: input.name };
  }
}

const userRouter = controllerToRouter(new UsersController());
const appRouter = t.router({ user: userRouter });

export type AppRouter = typeof appRouter;

Decorator compatibility

  • Standard decorators (TypeScript 5) work for class/method decorators.
  • Parameter decorators (@Ctx, @Input) require legacy decorators (experimentalDecorators: true).
  • If you prefer standard decorators only, use resolvers that receive the tRPC resolver object (e.g. ({ ctx, input }) => {}).

Examples

See the tests directory for more examples and the examples folder for Express and Fastify adapters.

Types package generation

Use trpc-controllers types to publish your server router types as a small npm package that any frontend can import.

  1. Add a types-only package (e.g. trpc-types/) that re-exports your router and has a tsconfig.json that emits declarations only:

    {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "composite": true,
        "declaration": true,
        "declarationMap": true,
        "emitDeclarationOnly": true,
        "module": "ESNext",
        "moduleResolution": "Bundler",
        "outDir": "dist",
        "types": []
      },
      "include": ["./src/**/*"]
    }
  2. From the repo root, run:

    npx trpc-controllers types --project ./trpc-types/tsconfig.json

    This runs tsc -p and, if installed, tsc-alias to rewrite path aliases (skip with --no-alias).

  3. Publish the generated package to npm with types pointing at the declaration output (e.g. "types": "dist/index.d.ts" and "files": ["dist"]).

  4. In your frontend, install that package and use it for a typed client:

    import superjson from 'superjson';
    import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
    import type { AppRouter } from 'server-trpc-types';
    
    const trpc = createTRPCProxyClient<AppRouter>({
      transformer: superjson,
      links: [httpBatchLink({ url: '/trpc' })],
    });

License

MIT