Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from eugene-manuilov/release/1.1.0
Browse files Browse the repository at this point in the history
Release/1.1.0
  • Loading branch information
eugene-manuilov authored Jun 18, 2017
2 parents 1760fb4 + d46d289 commit afed87b
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 128 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log

## v1.1.0 (2017-06-19)

**Implemented enhancements:**

- createActions and createRequests functions: `namespace` argument has been replaced with `args` to allow support multiple options, `namespace` argument becomes a part of `args` object.
- createActions: added ability to skip some fetch functions generation.
- createRequests: added ability to skip some request functions generation.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,38 @@ This package is intended to help you to build Redux actions and reducers for Wor

## Installation

NPM:
You can add it to your project by running following NPM or Yarn command in your terminal:

```
npm install redux-wordpress --save
```

Yarn:

```
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, namespace)
### 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']`.
- **namespace** _(string)_ - Optional. The namespace for your endpoints. By default it is `wp/v2`.
- **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.

```js
// actionCreators.js
Expand Down Expand Up @@ -76,13 +84,21 @@ const rootReducer = combineReducers({

export default rootReducer;
```
### createRequests(host, endpoints, namespace)
### 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']`.
- **namespace** _(string)_ - Optional. The namespace for your endpoints. By default it is `wp/v2`.
- **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.

```js
// actionCreators.js
Expand Down
22 changes: 21 additions & 1 deletion __tests__/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ test('Check actions created by createActions function', () => {
expect(typeof actions.fetchAllAuthors).toBe('function');
expect(typeof actions.fetchAllAuthorsEndpoint).toBe('function');
expect(typeof actions.fetchAllAuthorsEndpointById).toBe('function');
});
});

test('Check ability to skip some actions', () => {
const actions = createActions('test-rest-api', 'http://wordpress.test/wp-json/', ['books'], {
fetch: false,
fetchEndpoint: false,
fetchById: false,
fetchEndpointById: false,
fetchAll: false,
fetchAllEndpoint: false,
fetchAllEndpointById: false,
});

expect(typeof actions.fetchBooks).toBe('undefined');
expect(typeof actions.fetchBooksEndpoint).toBe('undefined');
expect(typeof actions.fetchBooksById).toBe('undefined');
expect(typeof actions.fetchBooksEndpointById).toBe('undefined');
expect(typeof actions.fetchAllBooks).toBe('undefined');
expect(typeof actions.fetchAllBooksEndpoint).toBe('undefined');
expect(typeof actions.fetchAllBooksEndpointById).toBe('undefined');
});
41 changes: 41 additions & 0 deletions __tests__/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createRequests } from '../lib/index';

test('Check requests created by createRequests function', () => {
const requests = createRequests('http://wordpress.test/wp-json/', ['books', 'authors']);

expect(typeof requests.requestBooks).toBe('function');
expect(typeof requests.requestBooksEndpoint).toBe('function');
expect(typeof requests.requestBooksById).toBe('function');
expect(typeof requests.requestBooksEndpointById).toBe('function');
expect(typeof requests.requestAllBooks).toBe('function');
expect(typeof requests.requestAllBooksEndpoint).toBe('function');
expect(typeof requests.requestAllBooksEndpointById).toBe('function');

expect(typeof requests.requestAuthors).toBe('function');
expect(typeof requests.requestAuthorsEndpoint).toBe('function');
expect(typeof requests.requestAuthorsById).toBe('function');
expect(typeof requests.requestAuthorsEndpointById).toBe('function');
expect(typeof requests.requestAllAuthors).toBe('function');
expect(typeof requests.requestAllAuthorsEndpoint).toBe('function');
expect(typeof requests.requestAllAuthorsEndpointById).toBe('function');
});

test('Check ability to skip some requests', () => {
const requests = createRequests('http://wordpress.test/wp-json/', ['books'], {
request: false,
requestEndpoint: false,
requestById: false,
requestEndpointById: false,
requestAll: false,
requestAllEndpoint: false,
requestAllEndpointById: false,
});

expect(typeof requests.requestBooks).toBe('undefined');
expect(typeof requests.requestBooksEndpoint).toBe('undefined');
expect(typeof requests.requestBooksById).toBe('undefined');
expect(typeof requests.requestBooksEndpointById).toBe('undefined');
expect(typeof requests.requestAllBooks).toBe('undefined');
expect(typeof requests.requestAllBooksEndpoint).toBe('undefined');
expect(typeof requests.requestAllBooksEndpointById).toBe('undefined');
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bugs": {
"url": "https://github.com/eugene-manuilov/redux-wordpress/issues"
},
"version": "1.0.0",
"version": "1.1.0",
"main": "lib/index",
"files": [
"*.md",
Expand Down
Loading

0 comments on commit afed87b

Please sign in to comment.