mirror of
https://github.com/metosin/reitit.git
synced 2026-02-11 22:05:16 +00:00
Allow customizing route resolution
- Support custom predicate for checking if path is a valid endpoint - Use default predicate to implement the current behaviour, where leaves of the route tree are endpoints
This commit is contained in:
parent
a68ac257e7
commit
841f581a17
2 changed files with 23 additions and 7 deletions
|
|
@ -334,13 +334,15 @@
|
||||||
;; Creating Routers
|
;; Creating Routers
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
||||||
(defn ^:no-doc default-router-options []
|
(defn ^:no-doc default-router-options []
|
||||||
{:lookup (fn lookup [[_ {:keys [name]}] _] (if name #{name}))
|
{:lookup (fn lookup [[_ {:keys [name]}] _] (if name #{name}))
|
||||||
:expand expand
|
:expand expand
|
||||||
:coerce (fn coerce [route _] route)
|
:coerce (fn coerce [route _] route)
|
||||||
:compile (fn compile [[_ {:keys [handler]}] _] handler)
|
:compile (fn compile [[_ {:keys [handler]}] _] handler)
|
||||||
:exception exception/exception
|
:exception exception/exception
|
||||||
:conflicts (fn throw! [conflicts] (exception/fail! :path-conflicts conflicts))})
|
:conflicts (fn throw! [conflicts] (exception/fail! :path-conflicts conflicts))
|
||||||
|
:endpoint impl/leaf-endpoint})
|
||||||
|
|
||||||
(defn router
|
(defn router
|
||||||
"Create a [[Router]] from raw route data and optionally an options map.
|
"Create a [[Router]] from raw route data and optionally an options map.
|
||||||
|
|
@ -360,7 +362,10 @@
|
||||||
| `:validate` | Function of `routes opts => ()` to validate route (data) via side-effects
|
| `:validate` | Function of `routes opts => ()` to validate route (data) via side-effects
|
||||||
| `:conflicts` | Function of `{route #{route}} => ()` to handle conflicting routes
|
| `:conflicts` | Function of `{route #{route}} => ()` to handle conflicting routes
|
||||||
| `:exception` | Function of `Exception => Exception ` to handle creation time exceptions (default `reitit.exception/exception`)
|
| `:exception` | Function of `Exception => Exception ` to handle creation time exceptions (default `reitit.exception/exception`)
|
||||||
| `:router` | Function of `routes opts => router` to override the actual router implementation"
|
| `:router` | Function of `routes opts => router` to override the actual router implementation
|
||||||
|
| `:endpoint` | Function of `prev-path path expanded-data data children => {:endpoint :inherit}`.
|
||||||
|
| | Used to transform data for this endpoint and data inherited to child routes.
|
||||||
|
| | Return falsy or `{:endpoint nil}` to skip path as endpoint and pass data to children."
|
||||||
([raw-routes]
|
([raw-routes]
|
||||||
(router raw-routes {}))
|
(router raw-routes {}))
|
||||||
([raw-routes opts]
|
([raw-routes opts]
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
coll
|
coll
|
||||||
coll))
|
coll))
|
||||||
|
|
||||||
(defn walk [raw-routes {:keys [path data routes expand]
|
(defn walk [raw-routes {:keys [path data routes expand endpoint]
|
||||||
:or {data [], routes []}
|
:or {data [], routes []}
|
||||||
:as opts}]
|
:as opts}]
|
||||||
(letfn
|
(letfn
|
||||||
|
|
@ -53,10 +53,15 @@
|
||||||
(sequential? (first maybe-arg)))
|
(sequential? (first maybe-arg)))
|
||||||
(nil? maybe-arg))
|
(nil? maybe-arg))
|
||||||
[{} args]
|
[{} args]
|
||||||
[maybe-arg (rest args)])
|
[maybe-arg (rest args)])]
|
||||||
macc (into macc (expand data opts))
|
(let [d (endpoint pacc path macc data childs)
|
||||||
child-routes (walk-many (str pacc path) macc (keep identity childs))]
|
data-for-endpoint (when (:endpoint d) (into macc (expand (:endpoint d) opts)))
|
||||||
(if (seq childs) (seq child-routes) [[(str pacc path) macc]])))))]
|
data-for-children (or (:inherit d) data)
|
||||||
|
macc (into macc (expand data-for-children opts))]
|
||||||
|
(-> (when data-for-endpoint [[(str pacc path) data-for-endpoint]])
|
||||||
|
(concat (when (seq childs) (-> (str pacc path)
|
||||||
|
(walk-many macc (keep identity childs))
|
||||||
|
(seq))))))))))]
|
||||||
(walk-one path (mapv identity data) raw-routes)))
|
(walk-one path (mapv identity data) raw-routes)))
|
||||||
|
|
||||||
(defn map-data [f routes]
|
(defn map-data [f routes]
|
||||||
|
|
@ -254,6 +259,12 @@
|
||||||
(query-parameter k v))))
|
(query-parameter k v))))
|
||||||
(str/join "&")))
|
(str/join "&")))
|
||||||
|
|
||||||
|
(defn leaf-endpoint
|
||||||
|
[_ _ _ data childs]
|
||||||
|
(when-not (seq childs)
|
||||||
|
{:endpoint data
|
||||||
|
:inherit {}}))
|
||||||
|
|
||||||
(defmacro goog-extend [type base-type ctor & methods]
|
(defmacro goog-extend [type base-type ctor & methods]
|
||||||
`(do
|
`(do
|
||||||
(def ~type (fn ~@ctor))
|
(def ~type (fn ~@ctor))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue