Correct if wrong: Since a tRPC procedure is a hook, they cant be used in the next api/ routes

Hooks can only be used inside components - this makes me think how limiting tRPC is if it can only be used from the front end :/ ? I have a procedure that my front end uses, now I want the backend (in next api/ route) to use this procedure too and send an email. I know there are a lot questions around using tRPC procedures outside a project (https://github.com/jlalmes/trpc-openapi i think this solves that dilemma), this is similar that I what to use the procedure, but just from the backend in the same project.
GitHub
GitHub - jlalmes/trpc-openapi: OpenAPI support for tRPC 🧩
OpenAPI support for tRPC 🧩. Contribute to jlalmes/trpc-openapi development by creating an account on GitHub.
3 Replies
cadams
cadams6mo ago
Just thinking, maybe I could send the email inside the tRPC procedure, and not use the Next api route. This essentially makes the Next api/ route redundant then ? please correct me if I am wrong
Endgame1013
Endgame10136mo ago
The tRPC docs generally discourage using tRPC to make server-side calls and instead opt to extract the tRPC procedure’s code to a separate function which could be called from inside a tRPC procedure, API route, etc: https://trpc.io/docs/server/server-side-calls
Server Side Calls | tRPC
You may need to call your procedure(s) directly from the same server they're hosted in, router.createCaller() can be used to achieve this.
Endgame1013
Endgame10136mo ago
If you really want to use tRPC to make the server-side call (maybe you want the middleware chain to be invoked, etc.), then the createCaller() function may fit your needs quite well: https://trpc.io/docs/server/server-side-calls#example-for-a-nextjs-api-endpoint
Server Side Calls | tRPC
You may need to call your procedure(s) directly from the same server they're hosted in, router.createCaller() can be used to achieve this.
Want results from more Discord servers?
Add your server
More Posts