redux-wordpress
v1.1.0
Published
Redux actions and reducer to work with WordPress REST API
Downloads
22
Maintainers
Readme
redux-wordpress
This package is intended to help you to build Redux actions and reducers for WordPress REST API endpoints.
Installation
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.
Usage
The package exports three function which you can use to create actions and build a reducer.
createActions(name, host, endpoints, args)
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
fetch
function need to be generated. - fetchEndpoint (boolean) - Determines whether or not
fetchEndpoint
function need to be generated. - fetchById (boolean) - Determines whether or not
fetchById
function need to be generated. - fetchEndpointById (boolean) - Determines whether or not
fetchEndpointById
function need to be generated. - fetchAll (boolean) - Determines whether or not
fetchAll
function need to be generated. - fetchAllEndpoint (boolean) - Determines whether or not
fetchAllEndpoint
function need to be generated. - fetchAllEndpointById (boolean) - Determines whether or not
fetchAllEndpointById
function 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) { ... }
// }
createReducer(name)
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
createActions
function.
// 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;
createRequests(host, endpoints, args)
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
request
function need to be generated. - requestEndpoint (boolean) - Determines whether or not
requestEndpoint
function need to be generated. - requestById (boolean) - Determines whether or not
requestById
function need to be generated. - requestEndpointById (boolean) - Determines whether or not
requestEndpointById
function need to be generated. - requestAll (boolean) - Determines whether or not
requestAll
function need to be generated. - requestAllEndpoint (boolean) - Determines whether or not
requestAllEndpoint
function need to be generated. - requestAllEndpointById (boolean) - Determines whether or not
requestAllEndpointById
function 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('my-api', 'http://mysite.test/wp-json/', ['books', 'authors']);
export function fetchInitialData() {
return dispatch => {
return Promise
.all([
requestBooks(...).then((data, response) => dispatch({action: 'books', data})),
requestAuthors(...).then((data, response) => dispatch({action: 'authors', data}))
])
.then(() => dispatch({action: 'loaded-initial-data'}));
};
}
Contribute
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.
LICENSE
The MIT License (MIT)