Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevinedry/nestjs-trpc
Providing native support for decorators and implement an opinionated approach that aligns with NestJS conventions.
https://github.com/kevinedry/nestjs-trpc
api express nestjs trpc typescript
Last synced: 7 days ago
JSON representation
Providing native support for decorators and implement an opinionated approach that aligns with NestJS conventions.
- Host: GitHub
- URL: https://github.com/kevinedry/nestjs-trpc
- Owner: KevinEdry
- License: mit
- Created: 2023-04-01T08:15:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-16T14:32:25.000Z (2 months ago)
- Last Synced: 2024-10-19T14:26:56.684Z (2 months ago)
- Topics: api, express, nestjs, trpc, typescript
- Language: TypeScript
- Homepage: https://NestJS-tRPC.io
- Size: 3.3 MB
- Stars: 36
- Watchers: 2
- Forks: 4
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Nestjs tRPC Adapter
An opinionated approach to building
End-to-end typesafe APIs with tRPC within NestJS.
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
- ✅ 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`, and `zod` with more drivers to come!## Quickstart
### Installation
To install **NestJS tRPC** with your preferred package manager, you can use any of the following commands:
```shell
# npm
npm install nestjs-trpc zod @trpc/server# pnpm
pnpm add nestjs-trpc zod @trpc/server# yarn
yarn add nestjs-trpc zod @trpc/server
```## How to use
Here's a brief example demonstrating how to use the decorators available in **NestJS tRPC**:
```typescript
// 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](https://nestjs-trpc.io/docs). 👈**
## All contributors
> NestJS tRPC is developed by [Kevin Edry](https://twitter.com/KevinEdry), which taken a huge inspiration from both NestJS and tRPC inner workings.