New to the t3 stack and tRPC, how do I share code between route functions correctly?
Maybe I'm going about this the complete wrong way. Basically, as you can see below I am trying to create two different routes that share a lot of logic that I want to make into a function. I tried making a function outside of the
discordRouter
but I couldn't figure out how to type ctx
correctly, so any thoughts would be appreciated. What I've tried so far is import { type createTRPCContext } from "~/server/api/trpc";
then type Context = ReturnType<typeof createTRPCContext>;
but that's a promise for the context and I feel like I'm going about it the wrong way.
3 Replies
I think I figured it out just by adding
to my
trpc.ts
and importing that, but if there's a better way please let me know! especially since trpc.ts
says YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
so i feel like this is still a hacky solution and not the correct way...yeah this doesn't take into account that in a
protectedProcecure
the session
and user
are guaranteed not null...nvm i shoulda just read the fuckin docs
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, createCallerFactory() can be used to achieve this. This is useful for server-side calls and for integration testing of your tRPC procedures.