mirror of
https://github.com/metosin/reitit.git
synced 2025-12-21 18:11:12 +00:00
Merge pull request #13 from metosin/RouteCoercion
Route coercion, fixes #4
This commit is contained in:
commit
defa0eac73
2 changed files with 25 additions and 2 deletions
|
|
@ -55,8 +55,11 @@
|
||||||
(meta-merge acc {k v}))
|
(meta-merge acc {k v}))
|
||||||
{} x))
|
{} x))
|
||||||
|
|
||||||
(defn resolve-routes [data opts]
|
(defn resolve-routes [data {:keys [coerce] :or {coerce identity} :as opts}]
|
||||||
(->> (walk data opts) (map-meta merge-meta)))
|
(->> (walk data opts)
|
||||||
|
(map-meta merge-meta)
|
||||||
|
(mapv (partial coerce))
|
||||||
|
(filterv identity)))
|
||||||
|
|
||||||
(defprotocol Routing
|
(defprotocol Routing
|
||||||
(routes [this])
|
(routes [this])
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,26 @@
|
||||||
(reitit/resolve-routes
|
(reitit/resolve-routes
|
||||||
["/api/:version/ping"] {})))))))
|
["/api/:version/ping"] {})))))))
|
||||||
|
|
||||||
|
(testing "route coercion"
|
||||||
|
(let [coerce (fn [[path meta]]
|
||||||
|
(if-not (:invalid? meta)
|
||||||
|
[path (assoc meta :path path)]))
|
||||||
|
router (reitit/router
|
||||||
|
["/api" {:roles #{:admin}}
|
||||||
|
["/ping" ::ping]
|
||||||
|
["/pong" ::pong]
|
||||||
|
["/hidden" {:invalid? true}
|
||||||
|
["/utter"]
|
||||||
|
["/crap"]]]
|
||||||
|
{:coerce coerce})]
|
||||||
|
(is (= [["/api/ping" {:name ::ping
|
||||||
|
:path "/api/ping",
|
||||||
|
:roles #{:admin}}]
|
||||||
|
["/api/pong" {:name ::pong
|
||||||
|
:path "/api/pong",
|
||||||
|
:roles #{:admin}}]]
|
||||||
|
(reitit/routes router)))))
|
||||||
|
|
||||||
(testing "bide sample"
|
(testing "bide sample"
|
||||||
(let [routes [["/auth/login" :auth/login]
|
(let [routes [["/auth/login" :auth/login]
|
||||||
["/auth/recovery/token/:token" :auth/recovery]
|
["/auth/recovery/token/:token" :auth/recovery]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue