Skip to content

arthurbarroso/re-frame-cljs-http

Repository files navigation

A cljs-http effect handler for re-frame

This library contains a re-frame effect handler that wraps cljs-http. It is a "port" of re-frame-http-fx and was created because I couldn't use ajax in a project of mine and am a huge fan of re-frame's way of dealing with effects.

Clojars Project

Usage

I'd like to start by recommending reading re-frame-http-fx's README. There are few differences between the libraries' APIs. Reading cljs-http's docs might also help.

First, add this library to your project's dependencies. After doing so, require it in the namespace you plan on making http requests:

(ns my.events
    (:require [re-frame.core :as re-frame]
              [re-frame-cljs-http.http-fx]))

The above re-frame-cljs-http.http-fx require registers a re-frame effect handler called http-cljs, which you'll be able to use like below:

(re-frame/reg-event-db
  ::good-http-result
  (fn [db [_ result]]
    (assoc db :http-result result)))

(re-frame/reg-event-db
  ::bad-http-result
  (fn [db [_ result]]
    (assoc db :http-result result :errors? true)))

(reg-event-fx
  :handler-with-http
  (fn [{:keys [db]} _]
    {:db   (assoc db :loading? true)
     :http-cljs {:method :post
                 :format :json
                 :params {:a "param"}
                 :on-success      [::good-http-result]
                 :on-failure      [::bad-http-result]}
                 :url "https://httpbin.org/post"}))

The key differences from re-frame-http-fx you can notice are:

  • There is no support for :timeout
  • No need to specify :response-format - cljs-http always returns the same format (js->clj)
  • :format accepts a few parameters:
    • :json (which is the default behavior)
    • :edn
    • :transit
    • :form
  • re-frame-http-cljs uses :url instead of :uri

P.s.: It is also possible to pass http-cljs a request vector.