Fork me on GitHub
#reitit
<
2018-03-17
>
ikitommi12:03:17

Initial swagger-support landed on master, as a separate module reitit/swagger. schema is still WIP, spec works with both clj & cljs. Body-formatting middleware/interceptor is this only difference between JVM & node -targets.

ikitommi12:03:07

If Muuntaja would target node too, the examples would look identical. Maybe it should…

ikitommi12:03:24

(require '[reitit.ring :as ring])
(require '[reitit.swagger :as swagger])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.coercion.spec :as spec])
(require '[reitit.coercion.schema :as schema])

(require '[schema.core :refer [Int]])
(require '[muuntaja.middleware])

(ring/ring-handler
  (ring/router
    ["/api"
     {:swagger {:id ::math}}

     ["/swagger.json"
      {:get {:no-doc true
             :swagger {:info {:title "my-api"}}
             :handler swagger/swagger-spec-handler}}]

     ["/spec" {:coercion spec/coercion}
      ["/plus"
       {:get {:summary "plus"
              :parameters {:query {:x int?, :y int?}}
              :responses {200 {:body {:total int?}}}
              :handler (fn [{{{:keys [x y]} :query} :parameters}]
                         {:status 200, :body {:total (+ x y)}})}}]]

     ["/schema" {:coercion schema/coercion}
      ["/plus"
       {:get {:summary "plus"
              :parameters {:query {:x Int, :y Int}}
              :responses {200 {:body {:total Int}}}
              :handler (fn [{{{:keys [x y]} :query} :parameters}]
                         {:status 200, :body {:total (+ x y)}})}}]]]

    {:data {:middleware [muuntaja.middleware/wrap-format
                         swagger/swagger-feature
                         rrc/coerce-exceptions-middleware
                         rrc/coerce-request-middleware
                         rrc/coerce-response-middleware]}}))