mirror of
https://github.com/metosin/reitit.git
synced 2026-01-06 23:49:50 +00:00
reitit.ring.middleware => reitit.middleware
This commit is contained in:
parent
164473cc75
commit
22bbe38b8c
8 changed files with 43 additions and 43 deletions
|
|
@ -49,7 +49,7 @@ To demonstrate the two approaches, below are response coercion middleware writte
|
|||
* Mounts only if `:coercion` and `:responses` are defined for the route
|
||||
|
||||
```clj
|
||||
(require '[reitit.ring.middleware :as middleware])
|
||||
(require '[reitit.middleware :as middleware])
|
||||
|
||||
(def coerce-response-middleware
|
||||
"Middleware for pluggable response coercion.
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ Reitit defines middleware as data:
|
|||
|
||||
## Middleware as data
|
||||
|
||||
All values in the `:middleware` vector in the route data are coerced into `reitit.ring.middleware/Middleware` Records with using the `reitit.ring.middleware/IntoMiddleware` Protocol. By default, functions, maps and `Middleware` records are allowed.
|
||||
All values in the `:middleware` vector in the route data are coerced into `reitit.middleware/Middleware` Records with using the `reitit.middleware/IntoMiddleware` Protocol. By default, functions, maps and `Middleware` records are allowed.
|
||||
|
||||
Records can have arbitrary keys, but the following keys have a special purpose:
|
||||
|
||||
|
|
@ -19,7 +19,7 @@ Records can have arbitrary keys, but the following keys have a special purpose:
|
|||
| ---------------|-------------|
|
||||
| `:name` | Name of the middleware as a qualified keyword (optional)
|
||||
| `:wrap` | The actual middleware function of `handler & args => request => response`
|
||||
| `:compile` | Middleware function generation function, see [compiling middleware](compiling_middleware.md).
|
||||
| `:compile` | Middleware compilation function, see [compiling middleware](compiling_middleware.md).
|
||||
|
||||
Middleware Records are accessible in their raw form in the compiled route results, thus available for inventories, creating api-docs etc.
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ The following produce identical middleware runtime function.
|
|||
### Record
|
||||
|
||||
```clj
|
||||
(require '[reitit.ring.middleware :as middleware])
|
||||
(require '[reitit.middleware :as middleware])
|
||||
|
||||
(def wrap2
|
||||
(middleware/create
|
||||
|
|
@ -89,7 +89,7 @@ Middleware can be optimized against an endpoint using [middleware compilation](c
|
|||
|
||||
## Transforming the middleware chain
|
||||
|
||||
There is an extra option in ring-router (actually, in the undelaying middleware-router): `:reitit.ring.middleware/transform` to transform the middleware chain per endpoint. It sees the vector of compiled middleware and should return a new vector of middleware.
|
||||
There is an extra option in ring-router (actually, in the undelaying middleware-router): `:reitit.middleware/transform` to transform the middleware chain per endpoint. It sees the vector of compiled middleware and should return a new vector of middleware.
|
||||
|
||||
#### Adding debug middleware between all other middleware
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
(ns example.server
|
||||
(:require [ring.adapter.jetty :as jetty]
|
||||
[reitit.ring.middleware :as middleware]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.ring.coercion :as coercion]))
|
||||
|
||||
(defonce ^:private server (atom nil))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
(ns reitit.ring.middleware
|
||||
(ns reitit.middleware
|
||||
(:require [meta-merge.core :refer [meta-merge]]
|
||||
[reitit.core :as r]
|
||||
[reitit.impl :as impl]))
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
(ns reitit.ring
|
||||
(:require [meta-merge.core :refer [meta-merge]]
|
||||
[reitit.ring.middleware :as middleware]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.core :as r]
|
||||
[reitit.impl :as impl]))
|
||||
|
||||
|
|
@ -82,7 +82,7 @@
|
|||
:delete {:middleware [wrap-delete]
|
||||
:handler delete-user}}]])
|
||||
|
||||
See router options from [[reitit.core/router]] and [[reitit.ring.middleware/router]]."
|
||||
See router options from [[reitit.core/router]] and [[reitit.middleware/router]]."
|
||||
([data]
|
||||
(router data nil))
|
||||
([data opts]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(ns reitit.ring.coercion
|
||||
(:require [clojure.walk :as walk]
|
||||
[spec-tools.core :as st]
|
||||
[reitit.ring.middleware :as middleware]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.ring.coercion.protocol :as protocol]
|
||||
[reitit.ring :as ring]
|
||||
[reitit.impl :as impl]))
|
||||
|
|
@ -126,16 +126,16 @@
|
|||
(middleware/create
|
||||
{:name ::coerce-parameters
|
||||
:compile (fn [{:keys [coercion parameters]} opts]
|
||||
(if (and coercion parameters)
|
||||
(let [coercers (request-coercers coercion parameters opts)]
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(let [coerced (coerce-request coercers request)]
|
||||
(handler (impl/fast-assoc request :parameters coerced))))
|
||||
([request respond raise]
|
||||
(let [coerced (coerce-request coercers request)]
|
||||
(handler (impl/fast-assoc request :parameters coerced) respond raise))))))))}))
|
||||
(if (and coercion parameters)
|
||||
(let [coercers (request-coercers coercion parameters opts)]
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(let [coerced (coerce-request coercers request)]
|
||||
(handler (impl/fast-assoc request :parameters coerced))))
|
||||
([request respond raise]
|
||||
(let [coerced (coerce-request coercers request)]
|
||||
(handler (impl/fast-assoc request :parameters coerced) respond raise))))))))}))
|
||||
|
||||
(def coerce-response-middleware
|
||||
"Middleware for pluggable response coercion.
|
||||
|
|
@ -144,14 +144,14 @@
|
|||
(middleware/create
|
||||
{:name ::coerce-response
|
||||
:compile (fn [{:keys [coercion responses]} opts]
|
||||
(if (and coercion responses)
|
||||
(let [coercers (response-coercers coercion responses opts)]
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(coerce-response coercers request (handler request)))
|
||||
([request respond raise]
|
||||
(handler request #(respond (coerce-response coercers request %)) raise)))))))}))
|
||||
(if (and coercion responses)
|
||||
(let [coercers (response-coercers coercion responses opts)]
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(coerce-response coercers request (handler request)))
|
||||
([request respond raise]
|
||||
(handler request #(respond (coerce-response coercers request %)) raise)))))))}))
|
||||
|
||||
(def coerce-exceptions-middleware
|
||||
"Middleware for handling coercion exceptions.
|
||||
|
|
@ -160,16 +160,16 @@
|
|||
(middleware/create
|
||||
{:name ::coerce-exceptions
|
||||
:compile (fn [{:keys [coercion parameters responses]} _]
|
||||
(if (and coercion (or parameters responses))
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(try
|
||||
(handler request)
|
||||
(catch #?(:clj Exception :cljs js/Error) e
|
||||
(handle-coercion-exception e identity #(throw %)))))
|
||||
([request respond raise]
|
||||
(try
|
||||
(handler request respond #(handle-coercion-exception % respond raise))
|
||||
(catch #?(:clj Exception :cljs js/Error) e
|
||||
(handle-coercion-exception e respond raise))))))))}))
|
||||
(if (and coercion (or parameters responses))
|
||||
(fn [handler]
|
||||
(fn
|
||||
([request]
|
||||
(try
|
||||
(handler request)
|
||||
(catch #?(:clj Exception :cljs js/Error) e
|
||||
(handle-coercion-exception e identity #(throw %)))))
|
||||
([request respond raise]
|
||||
(try
|
||||
(handler request respond #(handle-coercion-exception % respond raise))
|
||||
(catch #?(:clj Exception :cljs js/Error) e
|
||||
(handle-coercion-exception e respond raise))))))))}))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
(ns reitit.middleware-test
|
||||
(:require [clojure.test :refer [deftest testing is are]]
|
||||
[reitit.ring.middleware :as middleware]
|
||||
[reitit.middleware :as middleware]
|
||||
[clojure.set :as set]
|
||||
[reitit.core :as r])
|
||||
#?(:clj
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(ns reitit.ring-test
|
||||
(:require [clojure.test :refer [deftest testing is]]
|
||||
[clojure.set :as set]
|
||||
[reitit.ring.middleware :as middleware]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.ring :as ring]
|
||||
[reitit.core :as r])
|
||||
#?(:clj
|
||||
|
|
|
|||
Loading…
Reference in a new issue