Fork me on GitHub
#reitit
<
2023-09-18
>
Ben Sless07:09:23

Maybe it needs to be extended to IFn or AFn instead of Fn?

robert-stuttaford07:09:28

that sounds like a better plan, yeah

danielneal12:09:22

If you’ve got some routes, are there any helpers to add standard responses + schemas e.g. 400 bad request, to all the routes?

danielneal12:09:51

I guess it’s just data so I can use normal clojure functions, just wondering if there’s some helpers

Marc Gannon12:09:49

I couldn't find any helpers so that's what I ended up doing (though I used specter's transform instead). This is what I did in case it is helpful for you

(defn add-bad-request-response
  "Adds bad-request error schema to response docs for routes that may have coercion errors"
  [[route-uri route-data]]
  [route-uri
   (transform [(submap [:post :put :patch]) MAP-VALS :responses]
              #(merge {bad-request {:body schemas/ErrorResponseSchema}} %)
              route-data)])

(defn routes
  [component]
  (let [flattened-base-routes (reitit/routes (ring/router (base-routes component)))]
    (map add-bad-request-response flattened-base-routes))

danielneal12:09:41

Nice, yep I’ll do something similar then, thanks so much for the comment!

Marc Gannon12:09:36

No problem 👍

Ben Sless15:09:38

In which cases do you want to respond with bad request?

Ben Sless15:09:33

For bad schema it can be done with instantiating your own exception middleware

danielneal15:09:45

This is really for the docs, but for authenticated routes, I want to add 401 and 403 as responses, and for all routes 400 as incorrect.

Ben Sless15:09:52

The last one is a default handler (optional argument) The first two can be done via middlewares probably, no need for transformation In which cases do you want to return those codes?

danielneal16:09:51

I’m generating the docs using the new openapi feature, so as far as I understand the data needs to be there, for the open api middleware to work. I’ve got things working with the data transformation now, but I might be doing things not quite right.