[cljs-rest "1.1.0"]
A ClojureScript REST client, suitable for AJAX interaction with RESTful APIs.
(ns my-ns
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [<!]]
[cljs-rest :as rest]))
(def entries
(rest/resource-listing "https://api.whatever.org/entries/"))
(def entry-1
(rest/resource "https://api.whatever.org/entries/1/"))
(go
(<! (rest/options entries))
;; #cljs-rest.core.ResourceOptions{:success true :data {...}}
(<! (rest/get entries))
;; #cljs-rest.core.ResourceListing{:success true :resources [] ,,,}
(<! (rest/get entry-1))
;; #cljs-rest.core.Resource{:success false :status 404 ,,,}
(<! (rest/post! entries {:title "Foo" :body "Lorem"}))
;; #cljs-rest.core.Resource{:success true :data {:url "https://api.whatever.org/entries/1/" :title "Foo"} ...}
(<! (rest/first-resource entries))
;; #cljs-rest.core.Resource{:success true :data {...}}
;; And so on...
(<! (rest/head entries))
(<! (rest/head entry-1))
(<! (rest/put! entry-1 {:title "Bar" :body "Ipsum"}))
(<! (rest/patch! entry-1 {:body "Consectetur"}))
(<! (rest/delete! entry-1)))
If your REST server returns Link
headers for pagination, those headers are automatically parsed:
(go
(let [resources (<! (rest/get entries {:per_page 10 :page 3}))]
(get-in resources [:headers :link])
;; {:prev {:url "https://api.whatever.org/entries/?per_page=10&page=2"
;; :params {:per_page "10" :page "2"}}
;; :next {:url "https://api.whatever.org/entries/?per_page=10&page=4"
;; :params {:per_page "10" :page "4"}}}
))
Options are accepted from an :opts
keyword argument. Refer to cljs-http for options documentation.
Usage:
(rest/resource-listing url :opts opts)
(rest/resource url :opts opts)
When provided, a :constructor
keyword argument will be used to construct Resource
data.
Usage:
(defrecord Foo [])
(rest/resource-listing url :constructor map->Foo)
(rest/resource url :constructor map->Foo)
async->
The async->
macro, for conveniently performing sequential async calls, is similar to clojure.core/->
, but the return value is a core async channel, and each step may return a channel as well.
Example usage:
(async->
some-listing
rest/get
:resources
first
(rest/put! {:foo "bar"}))
If any step in the async sequence returns an instance of Error
, that will be the final value and subsequent steps will not be called. Example:
(defn response-error [listing]
(if (:success listing)
listing
(ex-info "Request Error" listing)))
(async->
some-listing
rest/get
response-error
;; If the request fails, the `ExceptionInfo` returned will be the final value,
;; and none of the following calls will be made:
:resources
first
whatever)
:link
header is parsedsuccess
status
headers
body
headers
includes both string keys (cljs-http default) as well as keywordsconfigure-format!
ResourceListing
provides a first-item
convenience methodResource
instances would drop :url
ResourceListing
with a :constructor
specified would corrupt resultsasync->
returning an instance of Error
will short-circuit the chain:error-handler
in cljs-rest.core/*opts*
will be called for error responsesFormData
payloads are automatically sent as multipart/form-data
Copyright (c) 2016, ReUp Distribution Inc All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close