All Projects → maksim-chekrishov → redux-rest-adapter

maksim-chekrishov / redux-rest-adapter

Licence: other
REST adapter for redux

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-rest-adapter

wwvue-cli
vue-cli升级版脚手架,应有尽有的开箱即用方法及配置,没有花里胡哨的晦涩难懂的操作,上手成本极低,现已新增simple(极简模式)、vue3和iview-template,是个很不错的垫脚石,来不及解释了赶紧上车😊😘
Stars: ✭ 15 (+15.38%)
Mutual labels:  axios
Vue2.x-mobileSystem
基于Vue2.0的移动端项目,项目没有使用vue-cli,全部手写,让小白更容易学习理解
Stars: ✭ 72 (+453.85%)
Mutual labels:  axios
vue-webpack-boilerplate
A webpack boilerplate with vue-loader, axios, vue-router and vuex
Stars: ✭ 51 (+292.31%)
Mutual labels:  axios
proffy
React Native + ReactJS + NodeJS project developed on RocketSeat NexLevelWeek. This project is based on an application for connect students and teachers.
Stars: ✭ 30 (+130.77%)
Mutual labels:  axios
chrome-extension-mocker
The most convenient tool to mock requests for axios, with built-in Chrome extension support.
Stars: ✭ 37 (+184.62%)
Mutual labels:  axios
vue-admin-webapp
基于vuejs+element-ui 后台管理系统
Stars: ✭ 62 (+376.92%)
Mutual labels:  axios
react-redux-jwt-auth
React Redux: Token Authentication example with JWT, React Router, Axios, Thunk Middleware
Stars: ✭ 86 (+561.54%)
Mutual labels:  axios
vue-acfun
AcFun(弹幕视频网)
Stars: ✭ 25 (+92.31%)
Mutual labels:  axios
vue-mall
vue企业级商城练手项目并且提供接口唷
Stars: ✭ 14 (+7.69%)
Mutual labels:  axios
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+615.38%)
Mutual labels:  axios
axios-curlirize
axios plugin converting requests to cURL commands, saving and logging them.
Stars: ✭ 152 (+1069.23%)
Mutual labels:  axios
nippo
本アプリケーションはYAPC::Hokkaido 2016の「Vue.jsによるWebアプリケーション開発」用に実装したサンプルアプリケーションです。
Stars: ✭ 17 (+30.77%)
Mutual labels:  axios
ts-vue-questionnaire
微型问卷调查系统 TypeScript 版本,演示账号:admin / admin
Stars: ✭ 89 (+584.62%)
Mutual labels:  axios
madao admin manage
🎉 VUE前后端分离管理系统,基于RBAC的后台管理。
Stars: ✭ 38 (+192.31%)
Mutual labels:  axios
Vue-CAMP
VueJS
Stars: ✭ 16 (+23.08%)
Mutual labels:  axios
vue-mintUI-demo
采用vue2、Mint UI,做的移动端项目
Stars: ✭ 17 (+30.77%)
Mutual labels:  axios
rocketshoes-react-native
NetShoes Clone with React Native and Redux
Stars: ✭ 38 (+192.31%)
Mutual labels:  axios
fastify-axios
Add axios http client to your fastify instance
Stars: ✭ 28 (+115.38%)
Mutual labels:  axios
NeteaseCloudWebApp
This is a vue for NeteaseCloud projects!
Stars: ✭ 2,512 (+19223.08%)
Mutual labels:  axios
use-axios-hooks
axios hooks for common network calls scenarios
Stars: ✭ 27 (+107.69%)
Mutual labels:  axios

redux-rest-adapter

NPM Code Climate Test Coverage

redux-rest-adapter is a tool for easy connection your REST api with redux store.

Compatible with json.api specification

Main points

  • Write code instead of reducers and actions for trivial data operations.

Changelog

Starts from v2.0.0 redux-rest-adapter based on axios and promise-middleware for easy access to promises and better experience with isomorphic app.

Versions 1.x.x

short-example.js

import EntityApi, {promiseMiddleware} from 'redux-rest-adapter';
import {createStore, applyMiddleware, combineReducers} from 'redux';

    const tagsApi = new EntityApi({
      entityName: 'TAG',
      endpointUrl: 'api/v2/tags'
    });

    const apiReducer = combineReducers({
      TAG: tagsApi.configureReducer()
    });

    const store = createStore(
      combineReducers({
        api: apiReducer
      }),
      {},
      applyMiddleware(promiseMiddleware())
    );

    store.dispatch(tagsApi.actions.load()).then(()=> {
        console.log(store.getState().api.TAG.data); // [{id:1, name:'tag1'}, {id:2, name:'tag2'}];
    })

