Fork me on GitHub
#reitit
<
2018-07-03
>
yogthos01:07:29

@ikitommi yeah I ended up going with middleware outside reitit, but I agree it would be nice to have first class support for that at some point. Would something like the following make sense for the syntax?

["/restricted/media"
    {:post
     {:summary "add a media attachment"
      :parameters {:multipart {:file TempFileUpload}}
      :handler (fn [{{ {{:keys [tempfile filename content-type]} :file} :multipart} :parameters}]               
                       (attachments/upload-media! {:user-id (:user-id identity)} file))}}]
the TempFileUpload would be the schema/spec definition for the multipart param same as in compojure-api, and then the :file param would contain the standard Ring multipart map with the :tempfile, :filename, and :content-type keys

👍 4
yogthos01:07:44

this would be consistent with the existing syntax for other types of parameters

roklenarcic07:07:26

When I use data-spec to specify parameters, if I say {:id int?} it is somehow converted into something that conforms. Is there a list of predicate symbols that get converted to their spec-tools coercing form automagically?

ikitommi07:07:23

@roklenarcic you should use coercion middleware to compile coercers with ring, should be described in the docs. the compile-request-coercers doesn't understand the endpoints (`:get`, :post etc).

ikitommi07:07:08

the data-spec... in reitit.coercion.spec, there is a IntoSpec protocol, which converts maps into data-specs. With data-specs, all map keys and values are wrapped into spec-tools.core.Spec, which does automatic type-resolution for predicates. Most core predicates are supported oob, and it's extendable. Here's the list: https://github.com/metosin/spec-tools/blob/master/src/spec_tools/parse.cljc

ikitommi08:07:41

the values can be also normal specs, just don't support runtime conforming - yet.

roklenarcic08:07:31

Oh I have coercion middleware, I just wasn't aware that it did compilation of coercions without explicit :compile in the map.

roklenarcic08:07:35

(defn create-router [router-data]
  (rr/router router-data
    {:data {:coercion reitit.coercion.spec/coercion
            :middleware [[mm/wrap-format muuntaja]
                         rrc/coerce-exceptions-middleware
                         rrc/coerce-request-middleware
                         rrc/coerce-response-middleware]}
     }))

roklenarcic08:07:34

re: muuntaja, if I add the middleware to a specific path in my route data, then the middleware gets added at the end of middleware stack, which doesn't work well for muuntaja. Is there some way to add middleware at the start of middleware stack?

ikitommi09:07:44

@roklenarcic would :middleware ^:prepend [wrap-format] work? Reitit uses meta-merge under the hoods (https://github.com/weavejester/meta-merge/blob/master/README.md#metadata)

👍 4