DEV Community

Rodrigo Nogueira
Rodrigo Nogueira

Posted on • Edited on

nest-trpc-native: Full NestJS Power + tRPC Type Safety with Zero Runtime Overhead

Hey NestJS + tRPC community! πŸ‘‹

I’m excited to release nest-trpc-native v0.3.0 β€” a decorator-first, native-feeling tRPC integration for NestJS.

No adapter glue code in your app layer. No compromise on NestJS features.

You write tRPC routers like Nest classes, with full support for dependency injection, guards, interceptors, pipes, filters, request-scoped providers, and end-to-end type safety.

nest-trpc-native router sample

Why this matters

Most tRPC + NestJS approaches push you toward one side:

  • Keep all NestJS lifecycle features, but lose clean tRPC developer experience, or
  • Keep pure tRPC style, but lose NestJS enhancers and request-scoped DI.

nest-trpc-native is designed so you can have both.

Core Features

  • @Router('users') + @Query(), @Mutation(), @Subscription()
  • Full support for @UseGuards(), @UseInterceptors(), @UsePipes(ValidationPipe), @UseFilters()
  • @Input() and @TrpcContext() for parameter extraction
  • Auto-generated router types (autoSchemaFile: 'src/@generated/server.ts')
  • Works with Express and Fastify
  • Zod or classic class-validator validation
  • Zero runtime dependencies in nest-trpc-native itself (host app controls Nest/tRPC peers)
  • Monorepo-friendly sample layout
  • Microservice transport pattern demonstrated in a focused sample

Router Example (NestJS style)

@Router('users')
@UseGuards(AuthGuard)
export class UsersRouter {
  constructor(private readonly usersService: UsersService) {}

  @Query({ input: FindOneSchema, output: UserSchema.nullable() })
  @UsePipes(new ValidationPipe({ transform: true }))
  findOne(
    @Input() input: { id: number },
    @TrpcContext('requestId') requestId: string,
  ) {
    return this.usersService.findById(input.id);
  }

  @Mutation({ input: CreateUserSchema, output: UserSchema })
  create(@Input() input: { name: string; email: string }) {
    return this.usersService.create(input);
  }
}
Enter fullscreen mode Exit fullscreen mode

Client side stays fully type-safe:

const user = await trpc.users.findOne.query({ id: 123 });
// autocomplete + compile-time safety
Enter fullscreen mode Exit fullscreen mode

Installation

npm install nest-trpc-native @trpc/server
npm install @nestjs/common @nestjs/core reflect-metadata rxjs
# optional:
npm install zod
Enter fullscreen mode Exit fullscreen mode

Then register the module:

TrpcModule.forRoot({
  path: '/trpc',
  autoSchemaFile: 'src/@generated/server.ts',
})
Enter fullscreen mode Exit fullscreen mode

Samples You Can Run Now

  • sample/00-showcase: full integration baseline (guards, interceptors, pipes, filters, request scope, Express/Fastify, typed clients, subscriptions)
  • sample/11-microservice-transport: tRPC edge gateway + Nest microservice transport (TCP)

From repo root:

npm run showcase
Enter fullscreen mode Exit fullscreen mode

Docs, Repo, and NPM

Docs: https://rodrigobnogueira.github.io/nest-trpc-native/docs/introduction

Repo: https://github.com/rodrigobnogueira/nest-trpc-native

NPM: https://www.npmjs.com/package/nest-trpc-native

Try it, break it, open issues, and share feedback.

Thanks!

nestjs #trpc #typescript

Top comments (0)