From 5c835c5ad276e206712f7cf2d4bf545d36478ee5 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Wed, 22 Aug 2018 18:56:31 +0300 Subject: [PATCH] reitit.frontend/router --- .../src/frontend/core.cljs | 6 ++---- examples/frontend/src/frontend/core.cljs | 6 ++---- .../reitit-frontend/src/reitit/frontend.cljs | 21 +++++++++++++------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/examples/frontend-controllers/src/frontend/core.cljs b/examples/frontend-controllers/src/frontend/core.cljs index 6ff8e78d..f279c249 100644 --- a/examples/frontend-controllers/src/frontend/core.cljs +++ b/examples/frontend-controllers/src/frontend/core.cljs @@ -1,6 +1,5 @@ (ns frontend.core (:require [reagent.core :as r] - [reitit.core :as re] [reitit.frontend :as rf] [reitit.frontend.easy :as rfe] [reitit.frontend.controllers :as rfc] @@ -45,7 +44,7 @@ (apply js/console.log params))) (def routes - (re/router + (rf/router ["/" ["" {:name ::frontpage @@ -72,8 +71,7 @@ (js/console.log "start" "item controller" (:id params))) :stop (fn [params] (js/console.log "stop" "item controller" (:id params)))}]}]]] - {:compile rc/compile-request-coercers - :data {:controllers [{:start (log-fn "start" "root-controller") + {:data {:controllers [{:start (log-fn "start" "root-controller") :stop (log-fn "stop" "root controller")}] :coercion rsc/coercion}})) diff --git a/examples/frontend/src/frontend/core.cljs b/examples/frontend/src/frontend/core.cljs index b536e575..fff1feef 100644 --- a/examples/frontend/src/frontend/core.cljs +++ b/examples/frontend/src/frontend/core.cljs @@ -1,6 +1,5 @@ (ns frontend.core (:require [reagent.core :as r] - [reitit.core :as re] [reitit.frontend :as rf] [reitit.frontend.easy :as rfe] [reitit.coercion :as rc] @@ -53,7 +52,7 @@ [:pre (with-out-str (fedn/pprint @match))]]) (def routes - (re/router + (rf/router ["/" ["" {:name ::frontpage @@ -66,8 +65,7 @@ :view item-page :parameters {:path {:id s/Int} :query {(s/optional-key :foo) s/Keyword}}}]] - {:compile rc/compile-request-coercers - :data {:coercion rsc/coercion}})) + {:data {:coercion rsc/coercion}})) (defn init! [] (rfe/start! routes diff --git a/modules/reitit-frontend/src/reitit/frontend.cljs b/modules/reitit-frontend/src/reitit/frontend.cljs index dd29697a..83dd3d01 100644 --- a/modules/reitit-frontend/src/reitit/frontend.cljs +++ b/modules/reitit-frontend/src/reitit/frontend.cljs @@ -1,8 +1,9 @@ (ns reitit.frontend "" - (:require [reitit.core :as reitit] - [clojure.set :as set] - [reitit.coercion :as coercion]) + (:require [clojure.set :as set] + [reitit.coercion :as coercion] + [reitit.coercion :as rc] + [reitit.core :as r]) (:import goog.Uri)) (defn query-params @@ -19,7 +20,7 @@ coerced parameters. Return nil if no match found." [router path] (let [uri (.parse Uri path)] - (if-let [match (reitit/match-by-path router (.getPath uri))] + (if-let [match (r/match-by-path router (.getPath uri))] (let [q (query-params uri) ;; Return uncoerced values if coercion is not enabled - so ;; that tha parameters are always accessible from same property. @@ -32,7 +33,15 @@ ([router name] (match-by-name router name {})) ([router name path-params] - (reitit/match-by-name router name path-params))) + (r/match-by-name router name path-params))) + +(defn router + "Create a `reitit.core.router` from raw route data and optionally an options map. + Enables request coercion. See [[reitit.core.router]] for details on options." + ([raw-routes] + (router raw-routes {})) + ([raw-routes opts] + (r/router raw-routes (merge {:compile rc/compile-request-coercers} opts)))) (defn match-by-name! "Logs problems using console.warn" @@ -40,7 +49,7 @@ (match-by-name! router name {})) ([router name path-params] (if-let [match (match-by-name router name path-params)] - (if (reitit/partial-match? match) + (if (r/partial-match? match) (if (every? #(contains? path-params %) (:required match)) match (let [defined (-> path-params keys set)