mirror of
https://github.com/metosin/reitit.git
synced 2026-02-08 04:43:12 +00:00
feat: add url-encode? options to path-params
This commit is contained in:
parent
248200aad3
commit
97bfafa907
4 changed files with 62 additions and 10 deletions
|
|
@ -46,7 +46,7 @@
|
||||||
(options [this])
|
(options [this])
|
||||||
(route-names [this])
|
(route-names [this])
|
||||||
(match-by-path [this path])
|
(match-by-path [this path])
|
||||||
(match-by-name [this name] [this name path-params]))
|
(match-by-name [this name] [this name path-params] [this name path-params opts]))
|
||||||
|
|
||||||
(defn router? [x]
|
(defn router? [x]
|
||||||
(satisfies? Router x))
|
(satisfies? Router x))
|
||||||
|
|
@ -124,7 +124,10 @@
|
||||||
(match nil)))
|
(match nil)))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(if-let [match (impl/fast-get lookup name)]
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
(match (impl/path-params path-params))))))))
|
(match (impl/path-params path-params))))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
|
(match (impl/path-params path-params opts))))))))
|
||||||
|
|
||||||
(defn lookup-router
|
(defn lookup-router
|
||||||
"Creates a lookup-router from resolved routes and optional
|
"Creates a lookup-router from resolved routes and optional
|
||||||
|
|
@ -163,7 +166,10 @@
|
||||||
(match nil)))
|
(match nil)))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(if-let [match (impl/fast-get lookup name)]
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
(match (impl/path-params path-params))))))))
|
(match (impl/path-params path-params))))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
|
(match (impl/path-params path-params opts))))))))
|
||||||
|
|
||||||
(defn trie-router
|
(defn trie-router
|
||||||
"Creates a special prefix-tree router from resolved routes and optional
|
"Creates a special prefix-tree router from resolved routes and optional
|
||||||
|
|
@ -210,7 +216,10 @@
|
||||||
(match nil)))
|
(match nil)))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(if-let [match (impl/fast-get lookup name)]
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
(match (impl/path-params path-params))))))))
|
(match (impl/path-params path-params))))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(if-let [match (impl/fast-get lookup name)]
|
||||||
|
(match (impl/path-params path-params opts))))))))
|
||||||
|
|
||||||
(defn single-static-path-router
|
(defn single-static-path-router
|
||||||
"Creates a fast router of 1 static route(s) and optional
|
"Creates a fast router of 1 static route(s) and optional
|
||||||
|
|
@ -239,7 +248,9 @@
|
||||||
(match-by-name [_ name]
|
(match-by-name [_ name]
|
||||||
(if (= n name) match))
|
(if (= n name) match))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(if (= n name) (impl/fast-assoc match :path-params (impl/path-params path-params))))))))
|
(if (= n name) (impl/fast-assoc match :path-params (impl/path-params path-params))))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(if (= n name) (impl/fast-assoc match :path-params (impl/path-params path-params opts))))))))
|
||||||
|
|
||||||
(defn mixed-router
|
(defn mixed-router
|
||||||
"Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
|
"Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
|
||||||
|
|
@ -270,7 +281,10 @@
|
||||||
(match-by-name wildcard-router name)))
|
(match-by-name wildcard-router name)))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(or (match-by-name static-router name path-params)
|
(or (match-by-name static-router name path-params)
|
||||||
(match-by-name wildcard-router name path-params)))))))
|
(match-by-name wildcard-router name path-params)))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(or (match-by-name static-router name path-params opts)
|
||||||
|
(match-by-name wildcard-router name path-params opts)))))))
|
||||||
|
|
||||||
(defn quarantine-router
|
(defn quarantine-router
|
||||||
"Creates two routers: [[mixed-router]] for non-conflicting routes
|
"Creates two routers: [[mixed-router]] for non-conflicting routes
|
||||||
|
|
@ -301,7 +315,10 @@
|
||||||
(match-by-name linear-router name)))
|
(match-by-name linear-router name)))
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(or (match-by-name mixed-router name path-params)
|
(or (match-by-name mixed-router name path-params)
|
||||||
(match-by-name linear-router name path-params)))))))
|
(match-by-name linear-router name path-params)))
|
||||||
|
(match-by-name [_ name path-params opts]
|
||||||
|
(or (match-by-name mixed-router name path-params opts)
|
||||||
|
(match-by-name linear-router name path-params opts)))))))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Creating Routers
|
;; Creating Routers
|
||||||
|
|
|
||||||
|
|
@ -297,8 +297,11 @@
|
||||||
|
|
||||||
(defn path-params
|
(defn path-params
|
||||||
"Convert parameters' values into URL-encoded strings, suitable for URL paths"
|
"Convert parameters' values into URL-encoded strings, suitable for URL paths"
|
||||||
[params]
|
([params] (maybe-map-values #(url-encode (into-string %)) params))
|
||||||
(maybe-map-values #(url-encode (into-string %)) params))
|
([params {:keys [url-encode?] :or {url-encode? true}}]
|
||||||
|
(if url-encode?
|
||||||
|
(path-params params)
|
||||||
|
(maybe-map-values #(into-string %) params))))
|
||||||
|
|
||||||
(defn- query-parameter [k v]
|
(defn- query-parameter [k v]
|
||||||
(str (form-encode (into-string k))
|
(str (form-encode (into-string k))
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,12 @@
|
||||||
:path "/api/ipa/large"
|
:path "/api/ipa/large"
|
||||||
:path-params {:size "large"}})
|
:path-params {:size "large"}})
|
||||||
(r/match-by-name router ::beer {:size "large"})))
|
(r/match-by-name router ::beer {:size "large"})))
|
||||||
|
(is (= (r/map->Match
|
||||||
|
{:template "/api/ipa/:size"
|
||||||
|
:data {:name ::beer}
|
||||||
|
:path "/api/ipa/:large"
|
||||||
|
:path-params {:size ":large"}})
|
||||||
|
(r/match-by-name router ::beer {:size ":large"} {:url-encode? false})))
|
||||||
(is (= (r/map->Match
|
(is (= (r/map->Match
|
||||||
{:template "/api/ipa/:size"
|
{:template "/api/ipa/:size"
|
||||||
:data {:name ::beer}
|
:data {:name ::beer}
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,33 @@
|
||||||
:u #uuid "c2541900-17a7-4353-9024-db8ac258ba4e"
|
:u #uuid "c2541900-17a7-4353-9024-db8ac258ba4e"
|
||||||
:k :kikka
|
:k :kikka
|
||||||
:qk ::kikka
|
:qk ::kikka
|
||||||
:nil nil}))))
|
:nil nil})))
|
||||||
|
(is (= {:n "1"
|
||||||
|
:n1 "-1"
|
||||||
|
:n2 "1"
|
||||||
|
:n3 "1"
|
||||||
|
:n4 "1"
|
||||||
|
:n5 "1"
|
||||||
|
:d "2.2"
|
||||||
|
:b "true"
|
||||||
|
:s "kikka"
|
||||||
|
:u "c2541900-17a7-4353-9024-db8ac258ba4e"
|
||||||
|
:k "kikka"
|
||||||
|
:qk "reitit.impl-test/kikka"
|
||||||
|
:nil nil}
|
||||||
|
(impl/path-params {:n 1
|
||||||
|
:n1 -1
|
||||||
|
:n2 (long 1)
|
||||||
|
:n3 (int 1)
|
||||||
|
:n4 (short 1)
|
||||||
|
:n5 (byte 1)
|
||||||
|
:d 2.2
|
||||||
|
:b true
|
||||||
|
:s "kikka"
|
||||||
|
:u #uuid "c2541900-17a7-4353-9024-db8ac258ba4e"
|
||||||
|
:k :kikka
|
||||||
|
:qk ::kikka
|
||||||
|
:nil nil} {:url-encode? false}))))
|
||||||
|
|
||||||
(deftest query-params-test
|
(deftest query-params-test
|
||||||
(are [x y]
|
(are [x y]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue