code-reviews

2020-07-13T19:07:47.161400Z

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 : )

dharrigan 2020-07-13T19:12:10.161600Z

would this work?

dharrigan 2020-07-13T19:12:13.161800Z

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

2020-07-13T19:17:17.163200Z

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

dharrigan 2020-07-13T19:17:56.163400Z

noice!

dharrigan 2020-07-13T19:18:22.163700Z

I do love me some Clojure expressiveness 🙂

2020-07-13T19:19:21.164100Z

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

2020-07-13T19:19:49.164500Z

it has the same conditional branching and delayed evaluation as if

2020-07-13T19:54:28.164700Z

oof butiful. Thanks team!

seancorfield 2020-07-13T20:13:29.165400Z

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
seancorfield 2020-07-13T20:13:49.165900Z

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

❤️ 1
2020-07-13T21:29:06.166400Z

Bam updated, thanks Sean!