metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.4k stars 252 forks source link

Consider supporting defmulti as handler #650

Open robert-stuttaford opened 10 months ago

robert-stuttaford commented 10 months ago

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)))