redux-resource-xhr
from npm:npm install redux-resource-xhr --save
crudRequest
action creator in your application:crudRequest
, and the library used for making the HTTP requests, xhr
.crudRequest( crudAction, options )
crudAction
: (String) The CRUD operation being performed. One of "create", "read", "update", or "delete". This determines the CRUD Action types that are dispatched.options
(Object): Options to configure the CRUD request.actionDefaults
: (Object) Properties that will be included on each dispatched action. All of the Request Action options are supported, such as resourceType
and resources
.dispatch
: (Function) The dispatch
function of a Redux store. If you're using redux-thunk
, this will be the first argument of the thunk.xhrOptions
: (Object) Options to pass to the xhr
library. You must pass an url
(or uri
) option. You will typically also want to pass json: true
, which will serialize your request body into JSON, as well as parse the response body as JSON. For more, see the examples below and the xhr documentation.transformData
]: (Function) An optional function to transform the data received by the server. It receives one argument, body
, which is the response from the server, parsed as JSON. Return a transformed list of resources
. This can be used to format the server response into a Redux Resource-compatible format. For more, see the guide on Resource objects.onPending
]: (Function) An optional function that allows you to modify the "pending" action, as well as control when it is dispatched. It is called with one argument: action
. When this function is provided, you will be responsible for dispatching the action.onAborted
]: (Function) An optional function that allows you to modify the "aborted" action, as well as control when it is dispatched. It is called with arguments (action, res)
. When this function is provided, you will be responsible for dispatching the action.onFailed
]: (Function) An optional function that allows you to modify the "failed" action, as well as control when it is dispatched. It is called with arguments (action, err, res)
. When this function is provided, you will be responsible for dispatching the action.onSucceeded
]: (Function) An optional function that allows you to modify the "succeeded" action, as well as control when it is dispatched. It is called with arguments (action, res, body)
. When this function is provided, you will be responsible for dispatching the action.transformData
instead of onSuceeded
.XMLHttpRequest
): An instance of a XMLHttpRequest
. Typically, you'll use this object to abort the request (should you need to) by calling myXhr.abort()
.xhr( options )
xhr
, and supports all of the same options and signatures.qs
object, then the object will be serialized into a query parameter using the querystringify
library. This library supports basic serialization, but we don't expect it to work for every API that you interface with.qsStringify
- a function with the signature (qs, options)
. It should return the string to be appended to the URI.qsStringifyOptions
- an object that is passed as the second argument to the qsStringify
method.qs
library, you might do this:onSucceeded
option of crudRequest
can be useful if your backend returns related resources in a single request.onSucceeded
and onFailed
options can also be used for chaining requests. You can make a second (or third, or fourth!) HTTP request in these callbacks. This is useful when you need to make multiple requests to get all of the data that your interface needs.readManyBooks({ pageNumber: 5 })
.