mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Default compile get the :handler from meta
This commit is contained in:
parent
5f1d6a61a6
commit
0a58510dc2
2 changed files with 52 additions and 39 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Reference in a new issue