nestjs-trpc-v2
v1.4.1
Published
A maintained fork of nestjs-trpc - End-to-end typesafe APIs with tRPC in NestJS
Downloads
1,880
Maintainers
Readme
⚠️ Important Notice: This is a maintained fork of the original nestjs-trpc by Kevin Edry. The original repository is no longer actively maintained. This fork continues development with modern tooling, updated dependencies, and ongoing support.
Introduction
nestjs-trpc-v2 is a library designed to integrate the capabilities of tRPC into the NestJS framework. It provides native support for decorators and implements an opinionated approach that aligns with NestJS conventions.
This v2 fork includes:
- 🔄 Active maintenance and updates
- 📦 Turborepo monorepo structure
- 🚀 Modern build tooling
- 🔒 Updated dependencies with security patches
- ✅ Automated CI/CD workflows
Features
- ✅ Supports most tRPC features out of the box with more to come.
- 🧙 Full static typesafety & autocompletion on the client, for inputs, outputs, and errors.
- 🙀 Implements the Nestjs opinionated approach to how tRPC works.
- ⚡️ Same client-side DX - We generate the AppRouter on the fly.
- 🔋 Examples are available in the ./examples folder.
- 📦 Out of the box support for Dependency Injection within the routes and procedures.
- 👀 Native support for
express,fastify, andzodwith more drivers to come!
Quickstart
Installation
To install nestjs-trpc-v2 with your preferred package manager, you can use any of the following commands:
# npm
npm install nestjs-trpc-v2 zod @trpc/server
# pnpm
pnpm add nestjs-trpc-v2 zod @trpc/server
# yarn
yarn add nestjs-trpc-v2 zod @trpc/serverPeer Dependencies
Make sure you have the following peer dependencies installed:
@nestjs/common(^9.3.8 || ^10.0.0 || ^11.0.0)@nestjs/core(^9.3.8 || ^10.0.0 || ^11.0.0)@trpc/server(^10.0.0 || ^11.0.0)reflect-metadata(^0.1.13 || ^0.2.0)rxjs(^7.8.1)zod(^3.14.0 || ^4.0.0)
How to use
Here's a brief example demonstrating how to use the decorators available in nestjs-trpc-v2:
// users.router.ts
import { Inject } from '@nestjs/common';
import { Router, Query, UseMiddlewares } from 'nestjs-trpc-v2';
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,
});
}
}
}👉 For more examples and detailed documentation, see NestJS-tRPC-v2.io. 👈
Development
This project uses Turborepo for managing the monorepo.
Prerequisites
- Node.js >= 18
- pnpm >= 10.0.0
Setup
# Clone the repository
git clone https://github.com/mguay22/nestjs-trpc-v2.git
cd nestjs-trpc-v2
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run development mode
pnpm devContributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
Credits & License
Original Library
nestjs-trpc-v2 is a maintained fork of nestjs-trpc, originally developed by Kevin Edry. The original library took huge inspiration from both NestJS and tRPC inner workings.
Original Contributors
This Fork
This fork is maintained by Michael Guay and the community.
License
MIT - See LICENSE file for details.
⭐ If this library helps you, please consider giving it a star!
For questions or support:
