tRPC

T

tRPC

Move Fast & Break Nothing. End-to-end typesafe APIs made easy.

Join

Is it possible to call a query from another query defined in the same router?

Say I have some code like the following: ``` t.router({ compute_all: t.procedure .query(async ({ctx, input})=>{...

Testing with React Testing Library

Hi! I am trying to test my
DestinationForm
DestinationForm
component using react testing library. My test is simply rendering the component: ```ts test('contains 2 input fields and a submit button', () => { const view = render(<DestinationForm />)...

Can I cache a response server-side?

For my project one of the trpc requests in my app works as an API Gateway to another API. I make a request to TRPC server, that makes a request with a secret API key to a 3rd party API, and returns the response to my user. In my case this 3rd party API has a rate limit that I'm going to be hitting soon....

Proxy TRPC server to another client

I've two TRPC servers, one essentially acts like a proxy, it validates auth and based on a specific property it forwards you to the destined server (with that one property removed). Now I have to write all those queries and mutation twice, is there a simpeler way to do this? Is there any way to bind a specific TRPC client to a server?...

Unexpected token

hi, kindly help me on this one

Throw custom TRPCError with specific cause

Hello everyone ! I'm trying to throw a custom TRPCError with a specific cause that i can differentiate from other trpc errors. My use case is to be able to catch error on client and display an error message if the error code is 'BAD_REQUEST' and a specific cause. After investigating i find that something like this should work in my mutation procedure: ``` throw new TRPCError({ message: 'Password changed too recently', code: 'BAD_REQUEST',...

Delay IsLoading....

i'm trying to delay a loading spinner from appearing if the api takes less than 200ms to get the data. I've tried a number of implementations and failed, can anyone help? ```js const { isLoading, isError, data } = trpc.ticketDomain.getByTicketId.useQuery(...

Clearing Cache

How do I clear the queryCache for a particular query using the trpc context? If that's not possible, how can I do that using queryClient?
queryClient.getQueryCache().clear()
queryClient.getQueryCache().clear()
I've tried the above but it clears the entire cache. I just want to clear the cache of particular key....

context at times empty

am new to trpc. i am usin iron-session with nextjs and its awesome. am forwarding the context. but sometimes session is empty. then it reload and session is not empty. tried it with static data. this doesnt happen with static data. any ideas? i have followed the trpc docs for adding trpc to an existing nextjs application.

query and mutate promise on next

I've followed the guide "Usage with Next.js" and now I only have the useQuery and use useMutation hooks exposed when I want to call a route. How do I also expose the regular query and mutate promises on these routes? Edit: Solution: ```...

testing a trpc hook

i'm brand new to testing and need some help with writing a test for a hook that is returning an object. im stuck with the error messages. i'm using trpc and react query ```js export function useGetTenantTickets() { const setTickets = useTenantStore((state) => state.setTickets); const { isLoading, isError } = trpc.ticketDomain.getByTenantId.useQuery(...

CAS Authentication

So long story short. My University is using CAS as authentication provider. I was thinking that I could use NextAuth with it to authenticate my users but I have no clue how am I supposed to do that. They don't support OAuth2 because they have old version of CAS. I asked on GH discussion in NextAuth for help but didn't get any. I also tried seeking help on Theo's discord twice this month but no luck. I don't know what to do and I feel like people working there have no clue what to do. I really want to be using TRPC with NextJS as I love this combo but how on earth can I authenticate my users using something like this? I was told I could use this lib https://www.npmjs.com/package/node-cas-client...

save return type to type var

Hello, I want to save the type that return from useQuery to type variable. For example, I have this complex return type from the query and would like to do something like type userInfoType = object. Please point me to some direction. Thank youuuuu ❤️...

Difference in type inference for vanilla client?

Hello, querying an endpoint with the vanilla tRPC client but at runtime the actual result isn't the inferenced type but {json: ..., meta: ...}, am I doing something wrong?

Zod Error Middleware

Hey lovely people. At Cal.com we make heavy use of zod for data validation. I was wondering if there's a way to create some kind of "error middleware" that can catch all Zod specific errors and serve the proper TRPC error back to the client. This would allow us to just use schema.parse instead of having to manually check schema.safeParse or having to use try/catch everywhere. Thanks for any input! 🙏...

clear cookie onError

I'd like to clear a cookie (in nextjs) when a 403 error happens. I've been looking at the global error handling: https://trpc.io/docs/error-handling#handling-errors and this looks like the right place, except that I don't have access to the response , which would be needed to clear a cookie. Is there a better place to do this, or is there a reason there is no access to the res ?...

Mutation with react-hook-form

Hello I'm trying to use mutation with react-hook-form. In the form, I will have to give interface of the input that I want, so I'm wondering whether it's possible to get the type of input to some mutation from trpc and pass it as input type.

Request context from getServerSideProps

The docs of tRPC context https://trpc.io/docs/v10/context
export async function createContext(opts: trpcNext.CreateNextContextOptions) {
export async function createContext(opts: trpcNext.CreateNextContextOptions) {
...

Auth with passport.js

Evening all,
Has anyone successfully setup trpc with passport.js for auth?...