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 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/__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'); +}); diff --git a/src/actions.js b/src/actions.js index 87be11d..9331558 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.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; 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;