reitit/doc/ring/transforming_middleware_chain.md
bplubell e175dc76c9 Fix incorrect ring-router doc references
It looks like documentation references to `ring-router` are left-overs
from early README examples - I couldn't find any code that ever used the
name. The Ring router named `reitit.ring/router`.

While it didn't take too long for me to realize why my `ring-router` was
not working, I had to look the examples to figure out the name of the
function was just `router` - which was confusing since the section
header stated `reitit-router`.
2023-01-31 15:51:31 -08:00

73 lines
2.1 KiB
Markdown

# Transforming the Middleware Chain
There is an extra option in the Ring router (actually, in the underlying middleware-router): `:reitit.middleware/transform` to transform the middleware chain per endpoint. Value should be a function or a vector of functions that get a vector of compiled middleware and should return a new vector of middleware.
## Example Application
```clj
(require '[reitit.ring :as ring])
(require '[reitit.middleware :as middleware])
(defn wrap [handler id]
(fn [request]
(handler (update request ::acc (fnil conj []) id))))
(defn handler [{::keys [acc]}]
{:status 200, :body (conj acc :handler)})
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[wrap 1] [wrap 2]]}
["/ping" {:get {:middleware [[wrap 3]]
:handler handler}}]])))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [1 2 3 :handler]}
```
### Reversing the Middleware Chain
```clj
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[wrap 1] [wrap 2]]}
["/ping" {:get {:middleware [[wrap 3]]
:handler handler}}]]
{::middleware/transform reverse})))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [3 2 1 :handler]}
```
## Interleaving Middleware
```clj
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[wrap 1] [wrap 2]]}
["/ping" {:get {:middleware [[wrap 3]]
:handler handler}}]]
{::middleware/transform #(interleave % (repeat [wrap :debug]))})))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [1 :debug 2 :debug 3 :debug :handler]}
```
### Printing Request Diffs
```clj
[metosin/reitit-middleware "0.5.18"]
```
Using `reitit.ring.middleware.dev/print-request-diffs` transformation, the request diffs between each middleware are printed out to the console. To use it, add the following router option:
```clj
:reitit.middleware/transform reitit.ring.middleware.dev/print-request-diffs
```
Sample output:
![Ring Request Diff](../images/ring-request-diff.png)