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

Is it possible to generate tRPC routes under one schema using different procedures? For example I have a users table and I want the fetch routes to be public but the create and update routes are protected. How could I achieve that? Thank you. #1057

Closed
damonleelcx opened this issue Feb 26, 2024 · 4 comments

Comments

@damonleelcx
Copy link

No description provided.

@ymc9
Copy link
Member

ymc9 commented Feb 26, 2024

Hi @damonleelcx , trpc routers are objects so I believe you can delete some member procedures of it. I haven't tried it myself yet though.

@damonleelcx
Copy link
Author

Hi @damonleelcx , trpc routers are objects so I believe you can delete some member procedures of it. I haven't tried it myself yet though.

Thanks for replaying.
But the createRouter method takes only one procedure (in the example below a publicProcedure is passed in) which results in all generated routes can only use that publicProcedure. However, for some of the router methods, for example the create method under the User router, I also want to use a protectedProcedure. And for other router methods, for example the findMany method, I am okay with the publicProcedure. But there's only one publicProcedure passed in, how do I make the code generator know which router methods to use the protectedProcedure and some others to use the publicProcedure?

image

image

@ymc9
Copy link
Member

ymc9 commented Mar 1, 2024

Hi @damonleelcx , trpc routers are objects so I believe you can delete some member procedures of it. I haven't tried it myself yet though.

Thanks for replaying. But the createRouter method takes only one procedure (in the example below a publicProcedure is passed in) which results in all generated routes can only use that publicProcedure. However, for some of the router methods, for example the create method under the User router, I also want to use a protectedProcedure. And for other router methods, for example the findMany method, I am okay with the publicProcedure. But there's only one publicProcedure passed in, how do I make the code generator know which router methods to use the protectedProcedure and some others to use the publicProcedure?

image

image

What I meant was to call createRouter twice and manipulate the router object. Something like the following (just wild thoughts, I haven't tried):

// without mutation
const router1 = createRouter(createTRPCRouter, publicProcedure);
delete router1['user']['create'];
...

// all methods
const router2 = createRouter(createTRPCRouter, protectedProcedure);

Also maybe a cleaner solution is putting the checking logic inside the procedure middleware so that you can use one procedure for all the routes.

@damonleelcx
Copy link
Author

Hi @damonleelcx , trpc routers are objects so I believe you can delete some member procedures of it. I haven't tried it myself yet though.

Thanks for replaying. But the createRouter method takes only one procedure (in the example below a publicProcedure is passed in) which results in all generated routes can only use that publicProcedure. However, for some of the router methods, for example the create method under the User router, I also want to use a protectedProcedure. And for other router methods, for example the findMany method, I am okay with the publicProcedure. But there's only one publicProcedure passed in, how do I make the code generator know which router methods to use the protectedProcedure and some others to use the publicProcedure?

image

image

What I meant was to call createRouter twice and manipulate the router object. Something like the following (just wild thoughts, I haven't tried):

// without mutation
const router1 = createRouter(createTRPCRouter, publicProcedure);
delete router1['user']['create'];
...

// all methods
const router2 = createRouter(createTRPCRouter, protectedProcedure);

Also maybe a cleaner solution is putting the checking logic inside the procedure middleware so that you can use one procedure for all the routes.

This will work. Thank you.

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

No branches or pull requests

2 participants