Skip to content

Commit

Permalink
Fix transform of route :handler keys
Browse files Browse the repository at this point in the history
A :handler key may be placed under a request method (like :post) in
Reitit's route syntax.
  • Loading branch information
weavejester committed Feb 3, 2025
1 parent 72e3b2c commit 50880be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/duct/module/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,29 @@
(map? data) (list data)
(keyword? data) (list {:name data}))))))

(def ^:private handler-keys
#{:handler :get :head :patch :delete :options :post :put :trace})
(def ^:private request-methods
#{:get :head :patch :delete :options :post :put :trace})

(defn- add-ref-to-key [m k]
(if (qualified-keyword? (m k)) (update m k ig/ref) m))
(def ^:private handler-indexes
(set (concat [[:handler]]
(map vector request-methods)
(map (fn [m] [m :handler]) request-methods))))

(defn- add-ref-to-key [m ks]
(if (qualified-keyword? (get-in m ks))
(update-in m ks ig/ref)
m))

(defn- add-refs-to-route-data [route-data]
(if (some route-data handler-keys)
(reduce add-ref-to-key route-data handler-keys)
(if (some #(get-in route-data %) handler-indexes)
(reduce add-ref-to-key route-data handler-indexes)
(assoc route-data :handler (ig/ref (:name route-data)))))

(defn- find-handlers-in-route-data [route-data]
(->> handler-keys (keep route-data) (filter ig/ref?) (map :key)))
(->> handler-indexes
(keep #(get-in route-data %))
(filter ig/ref?)
(map :key)))

(defmethod ig/expand-key :duct.module/web
[_ {:keys [features routes handler-opts]
Expand Down
7 changes: 5 additions & 2 deletions test/duct/module/web_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@
{:routes
[["/one" {:get (ig/ref ::handler1)}]
["/two" {:name ::handler2, :handler (ig/ref ::handler2)}]
["/three/" ["four" {:handler (ig/ref ::handler3)}]]]
["/three/" ["four" {:handler (ig/ref ::handler3)}]]
["/five" {:post {:handler (ig/ref ::handler4)}}]]
:data {}
:middleware
[(ig/ref :duct.middleware.web/defaults)
Expand All @@ -196,6 +197,7 @@
::handler1 {:name :foo}
::handler2 {:name :foo}
::handler3 {:name :foo}
::handler4 {:name :foo}
:duct.handler.reitit/default
{:not-found
(ig/ref :duct.handler.static/not-found)
Expand Down Expand Up @@ -228,5 +230,6 @@
:routes
[["/one" {:get ::handler1}]
["/two" ::handler2]
["/three/" ["four" {:handler ::handler3}]]]}}
["/three/" ["four" {:handler ::handler3}]]
["/five" {:post {:handler ::handler4}}]]}}
(ig/deprofile [:main])))))

0 comments on commit 50880be

Please sign in to comment.