Skip to content

Commit

Permalink
Read fragment string without decoding
Browse files Browse the repository at this point in the history
Users can use Malli decoding to control decoding per schema.
  • Loading branch information
Deraen committed Feb 16, 2023
1 parent 83c31e3 commit c2ac4bc
Show file tree
Hide file tree
Showing 4 changed files with 3,749 additions and 57 deletions.
2 changes: 1 addition & 1 deletion modules/reitit-core/src/reitit/coercion.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
:form (->ParameterCoercion :form-params :string true true)
:header (->ParameterCoercion :headers :string true true)
:path (->ParameterCoercion :path-params :string true true)
:fragment (->ParameterCoercion :fragment-params :string true true)})
:fragment (->ParameterCoercion :fragment :string true true)})

(defn ^:no-doc request-coercion-failed! [result coercion value in request serialize-failed-result]
(throw
Expand Down
23 changes: 6 additions & 17 deletions modules/reitit-frontend/src/reitit/frontend.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns reitit.frontend
(:require [clojure.set :as set]
[clojure.string :as str]
[reitit.coercion :as coercion]
[reitit.core :as r])
(:import goog.Uri
Expand All @@ -21,19 +20,6 @@
(map (juxt keyword #(query-param q %)))
(into {}))))

(defn fragment-params
"Given goog.Uri, read fragment parameters into Clojure map."
[^Uri uri]
(let [fp (.getFragment uri)]
(if-not (seq fp)
{}
(into {}
(comp
(map #(str/split % #"="))
(map (fn [[k v]]
[(keyword k) v])))
(str/split fp #"&")))))

(defn match-by-path
"Given routing tree and current path, return match with possibly
coerced parameters. Return nil if no match found.
Expand All @@ -51,14 +37,17 @@
coercion/coerce!)]
(if-let [match (r/match-by-path router (.getPath uri))]
(let [q (query-params uri)
fp (fragment-params uri)
match (assoc match :query-params q :fragment-params fp)
fragment (when (.hasFragment uri)
(.getFragment uri))
match (assoc match
:query-params q
:fragment fragment)
;; Return uncoerced values if coercion is not enabled - so
;; that tha parameters are always accessible from same property.
parameters (or (coerce! match)
{:path (:path-params match)
:query q
:fragment fp})]
:fragment fragment})]
(assoc match :parameters parameters))))))

(defn match-by-name
Expand Down
Loading

0 comments on commit c2ac4bc

Please sign in to comment.