• Stars
    star
    495
  • Rank 86,132 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flux REST API for redux infrastructure

Redux-api

Flux REST API for redux infrastructure

Build Status NPM version Coverage Status

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

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>
  window.ReduxApi = 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:

function adapterFetch(url, options) {
  return fetch(url, options);
}

However, you are not tied to using isomorphic-fetch. For instance, if you prefer to use jQuery, you can use the following adapter:

function adapterJquery(url, options) {
  return new Promise((success, error)=> {
    $.ajax({ ...options, url, success, error });
  });
}

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

import "isomorphic-fetch";
import reduxApi, {transformers} from "redux-api";
import adapterFetch from "redux-api/lib/adapters/fetch";
export default reduxApi({
  // simple endpoint description
  entry: `/api/v1/entry/:id`,
  // complex endpoint description
  regions: {
    url: `/api/v1/regions`,
    // reimplement default `transformers.object`
    transformer: transformers.array,
    // base endpoint options `fetch(url, options)`
    options: {
      headers: {
        "Accept": "application/json"
      }
    }
  }
}).use("fetch", adapterFetch(fetch));

index.jsx

import React, {PropTypes} from "react";
import { createStore, applyMiddleware, combineReducers } from "redux";
import thunk from "redux-thunk";
import { Provider, connect } from "react-redux";
import rest from "./rest"; //our redux-rest object

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(rest.reducers);
const store = createStoreWithMiddleware(reducer);

function select(state) {
  return { entry: state.entry, regions: state.regions };
}

class Application {
  static propTypes = {
    entry: PropTypes.shape({
      loading: PropTypes.bool.isRequired,
      data: PropTypes.shape({
        text: PropTypes.string
      }).isRequired
    }).isRequired,
    regions: PropTypes.shape({
      loading: PropTypes.bool.isRequired,
      data: PropTypes.array.isRequired
    }).isRequired,
    dispatch: PropTypes.func.isRequired
  };
  componentDidMount() {
    const {dispatch} = this.props;
    // fetch `/api/v1/regions
    dispatch(rest.actions.regions.sync());
    //specify id for GET: /api/v1/entry/1
    dispatch(rest.actions.entry({id: 1}));
  }
  render() {
    const {entry, regions} = this.props;
    const Regions = regions.data.map((item)=> <p>{ item.name }</p>)
    return (
      <div>
        Loading regions: { regions.loading }
        <Regions/>
        Loading entry: {entry.loading}
        <div>{{ entry.data.text }}</div>
      </div>
    );
  }
}

const SmartComponent = connect(select)(Application);

React.render(
  <Provider store={store}>
    <SmartComponent />
  </Provider>,
  document.getElementById("content")
);

Releases Changelog

More Repositories

1

webpcss

Postcss processor for prepare css to use webp images
JavaScript
53
star
2

grunt-webpcss

Process css file to generate addition css ruless to add webp compatble
JavaScript
26
star
3

yandex-disk-webdav

Python wrapper to work with yandex disk using webdav Basic Auth
Python
23
star
4

postcss-stream

TypeScript
9
star
5

page-scale-js

Scale any wep page
JavaScript
9
star
6

webpack-lazy-dev-server

TypeScript
8
star
7

shallow-equal-fuzzy

Fuzzy implementation of shallowEqual algoritm
JavaScript
6
star
8

genericrelationview

genericrelationview
Python
5
star
9

react-async-decorator

New way to organize your asynchronous flow in react applications
TypeScript
5
star
10

gulp-webpcss

Process css file to generate addition css ruless to add webp compatble
JavaScript
5
star
11

backbone-mixin

Mixin for Backbone.View with extend basic functionality
CoffeeScript
5
star
12

grunt-css-image

Plugin to generate css file wto bind all images from folder
CSS
4
star
13

css-image

generator css and scss templates for images
JavaScript
4
star
14

postcss-shared-options

TypeScript
3
star
15

django-dbbackupyandexdisk

Storage yandex-disk for django-dbbackup
Python
3
star
16

example-webpack-postcss-loader-webpcss

Example to use webpcss with webpack and postcss loader
JavaScript
3
star
17

gulp-css-image

Gulp task for generate css/scss classes from the existin images
CSS
3
star
18

node-css

simple css generator for node
JavaScript
2
star
19

sp-utils-gaconstructor

GAContructor for sp-utils
JavaScript
2
star
20

redux-controller

Controllers functions for Redux
JavaScript
2
star
21

grunt-image-preload

Grunt task for generate js file with list of image resourse
JavaScript
2
star
22

uploader

TypeScript
1
star
23

urldata

Library to extract url data from css property
JavaScript
1
star
24

openapi

TypeScript
1
star
25

codemirror-hql

plugin for codemirror.net support hql syntax
JavaScript
1
star
26

django-pattern

Default django pattern
Python
1
star
27

lexich.github.io

HTML
1
star
28

bootstrap-form-builder

formbuilder
CoffeeScript
1
star
29

sp-utils-serverclient

ServerClient for sp-utils
JavaScript
1
star
30

strftime

hard optimization https://github.com/samsonjs/strftime
JavaScript
1
star
31

generator-wrr

Yeoman genertor for: webpack, react, redux, react-router, redux-api
JavaScript
1
star