Fork me on GitHub
#code-reviews
<
2020-07-13
>
stopa19:07:47

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

dharrigan19:07:10

would this work?

dharrigan19:07:13

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

noisesmith19:07:17

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

dharrigan19:07:22

I do love me some Clojure expressiveness 🙂

noisesmith19:07:21

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

noisesmith19:07:49

it has the same conditional branching and delayed evaluation as if

stopa19:07:28

oof butiful. Thanks team!

seancorfield20:07:29

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

👍 6
seancorfield20:07:49

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

❤️ 3
stopa21:07:06

Bam updated, thanks Sean!