W
Wasp•6d ago
Noah

Per-Path Middleware for actions/operations

I'd like to configure middleware for a set of actions + ops re these docs https://wasp.sh/docs/advanced/middleware-config#3-customize-per-path-middleware But it looks like a simple URL prefix check which seems to only apply on apis in wasp, not ops or actions.
Configuring Middleware | Wasp
Wasp comes with a minimal set of useful Express middleware in every application. While this is good for most users, we realize some may wish to add, modify, or remove some of these choices both globally, or on a per-api/path basis.
3 Replies
kapa.ai
kapa.ai•6d ago
Hi @Noah. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You're correct in your observation. The per-path middleware configuration in Wasp, as described in the documentation, is indeed designed for API routes and not for operations (actions and queries). According to the Configuring Middleware documentation, the apiNamespace declaration is used to apply middleware to all API routes under a specific path. This is different from operations (actions and queries) in Wasp. The documentation states:
If you would like to modify the middleware for all API routes under some common path, you can define a `middlewareConfigFn` on an `apiNamespace`:
If you would like to modify the middleware for all API routes under some common path, you can define a `middlewareConfigFn` on an `apiNamespace`:
It's important to note that this method is specifically for API routes, not for operations. The documentation further clarifies:
noteThis gets installed at the router level for the path. Behind the scenes, this results in something like:

router.use('/foo/bar', fooBarNamespaceMiddleware)
noteThis gets installed at the router level for the path. Behind the scenes, this results in something like:

router.use('/foo/bar', fooBarNamespaceMiddleware)
For operations (actions and queries), Wasp uses a different approach. Currently, the knowledge sources don't provide information on how to configure middleware specifically for a set of operations. If you need to apply middleware to operations, you might want to consider using the global middleware configuration, which applies to all operations and APIs. However, this might not be ideal if you only want to target specific operations. For more specific needs or to configure middleware for particular operations, you may need to reach out to the Wasp community or check for any recent updates to the framework that might not be reflected in these knowledge sources. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Noah, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Noah
NoahOP•6d ago
hmmmm seems like a gap. Ideally would have like namspaces with lists of actions/ops
miho
miho•4d ago
Yep, we agree with you 😄 we are tracking it here: https://github.com/wasp-lang/wasp/issues/2396 Have you tried setting API middleware for a particular operation route? It could be a potential hack for now
GitHub
Allow specifying middleware for individual and groups of Operations...
If you want to execute some logic that is common for a group of Operations (and maybe also APIs), there is no simple way to do it -> we do have customMiddleware for APIs and api namespaces, but ...

Did you find this page helpful?