From d8d1d2feaca7c62f8e2f1a319855dda8c40ce4f7 Mon Sep 17 00:00:00 2001 From: tami5 Date: Mon, 3 Jan 2022 11:00:04 +0300 Subject: [PATCH] feat(middleware): inject keys instead of injecting `environment` This was originally the intended behavior. but it was ignored in last release. --- src/duct/reitit.clj | 7 ------- src/duct/reitit/middleware.clj | 8 ++++---- test/duct/reitit/middleware_test.clj | 14 +++++++------- test/foo/handler.clj | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/duct/reitit.clj b/src/duct/reitit.clj index 693c137..bd6629f 100644 --- a/src/duct/reitit.clj +++ b/src/duct/reitit.clj @@ -25,13 +25,6 @@ {::exception {:pretty? false} ::coercion {:pretty? false}}}) -(comment - (merge-configs base-config - (configs :development) - {:duct.core/handler-ns 'server.handler - ::coercion {:coercer 'spec} - ::muuntaja true})) - (defn- merge-to-options [configs] (reduce-kv (fn [acc k v] diff --git a/src/duct/reitit/middleware.clj b/src/duct/reitit/middleware.clj index 21df8f8..0b46df8 100644 --- a/src/duct/reitit/middleware.clj +++ b/src/duct/reitit/middleware.clj @@ -8,11 +8,11 @@ [duct.reitit.middleware.exception :as exception :refer [get-exception-middleware]])) ;; TODO: inject environment keys instead -(defm environment-middleware [{:keys [environment]} _ handler request] +(defm environment-middleware [opts _ handler request] (let [inject #(handler (into request %))] - (inject {:environment environment - :id (java.util.UUID/randomUUID) - :start-date (java.util.Date.)}))) + (inject (assoc (opts :environment) + :id (java.util.UUID/randomUUID) + :start-date (java.util.Date.))))) (defn- get-coercion-middleware [{:keys [pretty?] :as coercion}] (when coercion diff --git a/test/duct/reitit/middleware_test.clj b/test/duct/reitit/middleware_test.clj index fea1f51..a365a2a 100644 --- a/test/duct/reitit/middleware_test.clj +++ b/test/duct/reitit/middleware_test.clj @@ -36,9 +36,9 @@ (testing "Request Keys" (let [request (app {:request-method :get :uri "/identity"})] - (is (= [:environment ;; environment key [environment-middleware] - :form-params ;; .. + (is (= [:form-params ;; .. :id ;; Request id [environment-middleware] + :name ;; injected key [environment-middleware] :params ;; Merge of all types of params. NOTE: Doesn't seem accurate. :path-params ;; .. :query-params ;; .. @@ -49,10 +49,10 @@ :reitit.core/router] ;; Reitit Router (sort (keys request)))) (are [path expected-type] (= expected-type (type (get-in request path))) - [:id] UUID ;; Random UUID to the request injected by environment-middleware - [:start-date] Date ;; Start date injected by environment-middleware - [:environment :name] String ;; Environment map injected by environment-middleware - [:params] PersistentArrayMap))))) ;; A marge of all params types injected by parameters-middleware + [:id] UUID ;; Random UUID to the request injected by [environment-middleware] + [:start-date] Date ;; Start date injected by [environment-middleware] + [:name] String ;; injected key by [environment-middleware] + [:params] PersistentArrayMap))))) ;; A marge of all params types injected by [parameters-middleware] (defn is-int [str] (try (Integer/parseInt str) (catch Exception _ nil))) (deftest coercion-middleware-behavior @@ -84,7 +84,7 @@ (are [path expected-type] (= expected-type (type (get-in request path))) [:id] UUID ;; Random UUID to the request injected by environment-middleware [:start-date] Date ;; Start date injected by environment-middleware - [:environment :db] PersistentArrayMap ;; Environment map injected by environment-middleware + [:db] PersistentArrayMap ;; injected key by [environment-middleware] [:params] PersistentArrayMap))) (testing "Coercion Spec Response" diff --git a/test/foo/handler.clj b/test/foo/handler.clj index 73ffe97..b11adce 100644 --- a/test/foo/handler.clj +++ b/test/foo/handler.clj @@ -30,7 +30,7 @@ :uri (:uri r)}})}) (defmethod init-key :foo.handler/get-author [_ _] - (fn [{{:keys [db]} :environment}] + (fn [{:keys [db]}] {:status 200 :body (first db)}))