npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-wordpress

v1.1.0

Published

Redux actions and reducer to work with WordPress REST API

Downloads

39

Readme

redux-wordpress

npm version Build Status

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.
// 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.
// 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)