From 5796df494e66dc8a17c60d6495e948112d485f2d Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Sat, 11 Apr 2020 20:39:35 -0400 Subject: [PATCH] Change default-options-handler to default-options-resource To make the options resource more customizable. --- modules/reitit-ring/src/reitit/ring.cljc | 27 ++++++++++++++---------- test/cljc/reitit/ring_test.cljc | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 81c72b94..2411be75 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -28,11 +28,11 @@ (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-resource] :as opts}] (let [[top childs] (group-keys data) childs (cond-> childs - (and (not (:options childs)) (not (:handler top)) default-options-handler) - (assoc :options {:no-doc true, :handler default-options-handler})) + (and (not (:options childs)) (not (:handler top)) default-options-resource) + (assoc :options default-options-resource)) ->endpoint (fn [p d m s] (-> (middleware/compile-result [p d] opts s) (map->Endpoint) @@ -65,6 +65,10 @@ ([request respond _] (respond (handle request)))))) +(def default-options-resource + {:no-doc true + :handler default-options-handler}) + ;; ;; public api ;; @@ -74,11 +78,11 @@ support for http-methods and Middleware. See documentation on [[reitit.core/router]] for available options. In addition, the following options are available: - | key | description - | ---------------------------------------|------------- - | `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity) - | `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware - | `:reitit.ring/default-options-handler` | Default handler for `:options` method in endpoints (default: default-options-handler) + | key | description + | ----------------------------------------|------------- + | `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity) + | `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware + | `:reitit.ring/default-options-resource` | Default resource for `:options` method in endpoints (default: default-options-resource) Example: @@ -91,9 +95,10 @@ ([data] (router data nil)) ([data opts] - (let [opts (merge {:coerce coerce-handler - :compile compile-result - ::default-options-handler default-options-handler} opts)] + (let [opts (merge {:coerce coerce-handler + :compile compile-result + ::default-options-resource default-options-resource} + opts)] (r/router data opts)))) (defn routes diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 7c1d3432..e07d9d7d 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -241,7 +241,7 @@ [["/get" {:get (constantly response)}] ["/options" {:options (constantly response)}] ["/any" (constantly response)]] - {::ring/default-options-handler nil}))] + {::ring/default-options-resource nil}))] (testing "endpoint with a non-options handler" (is (= response (app {:request-method :get, :uri "/get"})))