From a25f336d800cd64217279f1a4be4b3c6b6107314 Mon Sep 17 00:00:00 2001 From: Alex King Date: Tue, 9 Apr 2019 16:23:33 +0200 Subject: [PATCH 01/13] Ignore anchors with '_self' not 'self' --- modules/reitit-frontend/src/reitit/frontend/history.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 35cbf52d..e1aba76d 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -92,7 +92,7 @@ (not (.-ctrlKey e)) (not (.-metaKey e)) (not (.-shiftKey e)) - (not (contains? #{"_blank" "self"} (.getAttribute el "target"))) + (not (contains? #{"_blank" "_self"} (.getAttribute el "target"))) ;; Left button (= 0 (.-button e)) ;; isContentEditable property is inherited from parents, From b50b44cced491a3e210d142d0d024eebf3a084c0 Mon Sep 17 00:00:00 2001 From: Alex King Date: Thu, 11 Apr 2019 22:08:03 +0200 Subject: [PATCH 02/13] Invert link 'target' check --- modules/reitit-frontend/src/reitit/frontend/history.cljs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index e1aba76d..025141aa 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -92,7 +92,8 @@ (not (.-ctrlKey e)) (not (.-metaKey e)) (not (.-shiftKey e)) - (not (contains? #{"_blank" "_self"} (.getAttribute el "target"))) + (or (not (.hasAttribute el "target")) + (contains? #{"" "_self"} (.getAttribute el "target"))) ;; Left button (= 0 (.-button e)) ;; isContentEditable property is inherited from parents, From 5ffb9fc59f8b23dd7382032e43e72dd443f45114 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Mon, 15 Apr 2019 11:40:59 +0300 Subject: [PATCH 03/13] Add ignore-anchor-click function --- doc/frontend/browser.md | 23 +++++++++++++++++++ .../src/reitit/frontend/history.cljs | 20 ++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/doc/frontend/browser.md b/doc/frontend/browser.md index 08d3b4b1..edf4b5c1 100644 --- a/doc/frontend/browser.md +++ b/doc/frontend/browser.md @@ -18,6 +18,29 @@ request to the server. This means the URL will look normal, but the downside is that the server must respond to all routes with correct file (`index.html`). Check examples for simple Ring handler example. +### Anchor click handling + +HTML5 History router will handle click events on anchors where the href +matches the route tree (and other [rules](../../modules/reitit-frontend/src/reitit/frontend/history.cljs#L84-L98)). +If you have need to control this logic, for example to handle some +anchor clicks where the href matches route tree normally (i.e. browser load) +you can provide `:ignore-anchor-click` function to add your own logic to +event handling: + +```clj +(rfe/start! + router + {:use-fragment false + :ignore-anchor-click (fn [e el] + (not= "false" (gobj/get (.-dataset el) "reititHandleClick")))}) + +;; Use data-reitit-handle-click to disable Reitit anchor handling +[:a + {:href (rfe/href ::about) + :data-reitit-handle-click false} + "About"] +``` + ## Easy Reitit frontend routers require storing the state somewhere and passing it to diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 025141aa..333c8c95 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -78,6 +78,9 @@ (if (exists? js/location) (.getDomain (.parse Uri js/location))) + ignore-anchor-click-fn (or (:ignore-anchor-click this) + (constantly true)) + ;; Prevent document load when clicking a elements, if the href points to URL that is part ;; of the routing tree." ignore-anchor-click @@ -99,6 +102,7 @@ ;; isContentEditable property is inherited from parents, ;; so if the anchor is inside contenteditable div, the property will be true. (not (.-isContentEditable el)) + (ignore-anchor-click-fn e el) (reitit/match-by-path router (.getPath uri))) (.preventDefault e) (let [path (str (.getPath uri) @@ -134,15 +138,23 @@ - on-navigate Function to be called when route changes. Takes two parameters, ´match´ and ´history´ object. Options: - - :use-fragment (default true) If true, onhashchange and location hash are used to store current route." + - :use-fragment (default true) If true, onhashchange and location hash are used to store current route. + + Options (Html5History): + - :ignore-anchor-click Function (event, anchor element) to check if Reitit + should handle click events on the anchor element. By default + hrefs matching the route tree are handled by Reitit." ([router on-navigate] (start! router on-navigate nil)) ([router on-navigate {:keys [use-fragment] - :or {use-fragment true}}] - (let [opts {:router router - :on-navigate on-navigate}] + :or {use-fragment true} + :as opts}] + (let [opts (-> opts + (dissoc :use-fragment) + (assoc :router router + :on-navigate on-navigate))] (-init (if use-fragment (map->FragmentHistory opts) (map->Html5History opts)))))) From 51c5aad4920821120eade68583fe3de0fa0d6de9 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 26 Apr 2019 15:10:17 +0300 Subject: [PATCH 04/13] Refactor ignore-anchor-click check to predicate function --- doc/frontend/browser.md | 6 +- .../src/reitit/frontend/history.cljs | 76 ++++++++++--------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/doc/frontend/browser.md b/doc/frontend/browser.md index edf4b5c1..7217e9f5 100644 --- a/doc/frontend/browser.md +++ b/doc/frontend/browser.md @@ -31,8 +31,10 @@ event handling: (rfe/start! router {:use-fragment false - :ignore-anchor-click (fn [e el] - (not= "false" (gobj/get (.-dataset el) "reititHandleClick")))}) + :ignore-anchor-click? (fn [router e el uri] + ;; Add additional check on top of the default checks + (and (rfh/ignore-anchor-click? router e el uri) + (not= "false" (gobj/get (.-dataset el) "reititHandleClick"))))}) ;; Use data-reitit-handle-click to disable Reitit anchor handling [:a diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 333c8c95..85eea8b6 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -67,6 +67,29 @@ (first (.composedPath original-event)) (.-target event)))) +(defn ignore-anchor-click? + "Precicate to check if the anchor click event default action + should be ignored. This logic will ignore the event + if anchor href matches the route tree, and in this case + the page location is updated using History API." + [router e el uri] + (let [current-domain (if (exists? js/location) + (.getDomain (.parse Uri js/location)))] + (and (or (and (not (.hasScheme uri)) (not (.hasDomain uri))) + (= current-domain (.getDomain uri))) + (not (.-altKey e)) + (not (.-ctrlKey e)) + (not (.-metaKey e)) + (not (.-shiftKey e)) + (or (not (.hasAttribute el "target")) + (contains? #{"" "_self"} (.getAttribute el "target"))) + ;; Left button + (= 0 (.-button e)) + ;; isContentEditable property is inherited from parents, + ;; so if the anchor is inside contenteditable div, the property will be true. + (not (.-isContentEditable el)) + (reitit/match-by-path router (.getPath uri))))) + (defrecord Html5History [on-navigate router listen-key click-listen-key] History (-init [this] @@ -74,42 +97,22 @@ (fn [e] (-on-navigate this (-get-path this))) - current-domain - (if (exists? js/location) - (.getDomain (.parse Uri js/location))) - - ignore-anchor-click-fn (or (:ignore-anchor-click this) - (constantly true)) + ignore-anchor-click-predicate (or (:ignore-anchor-click? this) + ignore-anchor-click?) ;; Prevent document load when clicking a elements, if the href points to URL that is part ;; of the routing tree." - ignore-anchor-click - (fn ignore-anchor-click - [e] - ;; Returns the next matching anchestor of event target - (when-let [el (closest-by-tag (event-target e) "a")] - (let [uri (.parse Uri (.-href el))] - (when (and (or (and (not (.hasScheme uri)) (not (.hasDomain uri))) - (= current-domain (.getDomain uri))) - (not (.-altKey e)) - (not (.-ctrlKey e)) - (not (.-metaKey e)) - (not (.-shiftKey e)) - (or (not (.hasAttribute el "target")) - (contains? #{"" "_self"} (.getAttribute el "target"))) - ;; Left button - (= 0 (.-button e)) - ;; isContentEditable property is inherited from parents, - ;; so if the anchor is inside contenteditable div, the property will be true. - (not (.-isContentEditable el)) - (ignore-anchor-click-fn e el) - (reitit/match-by-path router (.getPath uri))) - (.preventDefault e) - (let [path (str (.getPath uri) - (if (seq (.getQuery uri)) - (str "?" (.getQuery uri))))] - (.pushState js/window.history nil "" path) - (-on-navigate this path))))))] + ignore-anchor-click (fn [e] + ;; Returns the next matching anchestor of event target + (when-let [el (closest-by-tag (event-target e) "a")] + (let [uri (.parse Uri (.-href el))] + (when (ignore-anchor-click-predicate router e el uri) + (.preventDefault e) + (let [path (str (.getPath uri) + (if (seq (.getQuery uri)) + (str "?" (.getQuery uri))))] + (.pushState js/window.history nil "" path) + (-on-navigate this path))))))] (-on-navigate this (-get-path this)) (assoc this :listen-key (gevents/listen js/window goog.events.EventType.POPSTATE handler false) @@ -141,9 +144,10 @@ - :use-fragment (default true) If true, onhashchange and location hash are used to store current route. Options (Html5History): - - :ignore-anchor-click Function (event, anchor element) to check if Reitit - should handle click events on the anchor element. By default - hrefs matching the route tree are handled by Reitit." + - :ignore-anchor-click? Function (router, event, anchor element, uri) which will be called to + check if the anchor click event should be ignored. + To extend built-in check, you can call `reitit.frontend.history/ignore-anchor-click?` + function, which will ignore clicks if the href matches route tree." ([router on-navigate] (start! router on-navigate nil)) ([router From f8f44b9ea6e7a56723886cde219dbc8e84b8f6e1 Mon Sep 17 00:00:00 2001 From: Miikka Koskinen Date: Thu, 2 May 2019 11:52:13 +0300 Subject: [PATCH 05/13] Update doc/frontend/browser.md Co-Authored-By: Deraen --- doc/frontend/browser.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frontend/browser.md b/doc/frontend/browser.md index 7217e9f5..0d1d8b0d 100644 --- a/doc/frontend/browser.md +++ b/doc/frontend/browser.md @@ -24,7 +24,7 @@ HTML5 History router will handle click events on anchors where the href matches the route tree (and other [rules](../../modules/reitit-frontend/src/reitit/frontend/history.cljs#L84-L98)). If you have need to control this logic, for example to handle some anchor clicks where the href matches route tree normally (i.e. browser load) -you can provide `:ignore-anchor-click` function to add your own logic to +you can provide `:ignore-anchor-click?` function to add your own logic to event handling: ```clj From a19849fe58d6e5234e691ba0ed2201c1a8712db8 Mon Sep 17 00:00:00 2001 From: Alexander Kiel Date: Sat, 13 Jul 2019 15:04:06 +0200 Subject: [PATCH 06/13] Make Map Destructuring of Namespaced Keys more Beautiful It's possible to put the :keys keyword in the namespace of the keys one likes to destructure. With that one can use symbols in the vector again. One advantage of having symbols is, that Cursive grays them out if not used. I found two occurrences of unused destructured keys. --- CHANGELOG.md | 2 +- doc/ring/data_driven_middleware.md | 2 +- doc/ring/dynamic_extensions.md | 2 +- doc/ring/reverse_routing.md | 2 +- doc/ring/ring.md | 2 +- doc/ring/route_data_validation.md | 2 +- doc/ring/transforming_middleware_chain.md | 2 +- modules/reitit-core/src/reitit/coercion.cljc | 2 +- modules/reitit-core/src/reitit/interceptor.cljc | 6 +++--- modules/reitit-core/src/reitit/middleware.cljc | 4 ++-- modules/reitit-core/src/reitit/spec.cljc | 2 +- modules/reitit-http/src/reitit/http.cljc | 2 +- .../src/reitit/http/interceptors/dev.clj | 2 +- .../src/reitit/ring/middleware/dev.clj | 4 ++-- modules/reitit-pedestal/src/reitit/pedestal.clj | 2 +- modules/reitit-ring/src/reitit/ring.cljc | 2 +- .../src/reitit/interceptor/sieppari.clj | 2 +- modules/reitit-swagger/src/reitit/swagger.cljc | 2 +- test/clj/reitit/http_test.clj | 10 +++++----- test/cljc/reitit/ring_test.cljc | 10 +++++----- 20 files changed, 32 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c09ab90d..135264e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -539,7 +539,7 @@ We use [Break Versioning][breakver]. The version numbers follow a `. request (ring/get-match) :data ::roles)] (if (and (seq required) (not (set/subset? required roles))) {:status 403, :body "forbidden"} diff --git a/doc/ring/reverse_routing.md b/doc/ring/reverse_routing.md index e2d7b5af..98d5dc0e 100644 --- a/doc/ring/reverse_routing.md +++ b/doc/ring/reverse_routing.md @@ -12,7 +12,7 @@ Below is an example how to do reverse routing from a ring handler: (ring/ring-handler (ring/router [["/users" - {:get (fn [{:keys [::r/router]}] + {:get (fn [{::r/keys [router]}] {:status 200 :body (for [i (range 10)] {:uri (-> router diff --git a/doc/ring/ring.md b/doc/ring/ring.md index ce51b122..5e1c1709 100644 --- a/doc/ring/ring.md +++ b/doc/ring/ring.md @@ -155,7 +155,7 @@ A middleware and a handler: (fn [request] (handler (update request ::acc (fnil conj []) id)))) -(defn handler [{:keys [::acc]}] +(defn handler [{::keys [acc]}] {:status 200, :body (conj acc :handler)}) ``` diff --git a/doc/ring/route_data_validation.md b/doc/ring/route_data_validation.md index a2f938b1..9609dc70 100644 --- a/doc/ring/route_data_validation.md +++ b/doc/ring/route_data_validation.md @@ -155,7 +155,7 @@ Let's reuse the `wrap-enforce-roles` from [Dynamic extensions](dynamic_extension (s/def ::roles (s/coll-of ::role :into #{})) (defn wrap-enforce-roles [handler] - (fn [{:keys [::roles] :as request}] + (fn [{::keys [roles] :as request}] (let [required (some-> request (ring/get-match) :data ::roles)] (if (and (seq required) (not (set/subset? required roles))) {:status 403, :body "forbidden"} diff --git a/doc/ring/transforming_middleware_chain.md b/doc/ring/transforming_middleware_chain.md index 20191c5b..ec75cd80 100644 --- a/doc/ring/transforming_middleware_chain.md +++ b/doc/ring/transforming_middleware_chain.md @@ -12,7 +12,7 @@ There is an extra option in ring-router (actually, in the underlying middleware- (fn [request] (handler (update request ::acc (fnil conj []) id)))) -(defn handler [{:keys [::acc]}] +(defn handler [{::keys [acc]}] {:status 200, :body (conj acc :handler)}) (def app diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index b9da1519..737847dc 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -70,7 +70,7 @@ (-> request :muuntaja/request :format)) ;; TODO: support faster key walking, walk/keywordize-keys is quite slow... -(defn request-coercer [coercion type model {:keys [::extract-request-format ::parameter-coercion] +(defn request-coercer [coercion type model {::keys [extract-request-format parameter-coercion] :or {extract-request-format extract-request-format-default parameter-coercion default-parameter-coercion}}] (if coercion diff --git a/modules/reitit-core/src/reitit/interceptor.cljc b/modules/reitit-core/src/reitit/interceptor.cljc index 5b3de7d7..b6bfade9 100644 --- a/modules/reitit-core/src/reitit/interceptor.cljc +++ b/modules/reitit-core/src/reitit/interceptor.cljc @@ -33,7 +33,7 @@ #?(:clj clojure.lang.Keyword :cljs cljs.core.Keyword) - (into-interceptor [this data {:keys [::registry] :as opts}] + (into-interceptor [this data {::keys [registry] :as opts}] (if-let [interceptor (if registry (registry this))] (into-interceptor interceptor data opts) (throw @@ -108,7 +108,7 @@ (chain interceptors nil nil)) ([interceptors data] (chain interceptors data nil)) - ([interceptors data {:keys [::transform] :or {transform identity} :as opts}] + ([interceptors data {::keys [transform] :or {transform identity} :as opts}] (let [transform (if (vector? transform) (apply comp (reverse transform)) transform)] (->> interceptors (keep #(into-interceptor % data opts)) @@ -119,7 +119,7 @@ (defn compile-result ([route opts] (compile-result route opts nil)) - ([[_ {:keys [interceptors handler] :as data}] {:keys [::queue] :as opts} _] + ([[_ {:keys [interceptors handler] :as data}] {::keys [queue] :as opts} _] (let [chain (chain (into (vec interceptors) [handler]) data opts)] (map->Endpoint {:interceptors chain diff --git a/modules/reitit-core/src/reitit/middleware.cljc b/modules/reitit-core/src/reitit/middleware.cljc index 51d206be..b27e1d0c 100644 --- a/modules/reitit-core/src/reitit/middleware.cljc +++ b/modules/reitit-core/src/reitit/middleware.cljc @@ -17,7 +17,7 @@ #?(:clj clojure.lang.Keyword :cljs cljs.core.Keyword) - (into-middleware [this data {:keys [::registry] :as opts}] + (into-middleware [this data {::keys [registry] :as opts}] (if-let [middleware (if registry (registry this))] (into-middleware middleware data opts) (throw @@ -83,7 +83,7 @@ (if scope {:scope scope}))))) (defn- expand-and-transform - [middleware data {:keys [::transform] :or {transform identity} :as opts}] + [middleware data {::keys [transform] :or {transform identity} :as opts}] (let [transform (if (vector? transform) (apply comp (reverse transform)) transform)] (->> middleware (keep #(into-middleware % data opts)) diff --git a/modules/reitit-core/src/reitit/spec.cljc b/modules/reitit-core/src/reitit/spec.cljc index c0317629..c00c717c 100644 --- a/modules/reitit-core/src/reitit/spec.cljc +++ b/modules/reitit-core/src/reitit/spec.cljc @@ -123,7 +123,7 @@ (->Problem p nil d spec problems))) (keep identity) (seq) (vec)))) -(defn validate [routes {:keys [spec ::wrap] :or {spec ::default-data, wrap identity}}] +(defn validate [routes {:keys [spec] ::keys [wrap] :or {spec ::default-data, wrap identity}}] (when-let [problems (validate-route-data routes wrap spec)] (exception/fail! ::invalid-route-data diff --git a/modules/reitit-http/src/reitit/http.cljc b/modules/reitit-http/src/reitit/http.cljc index ef62ad08..a370e896 100644 --- a/modules/reitit-http/src/reitit/http.cljc +++ b/modules/reitit-http/src/reitit/http.cljc @@ -13,7 +13,7 @@ (update acc method expand opts) acc)) data ring/http-methods)]) -(defn compile-result [[path data] {:keys [::default-options-handler] :as opts}] +(defn compile-result [[path data] {::keys [default-options-handler] :as opts}] (let [[top childs] (ring/group-keys data) childs (cond-> childs (and (not (:options childs)) (not (:handler top)) default-options-handler) diff --git a/modules/reitit-interceptors/src/reitit/http/interceptors/dev.clj b/modules/reitit-interceptors/src/reitit/http/interceptors/dev.clj index bd652885..285434f4 100644 --- a/modules/reitit-interceptors/src/reitit/http/interceptors/dev.clj +++ b/modules/reitit-interceptors/src/reitit/http/interceptors/dev.clj @@ -24,7 +24,7 @@ (update :request dissoc ::r/match ::r/router))) (defn- handle [name stage] - (fn [{:keys [::original ::previous] :as ctx}] + (fn [{::keys [previous] :as ctx}] (let [current (polish ctx) previous (polish previous)] (printer/print-doc (diff-doc stage name previous current) printer) diff --git a/modules/reitit-middleware/src/reitit/ring/middleware/dev.clj b/modules/reitit-middleware/src/reitit/ring/middleware/dev.clj index 2d74c63d..de89996e 100644 --- a/modules/reitit-middleware/src/reitit/ring/middleware/dev.clj +++ b/modules/reitit-middleware/src/reitit/ring/middleware/dev.clj @@ -18,13 +18,13 @@ (defn polish [request] (dissoc request ::r/match ::r/router ::original ::previous)) -(defn printed-request [name {:keys [::original ::previous] :as request}] +(defn printed-request [name {::keys [previous] :as request}] (printer/print-doc (diff-doc :request name (polish previous) (polish request)) printer) (-> request (update ::original (fnil identity request)) (assoc ::previous request))) -(defn printed-response [name {:keys [::original ::previous] :as response}] +(defn printed-response [name {::keys [previous] :as response}] (printer/print-doc (diff-doc :response name (polish previous) (polish response)) printer) (-> response (update ::original (fnil identity response)) diff --git a/modules/reitit-pedestal/src/reitit/pedestal.clj b/modules/reitit-pedestal/src/reitit/pedestal.clj index 115ac991..fcca5a46 100644 --- a/modules/reitit-pedestal/src/reitit/pedestal.clj +++ b/modules/reitit-pedestal/src/reitit/pedestal.clj @@ -49,7 +49,7 @@ Executor (queue [_ interceptors] (->> interceptors - (map (fn [{:keys [::interceptor/handler] :as interceptor}] + (map (fn [{::interceptor/keys [handler] :as interceptor}] (or handler interceptor))) (keep ->interceptor))) (enqueue [_ context interceptors] diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index f0d769f4..81c72b94 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -28,7 +28,7 @@ (update acc method expand opts) acc)) data http-methods)]) -(defn compile-result [[path data] {:keys [::default-options-handler] :as opts}] +(defn compile-result [[path data] {::keys [default-options-handler] :as opts}] (let [[top childs] (group-keys data) childs (cond-> childs (and (not (:options childs)) (not (:handler top)) default-options-handler) diff --git a/modules/reitit-sieppari/src/reitit/interceptor/sieppari.clj b/modules/reitit-sieppari/src/reitit/interceptor/sieppari.clj index e7d0faed..1198dff4 100644 --- a/modules/reitit-sieppari/src/reitit/interceptor/sieppari.clj +++ b/modules/reitit-sieppari/src/reitit/interceptor/sieppari.clj @@ -9,7 +9,7 @@ (queue [_ interceptors] (queue/into-queue (map - (fn [{:keys [::interceptor/handler] :as interceptor}] + (fn [{::interceptor/keys [handler] :as interceptor}] (or handler interceptor)) interceptors))) (execute [_ interceptors request] diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 567c5ab6..1d4491ad 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -71,7 +71,7 @@ "Create a ring handler to emit swagger spec. Collects all routes from router which have an intersecting `[:swagger :id]` and which are not marked with `:no-doc` route data." (fn create-swagger - ([{:keys [::r/router ::r/match :request-method]}] + ([{::r/keys [router match] :keys [request-method]}] (let [{:keys [id] :or {id ::default} :as swagger} (-> match :result request-method :data :swagger) ids (trie/into-set id) strip-top-level-keys #(dissoc % :id :info :host :basePath :definitions :securityDefinitions) diff --git a/test/clj/reitit/http_test.clj b/test/clj/reitit/http_test.clj index 934ef9bc..4235b9ce 100644 --- a/test/clj/reitit/http_test.clj +++ b/test/clj/reitit/http_test.clj @@ -11,7 +11,7 @@ (defn interceptor [name] {:enter (fn [ctx] (update-in ctx [:request ::i] (fnil conj []) name))}) -(defn handler [{:keys [::i]}] +(defn handler [{::keys [i]}] {:status 200 :body (conj i :ok)}) (deftest http-router-test @@ -89,7 +89,7 @@ (is (= name (-> (r/match-by-name router name) :data :name)))))))) (def enforce-roles-interceptor - {:enter (fn [{{:keys [::roles] :as request} :request :as ctx}] + {:enter (fn [{{::keys [roles] :as request} :request :as ctx}] (let [required (some-> request (http/get-match) :data ::roles)] (if (and (seq required) (not (set/intersection required roles))) (-> ctx @@ -280,7 +280,7 @@ (let [interceptor (fn [name] {:name name :enter (fn [ctx] (update-in ctx [:request ::i] (fnil conj []) name))}) - handler (fn [{:keys [::i]}] {:status 200 :body (conj i :ok)}) + handler (fn [{::keys [i]}] {:status 200 :body (conj i :ok)}) request {:uri "/api/avaruus" :request-method :get} create (fn [options] (http/ring-handler @@ -492,14 +492,14 @@ (testing "1-arity" ((http/ring-handler (http/router []) - (fn [{:keys [::r/router]}] + (fn [{::r/keys [router]}] (is router)) {:executor sieppari/executor}) {})) (testing "3-arity" ((http/ring-handler (http/router []) - (fn [{:keys [::r/router]}] + (fn [{::r/keys [router]}] (is router)) {:executor sieppari/executor}) {} ::respond ::raise))) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 5dead344..7c1d3432 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -19,7 +19,7 @@ (mw handler (keyword (str name "_" name2 "_" name3)))) (defn handler - ([{:keys [::mw]}] + ([{::keys [mw]}] {:status 200 :body (conj mw :ok)}) ([request respond _] (respond (handler request)))) @@ -119,7 +119,7 @@ (is (= name (-> (r/match-by-name router name) :data :name)))))))) (defn wrap-enforce-roles [handler] - (fn [{:keys [::roles] :as request}] + (fn [{::keys [roles] :as request}] (let [required (some-> request (ring/get-match) :data ::roles)] (if (and (seq required) (not (set/intersection required roles))) {:status 403, :body "forbidden"} @@ -399,7 +399,7 @@ :wrap (fn [handler] (fn [request] (handler (update request ::mw (fnil conj []) name))))}) - handler (fn [{:keys [::mw]}] {:status 200 :body (conj mw :ok)}) + handler (fn [{::keys [mw]}] {:status 200 :body (conj mw :ok)}) request {:uri "/api/avaruus" :request-method :get} create (fn [options] (ring/ring-handler @@ -583,13 +583,13 @@ (testing "1-arity" ((ring/ring-handler (ring/router []) - (fn [{:keys [::r/router]}] + (fn [{::r/keys [router]}] (is router))) {})) (testing "3-arity" ((ring/ring-handler (ring/router []) - (fn [{:keys [::r/router]} _ _] + (fn [{::r/keys [router]} _ _] (is router))) {} ::respond ::raise))) From e5488ed216fe70d4a6df1b1aee96370ca442c378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwena=C3=ABl=20Ropert?= Date: Tue, 16 Jul 2019 17:54:15 +0200 Subject: [PATCH 07/13] Add space between keyword and curly --- examples/ring-spec-swagger/project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ring-spec-swagger/project.clj b/examples/ring-spec-swagger/project.clj index 571781c5..8b90d687 100644 --- a/examples/ring-spec-swagger/project.clj +++ b/examples/ring-spec-swagger/project.clj @@ -4,4 +4,4 @@ [ring/ring-jetty-adapter "1.7.1"] [metosin/reitit "0.3.9"]] :repl-options {:init-ns example.server} - :profiles{:dev {:dependencies [[ring/ring-mock "0.3.2"]]}}) + :profiles {:dev {:dependencies [[ring/ring-mock "0.3.2"]]}}) From 42e5746c2f18c86e02dbdd06d7c7bf2b721bb9de Mon Sep 17 00:00:00 2001 From: Oleg Date: Mon, 19 Aug 2019 22:36:18 +0300 Subject: [PATCH 08/13] Update docs for controller and identity Fixed typos in a sentence about `start`. Also updated the information about `stop` call, looks like it will be called with last controller identity value, not return value of params --- doc/frontend/controllers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/frontend/controllers.md b/doc/frontend/controllers.md index 4ee8de14..13dcef08 100644 --- a/doc/frontend/controllers.md +++ b/doc/frontend/controllers.md @@ -18,9 +18,9 @@ controller identity When you navigate to a route that has a controller, controller identity is first resolved by calling `identity` function, or by using `parameters` -declaration, or if neither is set, the identity is `nil`. Next controller -is initialized by calling `start` is called with the identity value. -When you exit that route, `stop` is called with the return value of `params.` +declaration, or if neither is set, the identity is `nil`. Next, the controller +is initialized by calling `start` with the controller identity value. +When you exit that route, `stop` is called with the last controller identity value. If you navigate to the same route with different match, identity gets resolved again. If the identity changes from the previous value, controller From 15395f823979109d867195727eb30cd061f69053 Mon Sep 17 00:00:00 2001 From: Oleg Date: Mon, 19 Aug 2019 22:41:36 +0300 Subject: [PATCH 09/13] Fix documentation about identity Updated the order of identity resolving according to current source. --- doc/frontend/controllers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/frontend/controllers.md b/doc/frontend/controllers.md index 13dcef08..18f8c6cf 100644 --- a/doc/frontend/controllers.md +++ b/doc/frontend/controllers.md @@ -17,8 +17,8 @@ controller identity * `start` & `stop` functions, which are called with controller identity When you navigate to a route that has a controller, controller identity -is first resolved by calling `identity` function, or by using `parameters` -declaration, or if neither is set, the identity is `nil`. Next, the controller +is first resolved by using `parameters` declaration, or by calling `identity` function, +or if neither is set, the identity is `nil`. Next, the controller is initialized by calling `start` with the controller identity value. When you exit that route, `stop` is called with the last controller identity value. From d00efb8cc62ced0800ada422fe6f9481fb19d696 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Fri, 30 Aug 2019 13:11:08 +0300 Subject: [PATCH 10/13] Update Aleph in example --- examples/http-swagger/project.clj | 2 +- examples/http-swagger/src/example/server.clj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/http-swagger/project.clj b/examples/http-swagger/project.clj index 3c6b2194..776ee007 100644 --- a/examples/http-swagger/project.clj +++ b/examples/http-swagger/project.clj @@ -2,6 +2,6 @@ :description "Reitit Http App with Swagger" :dependencies [[org.clojure/clojure "1.10.0"] [ring/ring-jetty-adapter "1.7.1"] - [aleph "0.4.6"] + [aleph "0.4.7-alpha5"] [metosin/reitit "0.3.9"]] :repl-options {:init-ns example.server}) diff --git a/examples/http-swagger/src/example/server.clj b/examples/http-swagger/src/example/server.clj index bf2c5d55..b768af9f 100644 --- a/examples/http-swagger/src/example/server.clj +++ b/examples/http-swagger/src/example/server.clj @@ -15,7 +15,7 @@ [reitit.http.spec :as spec] [spec-tools.spell :as spell] [ring.adapter.jetty :as jetty] - [aleph.http :as client] + [aleph.http :as aleph] [muuntaja.core :as m] [clojure.java.io :as io] [clojure.spec.alpha :as s] @@ -71,7 +71,7 @@ :responses {200 {:body any?}} :handler (fn [{{{:keys [seed results]} :query} :parameters}] (d/chain - (client/get + (aleph/get "https://randomuser.me/api/" {:query-params {:seed seed, :results results}}) :body From ca7403ccab11b53c223c758209bda7fca85fee6f Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Fri, 30 Aug 2019 13:11:22 +0300 Subject: [PATCH 11/13] Optionally run the app with aleph --- examples/http-swagger/src/example/server.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/http-swagger/src/example/server.clj b/examples/http-swagger/src/example/server.clj index b768af9f..4f4676af 100644 --- a/examples/http-swagger/src/example/server.clj +++ b/examples/http-swagger/src/example/server.clj @@ -146,6 +146,7 @@ (defn start [] (jetty/run-jetty #'app {:port 3000, :join? false, :async true}) + ;(aleph/start-server (aleph/wrap-ring-async-handler #'app) {:port 3000}) (println "server running in port 3000")) (comment From 031a777c8c8c84163c9a00f8386847d7e977a802 Mon Sep 17 00:00:00 2001 From: dimovich Date: Sun, 8 Sep 2019 18:28:50 +0300 Subject: [PATCH 12/13] Fix event definitions for routes with parameters The current implementation will fail for `(re-frame/dispatch [::navigate ::some-route {:some :params}])`. Update the definitions of `::navigate` and `::navigated!` events to accept the full route arguments. --- .../frontend-re-frame/src/cljs/frontend_re_frame/core.cljs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs b/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs index f7b33cc8..a15a34b0 100644 --- a/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs +++ b/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs @@ -18,7 +18,7 @@ (re-frame/reg-event-fx ::navigate - (fn [db [_ route]] + (fn [db [_ & route]] ;; See `navigate` effect in routes.cljs {::navigate! route})) @@ -59,8 +59,8 @@ ;; Triggering navigation from events. (re-frame/reg-fx ::navigate! - (fn [k params query] - (rfe/push-state k params query))) + (fn [route] + (apply rfe/push-state route))) ;;; Routes ;;; From 3644c2e3922bede4349dc77415a265474f3ec5a5 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 9 Sep 2019 21:14:40 +0300 Subject: [PATCH 13/13] FIxes #308 --- modules/reitit-http/src/reitit/http.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/reitit-http/src/reitit/http.cljc b/modules/reitit-http/src/reitit/http.cljc index a370e896..afd39a80 100644 --- a/modules/reitit-http/src/reitit/http.cljc +++ b/modules/reitit-http/src/reitit/http.cljc @@ -127,7 +127,7 @@ (dissoc :data) ; data is already merged into routes (cond-> (seq interceptors) (update-in [:data :interceptors] (partial into (vec interceptors))))) - router (reitit.http/router (r/routes router) router-opts) + router (reitit.http/router (r/routes router) router-opts) ;; will re-compile the interceptors enrich-request (ring/create-enrich-request inject-match? inject-router?) enrich-default-request (ring/create-enrich-default-request inject-router?)] (with-meta