redux-saga-requests-fetch
Fetch API driver to Redux-Saga - addon to simplify handling of AJAX requests.
Installation
To install the package, just run:
$ yarn add isomorphic-fetch redux-saga-requests-fetch
or...
$ npm install isomorphic-fetch redux-saga-requests-fetch
or you can just use CDN: https://unpkg.com/redux-saga-requests-fetch
.
Usage
For a general usage, see redux-saga-requests docs.
Regarding Fetch API related usage, here you can see a typical setup:
; // or a different fetch polyfill;; ;
And in order to create Fetch API requests, below:
;
should be translated to this:
const fetchUsers = type: 'FETCH_USERS' request: url: '/users/' method: 'POST' body: JSON headers: 'Content-Type': 'application/json' ;
The point is, you can use the same request config like you do with pure Fetch API, but you need to pass url
in the
config itself. Also, one additional parameter you could provide in the config is responseType
, which is set as json
as the default. Available response types are: 'arraybuffer'
, 'blob'
, 'formData'
, 'json'
, 'text'
, or null
(if you don't want a response stream to be read for the given response).
Also, this driver reads response streams automatically for you (depending on responseType
you choose)
and sets it as response.data
, so instead of doing response.json()
, just read response.data
.
Licence
MIT