Home

Awesome

lambdaisland/fetch

<!-- badges -->

cljdoc badge Clojars Project

<!-- /badges -->

ClojureScript wrapper around the JavaScript fetch API.

(require '[lambdaisland.fetch :as fetch])

(fetch/get "/foo.json")
#<promise
  {:status 200
   :headers {...}
   :body #js {...}}>

(fetch/post "/foo.transit" {:query-params {:foo "123"}
                            :body {:hello "world"}})
#<promise
  {:status 200
   :headers {...}
   :body #js {...}}>

A more typical ClojureScript example

(ns fetch.demo.core
  (:require [kitchen-async.promise :as p]
            [lambdaisland.fetch :as fetch]))

(p/try
  (p/let [resp (fetch/get
                "https://api.github.com/users/seisvelas/gists"
                {:accept :json
                 :content-type :json})]
    (prn (:body resp)))
  (p/catch :default e
     ;; log your exception here
    (prn :error e)))

An example of using fetch at the REPL


(p/let [res (fetch ...)]
  (def res res))

After that you have your response map in res and you can inspect it to see what is in there.

EDN support is opt-in, since it can increase your build size, and is not typically needed or wanted for a production setup. Require lambdaisland.fetch.edn to enable it.

Options

* = default

Examples

A simple JSON get request.

(require '[lambdaisland.fetch :as fetch])
(require '[goog.object :as gobj])

(-> (fetch/get "https://reqres.in/api/users/2")
    (.then (fn [resp]
             (-> resp
                 :body
                 ;; the actual response is a js object, not a clojure map
                 (gobj/get "data"))))
    (.then (fn [data]
             (js/console.log (gobj/get data "id")))))

Same example as above but using kitchen-async:

(require '[lambdaisland.fetch :as fetch])
(require '[kitchen-async.promise :as p])
(require '[goog.object :as gobj])

(p/let [resp (fetch/get "https://reqres.in/api/users/2")
        data (-> resp
                 :body
                 (gobj/get "data"))]
  (js/console.log (gobj/get data "id")))
<!-- opencollective -->

Lambda Island Open Source

<img align="left" src="https://github.com/lambdaisland/open-source/raw/master/artwork/lighthouse_readme.png">

 

fetch is part of a growing collection of quality Clojure libraries created and maintained by the fine folks at Gaiwan.

Pay it forward by becoming a backer on our Open Collective, so that we may continue to enjoy a thriving Clojure ecosystem.

You can find an overview of our projects at lambdaisland/open-source.

 

 

<!-- /opencollective --> <!-- contributing -->

Contributing

Everyone has a right to submit patches to fetch, and thus become a contributor.

Contributors MUST

Contributors SHOULD

If you submit a pull request that adheres to these rules, then it will almost certainly be merged immediately. However some things may require more consideration. If you add new dependencies, or significantly increase the API surface, then we need to decide if these changes are in line with the project's goals. In this case you can start by writing a pitch, and collecting feedback on it.

* This goes for features too, a feature needs to solve a problem. State the problem it solves, then supply a minimal solution.

** As long as this project has not seen a public release (i.e. is not on Clojars) we may still consider making breaking changes, if there is consensus that the changes are justified.

<!-- /contributing --> <!-- license-mpl -->

License

Copyright © 2020-2021 Arne Brasseur and Contributors

Licensed under the term of the Mozilla Public License 2.0, see LICENSE.

<!-- /license-mpl -->