MCPcopy Index your code
hub / github.com/KevinEdry/nestjs-trpc

github.com/KevinEdry/nestjs-trpc @v2.12.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.12.0 ↗ · + Follow
1,616 symbols 3,875 edges 248 files 34 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

<img alt="tRPC" src="https://i.imgur.com/JvsOXCg.png" />

Nestjs tRPC Adapter

An opinionated approach to building End-to-end typesafe APIs with tRPC within NestJS.

weekly downloads MIT License Discord

Demo
The client above is not importing any code from the server, only its type declarations.

Introduction

NestJS tRPC is a library designed to integrate the capabilities of tRPC into the NestJS framework. It aims to provide native support for decorators and implement an opinionated approach that aligns with NestJS conventions.

Features

  • Fast Rust-based CLI for type generation with rich error messages
  • Full static typesafety & autocompletion on the client, for inputs, outputs, and errors
  • Implements the NestJS opinionated approach to how tRPC works
  • Watch mode for auto-regeneration during development
  • Out of the box support for Dependency Injection within the routes and procedures
  • Native support for express, fastify, and zod with more drivers to come
  • Examples are available in the ./examples folder

Quickstart

Installation

To install NestJS tRPC with your preferred package manager, you can use any of the following commands:

# bun
bun add nestjs-trpc zod @trpc/server

# npm
npm install nestjs-trpc zod @trpc/server

# pnpm
pnpm add nestjs-trpc zod @trpc/server

# yarn
yarn add nestjs-trpc zod @trpc/server

Generate Types

Run the CLI to generate your AppRouter types:

# Generate types (one-time)
npx nestjs-trpc generate

# Watch mode for development
npx nestjs-trpc watch

How to use

Here's a brief example demonstrating how to use the decorators available in NestJS tRPC:

// users.router.ts
import { Inject } from '@nestjs/common';
import { Router, Query, UseMiddlewares } from 'nestjs-trpc';
import { UserService } from './user.service';
import { ProtectedMiddleware } from './protected.middleware';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';

const userSchema = z.object({
  name: z.string(),
  password: z.string()
})

@Router()
class UserRouter {
  constructor(
    @Inject(UserService) private readonly userService: UserService
  ) {}

  @UseMiddlewares(ProtectedMiddleware)
  @Query({ output: z.array(userSchema) })
  async getUsers() {
    try {
      return this.userService.getUsers();
    } catch (error: unknown) {
      throw new TRPCError({
        code: "INTERNAL_SERVER_ERROR",
        message: "An error has occured when trying to get users.",
        cause: error
      })
    }
  }
}

👉 See full documentation on NestJS-tRPC.io. 👈

All contributors

NestJS tRPC is developed by Kevin Edry, which taken a huge inspiration from both NestJS and tRPC inner workings.

<img width="720" height="50" src="https://contrib.rocks/image?repo=kevinedry/nestjs-trpc" alt="A table of avatars from the project's contributors" />

Extension points exported contracts — how you extend this code

TRPCMiddleware (Interface)
(no doc) [16 implementers]
packages/nestjs-trpc/lib/interfaces/middleware.interface.ts
FeatureCardProps (Interface)
(no doc)
docs/components/FeatureCard/index.tsx
RolesMeta (Interface)
(no doc)
examples/nestjs-fastify/src/roles.middleware.ts
TRPCContext (Interface)
(no doc) [6 implementers]
packages/nestjs-trpc/lib/interfaces/context.interface.ts
Stats (Interface)
(no doc)
docs/components/Hero/index.tsx
AuthContext (Interface)
(no doc)
examples/nestjs-fastify/src/roles.middleware.ts
ZodHelpers (Interface)
Trait methods for Zod schema processing, some may be used in future [1 implementers]
packages/nestjs-trpc/cli/src/parser/schema/helpers.rs
TableProps (Interface)
(no doc)
docs/components/Table/index.tsx

Core symbols most depended-on inside this repo

Query
called by 134
packages/nestjs-trpc/lib/decorators/query.decorator.ts
Router
called by 82
packages/nestjs-trpc/lib/decorators/router.decorator.ts
parse_file
called by 73
packages/nestjs-trpc/cli/src/parser/mod.rs
Mutation
called by 68
packages/nestjs-trpc/lib/decorators/mutation.decorator.ts
write_file
called by 33
packages/nestjs-trpc/cli/src/parser/imports/resolver.rs
generate_procedure_string
called by 17
packages/nestjs-trpc/cli/src/generator/server.rs
build_imports_map
called by 17
packages/nestjs-trpc/cli/src/parser/imports/resolver.rs
resolve_compiler_options
called by 16
packages/nestjs-trpc/cli/src/tsconfig.rs

Shape

Function 709
Method 518
Class 342
Interface 28
Enum 19

Languages

Rust59%
TypeScript41%

Modules by API surface

packages/nestjs-trpc/cli/src/generator/server.rs78 symbols
packages/nestjs-trpc/cli/src/parser/module.rs55 symbols
packages/nestjs-trpc/cli/src/config.rs50 symbols
packages/nestjs-trpc/cli/src/generator/mod.rs49 symbols
packages/nestjs-trpc/cli/src/parser/decorator.rs48 symbols
packages/nestjs-trpc/cli/src/parser/mod.rs42 symbols
packages/nestjs-trpc/cli/src/parser/router.rs41 symbols
packages/nestjs-trpc/cli/src/parser/imports/resolver.rs38 symbols
packages/nestjs-trpc/cli/src/parser/context.rs30 symbols
packages/nestjs-trpc/cli/src/generator/types.rs30 symbols
packages/nestjs-trpc/lib/factories/__tests__/procedure.factory.spec.ts27 symbols
packages/nestjs-trpc/cli/src/parser/schema/flatten/mod.rs27 symbols

For agents

$ claude mcp add nestjs-trpc \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact