|
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 | + |
92 | 98 | (defn wrap-inject-data |
93 | 99 | "Injects data into the request." |
94 | 100 | [handler data] |
|
118 | 124 | (def no-response-coercion |
119 | 125 | (constantly (dissoc default-coercion-matchers :response))) |
120 | 126 |
|
| 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 | + |
121 | 134 | ;; |
122 | 135 | ;; Muuntaja |
123 | 136 | ;; |
|
206 | 219 | ;; |
207 | 220 |
|
208 | 221 | (def api-middleware-defaults |
209 | | - {:formats ::default |
| 222 | + {::api-middleware-defaults true |
| 223 | + :formats ::default |
210 | 224 | :exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler |
211 | 225 | ::ex/request-validation ex/request-validation-handler |
212 | 226 | ::ex/request-parsing ex/request-parsing-handler |
|
244 | 258 |
|
245 | 259 | ### Options |
246 | 260 |
|
| 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 | +
|
247 | 270 | - **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it) |
248 | 271 | - **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data. |
249 | 272 |
|
250 | 273 | - **:formats** for Muuntaja middleware. Value can be a valid muuntaja options-map, |
251 | 274 | a Muuntaja instance or nil (to unmount it). See |
252 | | - https://github.com/metosin/muuntaja/wiki/Configuration for details. |
| 275 | + https://github.com/metosin/muuntaja/blob/master/doc/Configuration.md for details. |
253 | 276 |
|
254 | 277 | - **:middleware** vector of extra middleware to be applied last (just before the handler). |
255 | 278 |
|
|
266 | 289 | you might want to take look at using wrap-components |
267 | 290 | middleware manually.). Defaults to nil (middleware not mounted)." |
268 | 291 | ([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 | + {})) |
269 | 298 | (api-middleware handler api-middleware-defaults)) |
270 | 299 | ([handler options] |
271 | | - (let [options (api-middleware-options 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) |
272 | 327 | {:keys [exceptions components formats middleware ring-swagger coercion]} options |
273 | 328 | muuntaja (create-muuntaja formats)] |
274 | 329 |
|
|
0 commit comments