Redux-api
Flux REST API for redux infrastructure
Introduction
redux-api
solves the problem of writing clients to communicate with backends. It generates actions and reducers for making AJAX calls to API endpoints. You don't need to write a lot of boilerplate code if you use redux
and want to exchange data with server.
Inspired by Redux-rest and is intended to be used with Redux.
Documentation
See DOCS.md for API documentation.
Use cases
- AuthorizationJWT.md - example of JWT Authorization
- Scoping.md - use scoping or using multiple redux-api instance without naming intersections.
Install
With npm:
npm install redux-api --save
With bower:
bower install redux-api --save
If you don't use tools like webpack, browserify, etc and you want to load redux-api manually, the best way to add redux-api to your project is:
<script src="(...)/redux-api.min.js"></script><script> windowReduxApi = window"redux-api"; // or var ReduxApi = window"redux-api"; // initialization code</script>
=======
Remote calls
redux-api
doesn't bind you to a technology to make AJAX calls. It uses configurable adapters
- a pretty simple function which receives 2 arguments: endpoint
and options
, and returns a Promise as result. The default adapter uses isomorphic-fetch, and has an implementation like this:
{ return ;}
However, you are not tied to using isomorphic-fetch. For instance, if you prefer to use jQuery, you can use the following adapter:
{ return { $; };}
This implementation allows you to make any request and process any response.
And of course you have to set up adapter to your redux-api
instance before using.
reduxApi(....).use("fetch", adapterFetch)
=======
Examples
examples/isomorphic - React + Redux + React-Router + Redux-api with webpack and express + github API
Example
rest.js
;;; // simple endpoint description entry: `/api/v1/entry/:id` // complex endpoint description regions: url: `/api/v1/regions` // reimplement default `transformers.object` transformer: transformersarray // base endpoint options `fetch(url, options)` options: headers: "Accept": "application/json" ;
index.jsx
;;;;; //our redux-rest object const createStoreWithMiddleware = createStore;const reducer = ;const store = ; { return entry: stateentry regions: stateregions ;} static propTypes = entry: PropTypesshape loading: PropTypesboolisRequired data: PropTypesshape text: PropTypesstring isRequired isRequired regions: PropTypesshape loading: PropTypesboolisRequired data: PropTypesarrayisRequired isRequired dispatch: PropTypesfuncisRequired ; { const dispatch = thisprops; // fetch `/api/v1/regions ; //specify id for GET: /api/v1/entry/1 ; } { const entry regions = thisprops; const Regions = regionsdata return <div> Loading regions: regionsloading <Regions/> Loading entry: entryloading <div> entrydatatext </div> </div> ; } const SmartComponent = Application; React;