Iām trying to understand the :compile function that can be included as part of reitit middleware. It seems that the result of the compile function can be different types. I have seen one example where a compile function returns a wrap function, which implies the compile function can be used to set up middleware in a similar fashion to standard ring, but with some additional logic. (i.e. testing some option and returning nil prevents the middleware from mounting). However, I have seen other compile functions that return a map with a data key that I assume is being used to manipulate the router configuration. https://github.com/metosin/reitit/blob/master/modules/reitit-middleware/src/reitit/ring/middleware/parameters.clj a wrap key is still provided, which I assume is because the result of compile function does not provide a wrap.
Is it accurate to say that the use of the value returned from calling a compile function is conditional on the type? It seems that if a map is returned, the keys in that map will be merged into the router and can dynamically alter the behavior. If a function is returned, reitit assumes this is a pure wrap function and uses it directly as part of the middleware pipeline?
It should return anything that implements IntoMiddleware protocol, so many things. See impl here: https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/middleware.cljc
e.g. returning a function f, it gets coerced into Middleware Record {:wrap f}
hope this helps
Yes, it does! Thank you. One last detail: How do compile and wrap interact if they are both present for a specific middleware? I would assume compile takes precedent but what if it returns nil? Does this prevent the middleware from mounting or is the declared wrap used instead? Should they even both be present or should they be mutually exclusive?
As long as there is :compile , it will be used. To avoid infinite recursion, there is max depth of 10 (configurable) rounds of :compile. All keys are optional, ever :wrap. See the swagger middleware - they do not participate into request processing, just emit meta-data to the route creation (describe extra keys that are available for route data). All code for Middleware is in the ns I linked, please read the source for more details š
Thank you for your help!
there was a recent thread about this as well: https://clojurians.slack.com/archives/C7YF1SBT3/p1730828484775579