Skip to content

Commit

Permalink
works with and without hashed urls
Browse files Browse the repository at this point in the history
  • Loading branch information
grmble committed Oct 28, 2022
1 parent 2c44db8 commit b2305fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ pom.xml.asc
.nrepl-port
/.idea/*
*.iml
/.cpcache/
/.clj-kondo/.cache/
/.lsp/.cache/
/.calva/output-window/
14 changes: 11 additions & 3 deletions src/kee_frame/router.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@
(let [[_ path-params] route
{:keys [path] :as match} (apply reitit/match-by-name routes route)]
(when (valid? match)
(str (when hash? (str base-path "/#")) path
(str base-path (when hash? "/#") path
(when-some [q (:query-string path-params)] (str "?" q))
(when-some [h (:hash path-params)] (str "#" h))))))

(defn- remove-base-path [url base-path]
;; i don't want to think about how to quote the regex
;; also we would pay for compilation on every check
(if (str/starts-with? url base-path)
(subs url (count base-path))
url))

(defn match-url [routes base-path url]
(let [[path+query fragment] (-> url
(str/replace (re-pattern (str "^" base-path "/?#/")) "/")
(remove-base-path base-path)
(str/replace #"^/#/" "/")
(str/split #"#" 2))
[path query] (str/split path+query #"\?" 2)]
(some-> (reitit/match-by-path routes path)
Expand Down Expand Up @@ -119,7 +127,7 @@

(defn start! [{:keys [routes initial-db router app-db-spec root-component chain-links
screen scroll global-interceptors log-spec-error base-path]
:or {scroll true base-path "/"}
:or {scroll true base-path ""}
:as config}]
(deprecations config)
(when app-db-spec
Expand Down
31 changes: 22 additions & 9 deletions test/kee_frame/router_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,47 @@
[kee-frame.api :as api]
[reitit.core :as reitit]))

(defn router [routes hash?] (router/->ReititRouter (reitit/router routes) hash? nil))
(defn router [routes hash? base-path] (router/->ReititRouter (reitit/router routes) hash? base-path nil))

(deftest can-produce-hash-urls
(defn produce-urls-helper [hash? base-path]
(let [r (router [["/" :root]
["/item/:id" :item]] true)]
["/item/:id" :item]] hash? base-path)
hash-path (if hash? "/#" "")]
(testing "Root"
(is (= "/#/" (api/data->url r [:root]))))
(is (= (str base-path hash-path "/") (api/data->url r [:root]))))

(testing "Item"
(is (= "/#/item/1" (api/data->url r [:item {:id 1}]))))
(is (= (str base-path hash-path "/item/1") (api/data->url r [:item {:id 1}]))))

(testing "Item with missing id throws"
(is (thrown?
#?(:clj clojure.lang.ExceptionInfo
:cljs js/Error)
(api/data->url r [:item]))))))

(deftest can-parse-hash-urls
(defn parse-urls-helper [hash? base-path]
(let [r (router [["/" :root]
["/item/:id" :item]] true)]
["/item/:id" :item]] true base-path)
hash-path (if hash? "/#" "")]

(testing "Root"
(is (= :root (-> (api/url->data r "/")
(is (= :root (-> (api/url->data r (str base-path hash-path "/"))
:data
:name))))

(testing "Item with path params and query string"
(let [{:keys [data path-params query-string]} (api/url->data r "/item/1?query=string")]
(let [{:keys [data path-params query-string]}
(api/url->data r (str base-path hash-path "/item/1?query=string"))]
(is (= "query=string" query-string))
(is (= :item (:name data)))
(is (= "1" (:id path-params)))))))

(deftest can-produce-urls
(doseq [hash? [true false]
base-path ["" "/prefix"]]
(produce-urls-helper hash? base-path)))

(deftest can-parse-urls
(doseq [hash? [true false]
base-path ["" "/prefix"]]
(parse-urls-helper hash? base-path)))

0 comments on commit b2305fa

Please sign in to comment.