tRPC

T

tRPC

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

Join

How to manage custom errors (e.g. custom error codes) in tRPC?

What's the recommended way to add fields to a TRPCError? How do you make that typesafe also on the client (e.g. custom error code is a specific union type). Thanks...

typesafe permissions

Hi, So I wanted to infer all the procedures from my router recursively & assign a permission (string[]) to each one them. I wrote the following, ```ts type GetProceduresRecusivelyAndAssignPermissions<T extends AnyRouter> = { [K in keyof T]: T[K] extends AnyProcedure ? { permissions: string[] } : T[K] extends AnyRouter...

awaiting for procedure & logging the response.

Hi, I was wondering if there is a way to handle the return object via the post-middleware's? I know we could do something like, ```ts const logMiddleware = t.middleware(async ({ ctx, next }) => { const res = await next(); // insert logging here return res;...

createCaller Dependency Injection in Middleware ctx ?

createCaller makes it really easy to inject dependencies via anything that's created during the createContext function:
router.createCaller({ someClient: mockClient});
router.createCaller({ someClient: mockClient});
...

best practices for organizing routes/procedures?

i'm trying to find some practices/styles in which people generally define routes with trpc. currently im just doing a router per data entity, and defining crud operations on there, but there's a lot of caveats that are kind of defined with REST, that i'm unsure of how to handle here. one idea i had was to define my router as such: (assume each subrouter will have routes) ```ts export const selectionRouter = createTRPCRouter({ // CREATE...

Validating input inside middleware declaration

```js const enforceUserIsCreatorOfEvent = t.middleware(({ ctx, next, input }) => { if (!input.eventId || !ctx.session?.user) { throw new TRPCError({ code: "BAD_REQUEST" }); }...

Fetch errors on stale pages

Recently I have been getting a lot of fetch errors on stale pages, in particular ones that have queries running in React Contexts. I am on trpc 9. This happens when I am on a page that has at least 1 tRPC query, then I leave that tab or I minimize the window, then I return to it. I get either a full-on internal server error document from Node or an econn refused error in these cases, NOT a trpc error....

How to use querykeys from react-query

I am trying to implement a search query to an api that i am fetching via a procedure, i also read on getQueryKey but im not sure i understand how i can implement it now

Distribute typesafe tRPC Client in an NPM library

Hi ! super fan of trpc over here. We are building a javascript sdk for our API that is essentially a pacakged version of the trpc client. However when compiling the code, the "materialized" type is a generic client/router and not the inferred typesafe version....

Websocket is not defined error

I'm getting a "WebSocket is not defined error" on my next app connected to an express backend. Any idea what I'm doing wrong? My server/index file is `import * as trpcExpress from "@trpc/server/adapters/express"; import { applyWSSHandler } from "@trpc/server/adapters/ws";...

@trpc/server in a non-server environment Error in Azure CI

Im trying to add vitest unit tests for my trpc procedures. I followed some examples and on the local evn all works. Tests pass but in the Azure CI I get this error. ``` Error: You're trying to use @trpc/server in a non-server environment. This is not supported by default. #19 7.732 ❯ initTRPCInner node_modules/.pnpm/@trpc+server@10.8.1/node_modules/@trpc/server/dist/inde.mjs:808:23 #19 7.732 ❯ TRPCBuilder.create node_modules/.pnpm/@trpc+server@10.8.1/node_modules/@trpc/server/dist/inde.mjs:782:33...

How are people handling authorization?

I noticed that with V10, any mentions of trpc-shield are gone from the documentation. Also, it only has 200 stars on GH, which is not that much when compared to tRPC itself. So I would be interested in how you're are handling authorization when using tRPC?

Vitest context router caller

Hi, Im trying to setup vitest to test trpc. I would like to have a trpc approuter caller to be accessible from vitest context but im struggling with typings of appRouter.createCaller(ctx) return type ``` //vitest.setup.ts ...

basequery state changing cause infinite rerender

I try to use react component extension to debug the rerender in my code and found that this state change is the cause. I am not sure what's the problem here it's keep changing all the time. Is there anyway to stop this? btw, I am still on trpc9

WebSocket connection hangs after significant amount of data passed through the connection.

Hey! I've been really enjoying using tRPC on my latest project, but have gotten stuck getting websockets to work properly these last few days. I'm able to establish a connection and successfully pass data back and forth between the client and server, but after a certain amount of data has been passed over the connection, the server stops responding without any error (the connection remains alive)....

Trpc Vanilla client producing ts errors

Creating a vanilla trpc client to hopefully use in my Plasmo Extension and it gives me this error With i believe this being a key factor: "Type 'AbortSignalEsque' is missing the following properties from type 'AbortSignal': onabort, reason, throwIfAborted, removeEventListener, dispatchEvent"...

Cannot read properties of undefined (reading 'data') of res.error.data, when trpc errors out

Hello everyone, I am using @trpc/react-query alongside trpc for express, and I am experiencing a crash when I have an error ``` debug: Server listening on port: 3000 at development mode error: ERROR OCCURED: '[\n' +...

Is there an example of a real world non trivial app?

Something that includes type inference, nested fields, calculated fields, nested React components that use data coming from the backend as prop? The only example app I've found is this: https://todomvc.trpc.io/ But it doesn't have any of the things I mentioned above 😢 ...

How to organise output types?

I'm having a hard time trying to figure out what the best way to organise output types and I was wondering if you guys/gals have any tips. Things I'm having a hard time with: - Some output objects that may be returned in multiple places (e.g. users.get, users.getAll, posts.get (as Post.author), etc.) all need the same extra fields that can't come directly from Prisma (e.g. "the last post from the user" or "whether one can delete this user"); what's the best way to extract and reuse the logic for these fields? - How to mask output types (e.g. make sure that I don't return User.passwordHash ever)...