Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Base Procedures #78

Open
pachoclo opened this issue May 14, 2023 · 0 comments · May be fixed by #80
Open

Support Base Procedures #78

pachoclo opened this issue May 14, 2023 · 0 comments · May be fixed by #80

Comments

@pachoclo
Copy link

pachoclo commented May 14, 2023

The problem

When using the base procedures pattern with merged input, trpc panel throws the following error:

trpc-panel: Failed to parse procedure <procedure-name>, Couldn't parse node.

When we get this error, the procedure does not show on the panel UI at all.

More Context

tRPC allows us to use base procedures to reuse procedure code and prevent repetition. One feature of base procedures--that is not clearly shown in the documentation example--is input schema merging: if a procedure declares its own (zod) schema and "extends" a base procedure that also declares its own input schema, tRPC merges those input schemas into one.

For example:

// base procedure
const adminProcedure = t.procedure
  .input(z.object({ adminId: z.string().cuid() }))
  .use((opts) => {
    if (!opts.input.adminId) {
      throw new TRPCError({
        code: 'UNAUTHORIZED',
        message: "No admin ID provided",
      })
    }
 
    return opts.next()
  })

export const appRouter = t.router({
  banUser: adminProcedure
  .input(z.object({ userId: z.string().cuid() }))
  .mutation(() => {
    ...
  }),
})

The procedure banUser "extends" the base adminProcedure. tRPC will resolve the input required for banUser by merging the two declared inputs, requiring both adminId and userId.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant