code-reviews 2020-07-13

I wrote a quick wrap-cors middleware:

(defn wrap-cors [handler]
  (fn [request]
    (let [res (handler request)]
      (if-not res
        res
        (-> res
            (assoc-in [:headers "Access-Control-Allow-Origin"] "*")
            (assoc-in [:headers "Access-Control-Allow-Headers"] "*"))))))
am a bit unhappy with the if-not -- if you think there's a more concise way I can express this would love thoughts : )

would this work?

(defn wrap-cors [handler]
  (fn [request]
    (some-> (handler request)
            (assoc-in [:headers "Access-Control-Allow-Origin"] "*")
            (assoc-in [:headers "Access-Control-Allow-Headers"] "*"))))

also, the double assoc in could just be an update-in (update-in [:headers] assoc "Access-Control-Allow-Origin" "**" "Access-Control-Allow-Headers" "**")

I do love me some Clojure expressiveness 🙂

another way to replace (if-not x x ...) is (and x ...)

it has the same conditional branching and delayed evaluation as if

oof butiful. Thanks team!

FYI @stopachka There's an existing middleware library for that https://github.com/r0man/ring-cors which handles the preflight checking (which your code does not).

👍 2

(this library is what we use at work for production CORS checking)

❤️ 1

Bam updated, thanks Sean!