mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
doc: improve ring example in README.md
- format-response-middleware wasn't doing anything, use format-middleware instead as that's what the users usually want - add exception handling - document middleware stack a bit
This commit is contained in:
parent
524d0d4d57
commit
48fdb692ef
1 changed files with 29 additions and 22 deletions
47
README.md
47
README.md
|
|
@ -109,6 +109,7 @@ A Ring routing app with input & output coercion using [data-specs](https://githu
|
||||||
(require '[reitit.ring :as ring])
|
(require '[reitit.ring :as ring])
|
||||||
(require '[reitit.coercion.spec])
|
(require '[reitit.coercion.spec])
|
||||||
(require '[reitit.ring.coercion :as rrc])
|
(require '[reitit.ring.coercion :as rrc])
|
||||||
|
(require '[reitit.ring.middleware.exception :as exception])
|
||||||
(require '[reitit.ring.middleware.muuntaja :as muuntaja])
|
(require '[reitit.ring.middleware.muuntaja :as muuntaja])
|
||||||
(require '[reitit.ring.middleware.parameters :as parameters])
|
(require '[reitit.ring.middleware.parameters :as parameters])
|
||||||
|
|
||||||
|
|
@ -124,39 +125,45 @@ A Ring routing app with input & output coercion using [data-specs](https://githu
|
||||||
;; router data affecting all routes
|
;; router data affecting all routes
|
||||||
{:data {:coercion reitit.coercion.spec/coercion
|
{:data {:coercion reitit.coercion.spec/coercion
|
||||||
:muuntaja m/instance
|
:muuntaja m/instance
|
||||||
:middleware [parameters/parameters-middleware
|
:middleware [parameters/parameters-middleware ; decoding query & form params
|
||||||
|
muuntaja/format-middleware ; content negotiation
|
||||||
|
exception/exception-middleware ; converting exceptions to HTTP responses
|
||||||
rrc/coerce-request-middleware
|
rrc/coerce-request-middleware
|
||||||
muuntaja/format-response-middleware
|
|
||||||
rrc/coerce-response-middleware]}})))
|
rrc/coerce-response-middleware]}})))
|
||||||
```
|
```
|
||||||
|
|
||||||
Valid request:
|
Valid request:
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
(app {:request-method :get
|
(-> (app {:request-method :get
|
||||||
:uri "/api/math"
|
:uri "/api/math"
|
||||||
:query-params {:x "1", :y "2"}})
|
:query-params {:x "1", :y "2"}})
|
||||||
|
(update :body slurp))
|
||||||
; {:status 200
|
; {:status 200
|
||||||
; :body {:total 3}}
|
; :body "{\"total\":3}"
|
||||||
|
; :headers {"Content-Type" "application/json; charset=utf-8"}}
|
||||||
```
|
```
|
||||||
|
|
||||||
Invalid request:
|
Invalid request:
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
(app {:request-method :get
|
(-> (app {:request-method :get
|
||||||
:uri "/api/math"
|
:uri "/api/math"
|
||||||
:query-params {:x "1", :y "a"}})
|
:query-params {:x "1", :y "a"}})
|
||||||
;{:status 400,
|
(update :body jsonista.core/read-value))
|
||||||
; :body {:type :reitit.coercion/request-coercion,
|
; {:status 400
|
||||||
; :coercion :spec,
|
; :headers {"Content-Type" "application/json; charset=utf-8"}
|
||||||
; :spec "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:$spec20745/x :$spec20745/y]), :type :map, :keys #{:y :x}, :keys/req #{:y :x}})",
|
; :body {"spec" "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$8974/x :spec$8974/y]), :type :map, :leaf? false})"
|
||||||
; :problems [{:path [:y],
|
; "value" {"x" "1"
|
||||||
; :pred "clojure.core/int?",
|
; "y" "a"}
|
||||||
; :val "a",
|
; "problems" [{"via" ["spec$8974/y"]
|
||||||
; :via [:$spec20745/y],
|
; "path" ["y"]
|
||||||
; :in [:y]}],
|
; "pred" "clojure.core/int?"
|
||||||
; :value {:x "1", :y "a"},
|
; "in" ["y"]
|
||||||
; :in [:request :query-params]}}
|
; "val" "a"}]
|
||||||
|
; "type" "reitit.coercion/request-coercion"
|
||||||
|
; "coercion" "spec"
|
||||||
|
; "in" ["request" "query-params"]}}
|
||||||
```
|
```
|
||||||
|
|
||||||
## More examples
|
## More examples
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue