|
89 | 89 | ;; Options |
90 | 90 | ;; |
91 | 91 |
|
92 | | -;; 1.1.x |
93 | | -(defn get-options |
94 | | - "Extracts compojure-api options from the request." |
95 | | - [request] |
96 | | - (::options request)) |
97 | | - |
98 | 92 | (defn wrap-inject-data |
99 | 93 | "Injects data into the request." |
100 | 94 | [handler data] |
|
124 | 118 | (def no-response-coercion |
125 | 119 | (constantly (dissoc default-coercion-matchers :response))) |
126 | 120 |
|
127 | | -(defn coercion-matchers [request] |
128 | | - (let [options (get-options request)] |
129 | | - (if (contains? options :coercion) |
130 | | - (if-let [provider (:coercion options)] |
131 | | - (provider request)) |
132 | | - default-coercion-matchers))) |
133 | | - |
134 | 121 | ;; |
135 | 122 | ;; Muuntaja |
136 | 123 | ;; |
|
219 | 206 | ;; |
220 | 207 |
|
221 | 208 | (def api-middleware-defaults |
222 | | - {::api-middleware-defaults true |
223 | | - :formats ::default |
| 209 | + {:formats ::default |
224 | 210 | :exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler |
225 | 211 | ::ex/request-validation ex/request-validation-handler |
226 | 212 | ::ex/request-parsing ex/request-parsing-handler |
|
258 | 244 |
|
259 | 245 | ### Options |
260 | 246 |
|
261 | | - - **:formatter** either :ring-middleware-format or :muuntaja. |
262 | | - During 2.x pre-releases, this will be a required key, unless |
263 | | - :formats is provided, which is equivalent to setting to :muuntaja. |
264 | | - Stable 2.x releases will default to :ring-middleware-format if |
265 | | - not provided or :format is set, unless :formats is provided, |
266 | | - which is equivalent to setting to :muuntaja. |
267 | | - Stable 2.x will print a deprecation warning if implicitly |
268 | | - or explicitly set to :ring-middleware-format. |
269 | | -
|
270 | 247 | - **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it) |
271 | 248 | - **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data. |
272 | 249 |
|
273 | 250 | - **:formats** for Muuntaja middleware. Value can be a valid muuntaja options-map, |
274 | 251 | a Muuntaja instance or nil (to unmount it). See |
275 | | - https://github.com/metosin/muuntaja/blob/master/doc/Configuration.md for details. |
| 252 | + https://github.com/metosin/muuntaja/wiki/Configuration for details. |
276 | 253 |
|
277 | 254 | - **:middleware** vector of extra middleware to be applied last (just before the handler). |
278 | 255 |
|
|
289 | 266 | you might want to take look at using wrap-components |
290 | 267 | middleware manually.). Defaults to nil (middleware not mounted)." |
291 | 268 | ([handler] |
292 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
293 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
294 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be \n" |
295 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
296 | | - "`:formats` (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.") |
297 | | - {})) |
298 | 269 | (api-middleware handler api-middleware-defaults)) |
299 | 270 | ([handler options] |
300 | | - (when (and (::api-middleware-defaults options) |
301 | | - (not (:formatter options)) |
302 | | - (not (System/getProperty "compojure.api.middleware.global-default-formatter"))) |
303 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
304 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
305 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n" |
306 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
307 | | - ":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n" |
308 | | - "To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja") |
309 | | - {}))) |
310 | | - (let [formatter (or (:formatter options) |
311 | | - (when (or (contains? options :formats) |
312 | | - (= (System/getProperty "compojure.api.middleware.global-default-formatter") |
313 | | - ":muuntaja")) |
314 | | - :muuntaja) |
315 | | - (throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n" |
316 | | - "e.g., (api-middleware <handler> {:formatter :muuntaja})\n" |
317 | | - "To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n" |
318 | | - "explicitly chosen if not configured by `:format` (ring-middleware-format) or\n" |
319 | | - ":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n" |
320 | | - "To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja") |
321 | | - {})) |
322 | | - ;; TODO 2.x stable |
323 | | - :ring-middleware-format) |
324 | | - _ (assert (= :muuntaja formatter) |
325 | | - (str "Invalid :formatter: " (pr-str formatter) ". Must be :muuntaja.")) |
326 | | - options (api-middleware-options options) |
| 271 | + (let [options (api-middleware-options options) |
327 | 272 | {:keys [exceptions components formats middleware ring-swagger coercion]} options |
328 | 273 | muuntaja (create-muuntaja formats)] |
329 | 274 |
|
|
0 commit comments