Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parsing of query params to match-by-path in reitit.core #665

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/reitit-core/src/reitit/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
(if-let [match (match-by-path path)]
(-> (:data match)
(assoc :path-params (:params match))
(assoc :query-params (impl/query-params path))
(assoc :path path))))
(match-by-name [_ name]
(if-let [match (impl/fast-get lookup name)]
Expand Down Expand Up @@ -198,6 +199,7 @@
(if-let [match (and match-by-path (match-by-path path))]
(-> (:data match)
(assoc :path-params (:params match))
(assoc :query-params (impl/query-params path))
(assoc :path path))))
(match-by-name [_ name]
(if-let [match (impl/fast-get lookup name)]
Expand Down
30 changes: 30 additions & 0 deletions modules/reitit-core/src/reitit/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,36 @@
[params]
(maybe-map-values #(url-encode (into-string %)) params))

;; NOTE something like lambdaisland/url could help streamline this
(do
#?@(:clj [(defn query-params [url]
;; this impl is straight from chatgpt and probably not right
(let [url-parts (clojure.string/split url #"\?")
query (second url-parts)
params (and query (clojure.string/split query #"&"))]
(reduce (fn [acc param]
(let [[key value] (clojure.string/split param #"=" 2)
decoded-key (URLDecoder/decode key "UTF-8")
decoded-value (URLDecoder/decode (or value "") "UTF-8")]
(assoc acc decoded-key decoded-value)))
{}
params)))]

:cljs [(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
"Given goog.Uri, read query parameters into a Clojure map."
[^goog.Uri uri]
(let [q (.getQueryData uri)]
(->> q
(.getKeys)
(map (juxt keyword #(query-param q %)))
(into {}))))]))

(defn- query-parameter [k v]
(str (form-encode (into-string k))
"="
Expand Down
1 change: 1 addition & 0 deletions modules/reitit-frontend/src/reitit/frontend.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
(throw e))))
coercion/coerce!)]
(if-let [match (r/match-by-path router (.getPath uri))]
;; query-params already part of match here (TODO ensure getPath includes query)
(let [q (query-params uri)
fragment (when (.hasFragment uri)
(.getFragment uri))
Expand Down
6 changes: 6 additions & 0 deletions test/cljc/reitit/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,9 @@
(deftest routing-bug-test-538
(let [router (r/router [["/:a"] ["/:b"]] {:conflicts nil})]
(is (nil? (r/match-by-path router "")))))

(deftest query-string-support
(let [router (r/router [["/endpoint"]])
match (r/match-by-path router "/endpoint?foo=bar&foo=baz&some=123")]
(is (= ["bar" "baz"] (:foo (:query-params match))))
(is (= ["123"] (:some (:query-params match))))))
Loading