metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.43k stars 257 forks source link

Parameters middleware #134

Open ikitommi opened 6 years ago

ikitommi commented 6 years ago

leftover from #85 - currently wrap-params is required from Ring, could be optimized and packaged into reitit-middleware. the body-params could be pushed into Muuntaja (see https://github.com/metosin/muuntaja/issues/27).

Also, query-params could support the :rails format: https://github.com/JulianBirch/cljs-ajax#get-specific-settings

ikitommi commented 6 years ago

initial version done.

green-coder commented 5 years ago

It is possible to tag issues in commit messages (for example "related to #134"), Github will automatically add a link to the commit here.

valerauko commented 5 years ago

I can help finish this if you linked the commit of that "initial version" you mention

ikitommi commented 5 years ago

There is https://github.com/metosin/reitit/blob/master/modules/reitit-middleware/src/reitit/ring/middleware/parameters.clj, which is the current ring impl, I don't think there is anything else around. This issue is quite big, there could be 3 subissues:

devurandom commented 3 years ago

I currently handle form encoded requests like this:

(require '[reitit.coercion.spec :as rcs])
(require '[reitit.ring :as reitit-ring])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.ring.middleware.parameters :as parameters])

(reitit-ring/ring-handler
  (reitit-ring/router
    [["//some/path" {:post {:swagger {:consumes ["application/x-www-form-urlencoded"]
                                      :produces ["text/plain"]}
                            :parameters {:form {:text string?}}
                            :responses {200 {:body string?}}
                            :handler my-handler
                            :coercion rcs/coercion
                            :middleware [parameters/parameters-middleware
                                         rrc/coerce-exceptions-middleware
                                         rrc/coerce-request-middleware
                                         rrc/coerce-response-middleware]}}]]))

I am dealing with a caller to my API that does not appear to be very strict when it comes to this, so I would like to stick to the documentation they provide instead of doing auto negotiation. Thus I would like to keep the current behaviour and assume a form-encoded request and force a plain-text response, independently of the presence of content-type and accept headers.

How would I do this with the new Muuntaja-based form-encoded body handling?

devurandom commented 3 years ago

https://github.com/metosin/reitit/pull/381 seems related to the goal summarised in https://cljdoc.org/d/metosin/reitit/0.5.15/doc/ring/default-middleware#parameters-handling:

reitit.ring.middleware.parameters/parameters-middleware [...] will be factored into two parts: a query-parameters middleware and a Muuntaja format responsible for the the application/x-www-form-urlencoded body format.

devurandom commented 3 years ago

To make this issue more discoverable, I propose to link to it from the documentation in https://github.com/metosin/reitit/pull/510.