diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index 0cf00bbd..5043022a 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -55,11 +55,11 @@ (meta-merge acc {k v})) {} x)) -(defn resolve-routes [data {:keys [coerce] :or {coerce identity} :as opts}] - (->> (walk data opts) - (map-meta merge-meta) - (mapv (partial coerce)) - (filterv identity))) +(defn resolve-routes [data {:keys [coerce] :as opts}] + (cond-> (->> (walk data opts) + (map-meta merge-meta)) + coerce (->> (mapv (partial coerce)) + (filterv identity)))) (defn compile-route [compile [p m :as route]] [p m (if compile (compile route))]) @@ -71,6 +71,10 @@ (defrecord Match [template meta path handler params]) +(def default-router-options + {:coerce identity + :compile (comp :handler second)}) + (defrecord LinearRouter [routes data lookup] Routing (routes [_] @@ -91,8 +95,9 @@ See [[router]] for available options" ([routes] (linear-router routes {})) - ([routes {:keys [compile]}] - (let [compiled (map (partial compile-route compile) routes)] + ([routes opts] + (let [{:keys [compile]} (meta-merge default-router-options opts) + compiled (map (partial compile-route compile) routes)] (->LinearRouter routes (mapv (partial impl/create) compiled) @@ -119,8 +124,9 @@ See [[router]] for available options" ([routes] (lookup-router routes {})) - ([routes {:keys [compile]}] - (let [compiled (map (partial compile-route compile) routes)] + ([routes opts] + (let [{:keys [compile]} (meta-merge default-router-options opts) + compiled (map (partial compile-route compile) routes)] (when-let [route (some impl/contains-wilds? (map first routes))] (throw (ex-info @@ -150,7 +156,7 @@ | `:meta` | Initial expanded route-meta vector (default `[]`) | `:expand` | Function `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`) | `:coerce` | Function `[path meta] => [path meta]` to coerce resolved route, can throw or return `nil` (default `identity`) - | `:compile` | Function `[path meta] => handler` to compile a route handler" + | `:compile` | Function `[path meta] => handler` to compile a route handler (default `(comp :handler second)`)" ([data] (router data {})) ([data opts] diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index deeb9e4d..2da36f71 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -56,36 +56,43 @@ ["/api/:version/ping"] {}))))))) (testing "route coercion & compilation" - (let [compile-times (atom 0) - coerce (fn [[path meta]] - (if-not (:invalid? meta) - [path (assoc meta :path path)])) - compile (fn [[path meta]] - (swap! compile-times inc) - (constantly path)) - router (reitit/router - ["/api" {:roles #{:admin}} - ["/ping" ::ping] - ["/pong" ::pong] - ["/hidden" {:invalid? true} - ["/utter"] - ["/crap"]]] - {:coerce coerce - :compile compile})] - (testing "routes are coerced" - (is (= [["/api/ping" {:name ::ping - :path "/api/ping", - :roles #{:admin}}] - ["/api/pong" {:name ::pong - :path "/api/pong", - :roles #{:admin}}]] - (reitit/routes router)))) - (testing "route match contains compiled handler" - (is (= 2 @compile-times)) - (let [{:keys [handler]} (reitit/match-by-path router "/api/pong")] + (testing "custom compile" + (let [compile-times (atom 0) + coerce (fn [[path meta]] + (if-not (:invalid? meta) + [path (assoc meta :path path)])) + compile (fn [[path meta]] + (swap! compile-times inc) + (constantly path)) + router (reitit/router + ["/api" {:roles #{:admin}} + ["/ping" ::ping] + ["/pong" ::pong] + ["/hidden" {:invalid? true} + ["/utter"] + ["/crap"]]] + {:coerce coerce + :compile compile})] + (testing "routes are coerced" + (is (= [["/api/ping" {:name ::ping + :path "/api/ping", + :roles #{:admin}}] + ["/api/pong" {:name ::pong + :path "/api/pong", + :roles #{:admin}}]] + (reitit/routes router)))) + (testing "route match contains compiled handler" + (is (= 2 @compile-times)) + (let [{:keys [handler]} (reitit/match-by-path router "/api/pong")] + (is handler) + (is (= "/api/pong" (handler))) + (is (= 2 @compile-times)))))) + (testing "default compile" + (let [router (reitit/router ["/ping" (constantly "ok")])] + (println (reitit/match-by-path router "/ping")) + (let [{:keys [handler]} (reitit/match-by-path router "/ping")] (is handler) - (is (= "/api/pong" (handler))) - (is (= 2 @compile-times)))))) + (is (= "ok" (handler))))))) (testing "bide sample" (let [routes [["/auth/login" :auth/login]