I Quit tRPC. Here's What I Do Instead

preview_player
Показать описание
tRPC was and still is a good tool to quickly build full-stack projects. I stand by that. Here's why I haven't used it for months and the approach I've been using instead. This has worked super well for me, and I think it will for you too.

-- my links

Рекомендации по теме
Комментарии
Автор

Have you heard about the ts-rest library? It basically what you are manually doing and I prefer it so much more over trpc. It has End to end type safety, RPC-like client side interface, Tiny bundle size, Well-tested, production ready, No Code Generation, Zod support for runtime type checks, Full optional OpenAPI integration. You should do a video about it, it's so much simpler.

JohnMcclaned
Автор

Gotta say you’ve kinda missed the whole point of trpc, you didn’t have an “alternative” to trpc. You just hard coded what they do under the hood in a less secure way. Because now you have to actually infer each type and it’s not so different from type assertion with the only exception of throwing an error.
What trpc allows you is only once declare your types on the api handler itself(like a regular function) and automatically when you call it you get type safety without additional assertions or parsing(which can go wrong because of human error) and another huge win(one that Theo praises) is that the response is inferred automatically by what you return from the function, no need to type assert for the response

TheIpicon
Автор

zod is not a tRPC alternative... tRPC doesn't do any validation or parsing. it's even designed with libraries like zod in mind.

FunctionGermany
Автор

tRPC is not just about type safety. It offers many features that help to create a solid workflow, such as Contexts and Middlewares. While NextJS does have middleware support, the granularity that tRPC offers is very useful in many cases. Implementing ACL in tRPC routes is very easy using metadata in contexts, and you can centralize all the checks in middlewares to prevent the user from forgetting to implement the correct roles in every route. In a large or multi-tenant application, these features come in very handy.

stefanosandes
Автор

Really cool, but you still don't get auto completion for your endpoints, you have to know the api structure and what endpoint you want to hit, and manually atribute validators to them.

bernardoquina
Автор

very good video and a fresh approach. And to the point... if you want to move fast with Next versions, tRPC is slow to follow.
However, I think you should've mentioned downsides of your approach too. It's not a replacement to tRPC and tRPC is not only for type safety. It makes you define types only once. In your case, you have to define ApiResponse types, while in tRPC it gets inferred "automatically".

MirkoVukusic
Автор

sweet! now all that's left is a full-stack drizzle tutorial.

mmzzzmeemee
Автор

Bro, the last few days your uploads been stressing me out. I started a new project with prisma, and you uploaded the prisma video, I have been using tRPC with the same project, and now this.
I love the content, and that you share your opinion on packages/tech as you use them. Keep it up.

ogiswxk
Автор

Although i do understand what youre trying to do and it is what I have been doing for my projects where I do not use TRPC, I believe it is not correct to say that this gives any comparable degree of typesafety since the fact is axios response and request types are any/unkown so even if i give it an invalid type with an input that matches that type, it will happily oblige. This approach is the same one that people have used in the past as well, except that you are extracting types from the validator.

mm_subhan
Автор

With trpc i have no ability to acces the filesystem. I know that’s not the point of trpc, but that’s frustrating man, i just want to have something that does all.

universe_decoded
Автор

I guess that works for inputs, but is there a way to infer the return type of the route handler function?
Next API routes return a Response type, but you don’t seem to be able to get the exact type of the body response

mortysmith
Автор

for mutation u can use server actions
and for fetching data u can fetch it directly if it is a server component
I think this is 90% of cases, isn't it ?

kuroisan
Автор

It's crazy how in Javascript world we can say "in the past" meaning a couple of weeks ago lol

drgomesp
Автор

Why would you sacrifice all of that to work with the latest version of nextjs? At the end everything will change in about a year or two, I don't see it

yassinesafraoui
Автор

Well i feel good. I started doing this a few months ago on my own. Now that i see you are also doing this make s me feel good❤

UwU-dxhu
Автор

Would be nice to have function calls like trpc to abstract the http verbs away

onepunchman
Автор

Hi Josh, I like your content, this one too. But I think you missed the mark on the messaging here. Since you just did a custom implementation of a small part of what TRPC does and is actually a much better solution if you only need that. I too am in the process of rewriting stuff into the app directory. However, when you start to think about authentication (public / protectedProcedure), refetching of stale data etc. You'll quickly realise you're going to implement a lot of stuff TRPC already does and in that case I'd suggest to use TRPC or something similar that is tested through time.

strmchsr
Автор

Now, this is a good quality video!! Thank you! Explanation is clear and concise and also shows how to properly handle errors (which is what most videos don't care about, yet it is one of the most important parts of dev and differentiates good engineer from crappy one). Keep up the good work.

davidhavl
Автор

Why don't simply use Server Actions? Unless I need to go to production soon, I no longer see a use case for tRPC

PyrexCelso
Автор

Hey Josh, your VSCode setup looks like it is working real smoothly. My setup sucks! I can’t get my intellisense to work properly and I possibly have the wrong extensions. Would you mind sharing your configuration for VSCode with us noobs?

Gosurfdammit