Setup

your/known-entities-api.js

import EntityApi from 'redux-rest-adapter';

export const KnownEntitiesUrls = {
  NEWS_TAGS: 'news-tags',
  NEWS_TAG_FOR_EDIT: 'news-tags',
  //..
};
export default _.mapValues(KnownEntitiesUrls, (url, name) => new EntityApi({
  entityName: name,
  endpointUrl: 'api/v2/' + url
}));

your/api-reducer.js

import knownEntitiesApi from 'your/known-entities-api';

// Ability to extend default api reducers
const apiReducersExtensions = {
  NEWS_TAGS: tagsReducer
}

const apiReducers = _.mapValues(knownEntitiesApi, (api, key) => api.configureReducer(apiReducersExtensions[key]));

export default combineReducers(apiReducers);

your/index-reducer.js

import apiReducer from 'your/api-reducer';

export default combineReducers({
  api: apiReducer
  //..
});

your/configure-store.js

import indexReducer from 'your/index-reducer';
import {promiseMiddleware} from 'redux-rest-adapter';
//..
export default function configureStore(initialState) {
  return createStore(
    indexReducer,
    initialState,
    applyMiddleware(promiseMiddleware())
  );
}

your/entities-actions.js

import knownEntitiesApi from 'your/known-entities-api';

export default _.mapValues(knownEntitiesApi, entityApi => entityApi.actions);

Adapter is ready

Image devTools

Usage

Actions

import entitesActions from 'your/entities-actions';

dispatch(entitesActions.NEWS_TAG.load());                          // GET:    api/v2/news-tags
dispatch(entitesActions.NEWS_TAG.load(1));                         // GET:    api/v2/news-tags/1

// --- NOTE: HTTP methods for create and update operations can be configured
dispatch(entitesActions.NEWS_TAG.update(1, {name: 'new tag'}));    // PATCH:  api/v2/news-tags/1
dispatch(entitesActions.NEWS_TAG.create({name: 'new tag'}));       // POST:   api/v2/news-tags
dispatch(entitesActions.NEWS_TAG.remove(1));                       // DELETE: api/v2/news-tags/1

// --- Silent methods for changing store without sync with backend
dispatch(entitesActions.NEWS_TAG.set({name: 'new tag'}));          // set new data
dispatch(entitesActions.NEWS_TAG.reset());                         // reset to initial state

React component example

import entitesActions from 'your/entities-actions';

class TagsComponent extends Component {
  componentWillMount() {
    this.props.loadList();
  }

  //..

  componentWillUnmount() {
    this.props.resetEntryForEdit();
  }

  onTagFormSubmit = ()=> {
    const data = this.props.tagForEdit;
    if (data.id) {
      this.props.updateTag(data.id, data);
    } else {
      this.props.createTag(data);
    }
  }

  render() {
    return (
      this.props.pending ?
        <Loading/> :
        <div>
           {/*...*/}
        </div>
    );
  }
}

const mapStateToProps = (state) => ({
  list: state.api.NEWS_TAGS.data || [],
  pending: state.api.NEWS_TAGS._pending,
  tagForEdit: state.api.NEWS_TAG_FOR_EDIT.data || {}
});

const mapDispatchToProps = {
  createTag: entitiesActions.NEWS_TAG_FOR_EDIT.create,
  updateTag: entitiesActions.NEWS_TAG_FOR_EDIT.update,
  resetEntryForEdit: entitiesActions.NEWS_TAG_FOR_EDIT.reset,
  loadList: entitiesActions.NEWS_TAGS.load
};

const TagsContainer = connect(mapStateToProps, mapDispatchToProps)(TagsComponent);

export {TagsComponent, TagsContainer};

Configuration

EntityApi constructor options

Name Type Default Description
entityName String Required. will be used for naming state and actionTypes.
endpointUrl `String Required. endpointUrl
reducersBuilderCustom Object reducersBuilderDefault Customer can redefine interface of reducers-builder.js
axiosConfig Object {} axios config
resourceKey String 'data' Name of data property key at response object
idKey String 'id' Name of id property key at response data object. Required for CRUD reducer extensions
restHttpMethods Object {create:'post', update:'patch'} Customer can change HTTP methods used for create and update actions

TODO

Example of generated list reducer (basic CRUD operations)

See also

redux-localstorage-adapter

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].