tRPC

T

tRPC

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

Join

Is it possible to split the router definition with the imlementation?

I want to define the server router(input\output\meta) in a separate package from the server package I want to have a package that will contain the types I use + the router definition, another for the server itself(will use the router definition like an interface), and lastly the client package which ill only need to import the types package to work is this possible?...

Cache not working for `useQuery`

I have a query like this: ```js const { data: article, isFetching } = api.public.getArticle.useQuery({ id: targetId, });...

Zod File Upload Validation with Open-Api Support?

Hi guys, anyone know how to validate file upload with zod and get also open-api support?

Zod validation and open-api support for File on the server?

Hi guys, anyone know how to validate a File upload using zod? and also have open-api support?

is context cached?

If I put an object on the context that represents the User record from my database... ``` export async function createContext(event: H3Event){ if (!dbUser) { const userService = new UserService();...

JSON inferred router output not matching

Hello. I have a procedure query which is returning Json from a postgresql db using Prisma. The type in the query on the server is showing up as JsonValue (definitions below). The inferred type from the router.procedure.query() on a createTRPCProxyClient instance comes back as expected, but the type definition using inferRouterOutputs<AppRouter> types the Json data as string | null. Any ideas? Thanks. New to tRPC so sorry if this is a no-brainer....

Best way to implement input based validation on a router procedure

Hi guys, bit of a noob. I have already created a 'protectedProcedure', ensuring the user is logged in, but for some of my procedures, I also want to ensure the user is an ADMIN for the account specified on the input. This is my first try with validation just added at the top of the procedure implementation... ``` changeUserAccessWithinAccount: protectedProcedure .input(z.object({ user_id: z.number(), account_id: z.number(), access: z.enum([ACCOUNT_ACCESS.ADMIN, ACCOUNT_ACCESS.OWNER, ACCOUNT_ACCESS.READ_ONLY, ACCOUNT_ACCESS.READ_WRITE]) })) .query(async ({ ctx, input }) => {...

[Help] Turbo shared types

I have a turborepo with two apps (web and embed). web is a t3 stack and embed is a create-react-app. I am trying to use the trpc endpoint from web to embed. I managed to make it work by configuring web's cors. However, I am getting type issues when importing AppRouter in the embed app. I don't think you can just import AppRouter from web like import type { AppRouter } from "../../../web/src/server/routers/_app"; which is currently what I am doing. So my question is how would I go about this? do I need to somehow share the types between the two? if so how would I do it?. Should I just seperate trpc as an internal package?...

Cache SSG helper response

I'm using createProxySSGHelpers in GSSP with ssr: false in the global config. I trying to cache a response of a public procedure called there, but I'm running into a dead end. I tried following the "App caching" example in the docs (https://trpc.io/docs/caching#app-caching), but the responseMeta option is unavailable if ssr: false. (At least TS tells me so)...

Is there are any relationship between tRPC and RPC(or gRPC)?

So I'm a newbie, and I wonder if there's any connection between these two concepts (or something familiar). thanks for you guys help.

Inference on routes & procedures

I have some routes that all have the same methods/procedures, but the input and output is specific to them due to prisma. I made it this far in making a generic hook but the return type of the data is always "any". Any help would be appreciated. ```ts // server routes const routeOne = router({...

Input is too big for a single dispatch

I decided to try tRPC for my Crypto analytics dashboard. However, I'm having a hard time passing the time series data between the client & the server. First I received an HTTP error Request header too large I was able to fix this by setting maxURLLength However, now I receive another error "Input is too big for a single dispatch" ...

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