T
TanStack6d ago
deep-jade

dynamic middleware generation???

I did reach out to Tanner on twitter regarding this as well here but does anyone know if we can generate middleware dynamicaly on the fly? or must it be generated statically so bundled at build time. this as an example would be a dynamic middleware generator that checks user has the correct permissions in screenshot. if can't be done I'll just do as a utility function at top of handler but this would be nice if can do.
Alistair (@ahut10)
@tannerlinsley feel free to redirect, but curious in #tanstackstart if we can generate middleware dynamically, like in this rough example for a permission check? or if we need to statically define each middleware once so available at build time. btw, loving devX experience!!
From Alistair (@ahut10)
X
No description
10 Replies
firm-tan
firm-tan5d ago
I'm not sure if this is 100% what you mean but sounds similar to what I have and it works.
No description
No description
rival-black
rival-black5d ago
yep this works we should document this! @brinsleylogic want to contribute to the docs?
deep-jade
deep-jadeOP5d ago
awesome this is fantasic news!!! what a great way to do things. please keep me in the loop when its documented as I would love to share this around as a great pattern for enforcing roles via middleware that looks exactly what I started doing, but pulled it out as didn't have time to test it well and hadn't seen documented as a valid approach.
firm-tan
firm-tan4d ago
What's the idea around this? To add an example use case for authorization to the bottom of the middleware page?
rival-black
rival-black4d ago
could be a nice example yes.
deep-jade
deep-jadeOP4d ago
may be worth clarifying if you can dynamically generate server functions this way as well, however I suspect that isn't valid, I assume those need to be defined statically. but I could be wrong seeing it works for middleware
rival-black
rival-black4d ago
no this does not work for server functions
deep-jade
deep-jadeOP4d ago
sweet thats good to have that confirmed, figured that would be the case. awesome ty
rival-black
rival-black4d ago
IF that was possible, what would you want to use this for with server functions?
deep-jade
deep-jadeOP4d ago
make a generic CRUD handler, and then defind them something like this. const bannerCreate = generateCRUDCreate(bannerValidator, 'banner'); const widgetCreate = generateCrudCreate(widgetValidator, 'widget'); mainly as in a CRUD app 80% of it is all the same. but that being said isn't that big of a deal, the middleware one is def the best to make generic role/permission checking over different endpoints.

Did you find this page helpful?