cljs-ajax icon indicating copy to clipboard operation
cljs-ajax copied to clipboard

Implement an AjaxImpl that uses js/Fetch

Open JulianBirch opened this issue 9 years ago • 11 comments

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

JulianBirch avatar Aug 03 '16 10:08 JulianBirch

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 avatar Aug 03 '16 14:08 pesterhazy

@pesterhazy are you still working on this, or anyone else? If not, I'd be interested in working on it. Thanks!

AlexWheeler avatar Apr 21 '17 13:04 AlexWheeler

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 .

pesterhazy avatar Apr 21 '17 23:04 pesterhazy

Any news @AlexWheeler?

Was it harder than it looked or did things just get busy [no guilt implied]?

raymcdermott avatar Apr 24 '18 19:04 raymcdermott

@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!

AlexWheeler avatar Apr 24 '18 20:04 AlexWheeler

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.

JulianBirch avatar Apr 30 '18 21:04 JulianBirch

Does wrapping a polyfill like this one help? https://github.com/github/fetch

dijonkitchen avatar Jan 18 '19 18:01 dijonkitchen

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.

JulianBirch avatar Jan 19 '19 09:01 JulianBirch

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

zendevil avatar Jul 01 '20 09:07 zendevil

Any news on this? Will we see cljs-ajax using Fetch API anytime soon?

Jarzka avatar Jun 29 '22 08:06 Jarzka

This could be merged in: https://github.com/lambdaisland/fetch

loganpowell avatar Sep 22 '22 00:09 loganpowell