Collect multi-valued query params into vector.

This commit is contained in:
Pauli Jaakkola 2019-11-15 12:53:32 +02:00
parent da18a4f113
commit 0adb820bba
2 changed files with 19 additions and 2 deletions

View file

@ -3,7 +3,14 @@
[reitit.coercion :as coercion] [reitit.coercion :as coercion]
[reitit.coercion :as rc] [reitit.coercion :as rc]
[reitit.core :as r]) [reitit.core :as r])
(:import goog.Uri)) (:import goog.Uri
goog.Uri.QueryData))
(defn- query-param [^goog.Uri.QueryData q k]
(let [vs (.getValues q k)]
(if (< (alength vs) 2)
(aget vs 0)
(vec vs))))
(defn query-params (defn query-params
"Given goog.Uri, read query parameters into Clojure map." "Given goog.Uri, read query parameters into Clojure map."
@ -11,7 +18,7 @@
(let [q (.getQueryData uri)] (let [q (.getQueryData uri)]
(->> q (->> q
(.getKeys) (.getKeys)
(map (juxt keyword #(.get q %))) (map (juxt keyword #(query-param q %)))
(into {})))) (into {}))))
(defn match-by-path (defn match-by-path

View file

@ -39,6 +39,16 @@
:path {}}}) :path {}}})
(rf/match-by-path router "/foo"))) (rf/match-by-path router "/foo")))
(is (= (r/map->Match
{:template "/foo"
:data {:name ::foo}
:path-params {}
:query-params {:mode ["foo", "bar"]}
:path "/foo"
:parameters {:query {:mode ["foo", "bar"]}
:path {}}})
(rf/match-by-path router "/foo?mode=foo&mode=bar")))
(is (= "/foo" (is (= "/foo"
(r/match->path (rf/match-by-name router ::foo)))) (r/match->path (rf/match-by-name router ::foo))))