This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-02
Channels
- # ai (1)
- # aleph (16)
- # announcements (1)
- # architecture (51)
- # babashka (32)
- # beginners (27)
- # calva (3)
- # clerk (1)
- # clojure (49)
- # clojure-art (1)
- # clojure-denver (6)
- # clojure-europe (70)
- # clojure-nl (1)
- # clojure-norway (56)
- # clojure-uk (2)
- # clojuredesign-podcast (4)
- # clojurescript (57)
- # clr (15)
- # community-development (3)
- # conjure (1)
- # core-async (10)
- # data-science (1)
- # datalog (2)
- # datomic (3)
- # emacs (12)
- # events (1)
- # gratitude (4)
- # honeysql (9)
- # hyperfiddle (86)
- # jobs (4)
- # off-topic (10)
- # pedestal (5)
- # portal (11)
- # practicalli (2)
- # reitit (7)
- # releases (3)
- # remote-jobs (1)
- # sql (15)
- # tools-build (8)
- # xtdb (4)
Suppose I have a large stack of middleware for my app, and I have one function that performs a request transformation early in the chain before coercion etc happens. But I only want this applied to a small set of functions. It seems the middleware function should be able to accept a boolean to conditionalize the logic internally, but I can’t seem to figure out how to call it at the router where I want it to apply. It seems like :middleware [wrap-my-request true]
but this doesn’t seem to fit the reitit model, or I’m not applying it correctly. What am I missing?
If that middleware could be the last in the chain, it could be attached to the resource itself.
Since that's not your case, there most reasonable option is to create a compiled middleware and include it as one of the first in the main chain: https://cljdoc.org/d/metosin/reitit/0.7.0-alpha4/doc/ring/compiling-middleware
The boolean parameter that you describe can be used as :middleware [[wrap true]]
, but it will be just a parameter to the middleware function - it won't help you conditionally enable it for specific endpoints.
Another, less direct, approach is to transform the middleware chain with a transformer. But if compiled middleware works for you (I don't see a reason why it wouldn't), you should use that instead.
I’ll try the compiled middleware, I just wasn’t aware I could apply it in the stack of app middleware as well as on a specific endpoint. I guess it’s no difference, it’s data all the way down.
But I still thought the boolean parameter could have conditionalized the behavior in each endpoint, even if it ended up called for each one of them
The boolean parameter is there just for convenience, AFAICT [wrap true]
is not different from #(wrap % true)
. So that boolean arg will be static, you won't be able to make it conditional based on the resource or anything else.
You can make a middleware conditional based on some data in the request, but that would be logic internal to the middleware itself.
OK got it. That makes sense.