mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
rename package
This commit is contained in:
parent
5baa793810
commit
708fa24590
7 changed files with 51 additions and 50 deletions
13
README.md
13
README.md
|
|
@ -64,21 +64,22 @@ A Ring routing app with input & output coercion using [data-specs](https://githu
|
||||||
```clj
|
```clj
|
||||||
(require '[reitit.ring :as ring])
|
(require '[reitit.ring :as ring])
|
||||||
(require '[reitit.coercion.spec])
|
(require '[reitit.coercion.spec])
|
||||||
(require '[reitit.ring.coercion-middleware :as mw])
|
(require '[reitit.ring.coercion :as rrc])
|
||||||
|
|
||||||
(def app
|
(def app
|
||||||
(ring/ring-handler
|
(ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/api"
|
["/api"
|
||||||
["/math" {:get {:coercion reitit.coercion.spec/coercion
|
["/math" {:get {:parameters {:query {:x int?, :y int?}}
|
||||||
:parameters {:query {:x int?, :y int?}}
|
|
||||||
:responses {200 {:schema {:total pos-int?}}}
|
:responses {200 {:schema {:total pos-int?}}}
|
||||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:total (+ x y)}})}}]]
|
:body {:total (+ x y)}})}}]]
|
||||||
{:data {:middleware [mw/coerce-exceptions-middleware
|
;; router data effecting all routes
|
||||||
mw/coerce-request-middleware
|
{:data {:coercion reitit.coercion.spec/coercion
|
||||||
mw/coerce-response-middleware]}})))
|
:middleware [rrc/coerce-exceptions-middleware
|
||||||
|
rrc/coerce-request-middleware
|
||||||
|
rrc/coerce-response-middleware]}})))
|
||||||
```
|
```
|
||||||
|
|
||||||
Valid request:
|
Valid request:
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ The coerced parameters can be read under `:parameters` key in the request.
|
||||||
|
|
||||||
## Coercion Middleware
|
## Coercion Middleware
|
||||||
|
|
||||||
Defining a coercion for a route data doesn't do anything, as it's just data. We have to attach some code to apply the actual coercion. We can use the middleware from `reitit.ring.coercion-middleware`:
|
Defining a coercion for a route data doesn't do anything, as it's just data. We have to attach some code to apply the actual coercion. We can use the middleware from `reitit.ring.coercion`:
|
||||||
|
|
||||||
* `coerce-request-middleware` for the parameter coercion
|
* `coerce-request-middleware` for the parameter coercion
|
||||||
* `coerce-response-middleware` for the response coercion
|
* `coerce-response-middleware` for the response coercion
|
||||||
|
|
@ -57,7 +57,7 @@ Defining a coercion for a route data doesn't do anything, as it's just data. We
|
||||||
Here's an full example for applying coercion with Reitit, Ring and Schema:
|
Here's an full example for applying coercion with Reitit, Ring and Schema:
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
(require '[reitit.ring.coercion-middleware :as mw])
|
(require '[reitit.ring.coercion :as rrc])
|
||||||
(require '[reitit.coercion.schema])
|
(require '[reitit.coercion.schema])
|
||||||
(require '[reitit.ring :as ring])
|
(require '[reitit.ring :as ring])
|
||||||
(require '[schema.core :as s])
|
(require '[schema.core :as s])
|
||||||
|
|
@ -84,9 +84,9 @@ Here's an full example for applying coercion with Reitit, Ring and Schema:
|
||||||
(-> parameters :path :z))]
|
(-> parameters :path :z))]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:total total}}))}}]]
|
:body {:total total}}))}}]]
|
||||||
{:data {:middleware [mw/coerce-exceptions-middleware
|
{:data {:middleware [rrc/coerce-exceptions-middleware
|
||||||
mw/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
mw/coerce-response-middleware]}})))
|
rrc/coerce-response-middleware]}})))
|
||||||
```
|
```
|
||||||
|
|
||||||
Valid request:
|
Valid request:
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(ns example.server
|
(ns example.server
|
||||||
(:require [ring.adapter.jetty :as jetty]
|
(:require [ring.adapter.jetty :as jetty]
|
||||||
[reitit.middleware :as middleware]
|
[reitit.middleware :as middleware]
|
||||||
[reitit.ring.coercion-middleware :as coercion-middleware]))
|
[reitit.ring.coercion :as rrc]))
|
||||||
|
|
||||||
(defonce ^:private server (atom nil))
|
(defonce ^:private server (atom nil))
|
||||||
|
|
||||||
|
|
@ -10,9 +10,9 @@
|
||||||
;; to be set with :extract-request-format and extract-response-format
|
;; to be set with :extract-request-format and extract-response-format
|
||||||
(defn wrap-coercion [handler resource]
|
(defn wrap-coercion [handler resource]
|
||||||
(middleware/chain
|
(middleware/chain
|
||||||
[coercion-middleware/coerce-request-middleware
|
[rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware
|
rrc/coerce-response-middleware
|
||||||
coercion-middleware/coerce-exceptions-middleware]
|
rrc/coerce-exceptions-middleware]
|
||||||
handler
|
handler
|
||||||
resource))
|
resource))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
[ring.middleware.params]
|
[ring.middleware.params]
|
||||||
[muuntaja.middleware]
|
[muuntaja.middleware]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.ring.coercion-middleware :as coercion-middleware]
|
[reitit.ring.coercion :as rrc]
|
||||||
[example.dspec]
|
[example.dspec]
|
||||||
[example.schema]
|
[example.schema]
|
||||||
[example.spec]))
|
[example.spec]))
|
||||||
|
|
@ -18,9 +18,9 @@
|
||||||
example.spec/routes]
|
example.spec/routes]
|
||||||
{:data {:middleware [ring.middleware.params/wrap-params
|
{:data {:middleware [ring.middleware.params/wrap-params
|
||||||
muuntaja.middleware/wrap-format
|
muuntaja.middleware/wrap-format
|
||||||
coercion-middleware/coerce-exceptions-middleware
|
rrc/coerce-exceptions-middleware
|
||||||
coercion-middleware/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]}})))
|
rrc/coerce-response-middleware]}})))
|
||||||
|
|
||||||
(defn restart []
|
(defn restart []
|
||||||
(swap! server (fn [x]
|
(swap! server (fn [x]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
(ns reitit.ring.coercion-middleware
|
(ns reitit.ring.coercion
|
||||||
(:require [reitit.coercion :as coercion]
|
(:require [reitit.coercion :as coercion]
|
||||||
[reitit.impl :as impl]))
|
[reitit.impl :as impl]))
|
||||||
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
[muuntaja.core :as m]
|
[muuntaja.core :as m]
|
||||||
[muuntaja.format.jsonista :as jsonista-format]
|
[muuntaja.format.jsonista :as jsonista-format]
|
||||||
[jsonista.core :as j]
|
[jsonista.core :as j]
|
||||||
[reitit.ring.coercion-middleware :as coercion-middleware]
|
[reitit.ring.coercion :as rrc]
|
||||||
[reitit.coercion.spec :as spec]
|
[reitit.coercion.spec :as spec]
|
||||||
[reitit.coercion.schema :as schema]
|
[reitit.coercion.schema :as schema]
|
||||||
[reitit.coercion :as coercion]
|
[reitit.coercion :as coercion]
|
||||||
|
|
@ -36,14 +36,14 @@
|
||||||
(s/def ::k (s/keys :req-un [::x ::y]))
|
(s/def ::k (s/keys :req-un [::x ::y]))
|
||||||
|
|
||||||
(let [spec (spec/into-spec {:x int?, :y int?} ::jeah)
|
(let [spec (spec/into-spec {:x int?, :y int?} ::jeah)
|
||||||
coercers (#'coercion-middleware/request-coercers spec/coercion {:body spec})
|
coercers (#'rrc/request-coercers spec/coercion {:body spec})
|
||||||
params {:x "1", :y "2"}
|
params {:x "1", :y "2"}
|
||||||
request {:body-params {:x "1", :y "2"}}]
|
request {:body-params {:x "1", :y "2"}}]
|
||||||
|
|
||||||
;; 4600ns
|
;; 4600ns
|
||||||
(bench!
|
(bench!
|
||||||
"coerce-parameters"
|
"coerce-parameters"
|
||||||
(#'coercion-middleware/coerce-request-middleware coercers request))
|
(#'rrc/coerce-request-middleware coercers request))
|
||||||
|
|
||||||
;; 2700ns
|
;; 2700ns
|
||||||
(bench!
|
(bench!
|
||||||
|
|
@ -102,24 +102,24 @@
|
||||||
app (ring/ring-handler
|
app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
routes
|
routes
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware]
|
{:data {:middleware [rrc/coerce-request-middleware]
|
||||||
:coercion coercion}}))
|
:coercion coercion}}))
|
||||||
app2 (ring/ring-handler
|
app2 (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
routes
|
routes
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware]
|
{:data {:middleware [rrc/coerce-request-middleware]
|
||||||
:coercion coercion}}))
|
:coercion coercion}}))
|
||||||
app3 (ring/ring-handler
|
app3 (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
routes
|
routes
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/wrap-coerce-response]
|
rrc/wrap-coerce-response]
|
||||||
:coercion coercion}}))
|
:coercion coercion}}))
|
||||||
app4 (ring/ring-handler
|
app4 (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
routes
|
routes
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion coercion}}))
|
:coercion coercion}}))
|
||||||
req {:request-method :get
|
req {:request-method :get
|
||||||
:uri "/api/ping"
|
:uri "/api/ping"
|
||||||
|
|
@ -156,8 +156,8 @@
|
||||||
:get {:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
:get {:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body {:total (+ x y)}})}}]]
|
:body {:total (+ x y)}})}}]]
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion spec/coercion}})))
|
:coercion spec/coercion}})))
|
||||||
|
|
||||||
(app
|
(app
|
||||||
|
|
@ -205,8 +205,8 @@
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
||||||
{:data {:middleware [[mm/wrap-format m]
|
{:data {:middleware [[mm/wrap-format m]
|
||||||
coercion-middleware/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion schema/coercion}}))
|
:coercion schema/coercion}}))
|
||||||
request {:request-method :post
|
request {:request-method :post
|
||||||
:uri "/plus"
|
:uri "/plus"
|
||||||
|
|
@ -229,8 +229,8 @@
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion schema/coercion}}))
|
:coercion schema/coercion}}))
|
||||||
request {:request-method :post
|
request {:request-method :post
|
||||||
:uri "/plus"
|
:uri "/plus"
|
||||||
|
|
@ -253,8 +253,8 @@
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion spec/coercion}}))
|
:coercion spec/coercion}}))
|
||||||
request {:request-method :post
|
request {:request-method :post
|
||||||
:uri "/plus"
|
:uri "/plus"
|
||||||
|
|
@ -282,8 +282,8 @@
|
||||||
:handler (fn [request]
|
:handler (fn [request]
|
||||||
(let [body (-> request :parameters :body)]
|
(let [body (-> request :parameters :body)]
|
||||||
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
{:status 200, :body {:result (+ (:x body) (:y body))}}))}}]
|
||||||
{:data {:middleware [coercion-middleware/coerce-request-middleware
|
{:data {:middleware [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware]
|
rrc/coerce-response-middleware]
|
||||||
:coercion spec/coercion}}))
|
:coercion spec/coercion}}))
|
||||||
request {:request-method :post
|
request {:request-method :post
|
||||||
:uri "/plus"
|
:uri "/plus"
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
(:require [clojure.test :refer [deftest testing is]]
|
(:require [clojure.test :refer [deftest testing is]]
|
||||||
[schema.core :as s]
|
[schema.core :as s]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.ring.coercion-middleware :as coercion-middleware]
|
[reitit.ring.coercion :as rrc]
|
||||||
[reitit.coercion.spec :as spec]
|
[reitit.coercion.spec :as spec]
|
||||||
[reitit.coercion.schema :as schema])
|
[reitit.coercion.schema :as schema])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
|
|
@ -53,8 +53,8 @@
|
||||||
:coercion spec/coercion}})))]
|
:coercion spec/coercion}})))]
|
||||||
|
|
||||||
(testing "withut exception handling"
|
(testing "withut exception handling"
|
||||||
(let [app (create [coercion-middleware/coerce-request-middleware
|
(let [app (create [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware])]
|
rrc/coerce-response-middleware])]
|
||||||
|
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
|
|
@ -74,9 +74,9 @@
|
||||||
(app invalid-request2))))))
|
(app invalid-request2))))))
|
||||||
|
|
||||||
(testing "with exception handling"
|
(testing "with exception handling"
|
||||||
(let [app (create [coercion-middleware/coerce-exceptions-middleware
|
(let [app (create [rrc/coerce-exceptions-middleware
|
||||||
coercion-middleware/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware])]
|
rrc/coerce-response-middleware])]
|
||||||
|
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
|
|
@ -108,8 +108,8 @@
|
||||||
:coercion schema/coercion}})))]
|
:coercion schema/coercion}})))]
|
||||||
|
|
||||||
(testing "withut exception handling"
|
(testing "withut exception handling"
|
||||||
(let [app (create [coercion-middleware/coerce-request-middleware
|
(let [app (create [rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware])]
|
rrc/coerce-response-middleware])]
|
||||||
|
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
|
|
@ -129,9 +129,9 @@
|
||||||
(app invalid-request2))))
|
(app invalid-request2))))
|
||||||
|
|
||||||
(testing "with exception handling"
|
(testing "with exception handling"
|
||||||
(let [app (create [coercion-middleware/coerce-exceptions-middleware
|
(let [app (create [rrc/coerce-exceptions-middleware
|
||||||
coercion-middleware/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
coercion-middleware/coerce-response-middleware])]
|
rrc/coerce-response-middleware])]
|
||||||
|
|
||||||
(testing "all good"
|
(testing "all good"
|
||||||
(is (= {:status 200
|
(is (= {:status 200
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue