diff --git a/CHANGELOG.md b/CHANGELOG.md index e17aa1fe..b5f7dfed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ controller is interested in, as data, which should cover most use cases: `{:start start-fn, :parameters {:path [:foo-id]}}` +## `reitit-ring` + +* Allow Middleware to compile to `nil` with Middleware Registries, fixes to [#216](https://github.com/metosin/reitit/issues/216). + +## `reitit-http` + +* Allow Interceptors to compile to `nil` with Interceptor Registries, related to [#216](https://github.com/metosin/reitit/issues/216). + ## 0.2.13 (2019-01-26) * Don't throw `StringIndexOutOfBoundsException` with empty path lookup on wildcard paths, fixes [#209](https://github.com/metosin/reitit/issues/209) diff --git a/modules/reitit-core/src/reitit/interceptor.cljc b/modules/reitit-core/src/reitit/interceptor.cljc index a89f455d..f95bad34 100644 --- a/modules/reitit-core/src/reitit/interceptor.cljc +++ b/modules/reitit-core/src/reitit/interceptor.cljc @@ -33,20 +33,20 @@ #?(:clj clojure.lang.Keyword :cljs cljs.core.Keyword) (into-interceptor [this data {:keys [::registry] :as opts}] - (or (if-let [interceptor (if registry (registry this))] - (into-interceptor interceptor data opts)) - (throw - (ex-info - (str - "Interceptor " this " not found in registry.\n\n" - (if (seq registry) - (str - "Available interceptors in registry:\n" - (with-out-str - (pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v})))) - "see [reitit.interceptor/router] on how to add interceptor to the registry.\n") "\n") - {:id this - :registry registry})))) + (if-let [interceptor (if registry (registry this))] + (into-interceptor interceptor data opts) + (throw + (ex-info + (str + "Interceptor " this " not found in registry.\n\n" + (if (seq registry) + (str + "Available interceptors in registry:\n" + (with-out-str + (pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v})))) + "see [reitit.interceptor/router] on how to add interceptor to the registry.\n") "\n") + {:id this + :registry registry})))) #?(:clj clojure.lang.APersistentVector :cljs cljs.core.PersistentVector) diff --git a/modules/reitit-core/src/reitit/middleware.cljc b/modules/reitit-core/src/reitit/middleware.cljc index f3515684..119f0d6d 100644 --- a/modules/reitit-core/src/reitit/middleware.cljc +++ b/modules/reitit-core/src/reitit/middleware.cljc @@ -17,20 +17,20 @@ #?(:clj clojure.lang.Keyword :cljs cljs.core.Keyword) (into-middleware [this data {:keys [::registry] :as opts}] - (or (if-let [middleware (if registry (registry this))] - (into-middleware middleware data opts)) - (throw - (ex-info - (str - "Middleware " this " not found in registry.\n\n" - (if (seq registry) - (str - "Available middleware in registry:\n" - (with-out-str - (pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v})))) - "see [reitit.middleware/router] on how to add middleware to the registry.\n") "\n") - {:id this - :registry registry})))) + (if-let [middleware (if registry (registry this))] + (into-middleware middleware data opts) + (throw + (ex-info + (str + "Middleware " this " not found in registry.\n\n" + (if (seq registry) + (str + "Available middleware in registry:\n" + (with-out-str + (pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v})))) + "see [reitit.middleware/router] on how to add middleware to the registry.\n") "\n") + {:id this + :registry registry})))) #?(:clj clojure.lang.APersistentVector :cljs cljs.core.PersistentVector) diff --git a/test/cljc/reitit/interceptor_test.cljc b/test/cljc/reitit/interceptor_test.cljc index 8071e48f..11f32964 100644 --- a/test/cljc/reitit/interceptor_test.cljc +++ b/test/cljc/reitit/interceptor_test.cljc @@ -78,6 +78,10 @@ #"Interceptor :enter not found in registry" (create [:enter])))) + (testing "existing keyword, compiling to nil" + (let [app (create [:enter] {::interceptor/registry {:enter {:compile (constantly nil)}}})] + (is (= [:ok] (app ctx))))) + (testing "as map" (reset! calls 0) (let [app (create [{:enter (:enter (enter :value))}])] diff --git a/test/cljc/reitit/middleware_test.cljc b/test/cljc/reitit/middleware_test.cljc index 7933ed2b..2bfc2f82 100644 --- a/test/cljc/reitit/middleware_test.cljc +++ b/test/cljc/reitit/middleware_test.cljc @@ -12,7 +12,7 @@ (defn create ([middleware] - (create middleware nil)) + (create middleware nil)) ([middleware opts] (middleware/chain middleware @@ -65,6 +65,10 @@ #"Middleware :wrap not found in registry" (create [:wrap])))) + (testing "existing keyword, compiling to nil" + (let [app (create [:wrap] {::middleware/registry {:wrap {:compile (constantly nil)}}})] + (is (= [:ok] (app request))))) + (testing "as function vector with value(s)" (reset! calls 0) (let [app (create [[wrap :value]])]