:reitit.ring/default-options-endpoint & :reitit.http/default-options-endpoint

This commit is contained in:
Tommi Reiman 2020-05-12 21:21:39 +03:00
parent d594b6a65d
commit d36c47d7b1
5 changed files with 36 additions and 18 deletions

View file

@ -48,6 +48,15 @@ is called the first time, so that `rfe/push-state` and such can be called
* `reitit.ring/routes` strips away `nil` routes, fixes [#394](https://github.com/metosin/reitit/issues/394)
* `reitit.ring/create-file-handler` to serve files from filesystem, fixes [#395](https://github.com/metosin/reitit/issues/395)
* **BREAKING**: router option `:reitit.ring/default-options-handler` is deprecated
* fails with router creation time error
* use `:reitit.ring/default-options-endpoint` instead, takes an expandable route data instead just of a handler.
### `reitit-http`
* **BREAKING**: router option `:reitit.http/default-options-handler` is deprecated
* fails with router creation time error
* use `:reitit.http/default-options-endpoint` instead, takes an expandable route data instead just of a handler.
### `reitit-spec`

View file

@ -14,11 +14,11 @@ Read more about the [Ring Concepts](https://github.com/ring-clojure/ring/wiki/Co
It accepts the following options:
| key | description |
| ---------------------------------------|-------------|
| `:reitit.middleware/transform` | Function of `[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 of `[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-endpoint` | Default endpoint for `:options` method (default: default-options-endpoint)
Example router:

View file

@ -1,6 +1,7 @@
(ns reitit.http
(:require [meta-merge.core :refer [meta-merge]]
[reitit.interceptor :as interceptor]
[reitit.exception :as ex]
[reitit.ring :as ring]
[reitit.core :as r]))
@ -13,11 +14,11 @@
(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-endpoint expand] :as opts}]
(let [[top childs] (ring/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-endpoint)
(assoc :options (expand default-options-endpoint opts)))
compile (fn [[path data] opts scope]
(interceptor/compile-result [path data] opts scope))
->endpoint (fn [p d m s]
@ -47,11 +48,11 @@
support for http-methods and Interceptors. See documentation on [[reitit.core/router]]
for available options. In addition, the following options are available:
| key | description
| ---------------------------------------|-------------
| `:reitit.interceptor/transform` | Function or vector of functions of type `[Interceptor] => [Interceptor]` to transform the expanded Interceptors (default: identity)
| `:reitit.interceptor/registry` | Map of `keyword => IntoInterceptor` to replace keyword references into Interceptors
| `:reitit.http/default-options-handler` | Default handler for `:options` method in endpoints (default: reitit.ring/default-options-handler)
| key | description
| ----------------------------------------|-------------
| `:reitit.interceptor/transform` | Function or vector of functions of type `[Interceptor] => [Interceptor]` to transform the expanded Interceptors (default: identity)
| `:reitit.interceptor/registry` | Map of `keyword => IntoInterceptor` to replace keyword references into Interceptors
| `:reitit.http/default-options-endpoint` | Default endpoint for `:options` method in endpoints (default: reitit.ring/default-options-endpoint)
Example:
@ -66,7 +67,10 @@
([data opts]
(let [opts (merge {:coerce coerce-handler
:compile compile-result
::default-options-handler ring/default-options-handler} opts)]
::default-options-endpoint ring/default-options-endpoint} opts)]
(when (contains? opts ::default-options-handler)
(ex/fail! (str "Option :reitit.http/default-options-handler is deprecated."
" Use :reitit.http/default-options-endpoint instead.")))
(r/router data opts))))
(defn routing-interceptor

View file

@ -1,12 +1,12 @@
(ns reitit.ring
(:require [meta-merge.core :refer [meta-merge]]
[reitit.middleware :as middleware]
[reitit.exception :as ex]
[reitit.core :as r]
[reitit.impl :as impl]
#?@(:clj [[ring.util.mime-type :as mime-type]
[ring.util.response :as response]])
[clojure.string :as str]
[reitit.exception :as ex]))
[clojure.string :as str]))
(declare get-match)
(declare get-router)

View file

@ -6,7 +6,8 @@
[reitit.interceptor.sieppari :as sieppari]
[reitit.http :as http]
[reitit.ring :as ring]
[reitit.core :as r]))
[reitit.core :as r])
(:import (clojure.lang ExceptionInfo)))
(defn interceptor [name]
{:enter (fn [ctx] (update-in ctx [:request ::i] (fnil conj []) name))})
@ -169,6 +170,10 @@
(testing "handler rejects"
(is (= -406 (:status (app {:request-method :get, :uri "/pong"}))))))))))
(deftest default-options-handler-test
(testing "Assertion fails when using deprecated options-handler"
(is (thrown? ExceptionInfo (ring/router [] {::ring/default-options-handler (fn [_])})))))
(deftest default-options-handler-test
(let [response {:status 200, :body "ok"}]
@ -199,7 +204,7 @@
[["/get" {:get (constantly response)}]
["/options" {:options (constantly response)}]
["/any" (constantly response)]]
{::http/default-options-handler nil})
{::http/default-options-endpoint nil})
{:executor sieppari/executor})]
(testing "endpoint with a non-options handler"