You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I _love_[tRPC](https://trpc.io/), [KATT (Alex Johansson)](https://github.com/KATT) and all the [other maintainers](https://github.com/trpc/trpc/graphs/contributors) have done some amazing work, and for applications with a single Next.js app, or an express server only consumed by TRPC clients, I whole heartily recommend using tRPC! Also I have undoubtedly taken inspiration from tRPC for tREST.
@@ -19,24 +15,3 @@ tREST allows you design an API as you would "normally", e.g. GET, POST, PUT, DEL
19
15
tRPC structures your API as RPC calls such as `/trpc/getPosts` or `/trpc/getPostComments` etc, this provides an arguably simpler API for the client implementation, however, you loose the predictability of REST(ish) APIs if you have consumers who aren't in Typescript (able to us @ts-rest) or public consumers.
20
16
21
17
tRPC has many plugins to solve this issue by mapping the API implementation to a REST-like API, however, these approaches are often a bit clunky and reduce the safety of the system overall, tREST does this heavy lifting in the client and server implementations rather than requiring a second layer of abstraction and API endpoint(s) to be defined.
22
-
23
-
|**Features**| REST | tRPC | tREST |
24
-
| ----------------- | ---- | ----- | ------ |
25
-
| E2E Type Safe | ❌ | ✅ | ✅ |
26
-
| Protocol | REST | RPC | REST |
27
-
| Public API | ✅ | ❌ | ✅ |
28
-
| Zod/Yup/Joi | ❌ | ✅ | 🏗 v1.0 |
29
-
| WebSocket Support | ❌ | ✅ | ❌ |
30
-
| Cmd+Click Access | ❌ | 🏗 v10 | ✅ |
31
-
| Separate Contract | ❌ | ❌ | ✅ |
32
-
33
-
tREST also supports [Nest](https://nestjs.com/), it appears adding Nest to tRPC is against the Nest controller principles, so it is not recommended.
Define a contract with the `@ts-rest/core` package, you may nest routers within a router, generally you'd want a router for each nested resource e.g. `/users/:id/posts` could have a nested router `contract.users.posts`, this path is what you'd use on the client to query the API.
4
+
5
+
Breaking down the contract to sub-routers also allows you to split up the backend implementation, for example in Nest.js you could have multiple controllers for the sub-routers.
Since 2.0 ts-rest-api has a built-in error handling, all you need to do is define the response status codes in the contract. The Nest/Express libraries handle the rest for you, letting you utilise HTTP status codes fully without worrying about type safety!
4
+
5
+
```typescript
6
+
exportconst routerBasic =c.router({
7
+
updateUser: c.mutation({
8
+
method: 'PATCH',
9
+
path: ({ id }: { id:string }) =>`/basic/users/${id}`,
0 commit comments