mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 16:31:11 +00:00
Use query-string fn in core
This commit is contained in:
parent
434bc25cd6
commit
245902bd4d
3 changed files with 28 additions and 30 deletions
|
|
@ -16,19 +16,6 @@
|
||||||
(map (juxt keyword #(.get q %)))
|
(map (juxt keyword #(.get q %)))
|
||||||
(into {}))))
|
(into {}))))
|
||||||
|
|
||||||
(defn query-string
|
|
||||||
"Given map, creates "
|
|
||||||
[m]
|
|
||||||
(str/join "&" (map (fn [[k v]]
|
|
||||||
(str (js/encodeURIComponent (name k))
|
|
||||||
"="
|
|
||||||
;; FIXME: create protocol to handle how types are converted to string
|
|
||||||
;; FIXME: array to multiple params
|
|
||||||
(if (coll? v)
|
|
||||||
(str/join "," (map #(js/encodeURIComponent %) v))
|
|
||||||
(js/encodeURIComponent v))))
|
|
||||||
m)))
|
|
||||||
|
|
||||||
(defn match-by-path
|
(defn match-by-path
|
||||||
"Given routing tree and current path, return match with possibly
|
"Given routing tree and current path, return match with possibly
|
||||||
coerced parameters. Return nil if no match found."
|
coerced parameters. Return nil if no match found."
|
||||||
|
|
@ -47,12 +34,7 @@
|
||||||
(assoc match :parameters parameters)))))
|
(assoc match :parameters parameters)))))
|
||||||
|
|
||||||
(defn match-by-name
|
(defn match-by-name
|
||||||
[router name params]
|
([router name]
|
||||||
;; FIXME: move router not initialized to re-frame integration?
|
(match-by-name router name {}))
|
||||||
(if router
|
([router name path-params]
|
||||||
(or (reitit/match-by-name router name params)
|
(reitit/match-by-name router name path-params)))
|
||||||
;; FIXME: return nil?
|
|
||||||
(do
|
|
||||||
(js/console.error "Can't create URL for route " (pr-str name) (pr-str params))
|
|
||||||
nil))
|
|
||||||
::not-initialized))
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[goog.events :as e]
|
[goog.events :as e]
|
||||||
[goog.dom :as dom]
|
[goog.dom :as dom]
|
||||||
|
[reitit.core :as r]
|
||||||
[reitit.frontend :as rf])
|
[reitit.frontend :as rf])
|
||||||
(:import goog.history.Html5History
|
(:import goog.history.Html5History
|
||||||
goog.Uri))
|
goog.Uri))
|
||||||
|
|
@ -100,11 +101,8 @@
|
||||||
(close-fn)))
|
(close-fn)))
|
||||||
|
|
||||||
(defn- match->token [history match k params query]
|
(defn- match->token [history match k params query]
|
||||||
;; FIXME: query string
|
(some->> (r/match->path match query)
|
||||||
(if-let [path (:path match)]
|
(path->token history)))
|
||||||
(str (path->token history path)
|
|
||||||
(if query
|
|
||||||
(str "?" (rf/query-string query))))))
|
|
||||||
|
|
||||||
(defn href
|
(defn href
|
||||||
([state k]
|
([state k]
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@
|
||||||
[schema.core :as s]
|
[schema.core :as s]
|
||||||
[reitit.coercion.schema :as schema-coercion]))
|
[reitit.coercion.schema :as schema-coercion]))
|
||||||
|
|
||||||
|
(defn m [x]
|
||||||
|
(assoc x :data nil :result nil))
|
||||||
|
|
||||||
(deftest match-by-path-test
|
(deftest match-by-path-test
|
||||||
(testing "simple"
|
(testing "simple"
|
||||||
(let [router (r/router ["/"
|
(let [router (r/router ["/"
|
||||||
|
|
@ -20,6 +23,10 @@
|
||||||
:parameters {:query {}
|
:parameters {:query {}
|
||||||
:path {}}})
|
:path {}}})
|
||||||
(rf/match-by-path router "/")))
|
(rf/match-by-path router "/")))
|
||||||
|
|
||||||
|
(is (= "/"
|
||||||
|
(r/match->path (rf/match-by-name router ::frontpage))))
|
||||||
|
|
||||||
(is (= (r/map->Match
|
(is (= (r/map->Match
|
||||||
{:template "/foo"
|
{:template "/foo"
|
||||||
:data {:name ::foo}
|
:data {:name ::foo}
|
||||||
|
|
@ -27,7 +34,11 @@
|
||||||
:path "/foo"
|
:path "/foo"
|
||||||
:parameters {:query {}
|
:parameters {:query {}
|
||||||
:path {}}})
|
:path {}}})
|
||||||
(rf/match-by-path router "/foo")))))
|
(rf/match-by-path router "/foo")))
|
||||||
|
|
||||||
|
|
||||||
|
(is (= "/foo"
|
||||||
|
(r/match->path (rf/match-by-name router ::foo))))))
|
||||||
|
|
||||||
(testing "schema coercion"
|
(testing "schema coercion"
|
||||||
(let [router (r/router ["/"
|
(let [router (r/router ["/"
|
||||||
|
|
@ -42,7 +53,11 @@
|
||||||
:path "/5"
|
:path "/5"
|
||||||
:parameters {:query {}
|
:parameters {:query {}
|
||||||
:path {:id 5}}})
|
:path {:id 5}}})
|
||||||
(assoc (rf/match-by-path router "/5") :data nil :result nil)))
|
(m (rf/match-by-path router "/5"))))
|
||||||
|
|
||||||
|
(is (= "/5"
|
||||||
|
(r/match->path (rf/match-by-name router ::foo {:id 5}))))
|
||||||
|
|
||||||
(is (= (r/map->Match
|
(is (= (r/map->Match
|
||||||
{:template "/:id"
|
{:template "/:id"
|
||||||
:path-params {:id "5"}
|
:path-params {:id "5"}
|
||||||
|
|
@ -50,4 +65,7 @@
|
||||||
:path "/5"
|
:path "/5"
|
||||||
:parameters {:path {:id 5}
|
:parameters {:path {:id 5}
|
||||||
:query {:mode :foo}}})
|
:query {:mode :foo}}})
|
||||||
(assoc (rf/match-by-path router "/5?mode=foo") :data nil :result nil))))))
|
(m (rf/match-by-path router "/5?mode=foo"))))
|
||||||
|
|
||||||
|
(is (= "/5?mode=foo"
|
||||||
|
(r/match->path (rf/match-by-name router ::foo {:id 5}) {:mode :foo}))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue