From cd0294fc4f4a26e6807774a06cbb7abcd870d386 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Sun, 18 Jun 2017 23:17:02 +0300 Subject: [PATCH 1/8] Updated version to 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 50c9dc7..e3001eb 100644 --- a/package.json +++ b/package.json @@ -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", From cbd94312b357ba1ecc7e7a2767ae3a6e47402371 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Sun, 18 Jun 2017 23:24:49 +0300 Subject: [PATCH 2/8] Added changelog file --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..edc36dc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +# Change Log + +## v1.1.0 (2017-06-dd) + +**Implemented enhancements:** + From fc9d34284fc0e740d1460af78f970c38674ed2d2 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Sun, 18 Jun 2017 23:46:45 +0300 Subject: [PATCH 3/8] Updated readme file --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 979d8e1..d982542 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,18 @@ 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. From 5cf03a47cd549f3374818b738a90109f61fa0866 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Mon, 19 Jun 2017 00:12:54 +0300 Subject: [PATCH 4/8] Updated createActions function to support args object --- src/actions.js | 245 +++++++++++++++++++++++++++---------------------- 1 file changed, 134 insertions(+), 111 deletions(-) diff --git a/src/actions.js b/src/actions.js index 87be11d..8d33917 100644 --- a/src/actions.js +++ b/src/actions.js @@ -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.fetchAllEnpointById !== 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; From 2e6dfab9f92c973235b6e3e5f669a6106cba764c Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Mon, 19 Jun 2017 00:21:50 +0300 Subject: [PATCH 5/8] Added test to check ability to turn off some functions --- __tests__/actions.js | 22 +++++++++++++++++++++- src/actions.js | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/__tests__/actions.js b/__tests__/actions.js index 29a1f75..98c589c 100644 --- a/__tests__/actions.js +++ b/__tests__/actions.js @@ -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'); -}); \ No newline at end of file +}); + +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'); +}); diff --git a/src/actions.js b/src/actions.js index 8d33917..9331558 100644 --- a/src/actions.js +++ b/src/actions.js @@ -174,7 +174,7 @@ export default function createActions(name, host, endpoints, args = {}) { }; } - if (options.fetchAllEnpointById !== false) { + if (options.fetchAllEndpointById !== false) { actions[`fetchAll${endpointName}EndpointById`] = (id, endpoint2, params = {}) => (dispatch) => { const type = `@@wp/${name}/fetched-all-by-id/${endpoint}/${endpoint2}`; From 25123721b8f2ee6939bdfcc9c262806fcbfec410 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Mon, 19 Jun 2017 00:30:57 +0300 Subject: [PATCH 6/8] Added ability to select what requests need to be generated --- src/requests.js | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/requests.js b/src/requests.js index 3f0fa7a..7f645cd 100644 --- a/src/requests.js +++ b/src/requests.js @@ -1,20 +1,48 @@ import { qs, upperFirst, trimEnd, requestAll, requestSingle } from './helpers'; -export default function createRequests(host, endpoints, namespace = 'wp/v2') { +export default function createRequests(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 requests = {}; + const namespace = options.namespace || 'wp/v2'; endpoints.forEach((endpoint) => { const normalizedURL = trimEnd(host, '/'); const endpointName = upperFirst(endpoint); - requests[`request${endpointName}`] = (params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}?${qs(params)}`); - requests[`request${endpointName}Endpoint`] = (endpoint2, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${endpoint2}?${qs(params)}`); - requests[`request${endpointName}ById`] = (id, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${id}?${qs(params)}`); - requests[`request${endpointName}EndpointById`] = (id, endpoint2, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${id}/${endpoint2}?${qs(params)}`); + if (options.request !== false) { + requests[`request${endpointName}`] = (params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}?${qs(params)}`); + } + + if (options.requestEndpoint !== false) { + requests[`request${endpointName}Endpoint`] = (endpoint2, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${endpoint2}?${qs(params)}`); + } + + if (options.requestById !== false) { + requests[`request${endpointName}ById`] = (id, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${id}?${qs(params)}`); + } + + if (options.requestEndpointById !== false) { + requests[`request${endpointName}EndpointById`] = (id, endpoint2, params = {}) => requestSingle(`${normalizedURL}/${namespace}/${endpoint}/${id}/${endpoint2}?${qs(params)}`); + } + + if (options.requestAll !== false) { + requests[`requestAll${endpointName}`] = (params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}`, params); + } + + if (options.requestAllEndpoint !== false) { + requests[`requestAll${endpointName}Endpoint`] = (endpoint2, params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}/${endpoint2}`, params); + } - requests[`requestAll${endpointName}`] = (params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}`, params); - requests[`requestAll${endpointName}Endpoint`] = (endpoint2, params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}/${endpoint2}`, params); - requests[`requestAll${endpointName}EndpointById`] = (id, endpoint2, params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}/${id}/${endpoint2}`, params); + if (options.requestAllEndpointById !== false) { + requests[`requestAll${endpointName}EndpointById`] = (id, endpoint2, params = {}) => requestAll(`${normalizedURL}/${namespace}/${endpoint}/${id}/${endpoint2}`, params); + } }); return requests; From 26895d69a791a68722eb004e422310bba9a47108 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Mon, 19 Jun 2017 00:35:07 +0300 Subject: [PATCH 7/8] Added tests for createRequests function --- __tests__/requests.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 __tests__/requests.js diff --git a/__tests__/requests.js b/__tests__/requests.js new file mode 100644 index 0000000..5fe85e8 --- /dev/null +++ b/__tests__/requests.js @@ -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'); +}); From e99b13f758188b72be26853450f1c1d31fc13cd2 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Mon, 19 Jun 2017 00:51:28 +0300 Subject: [PATCH 8/8] Updated changelog and readme files. --- CHANGELOG.md | 5 ++++- README.md | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edc36dc..6e17f98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. \ No newline at end of file diff --git a/README.md b/README.md index d982542..4b444e2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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