cljs-ajax
cljs-ajax copied to clipboard
Implement an AjaxImpl that uses js/Fetch
This hasn't been fully investigated but a) it would future proof cljs-ajax and b) it would solve the ReactNative problem. Cross-reference #141
I can confirm that using fetch works fine on RN, so this would be a good approach
Here's a snippet that may serve as a good starting point for a fetch backend for cljs-ajax:
(-> (js/fetch uri (clj->js m))
(.then (fn [r]
(if (.-ok r)
;; good
(let [content-type (-> r .-headers (.get "content-type"))]
(if (clojure.string/includes? content-type "application/transit+json")
(.text r)
(throw (ex-info "Content-type is not transit" {:content-type content-type}))))
;; bad
(.then (.text r)
(fn [text]
(let [payload (transit/read transit-reader text)
err {:status (.-status r)
:status-text (.-statusText r)
:failure :error
:response payload}]
(throw err)))))))
(.then (fn [text]
(transit/read transit-reader text)))
(.then handler #(error-handler (if (map? %) % {:message "Fetch promise failed" :reason %})))
;; in case of a handler error, break out of the try block
;; to throw an actual exception
(.catch (fn [err] (js/setTimeout #(throw err)))))
As you can see, I struggle with throwing regular exception out of a promise-based api. The problem, essentially, is that .then and .catch methods are all wrapped in try blocks. So if we just call the handler in a .then method, exceptions in that functions will be swallowed, or reported as network request errors, neither of which is particularly desirable.
As a workaround I use setTimeout in the code, but this may not be an optimal solution.
Also the transit-related code is probably not applicable as cljs-ajax does that by itseslf.
@pesterhazy are you still working on this, or anyone else? If not, I'd be interested in working on it. Thanks!
Alex, go for it! :+1:
On Apr 21, 2017 15:15, "Alex Wheeler" [email protected] wrote:
@pesterhazy https://github.com/pesterhazy are you still working on this, or anyone else? If not, I'd be interested in working on it. Thanks!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JulianBirch/cljs-ajax/issues/155#issuecomment-296187315, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGfWGuBsLMBKF4rocwovYcDOTsGGpDqks5ryKvcgaJpZM4Jbf6l .
Any news @AlexWheeler?
Was it harder than it looked or did things just get busy [no guilt implied]?
@raymcdermott I had a lot of trouble gettin cljs working with fetch and definitely just got busy. I know a lot of work has been done on cljs since so would be cool to spike on it again, but definitely take a stab at it if ya have time!
Yeah, I closed it because, after two years, there didn't seem to be a lot of demand and, in all honesty, I'm not convinced it really serves any purpose. Would still accept, though.
Does wrapping a polyfill like this one help? https://github.com/github/fetch
I’m afraid not. The challenge is to make cljs-ajax use the fetch API, not to simulate the fetch API.
On Fri, 18 Jan 2019 at 18:06, Jonathan Chen [email protected] wrote:
Does wrapping a polyfill like this one help? https://github.com/github/fetch
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JulianBirch/cljs-ajax/issues/155#issuecomment-455636254, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFIk1nySR3-Be7A_3JNOmq2_C9kOknEks5vEg0ygaJpZM4Jbf6l .
-- Sent from an iPhone, please excuse brevity and typos.
Would be great if this works. @JulianBirch, I'd love to give it a try. Can you tell me anything that will complete my job faster? Don't know where to start. This implements js/Fetch: https://github.com/superstructor/re-frame-fetch-fx
Any news on this? Will we see cljs-ajax using Fetch API anytime soon?
This could be merged in: https://github.com/lambdaisland/fetch