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.
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.
(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]}}
(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]}}
(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]}}
[metosin/reitit-interceptors "0.7.2"]
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:
:reitit.interceptor/transform reitit.http.interceptor.dev/print-context-diffs
Sample output:
Sample applications (uncomment the option to see the diffs):
Can you improve this documentation? These fine people already did:
Tommi Reiman, Juho Teperi, Miikka Koskinen, Joel Kaasinen & tjalkaneEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close