Skip to content

Commit

Permalink
Add :http/req fx handler
Browse files Browse the repository at this point in the history
This should unify the shape of the map passed to both on-success and
on-failure handlers for all types of situations. Headers are now also
provided. As it is a breaking change it is introduced as a new fx
handler.

Fixes day8#7
  • Loading branch information
superstructor committed May 15, 2019
1 parent b71e118 commit 9cf73bc
Showing 1 changed file with 75 additions and 7 deletions.
82 changes: 75 additions & 7 deletions src/day8/re_frame/http_fx.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,75 @@
[re-frame.core :refer [reg-fx dispatch console]]
[ajax.core :as ajax]))

(defn ajax-xhrio-handler-v2
"ajax-request only provides a single handler for success and errors"
[on-success on-failure xhrio [success? response]]
; see http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
(let [headers (js->clj (.getResponseHeaders xhrio) :keywordize-keys true)

details (cond->
{:uri (.getLastUri xhrio)
:last-method (.-lastMethod_ xhrio)
:headers headers
:status (.getStatus xhrio)
:status-text (.getStatusText xhrio)}

(and success?
(= :parse (:failure response)))
;; Successful response with a body parse error.
;; WARNING The map returned for this case is pretty broken.
;; For example, the :status-text contains the :parse-error
;; when it should contain the actual :status-text! Thus we
;; treat this case with some special care.
(merge
{:parse-error {:failure :parse
:status-text (:status-text response)
:original-text (:original-text response)}})

;; Successful response with a parsable body.
success?
(merge {:response response})

:failure
(merge
{:failure (:failure response)
:debug-message (-> xhrio .getLastErrorCode
(errors/getDebugMessage))}
(if (contains? response :parse-error)
;; Failure response with a body parse error.
{:parse-error (select-keys (:parse-error response)
[:failure :status-text :original-text])}
;; Failure response with a parsable body.
{:response (:response response)})))]
(if success?
(on-success details)
(on-failure details))))

(defn request->xhrio-options-v2
[{:as request
:keys [on-success on-failure]
:or {on-success [:http-no-on-success]
on-failure [:http-no-on-failure]}}]
; wrap events in cljs-ajax callback
(let [api (new js/goog.net.XhrIo)]
(-> request
(assoc
:api api
:handler (partial ajax-xhrio-handler-v2
#(dispatch (conj on-success %))
#(dispatch (conj on-failure %))
api))
(dissoc :on-success :on-failure))))

(defn http-effect-v2
[request]
(let [seq-request-maps (if (sequential? request) request [request])]
(doseq [request seq-request-maps]
(-> request request->xhrio-options-v2 ajax/ajax-request))))

(reg-fx :http/req http-effect-v2)


;; I provide the :http-xhrio effect handler leveraging cljs-ajax lib
;; see API docs https://github.com/JulianBirch/cljs-ajax
;; Note we use the ajax-request.
Expand All @@ -19,7 +88,7 @@
;; [:success-event "my-token" result]


(defn ajax-xhrio-handler
(defn ajax-xhrio-handler-v1
"ajax-request only provides a single handler for success and errors"
[on-success on-failure xhrio [success? response]]
; see http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
Expand All @@ -34,8 +103,7 @@
response)]
(on-failure details))))


(defn request->xhrio-options
(defn request->xhrio-options-v1
[{:as request
:keys [on-success on-failure]
:or {on-success [:http-no-on-success]
Expand All @@ -45,17 +113,17 @@
(-> request
(assoc
:api api
:handler (partial ajax-xhrio-handler
:handler (partial ajax-xhrio-handler-v1
#(dispatch (conj on-success %))
#(dispatch (conj on-failure %))
api))
(dissoc :on-success :on-failure))))

(defn http-effect
(defn http-effect-v1
[request]
(console :warn "re-frame-http-fx: \":http-xhrio\" fx is deprecated. Use \":http/req\".")
(let [seq-request-maps (if (sequential? request) request [request])]
(doseq [request seq-request-maps]
(-> request request->xhrio-options ajax/ajax-request))))
(-> request request->xhrio-options-v1 ajax/ajax-request))))

(reg-fx :http-xhrio http-effect)
(reg-fx :http-xhrio http-effect-v1)

0 comments on commit 9cf73bc

Please sign in to comment.