reitit icon indicating copy to clipboard operation
reitit copied to clipboard

Consider supporting defmulti as handler

Open robert-stuttaford opened this issue 1 year ago • 0 comments

We use defmulti to dispatch API actions on a map key. To avoid needing to wrap that defmulti in a normal function, so that it can be used as a Reitit ring handler, these two protocols need to be extended. In the sample code below, all I did was repeat the clojure.lang.Fn implementation.

(extend-protocol reitit.core/Expand
  clojure.lang.MultiFn
  (expand [this _] {:handler this}))

(extend-protocol reitit.interceptor/IntoInterceptor
  clojure.lang.MultiFn
  (into-interceptor [this data opts]
    (reitit.interceptor/into-interceptor
     {:name     :reitit.interceptor/handler
      ::handler this
      :enter    (fn [ctx]
                  (assoc ctx :response (this (:request ctx))))}
     data opts)))

robert-stuttaford avatar Sep 18 '23 07:09 robert-stuttaford