Git Product home page Git Product logo

redux-wordpress's Introduction

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('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'}));
    };
}

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)

redux-wordpress's People

Contributors

eugene-manuilov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jkosoy

redux-wordpress's Issues

createRequests API doesn't match createActionsAPI

Looks like createRequests is supposed to be

export default function createRequests(name, host, endpoints, args = {})

And subsequently account for the namespace appropriately. As it sits if you follow the tutorial on the README you get

TypeError: endpoints.forEach is not a function

as endpoints is one argument off.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.