diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4b9d65..0f41e525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## UNRELEASED + + +### `reitit-core` + +* `reitit.core/Expand` can be extended, fixes [#201](https://github.com/metosin/reitit/issues/201). + ## 0.2.10 (2018-12-30) ### `reitit-core` diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 3573bd1b..834103bb 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -112,10 +112,10 @@ (f conflicts) {:conflicts conflicts}))) -(defn name-lookup [[_ {:keys [name]}] _] +(defn- name-lookup [[_ {:keys [name]}] _] (if name #{name})) -(defn find-names [routes _] +(defn- find-names [routes _] (into [] (keep #(-> % second :name)) routes)) (defn- compile-route [[p m :as route] {:keys [compile] :as opts}] @@ -164,13 +164,6 @@ ([match query-params] (some-> match :path (cond-> query-params (str "?" (impl/query-string query-params)))))) -(def default-router-options - {:lookup name-lookup - :expand expand - :coerce (fn [route _] route) - :compile (fn [[_ {:keys [handler]}] _] handler) - :conflicts (partial throw-on-conflicts! path-conflicts-str)}) - (defn linear-router "Creates a linear-router from resolved routes and optional expanded options. See [[router]] for available options." @@ -415,6 +408,13 @@ (or (match-by-name mixed-router name path-params) (match-by-name linear-router name path-params))))))) +(defn ^:no-doc default-router-options [] + {:lookup name-lookup + :expand expand + :coerce (fn [route _] route) + :compile (fn [[_ {:keys [handler]}] _] handler) + :conflicts (partial throw-on-conflicts! path-conflicts-str)}) + (defn router "Create a [[Router]] from raw route data and optionally an options map. Selects implementation based on route details. The following options @@ -435,7 +435,7 @@ ([raw-routes] (router raw-routes {})) ([raw-routes opts] - (let [{:keys [router] :as opts} (merge default-router-options opts) + (let [{:keys [router] :as opts} (merge (default-router-options) opts) routes (resolve-routes raw-routes opts) path-conflicting (path-conflicting-routes routes) name-conflicting (name-conflicting-routes routes) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 5bfe9c13..c64525e8 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -314,3 +314,12 @@ (-> ["/api" (list "/ipa")] (r/router)))))) + +(defrecord Named [n] + r/Expand + (r/expand [_ _] {:name n})) + +(deftest default-expand-test + (let [router (r/router ["/endpoint" (->Named :kikka)])] + (is (= [["/endpoint" {:name :kikka}]] + (r/routes router)))))