tRPC

T

tRPC

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

Join

What is the pattern for unsubscribing from a subscription?

Hi there! I'd like to prevent unnecessary connections to a websocket server across multiple renders. What is the correct pattern to ensure this? ```ts api.live.onNewIncident.useSubscription(undefined, { onData: () => {...

How to add a short delay between requests

Environment: nextjs 13, node 18, npm, trpc 10.9.0 I'm wondering if it's possible to add a short delay between requests when either using httpBatchLink or httpLink. Background: I'm in a bit of a volatile situation as a 3rd-party API of my app doesn't support any more than 10 requests per minute. On a particular page there are sent several http requests of which multiple calls the 3rd-party API. I have implemented Upstash/redis caching for the requests, however whenever the cache have expired all requests are sent at the same time, thus no calls hits the cache as no requests has gone through and filled the cache yet. ...

Global metadata / filters: Re-validate all queries

Hi all, Simple question, I'm looking for the most efficient way to add some global state to all queries, in this case it's a global filter, for this use-case, let's call it testModeFilter. In our dashboard this could be a Context / Provider that manages this state, ideally, when testModeFilter, we want to: 1. invalidate all queries / refetch mounted queries...

Attempted import error: 'hashQueryKey' is not exported from '@tanstack/react-query' issue

i'm getting this error while trying trpc with latest next version , i tried downgrading but got another error Error: (0 , react__WEBPACK_IMPORTED_MODULE_3__.createContext) is not a function

Trouble getting a TRPC client in Astro SSR pages, instead I get a noop

I'd like to add TRPC to an Astro site. I have an SSR route here (https://github.com/aisflat439/another-abandoned-idea-app/blob/main/packages/web/src/pages/index.astro) that imports my client. The client is straightforward and can be found here (https://github.com/aisflat439/another-abandoned-idea-app/blob/main/packages/web/src/trpc/client/index.ts). I get types on my trpcAstro client so I'm relatively confident that my server is set up correctly but here it is for reference (https://github.com/aisflat439/another-abandoned-idea-app/blob/main/packages/web/src/trpc/server/index.ts). I've referenced this https://www.thomasledoux.be/blog/using-trpc-astro-islands-react along with a handful of sites I've found to reference on github. But no matter how I configure this I seem to get an empty client (see attached image)...
No description

Why webhooks does not work with TRPC?

I have a what suppose to be a webhook: ``` deposit: publicProcedure.input(z.any()).mutation(async ({ ctx, input }) => { console.log(input);...

tRPC set up error

Hey guys, I've been trying to set up tRPC, but unfortunatyy getting this error and it is really unclear how to solve this. I've added the error and router here below. I'm using npm.
No description

Vercel maxDuration with tRPC

How do you extend extend tRPC timeouts on Vercel? I tried this in vercel.json: ```json { "functions": {...
Solution:
Nvm I got it
src/pages/api/trpc/[trpc].ts
src/pages/api/trpc/[trpc].ts
...

Where is client side error handling documented?

In the docs, I can only find error handling for the backend. But I want to get the TRPCError code in the frontend.

does api.myQuery.invalidate() works if the query is not made at the first place?

from testing, the invalidate runs the query, but it shouldn't because there is no cache? how do i invalidate ONLY when the cache exist ?...

working with custom errors and trpc errorFormatter

Hey guys. I'm trying to refactor my app so that all errors extend a BaseError class to make dealing with error codes and user-friendly messages easier. I'm having trouble converting these to the correct shape in the tRPC errorFormatter. Right now when I throw these custom errors tRPC doesnt recognize them and converts all of them to INTERNAL_SERVER_ERROR's. I see that some error codes get translated in 3 places: the jsonrpc error code, the http status code, and an HTTP status,. There also seems to be some additional structure. My end goal is to be able to convert my errors to the 'normal' shape so that trpc-openapi can correctly return them. Is there a reasonable way to do this in tRPC or should I do something different?...

tRPC and OpenTelemetry

Does anyone have experience with instrumenting tRPC with OTEL for tracing? It would be pretty neat to have an auto-instrumentation package like Prisma did : https://www.npmjs.com/package/@prisma/instrumentation...

Multiple TRPC endpoints on same API, with discrete clients? Any gotchas?

I'm building a Sveltekit app that has several pages that will need to behave like SPA's. Rather than have one TRPC enpoint in the api routes, I'd like to divide each section of the app into it's own trpc server handler, and be able to call a discrete client for that portion. This would allow separation of concerns for each portion of the app. My plan is the following routes ( and maybe more as the app grows) - /routes/api/estimating/trpc/[...procedures] - /routes/api/scheduling/trpc/[...procedures] - /routes/api/reporting/trpc/[...procedures] ...

Proper way to revalidate SSR but from Client Side, ie. after a success Mutation.

This question has been also posted on an specific thread but posting this here will allow people to also find a quicker solution. Hopefully the question is self explanatory. But someone has any question about my approach let me know, I'll be happy to clearify it. https://discord.com/channels/867764511159091230/1072167092545138770/1164149856189808682...

How to implement tRPC auth ?

Do you have any article/github links for how can i create auth in tRPC ?

tRPC middleware infer type from another protectedProcedure

Hello, protectedProcedure check and adds a not nullable user to the ctx. However the studyMiddleware does not know that user is not nullable, is there a way for the middleware to be aware of it? In my current code I had to validate the user is not nullable inside the middleware and had to manually pass the user to the ctx user: opts.ctx.user, to make user not nullable inside the mutation. Here is a sample of my code ...

tRPC requests on client vs server for NextJs

Hello, I'm new to tRPC, and am confused on one thing. I have watched videos of some using a server client to make their tRPC requests on server side with async/await before sending the data with html back to the user, and the more common approach I've seen is requesting with 'use client'. When is it practical to use these approaches?
Also, I don't quite understand how to properly set up a server client in tRPC with next in regards to providing context....

passing user input state variable to all api calls

I have a multi organization app and need a way of passing the current organizationId on all requests. Auth to this organization needs to be verified in a procedure. My presumption is that is best set as a header i.e. x-selected-organization is there an alternative way to achieve this possibly given a custom link?