|
187 | 187 | (map middleware-fn) |
188 | 188 | (apply comp identity))) |
189 | 189 |
|
| 190 | +;; |
| 191 | +;; ring-middleware-format stuff |
| 192 | +;; |
| 193 | + |
| 194 | +(def ^:private default-mime-types |
| 195 | + {:json "application/json" |
| 196 | + :json-kw "application/json" |
| 197 | + :edn "application/edn" |
| 198 | + :clojure "application/clojure" |
| 199 | + :yaml "application/x-yaml" |
| 200 | + :yaml-kw "application/x-yaml" |
| 201 | + :yaml-in-html "text/html" |
| 202 | + :transit-json "application/transit+json" |
| 203 | + :transit-msgpack "application/transit+msgpack"}) |
| 204 | + |
| 205 | +(defn mime-types |
| 206 | + [format] |
| 207 | + (get default-mime-types format |
| 208 | + (some-> format :content-type))) |
| 209 | + |
| 210 | +(def ^:private response-only-mimes #{:clojure :yaml-in-html}) |
| 211 | + |
| 212 | +(defn ->mime-types [formats] (keep mime-types formats)) |
| 213 | + |
190 | 214 | ;; |
191 | 215 | ;; swagger-data |
192 | 216 | ;; |
|
216 | 240 | ;; Api Middleware |
217 | 241 | ;; |
218 | 242 |
|
219 | | -(def api-middleware-defaults |
220 | | - {::api-middleware-defaults true |
221 | | - :formats ::default |
| 243 | +(def api-middleware-defaults-v1 |
| 244 | + {:format {:formats [:json-kw :yaml-kw :edn :transit-json :transit-msgpack] |
| 245 | + :params-opts {} |
| 246 | + :response-opts {}} |
| 247 | + :exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler |
| 248 | + ::ex/request-validation ex/request-validation-handler |
| 249 | + ::ex/request-parsing ex/request-parsing-handler |
| 250 | + ::ex/response-validation ex/response-validation-handler |
| 251 | + ::ex/default ex/safe-handler}} |
| 252 | + :coercion (constantly default-coercion-matchers) |
| 253 | + :ring-swagger nil}) |
| 254 | + |
| 255 | +(def api-middleware-defaults-v2 |
| 256 | + {:formats ::default |
222 | 257 | :exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler |
223 | 258 | ::ex/request-validation ex/request-validation-handler |
224 | 259 | ::ex/request-parsing ex/request-parsing-handler |
|
228 | 263 | :coercion coercion/default-coercion |
229 | 264 | :ring-swagger nil}) |
230 | 265 |
|
231 | | -(defn api-middleware-options [options] |
232 | | - (rsc/deep-merge api-middleware-defaults options)) |
| 266 | +(defn api-middleware-options-v1 [options] |
| 267 | + (rsc/deep-merge api-middleware-defaults-v1 options)) |
| 268 | + |
| 269 | +(defn api-middleware-options-v2 [options] |
| 270 | + (rsc/deep-merge api-middleware-defaults-v2 options)) |
233 | 271 |
|
234 | 272 | ;; TODO: test all options! (https://github.com/metosin/compojure-api/issues/137) |
235 | 273 | (defn api-middleware |
|
256 | 294 |
|
257 | 295 | ### Options |
258 | 296 |
|
259 | | - - **:formatter** either :ring-middleware-format or :muuntaja. |
260 | | - During 2.x pre-releases, this will be a required key, unless |
261 | | - :formats is provided, which is equivalent to setting to :muuntaja. |
262 | | - Stable 2.x releases will default to :ring-middleware-format if |
263 | | - not provided or :format is set, unless :formats is provided, |
264 | | - which is equivalent to setting to :muuntaja. |
265 | | - Stable 2.x will print a deprecation warning if implicitly |
266 | | - or explicitly set to :ring-middleware-format. |
267 | | -
|
268 | 297 | - **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it) |
269 | 298 | - **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data. |
270 | 299 |
|
|
286 | 315 | :components restructuring. (If you are using api, |
287 | 316 | you might want to take look at using wrap-components |
288 | 317 | middleware manually.). Defaults to nil (middleware not mounted)." |
289 | | - ([handler] |
290 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
291 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
292 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be \n" |
293 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
294 | | - "`:formats` (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.") |
295 | | - {})) |
296 | | - (api-middleware handler api-middleware-defaults)) |
| 318 | + ([handler] (api-middleware handler api-middleware-defaults-v2)) |
297 | 319 | ([handler options] |
298 | | - (when (and (::api-middleware-defaults options) |
299 | | - (not (:formatter options)) |
300 | | - (not (System/getProperty "compojure.api.middleware.global-default-formatter"))) |
301 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
302 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
303 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n" |
304 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
305 | | - ":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n" |
306 | | - "To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja") |
307 | | - {}))) |
308 | | - (let [formatter (or (:formatter options) |
309 | | - (when (or (contains? options :formats) |
310 | | - (= (System/getProperty "compojure.api.middleware.global-default-formatter") |
311 | | - ":muuntaja")) |
312 | | - :muuntaja) |
313 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
314 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
315 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n" |
316 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
317 | | - ":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n" |
318 | | - "To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja") |
319 | | - {})) |
320 | | - ;; TODO 2.x stable |
321 | | - :ring-middleware-format) |
322 | | - _ (assert (= :muuntaja formatter) |
323 | | - (str "Invalid :formatter: " (pr-str formatter) ". Must be :muuntaja.")) |
324 | | - options (api-middleware-options options) |
| 320 | + (let [options (if (:format options) |
| 321 | + (assert (not (:format options))) |
| 322 | + (api-middleware-options-v2 options)) |
325 | 323 | {:keys [exceptions components formats middleware ring-swagger coercion]} options |
326 | 324 | muuntaja (create-muuntaja formats)] |
327 | 325 |
|
|
0 commit comments