Skip to content

Commit

Permalink
feat(middleware): inject keys instead of injecting environment
Browse files Browse the repository at this point in the history
This was originally the intended behavior. but it was ignored in last
release.
  • Loading branch information
kkharji committed Jan 3, 2022
1 parent 8331954 commit d8d1d2f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/duct/reitit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions src/duct/reitit/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions test/duct/reitit/middleware_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;; ..
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/foo/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)}))


0 comments on commit d8d1d2f

Please sign in to comment.