metosin / reitit

A fast data-driven routing library for Clojure/Script

Home Page:https://cljdoc.org/d/metosin/reitit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider supporting defmulti as handler

robert-stuttaford opened this issue · 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)))