Redux Thunk Rest
NOTE This module is still a work in progress and has not been completed yet.
This package is helps remove some of the boilerplate cruft for creating RESTful actions, action creators, and reducers for Redux with Redux Thunk.
Installation
yarn add redux-thunk-rest
Use
Using the reduxThunkRest
function from this package will create an object with:
- Actions
- Action Creators
- Reducer
To work with resourceful REST APIs.
; const bookResource = ; // Using the reducersconst reducer = ; // Using the action creator// const findAll = bookResource.actionCreators.findAllconst actionCreators: findAll = bookResource; store;
This code will create:
actions = findAllStart: 'BOOK@FINDALL_START' findAllSuccess: 'BOOK@FINDALL_SUCCESS' findAllError: 'BOOK@FINDALL_ERROR' findOneStart: 'BOOK@FINDONE_START' findOneSuccess: 'BOOK@FINDONE_SUCCESS' findOneError: 'BOOK@FINDONE_ERROR' createStart: 'BOOK@CREATE_START' createSuccess: 'BOOK@CREATE_SUCCESS' createError: 'BOOK@CREATE_ERROR' updateStart: 'BOOK@UPDATE_START' updateSuccess: 'BOOK@UPDATE_SUCCESS' updateError: 'BOOK@UPDATE_ERROR' destroyStart: 'BOOK@DESTROY_START' destroySuccess: 'BOOK@DESTROY_SUCCESS' destroyError: 'BOOK@DESTROY_ERROR'; actionCreators = { // Parse JSON data for success requests or reject and parse error JSON ; } type: actionsfindAllStart data type: actionsfindAllSuccess data type: actionsfindAllError data { // Parse JSON data for success requests or reject and parse error JSON ; } type: actionsfindOneStart data type: actionsfindOneSuccess data type: actionsfindOneError data { // Parse JSON data for success requests or reject and parse error JSON ; } type: actionscreateStart data type: actionscreateSuccess data type: actionscreateError data { // Parse JSON data for success requests or reject and parse error JSON ; } type: actionsupdateStart data type: actionsupdateSuccess data type: actionsupdateError data { ; } type: actionsdestroyStart data type: actionsdestroySuccess data type: actionsdestroyError data ; reducer = ;
Customization
There are some basic ways to customize the reducers and action creators created by createResource
:
Customizing The Primary Key
Some servers do use id
as the primary key for resources.
In the options object, a primaryKey
property can be passed to customize the primaryKey that is used for unionBy
de-duplication.
Ex.
const actions actionCreators reducer = ;
Customizing The Reducers
To add custom reducers to the reducer created by createResource
, a reducers
object with custom action types can be added to customize the reducer for both loading
and items
.
Ex.
const actions actionCreators reducer = ;
Customizing Request Headers
const actions actionCreators reducer = ;