This namespace contains utilities to do AJAX requests in a reacl-c application, based on cljs-ajax
.
At a glance, you first define requests with GET
, POST
and PUT
.
GET requests are usually there to get some data from a server and
display it to the user. Typically you want to use fetch
or a
variant of it to do that, or the lower level execute
. For
example:
(c/fragment (fetch (GET "/resource"))
(c/dynamic pr-str))
POST and PUT requests are typically sent in response to an action by
the user. The delivery
item and the deliver
action are made
to help with that:
(delivery
(dom/button
{:onclick #(c/return :action (deliver (PUT "/resource" {:body v})))}))
For details on the various options of the request constructors, see
the documentation
of cljs-ajax
.
Also see reacl-c-basics.ajax-test-util
for an easy way to
simulate server responses in unit tests.
This namespace contains utilities to do AJAX requests in a reacl-c application, based on `cljs-ajax`. At a glance, you first define requests with [[GET]], [[POST]] and [[PUT]]. GET requests are usually there to get some data from a server and display it to the user. Typically you want to use [[fetch]] or a variant of it to do that, or the lower level [[execute]]. For example: ``` (c/fragment (fetch (GET "/resource")) (c/dynamic pr-str)) ``` POST and PUT requests are typically sent in response to an action by the user. The [[delivery]] item and the [[deliver]] action are made to help with that: ``` (delivery (dom/button {:onclick #(c/return :action (deliver (PUT "/resource" {:body v})))})) ``` For details on the various options of the request constructors, see the documentation of [`cljs-ajax`](https://github.com/JulianBirch/cljs-ajax#getpostput). Also see [[reacl-c-basics.ajax-test-util]] for an easy way to simulate server responses in unit tests.
(DELETE uri & [options])
Returns a DELETE request.
Returns a DELETE request.
(deliver req & [info])
Returns an action to add the given request to the end of the next
queue delivery
up in the item tree. An arbitrary info
value can be attached, identifying or describing the request.
Returns an action to add the given request to the end of the next queue [[delivery]] up in the item tree. An arbitrary `info` value can be attached, identifying or describing the request.
(delivery item)
(delivery item options)
(delivery item transition manage)
Returns an item that manages the execution of Ajax requests in its
local state. Use deliver
to get an action that can be emitted by
item
that adds a new delivery job to an internal queue.
:transition
option:
Jobs transition from a :pending state,
over :running into :completed (successful or not), and for each
transition (transition state job)
is evaluated on the returned
item's state, which must return a [[reacl-c.core/return]] value.
:manage
option:
Some control over the queue can be achieved by specifying manage
which is called on the queue after it changed and must return an
updated queue. Newest items are at the end of the queue.
:execute
option:
The item function to actually execute the requests can also be
explicitly set; it defaults to execute
.
Note: (delivery item transition manage)
is deprecated.
Returns an item that manages the execution of Ajax requests in its local state. Use [[deliver]] to get an action that can be emitted by `item` that adds a new delivery job to an internal queue. `:transition` option: Jobs transition from a :pending state, over :running into :completed (successful or not), and for each transition `(transition state job)` is evaluated on the returned item's state, which must return a [[reacl-c.core/return]] value. `:manage` option: Some control over the queue can be achieved by specifying `manage` which is called on the queue after it changed and must return an updated queue. Newest items are at the end of the queue. `:execute` option: The item function to actually execute the requests can also be explicitly set; it defaults to [[execute]]. Note: `(delivery item transition manage)` is deprecated.
(delivery-job-info job)
Returns the info object passed to deliver
.
Returns the info object passed to [[deliver]].
(delivery-job-response job)
Returns the response for this job, or nil if the status is not :completed
.
Returns the response for this job, or nil if the status is not `:completed`.
(delivery-job-status job)
Returns the status of this job, :pending
, :running
or :completed
.
Returns the status of this job, `:pending`, `:running` or `:completed`.
(delivery-job? value)
Returns whether the given value is a delivery job.
Returns whether the given value is a delivery job.
(error-response status & [body])
Returns a response that is considered a failure, with a map with :status
and :body
keys as its response-value
.
Returns a response that is considered a failure, with a map with `:status` and `:body` keys as its [[response-value]].
(execute request)
A subscription that will execute the given request and deliver the response as soon as it is available.
A subscription that will execute the given request and deliver the response as soon as it is available.
(execute! request)
A subscription that will execute the given
request. If you want access to the response, use execute
.
A subscription that will execute the given request. If you want access to the response, use [[execute]].
(fetch req)
Returns an invisible item, that will execute the given request whenever its state is or becomes nil, and set its state to the success or error response as soon as it is available.
Returns an invisible item, that will execute the given request whenever its state is or becomes nil, and set its state to the success or error response as soon as it is available.
(fetch-once req f)
Returns an invisible item, that will execute the given Ajax
request once, when mounted. When the request completes with an error
or success, then (f state response)
is evaluated, which must
return a [[reacl-c.core/return]] value.
Returns an invisible item, that will execute the given Ajax request once, when mounted. When the request completes with an error or success, then `(f state response)` is evaluated, which must return a [[reacl-c.core/return]] value.
(fetch-when req cond)
Returns an invisible item, that will execute the given request once
if cond
is true, and also whenever cond
changes from false to
true. Updates its state to the response after each request
completed (successful or not).
Returns an invisible item, that will execute the given request once if `cond` is true, and also whenever `cond` changes from false to true. Updates its state to the response after each request completed (successful or not).
(fetch-when+state req cond)
Returns an invisible item, that will execute the given request once
if cond
is true, and also whenever cond
changes from false to
true. Updates its state to a tuple of the response and a loading flag:
true
when a request is started, but hasn't completed yetfalse
when a request is completed.Initialize the state to false or nil.
Returns an invisible item, that will execute the given request once if `cond` is true, and also whenever `cond` changes from false to true. Updates its state to a tuple of the response and a loading flag: - `true` when a request is started, but hasn't completed yet - `false` when a request is completed. Initialize the state to false or nil.
(map-ok-response req f & args)
Convert the response-value
of every successfull responses to the given request through a
call to (f response & args)
.
Convert the [[response-value]] of every successfull responses to the given request through a call to `(f response & args)`.
(map-response req f & args)
Convert every response to the given request through a call to (f response & args)
.
Convert every response to the given request through a call to `(f response & args)`.
(ok-response result)
Returns a response that is considered a success with the given value as its response-value
.
Returns a response that is considered a success with the given value as its [[response-value]].
(OPTIONS uri & [options])
Returns a OPTIONS request.
Returns a OPTIONS request.
(PATCH uri & [options])
Returns a PATCH request.
Returns a PATCH request.
(PURGE uri & [options])
Returns a PURGE request.
Returns a PURGE request.
(request? value)
Returns whether the given value is a request.
Returns whether the given value is a request.
(response-ok? response)
Returns if the given response represents success, i.e. the status code was in the 2xx range.
Returns if the given response represents success, i.e. the status code was in the 2xx range.
(response-value response)
Returns the (parsed) response body of the given response if ok, or a map with :status
and :body
keys if not ok.
Returns the (parsed) response body of the given response if ok, or a map with `:status` and `:body` keys if not ok.
(response? value)
Returns whether the given value is a response.
Returns whether the given value is a response.
(TRACE uri & [options])
Returns a TRACE request.
Returns a TRACE request.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close