tRPC

T

tRPC

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

Join

Complex type inference on router outputs?

What are the best practices on complex outputs from routers and typing on the FE? We are doing quite a bit of munging of data from multiple sources, so there's no real concept of an upstream "primitive" as there would be in a simple trpc + nextjs + prisma setup. How is everyone doing typing in a scenario like this? It feels brittle to bind a UI element to the response from a router as that will intrinsically bind those two elements together, making the component non-reusable. Is there a typing layer for trpc that sorta behaves like GQL, where you get a meta layer of types from the router responses?...

Where to put clean up code?

I have a DB connection setup in my createContext that I must explicitly close per-request. But I cannot find any good spots in the TRPC lifecycle to make that call. I tried just running it in a middleware, but seemingly that is invoked on each query in a batch independently, whereas I need it to be called once all queries have finished. FWIW: I am using create-t3-app with @vercel/postgres and createClient. ...

Trigger lambda configured with trpc using other events

Can I trigger a lambda configured with trpc api gateway adapter using events other than httpApi like from an sqs or a dynamodb stream event, how would I handle these requests with the router?

useInfiniteQuery does not exist on appRouter t3-Stack Prisma

I'm using appRouter and trying to do pagination with useInfiniteQuery() but i've got the next typescript error: Property 'useInfiniteQuery' does not exist on type '{ query: Resolver<BuildProcedure<"query", { _config: RootConfig<{ ctx: { headers: Headers; db: PrismaClient<{ log: ("query" | "warn" | "error")[]; }, never, DefaultArgs>; session: Session | null; }; meta: object; errorShape: { ...; }; transformer: typeof SuperJSON; }>; ... 5 more ...; _output_out: unique symbol; } and I can't find anything about it. ...

Datadog traces

Hey, has anyone had any luck setting up tracing with tRPC? Specifically with dd-trace-js. Weโ€™re using Datadog and because tRPC handles all errors internally, the tracer doesnโ€™t actually get access to the stack trace. Likewise, it stops creating spans before hitting the tRPC route and only resumes if any library underneath is instrumented....

Asynchronous subscribe and unsubscribe methods in observable

Hello, I have been looking at the documentation about subscriptions but I am not sure about one thing. This is the article I am looking at: https://trpc.io/docs/subscriptions The code example mentions that the event emitter could be replaced by Redis. However, I am not sure how this would work. When doing PubSub with Redis your subscribe/unsubscribe methods are typically asynchronous. The "observable" from tRPC only takes a synchronous subscribe function and must return a synchronous unsubscribe function....

trpc v11 error: The transformer property has moved to httpLink/httpBatchLink/wsLink

Just upgraded tRPC to v11 and I directly got this error: Type typeof SuperJSON is not assignable to type TypeError<"The transformer property has moved to httpLink/httpBatchLink/wsLink"> Type typeof SuperJSON is not assignable to type "The transformer property has moved to httpLink/httpBatchLink/wsLink" ...

Typescript optimisation

I'm trying to optimise the generated types from the router, as I see that lots of types for context are being typed over and over instead of being made once and re-used, which influences total type to infer alot. I managed to cut my total router dts from 1.5mb -> 840kb already (1.5mb being the tsc limit) But I think we can do better ...
No description

Is it possible to change how TRPC maps routers to URL paths?

Hello there, in my current project I would like to map merged routers to subpaths in the url. For example, there is one router to handle auth-related api calls and another one for project settings. At the moment, when I merge both routers into a "root" router, the procedures are available under e.g. /auth.signup or /project.create. This is perfectly fine until I merge two routers that each merge more routers. Then it becomes e.g. /auth.signup.verify. As I am building an API that should be usable by external users (not only my own frontend), I would prefer it to be /auth/signup.verify to prevent confusion. Is there an option to achieve this result?...
Solution:
I noticed that this would make batching unusable and therefore expect it not to be possible.

Create user on the function mutate, with TRPC and Payload

Hi everyone, I have faced issues with create user on the function mutate, with TRPC, I cant trace and debug it. TRPC is connected based on the log here as it return some testing value. Please help me. I been stucking for almost a week page.tsx "use client"; ...
No description

How to not prefetch on getServerSideProps if it's already fetched?

I have a getServerSideProps that utilizes createServerSideHelpers, and when I access a page I prefetch ~10 queries. But this is extremely slow, is there a way to only prefetch once, and if it's in cache don't prefetch?

Return undefined if param is not there

In this code cityById could be undefined and I want that this checks this and then weatherData gets undefined. Problem: I can't wrap this in a condition because of React's Hook Rules ```ts...

useQuery caching data

Hello, I have a problem. I want to do such a thing as checking for the existence of a nickname in the system. It all seems to work, but after a while the system simply does not send new requests to trpc and uses as if hashed data

Error using createTRPCQueryUtils

Here is my code. ```tsx const trpc = createTRPCReact<TRPCRouter>(); const queryClient = new QueryClient(); const trpcQueryUtils = createTRPCQueryUtils({ queryClient, client: trpc });...

Why do we pass the database client in the context

Hey there ๐Ÿ‘‹ In the examples and in T3app we pass the db client in the trpc context ``` export async function createContextInner(opts?: CreateInnerContextOptions) {...

Subscriptions between different services

How do you make subscriptions to work between different services? For example if we have two different services- teacher and student, both have separate backend and frontend. Can we use redis or something similar so they would communicate correctly? Also it wouldn't work for multi instances for the same instances as well, right? If we save everything in the instance memory, or am I wrong?

Serving files with TRPC and Express

Hi! Is there any way to serve files using express while using trpc typed way? As in using
...

TRPC useQuery() in NextJS full stack with TypeScript

Hi, I'm relatively new to web dev in general and just trying to get to grips with using TRPC so please bear with me! I've set up a NextJS fullstack app in TypeScript, using Kinde and MongoDB. I've managed to get TRPC to add a user to the database from the frontend. So the setup of TRPC throughout the app should in theory be fine. However I cannot seem to get the useQuery() hook to retrieve users. The frontend code is as follows: ` const {data: userData} = trpc.getUsers.useQuery();...

Not getting "User" type on context.

I am creating a few "custom" procedures that uses middleware to authenticate a user, like this:
const ownerProcedure = t.procedure.use(async(opts) => await securityHandler.authenticateOwner(opts));
const ownerProcedure = t.procedure.use(async(opts) => await securityHandler.authenticateOwner(opts));
This is the method used for authenticating (It's part of a class with other methods btw):...
No description

Type error when creating a server-side context ๐Ÿค”

I am trying to set up my context for TRPC server, and I get the attached error. I am using TRPC v10, Node 20.7.0, TS 5.3.3 and Fastify for the API server. It seems the createHTTPHandler import is only suitable for vanilla HTTP module typings?...
No description