This package is intended to help you to build Redux actions and reducers for WordPress REST API endpoints.
You can add it to your project by running following NPM or Yarn command in your terminal:
npm install redux-wordpress --save
yarn add redux-wordpress
This package uses ES6 fetch and promises to make AJAX requests, so you might also need to install isomorphic-fetch and es6-promise packages to make sure it works correctly during server rendering or in older browsers.
The package exports three function which you can use to create actions and build a reducer.
Returns an object with a set of function which you can use to fetch data from REST API.
- name (string) - Arbitrary name which will be used in action types to distinguish different actions.
 - host (string) - URL address to your API's root. Usually it will look like: 
http://mysite.com/wp-json/. - endpoints (array) - A list of endpoints which you want to build actions for. It could be something like 
['posts', 'categories']. - args (object) - Optional. The options objects which supports following params:
- namespace (string) - The namespace for your endpoints. By default it is 
wp/v2. - fetch (boolean) - Determines whether or not 
fetchfunction need to be generated. - fetchEndpoint (boolean) - Determines whether or not 
fetchEndpointfunction need to be generated. - fetchById (boolean) - Determines whether or not 
fetchByIdfunction need to be generated. - fetchEndpointById (boolean) - Determines whether or not 
fetchEndpointByIdfunction need to be generated. - fetchAll (boolean) - Determines whether or not 
fetchAllfunction need to be generated. - fetchAllEndpoint (boolean) - Determines whether or not 
fetchAllEndpointfunction need to be generated. - fetchAllEndpointById (boolean) - Determines whether or not 
fetchAllEndpointByIdfunction need to be generated. 
 - namespace (string) - The namespace for your endpoints. By default it is 
 
// actionCreators.js
import { createActions } from 'redux-wordpress';
const actions = createActions('my-api', 'http://mysite.test/wp-json/', ['books', 'authors']);
export default actions;
// will export:
//
// {
//     fetchBooks(params) { ... },
//     fetchBooksEndpoint(endpoint, params) { ... },
//     fetchBooksById(id, params) { ... },
//     fetchBooksEndpointById(id, endpoint, params) { ... },
//     fetchAllBooks(params) { ... },
//     fetchAllBooksEndpoint(endpoint, params) { ... },
//     fetchAllBooksEndpointById(id, endpoint, params) { ... },
//     fetchAuthors(params) { ... },
//     fetchAuthorsEndpoint(endpoint, params) { ... },
//     fetchAuthorsById(id, params) { ... },
//     fetchAuthorsEndpointById(id, endpoint, params) { ... },
//     fetchAllAuthors(params) { ... },
//     fetchAllAuthorsEndpoint(endpoint, params) { ... },
//     fetchAllAuthorsEndpointById(id, endpoint, params) { ... }
// }Returns a reducer function which you can use to catch data returned by a fetch action.
- name (string) - A name which will be used to catch proper actions. It should be the same name as you passed to 
createActionsfunction. 
// reducers.js
import { createReducer } from 'redux-wordpress';
const rootReducer = combineReducers({
    wp: createReducer('my-api') // name should match to what we passed to "createActions" function
});
export default rootReducer;Helper function which generates request functions to endpoints which you can use to group multiple requests into one action:
- host (string) - URL address to your API's root. Usually it will look like: 
http://mysite.com/wp-json/. - endpoints (array) - A list of endpoints which you want to build actions for. It could be something like 
['posts', 'categories']. - args (object) - Optional. The options objects which supports following params:
- namespace (string) - The namespace for your endpoints. By default it is 
wp/v2. - request (boolean) - Determines whether or not 
requestfunction need to be generated. - requestEndpoint (boolean) - Determines whether or not 
requestEndpointfunction need to be generated. - requestById (boolean) - Determines whether or not 
requestByIdfunction need to be generated. - requestEndpointById (boolean) - Determines whether or not 
requestEndpointByIdfunction need to be generated. - requestAll (boolean) - Determines whether or not 
requestAllfunction need to be generated. - requestAllEndpoint (boolean) - Determines whether or not 
requestAllEndpointfunction need to be generated. - requestAllEndpointById (boolean) - Determines whether or not 
requestAllEndpointByIdfunction need to be generated. 
 - namespace (string) - The namespace for your endpoints. By default it is 
 
// actionCreators.js
import { createRequests } from 'redux-wordpress';
const requests = createRequests('http://mysite.test/wp-json/', ['books', 'authors']);
export function fetchInitialData() {
    return dispatch => {
        return Promise
            .all([
                requests.requestBooks(...).then((data, response) => dispatch({action: 'books', data})),
                requests.requestAuthors(...).then((data, response) => dispatch({action: 'authors', data}))
            ])
            .then(() => dispatch({action: 'loaded-initial-data'}));
    };
}What to help or have a suggestion? Open a new ticket and we can discuss it or submit pull request. Please, make sure you run npm test before submitting a pull request.
The MIT License (MIT)