reitit/doc/http/transforming_interceptor_chain.md

85 lines
2.9 KiB
Markdown
Raw Normal View History

2018-12-30 14:53:15 +00:00
# Transforming the Interceptor Chain
There is an extra option in http-router (actually, in the underlying interceptor-router): `:reitit.interceptor/transform` to transform the interceptor chain per endpoint. Value should be a function or a vector of functions that get a vector of compiled interceptors and should return a new vector of interceptors.
2019-01-27 20:11:47 +00:00
**Note:** the last interceptor in the chain is usually the handler, compiled into an Interceptor. Applying a transformation `clojure.core/reverse` would put this interceptor into first in the chain, making the rest of the interceptors effectively unreachable. There is a helper `reitit.interceptor/transform-butlast` to transform all but the last interceptor.
2018-12-30 14:53:15 +00:00
## Example Application
```clj
(require '[reitit.http :as http])
(require '[reitit.interceptor.sieppari :as sieppari])
(defn interceptor [message]
{:enter (fn [ctx] (update-in ctx [:request :message] (fnil conj []) message))})
(defn handler [req]
{:status 200
:body (select-keys req [:message])})
(def app
(http/ring-handler
(http/router
["/api" {:interceptors [(interceptor 1) (interceptor 2)]}
["/ping" {:get {:interceptors [(interceptor 3)]
:handler handler}}]])
{:executor sieppari/executor}))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body {:message [1 2 3]}}
```
### Reversing the Interceptor Chain
```clj
(def app
(http/ring-handler
(http/router
["/api" {:interceptors [(interceptor 1) (interceptor 2)]}
["/ping" {:get {:interceptors [(interceptor 3)]
:handler handler}}]]
{::interceptor/transform (interceptor/transform-butlast reverse)})
{:executor sieppari/executor}))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body {:message [3 2 1]}}
```
### Interleaving Interceptors
```clj
(def app
(http/ring-handler
(http/router
["/api" {:interceptors [(interceptor 1) (interceptor 2)]}
["/ping" {:get {:interceptors [(interceptor 3)]
:handler handler}}]]
{::interceptor/transform #(interleave % (repeat (interceptor :debug)))})
{:executor sieppari/executor}))
(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body {:message [1 :debug 2 :debug 3 :debug]}}
```
### Printing Context Diffs
```clj
2025-01-31 12:06:05 +00:00
[metosin/reitit-interceptors "0.8.0-alpha1"]
2018-12-30 14:53:15 +00:00
```
Using `reitit.http.interceptors.dev/print-context-diffs` transformation, the context diffs between each interceptor are printed out to the console. To use it, add the following router option:
```clj
:reitit.interceptor/transform reitit.http.interceptor.dev/print-context-diffs
```
Sample output:
![Http Context Diff](../images/http-context-diff.png)
2018-12-30 15:42:36 +00:00
Sample applications (uncomment the option to see the diffs):
* Sieppari: https://github.com/metosin/reitit/blob/master/examples/http-swagger/src/example/server.clj
* Pedestal: https://github.com/metosin/reitit/blob/master/examples/pedestal-swagger/src/example/server.clj