mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
Pass opts to expand and expand-middleware
This commit is contained in:
parent
f39ef9ecad
commit
24bb758e99
4 changed files with 21 additions and 20 deletions
|
|
@ -361,7 +361,7 @@ Routers can be configured via options. Options allow things like [`clojure.spec`
|
||||||
| `:path` | Base-path for routes (default `""`)
|
| `:path` | Base-path for routes (default `""`)
|
||||||
| `:routes` | Initial resolved routes (default `[]`)
|
| `:routes` | Initial resolved routes (default `[]`)
|
||||||
| `:meta` | Initial expanded route-meta vector (default `[]`)
|
| `:meta` | Initial expanded route-meta vector (default `[]`)
|
||||||
| `:expand` | Function of `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
|
| `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
|
||||||
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
|
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
|
||||||
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler
|
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,31 +5,32 @@
|
||||||
(:import (reitit.impl Route))))
|
(:import (reitit.impl Route))))
|
||||||
|
|
||||||
(defprotocol Expand
|
(defprotocol Expand
|
||||||
(expand [this]))
|
(expand [this opts]))
|
||||||
|
|
||||||
(extend-protocol Expand
|
(extend-protocol Expand
|
||||||
|
|
||||||
#?(:clj clojure.lang.Keyword
|
#?(:clj clojure.lang.Keyword
|
||||||
:cljs cljs.core.Keyword)
|
:cljs cljs.core.Keyword)
|
||||||
(expand [this] {:name this})
|
(expand [this _] {:name this})
|
||||||
|
|
||||||
#?(:clj clojure.lang.PersistentArrayMap
|
#?(:clj clojure.lang.PersistentArrayMap
|
||||||
:cljs cljs.core.PersistentArrayMap)
|
:cljs cljs.core.PersistentArrayMap)
|
||||||
(expand [this] this)
|
(expand [this _] this)
|
||||||
|
|
||||||
#?(:clj clojure.lang.PersistentHashMap
|
#?(:clj clojure.lang.PersistentHashMap
|
||||||
:cljs cljs.core.PersistentHashMap)
|
:cljs cljs.core.PersistentHashMap)
|
||||||
(expand [this] this)
|
(expand [this _] this)
|
||||||
|
|
||||||
#?(:clj clojure.lang.Fn
|
#?(:clj clojure.lang.Fn
|
||||||
:cljs function)
|
:cljs function)
|
||||||
(expand [this] {:handler this})
|
(expand [this _] {:handler this})
|
||||||
|
|
||||||
nil
|
nil
|
||||||
(expand [_]))
|
(expand [_ _]))
|
||||||
|
|
||||||
(defn walk [data {:keys [path meta routes expand]
|
(defn walk [data {:keys [path meta routes expand]
|
||||||
:or {path "", meta [], routes [], expand expand}}]
|
:or {path "", meta [], routes [], expand expand}
|
||||||
|
:as opts}]
|
||||||
(letfn
|
(letfn
|
||||||
[(walk-many [p m r]
|
[(walk-many [p m r]
|
||||||
(reduce #(into %1 (walk-one p m %2)) [] r))
|
(reduce #(into %1 (walk-one p m %2)) [] r))
|
||||||
|
|
@ -40,7 +41,7 @@
|
||||||
[meta childs] (if (vector? maybe-meta)
|
[meta childs] (if (vector? maybe-meta)
|
||||||
[{} args]
|
[{} args]
|
||||||
[maybe-meta (rest args)])
|
[maybe-meta (rest args)])
|
||||||
macc (into macc (expand meta))]
|
macc (into macc (expand meta opts))]
|
||||||
(if (seq childs)
|
(if (seq childs)
|
||||||
(walk-many (str pacc path) macc childs)
|
(walk-many (str pacc path) macc childs)
|
||||||
[[(str pacc path) macc]]))))]
|
[[(str pacc path) macc]]))))]
|
||||||
|
|
@ -151,7 +152,7 @@
|
||||||
| `:path` | Base-path for routes (default `\"\"`)
|
| `:path` | Base-path for routes (default `\"\"`)
|
||||||
| `:routes` | Initial resolved routes (default `[]`)
|
| `:routes` | Initial resolved routes (default `[]`)
|
||||||
| `:meta` | Initial expanded route-meta vector (default `[]`)
|
| `:meta` | Initial expanded route-meta vector (default `[]`)
|
||||||
| `:expand` | Function of `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
|
| `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
|
||||||
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
|
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
|
||||||
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler"
|
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler"
|
||||||
([data]
|
([data]
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,22 @@
|
||||||
[reitit.core :as reitit]))
|
[reitit.core :as reitit]))
|
||||||
|
|
||||||
(defprotocol ExpandMiddleware
|
(defprotocol ExpandMiddleware
|
||||||
(expand-middleware [this]))
|
(expand-middleware [this opts]))
|
||||||
|
|
||||||
(extend-protocol ExpandMiddleware
|
(extend-protocol ExpandMiddleware
|
||||||
|
|
||||||
#?(:clj clojure.lang.APersistentVector
|
#?(:clj clojure.lang.APersistentVector
|
||||||
:cljs cljs.core.PersistentVector)
|
:cljs cljs.core.PersistentVector)
|
||||||
(expand-middleware [[f & args]]
|
(expand-middleware [[f & args] _]
|
||||||
(fn [handler]
|
(fn [handler]
|
||||||
(apply f handler args)))
|
(apply f handler args)))
|
||||||
|
|
||||||
#?(:clj clojure.lang.Fn
|
#?(:clj clojure.lang.Fn
|
||||||
:cljs function)
|
:cljs function)
|
||||||
(expand-middleware [this] this)
|
(expand-middleware [this _] this)
|
||||||
|
|
||||||
nil
|
nil
|
||||||
(expand-middleware [_]))
|
(expand-middleware [_ _]))
|
||||||
|
|
||||||
(defn- ensure-handler! [path meta scope]
|
(defn- ensure-handler! [path meta scope]
|
||||||
(when-not (:handler meta)
|
(when-not (:handler meta)
|
||||||
|
|
@ -28,18 +28,18 @@
|
||||||
(merge {:path path, :meta meta}
|
(merge {:path path, :meta meta}
|
||||||
(if scope {:scope scope}))))))
|
(if scope {:scope scope}))))))
|
||||||
|
|
||||||
(defn compose-middleware [middleware]
|
(defn compose-middleware [middleware opts]
|
||||||
(->> middleware
|
(->> middleware
|
||||||
(keep identity)
|
(keep identity)
|
||||||
(map expand-middleware)
|
(map #(expand-middleware % opts))
|
||||||
(apply comp identity)))
|
(apply comp identity)))
|
||||||
|
|
||||||
(defn compile-handler
|
(defn compile-handler
|
||||||
([route opts]
|
([route opts]
|
||||||
(compile-handler route opts nil))
|
(compile-handler route opts nil))
|
||||||
([[path {:keys [middleware handler] :as meta}] _ scope]
|
([[path {:keys [middleware handler] :as meta}] opts scope]
|
||||||
(ensure-handler! path meta scope)
|
(ensure-handler! path meta scope)
|
||||||
((compose-middleware middleware) handler)))
|
((compose-middleware middleware opts) handler)))
|
||||||
|
|
||||||
(defn router
|
(defn router
|
||||||
([data]
|
([data]
|
||||||
|
|
|
||||||
|
|
@ -30,11 +30,11 @@
|
||||||
(defn get-match [request]
|
(defn get-match [request]
|
||||||
(::match request))
|
(::match request))
|
||||||
|
|
||||||
(defn coerce-handler [[path meta] {:keys [expand]}]
|
(defn coerce-handler [[path meta] {:keys [expand] :as opts}]
|
||||||
[path (reduce
|
[path (reduce
|
||||||
(fn [acc method]
|
(fn [acc method]
|
||||||
(if (contains? acc method)
|
(if (contains? acc method)
|
||||||
(update acc method expand)
|
(update acc method expand opts)
|
||||||
acc)) meta http-methods)])
|
acc)) meta http-methods)])
|
||||||
|
|
||||||
(defn compile-handler [[path meta] opts]
|
(defn compile-handler [[path meta] opts]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue