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 #4 from eugene-manuilov/feature/generator-args
Browse files Browse the repository at this point in the history
Generator args
  • Loading branch information
eugene-manuilov authored Jun 18, 2017
2 parents fc9d342 + e99b13f commit d46d289
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 125 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change Log

## v1.1.0 (2017-06-dd)
## 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.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ This package uses ES6 fetch and promises to make AJAX requests, so you might als

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');
});
245 changes: 134 additions & 111 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,123 +52,146 @@ const getErrorHandlerById = (dispatch, type, id, params) => (error) => {
});
};

export default function createActions(name, host, endpoints, namespace = 'wp/v2') {
export default function createActions(name, host, endpoints, args = {}) {
let options = args;

// fallback for previous version of this function where the last param was
// for namespace argument
if (typeof args === 'string') {
options = { namespace: args };
}

const actions = {};
const normalizedHost = trimEnd(host, '/');
const namespace = options.namespace || 'wp/v2';

endpoints.forEach((endpoint) => {
const endpointName = upperFirst(endpoint);

actions[`fetch${endpointName}`] = (params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching/${endpoint}`,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}?${qs(params)}`,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};

actions[`fetch${endpointName}Endpoint`] = (endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching/${endpoint}/${endpoint2}`,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${endpoint2}?${qs(params)}`,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};

actions[`fetch${endpointName}ById`] = (id, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-by-id/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching-by-id/${endpoint}`,
id,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${id}?${qs(params)}`,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};

actions[`fetch${endpointName}EndpointById`] = (id, endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-by-id/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-by-id/${endpoint}/${endpoint2}`,
id,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${id}/${endpoint2}?${qs(params)}`,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};

actions[`fetchAll${endpointName}`] = (params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching-all/${endpoint}`,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}`,
params,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};

actions[`fetchAll${endpointName}Endpoint`] = (endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-all/${endpoint}/${endpoint2}`,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}/${endpoint2}`,
params,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};

actions[`fetchAll${endpointName}EndpointById`] = (id, endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all-by-id/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-all-by-id/${endpoint}/${endpoint2}`,
id,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}/${id}/${endpoint2}`,
params,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};
if (options.fetch !== false) {
actions[`fetch${endpointName}`] = (params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching/${endpoint}`,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}?${qs(params)}`,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};
}

if (options.fetchEndpoint !== false) {
actions[`fetch${endpointName}Endpoint`] = (endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching/${endpoint}/${endpoint2}`,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${endpoint2}?${qs(params)}`,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};
}

if (options.fetchById !== false) {
actions[`fetch${endpointName}ById`] = (id, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-by-id/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching-by-id/${endpoint}`,
id,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${id}?${qs(params)}`,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};
}

if (options.fetchEndpointById !== false) {
actions[`fetch${endpointName}EndpointById`] = (id, endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-by-id/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-by-id/${endpoint}/${endpoint2}`,
id,
params,
});

return fetchSingle(
`${normalizedHost}/${namespace}/${endpoint}/${id}/${endpoint2}?${qs(params)}`,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};
}

if (options.fetchAll !== false) {
actions[`fetchAll${endpointName}`] = (params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all/${endpoint}`;

dispatch({
type: `@@wp/${name}/fetching-all/${endpoint}`,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}`,
params,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};
}

if (options.fetchAllEndpoint !== false) {
actions[`fetchAll${endpointName}Endpoint`] = (endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-all/${endpoint}/${endpoint2}`,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}/${endpoint2}`,
params,
getSuccessHandler(dispatch, type, params),
getErrorHandler(dispatch, type, params),
);
};
}

if (options.fetchAllEndpointById !== false) {
actions[`fetchAll${endpointName}EndpointById`] = (id, endpoint2, params = {}) => (dispatch) => {
const type = `@@wp/${name}/fetched-all-by-id/${endpoint}/${endpoint2}`;

dispatch({
type: `@@wp/${name}/fetching-all-by-id/${endpoint}/${endpoint2}`,
id,
params,
});

return fetchAll(
`${normalizedHost}/${namespace}/${endpoint}/${id}/${endpoint2}`,
params,
getSuccessHandlerById(dispatch, type, id, params),
getErrorHandlerById(dispatch, type, id, params),
);
};
}
});

return actions;
Expand Down
Loading

0 comments on commit d46d289

Please sign in to comment.