diff --git a/.gitignore b/.gitignore index 659fedb..30a3bae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules coverage *.log +lib diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..40dddf8 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +src/** +.babelrc +.eslintrc +Makefile +.gitignore diff --git a/Makefile b/Makefile index 4defdbf..54e574e 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ MOCHA_ARGS= --compilers js:babel-register \ MOCHA_TARGET=src/**/*-test.js build: - $(BIN)/babel src --out-dir lib + $(BIN)/babel src --out-dir lib --ignore "src/__tests__/**" clean: rm -rf lib diff --git a/README.md b/README.md index 34201db..4c3c4c4 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ Any data passed as the first parameter will be treated as the payload to be sent "meta": { "api": true, "method": "POST", - "endpoint": "/sample", + "endpoint": "/items", "types": [ "ITEMS_POST_REQUEST", "ITEMS_POST_SUCCESS", @@ -168,7 +168,7 @@ In this case, we are updating primary item `15` with a new object "meta": { "api": true, "method": "PUT", - "endpoint": "/sample/10", + "endpoint": "/items/10", "types": [ "ITEMS_PUT_REQUEST", "ITEMS_PUT_SUCCESS", @@ -181,7 +181,7 @@ In this case, we are updating primary item `15` with a new object ### DELETE ```js let deleteItem = createAPIAction('ITEMS', 'DELETE', '/items' ); -updateItem(15); +deleteItem(15); ``` In the case of `DELETE`, you just need to specify the primary id of tha which you want to delete. @@ -201,7 +201,7 @@ No need to pass in any payload data, as that would get dropped anyways because o "meta": { "api": true, "method": "DELETE", - "endpoint": "/sample/5", + "endpoint": "/items/5", "types": [ "ITEMS_DELETE_REQUEST", "ITEMS_DELETE_SUCCESS", diff --git a/lib/__tests__/createAPIAction-test.js b/lib/__tests__/createAPIAction-test.js deleted file mode 100644 index aae3a9e..0000000 --- a/lib/__tests__/createAPIAction-test.js +++ /dev/null @@ -1,267 +0,0 @@ -'use strict'; - -var _ = require('../'); - -var _fluxStandardAction = require('flux-standard-action'); - -describe('createAPIAction()', function () { - describe('resulting action creator', function () { - var type = 'TYPE'; - - it('returns a valid FSA', function () { - var actionCreator = (0, _.createAPIAction)(type, 'GET', '/sample', function (b) { - return b; - }); - var action = actionCreator(); - expect((0, _fluxStandardAction.isFSA)(action)).to.be.true; - }); - - it('uses return value as payload', function () { - var actionCreator = (0, _.createAPIAction)(type, 'GET', '/sample', function (b) { - return b; - }, function (b) { - return b; - }); - var action = actionCreator(); - expect(action).to.deep.equal({ - type: type, - payload: {}, - meta: { - api: true, - method: 'GET', - endpoint: '/sample', - types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')] - } - }); - }); - - it('uses identity function if actionCreator is not a function', function () { - var actionCreator = (0, _.createAPIAction)(type, 'POST', '/sample'); - var foobar = { foo: 'bar' }; - var action = actionCreator(foobar); - expect(action).to.deep.equal({ - type: type, - payload: foobar, - meta: { - api: true, - method: 'POST', - endpoint: '/sample', - types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')] - } - }); - expect((0, _fluxStandardAction.isFSA)(action)).to.be.true; - }); - - it('accepts a second parameter for adding meta to object', function () { - var actionCreator = (0, _.createAPIAction)(type, 'POST', '/sample', null, function (_ref) { - var cid = _ref.cid; - return { cid: cid }; - }); - var foobar = { foo: 'bar', cid: 5 }; - var action = actionCreator(foobar); - expect(action).to.deep.equal({ - type: type, - payload: foobar, - meta: { - api: true, - method: 'POST', - endpoint: '/sample', - cid: 5, - types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')] - } - }); - expect((0, _fluxStandardAction.isFSA)(action)).to.be.true; - }); - - it('sets error to true if payload is an Error object', function () { - var actionCreator = (0, _.createAPIAction)(type, 'GET', '/sample'); - var errObj = new TypeError('this is an error'); - - var errAction = actionCreator(errObj); - expect(errAction).to.deep.equal({ - type: type, - payload: errObj, - error: true, - meta: { - api: true, - method: 'GET', - endpoint: '/sample', - types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')] - } - }); - expect((0, _fluxStandardAction.isFSA)(errAction)).to.be.true; - - var actionCreatorPost = (0, _.createAPIAction)(type, 'POST', '/sample'); - var foobar = { foo: 'bar', cid: 5 }; - var noErrAction = actionCreatorPost(foobar); - expect(noErrAction).to.deep.equal({ - type: type, - payload: foobar, - meta: { - api: true, - method: 'POST', - endpoint: '/sample', - types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')] - } - }); - expect((0, _fluxStandardAction.isFSA)(noErrAction)).to.be.true; - }); - - it('test GET endoint, ALL elements', function () { - var getItems = (0, _.createAPIAction)(type, 'GET', '/sample', function (b) { - return b; - }, function (b) { - return b; - }); - expect(getItems()).to.deep.equal({ - type: type, - payload: {}, - meta: { - api: true, - method: 'GET', - endpoint: '/sample', - types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')] - } - }); - }); - - it('test POST Data endpoint', function () { - var createItems = (0, _.createAPIAction)(type, 'POST', '/sample'); - var postData = { name: 'james' }; - expect(createItems(postData)).to.deep.equal({ - type: type, - payload: postData, - meta: { - api: true, - method: 'POST', - endpoint: '/sample', - types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')] - } - }); - }); - - it('test PUT Data endpoint', function () { - var updateItems = (0, _.createAPIAction)(type, 'PUT', '/sample'); - var postID = 10; - var postData = { name: 'james' }; - expect(updateItems(postID, postData)).to.deep.equal({ - type: type, - payload: postData, - meta: { - api: true, - method: 'PUT', - endpoint: '/sample/10', - types: [type.concat('_PUT_REQUEST'), type.concat('_PUT_SUCCESS'), type.concat('_PUT_FAILURE')] - } - }); - }); - - it('test DELETE Data endpoint', function () { - var deleteItems = (0, _.createAPIAction)(type, 'DELETE', '/sample'); - var postID = 5; - expect(deleteItems(postID)).to.deep.equal({ - type: type, - payload: {}, - meta: { - api: true, - method: 'DELETE', - endpoint: '/sample/5', - types: [type.concat('_DELETE_REQUEST'), type.concat('_DELETE_SUCCESS'), type.concat('_DELETE_FAILURE')] - } - }); - }); - }); - - describe('Testing Custom Endpoints', function () { - var type = 'TYPE'; - it('test GET with Custom Endpoint', function () { - var customEndpoint = function customEndpoint() { - return '/tester/mctesterson'; - }; - var getItems = (0, _.createAPIAction)(type, 'GET', customEndpoint); - expect(getItems()).to.deep.equal({ - type: type, - payload: {}, - meta: { - api: true, - method: 'GET', - endpoint: '/tester/mctesterson', - types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')] - } - }); - }); - - it('test Get by ID with Custom Endpoint', function () { - var customEndpoint = function customEndpoint(p) { - return '/tester/' + p + '/mctesterson'; - }; - var getItems = (0, _.createAPIAction)(type, 'GET', customEndpoint); - expect(getItems(10)).to.deep.equal({ - type: type, - payload: 10, - meta: { - api: true, - method: 'GET', - endpoint: '/tester/10/mctesterson', - types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')] - } - }); - }); - - it('test POST with Custom Endpoint', function () { - var customEndpoint = function customEndpoint(params) { - return '/user/' + params.id + '/ronald/' + params.name; - }; - var createItem = (0, _.createAPIAction)(type, 'POST', customEndpoint); - var payload = { id: 10, name: 'james' }; - expect(createItem(payload)).to.deep.equal({ - type: type, - payload: payload, - meta: { - api: true, - method: 'POST', - endpoint: '/user/10/ronald/james', - types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')] - } - }); - }); - - it('test PUT with Custom Endpoint', function () { - var customEndpoint = function customEndpoint(params) { - return '/user/' + params.id; - }; - var updateItem = (0, _.createAPIAction)(type, 'PUT', customEndpoint); - var payload = { id: 10, name: 'james' }; - expect(updateItem(payload)).to.deep.equal({ - type: type, - payload: payload, - meta: { - api: true, - method: 'PUT', - endpoint: '/user/10', - types: [type.concat('_PUT_REQUEST'), type.concat('_PUT_SUCCESS'), type.concat('_PUT_FAILURE')] - } - }); - }); - - it('test DELETE with Custom Endpoint', function () { - var customEndpoint = function customEndpoint(_ref2) { - var id = _ref2.id; - var accountID = _ref2.accountID; - return '/user/' + id + '/account/' + accountID; - }; - var deleteItem = (0, _.createAPIAction)(type, 'DELETE', customEndpoint); - var payload = { id: 10, accountID: 25 }; - expect(deleteItem(payload)).to.deep.equal({ - type: type, - payload: payload, - meta: { - api: true, - method: 'DELETE', - endpoint: '/user/10/account/25', - types: [type.concat('_DELETE_REQUEST'), type.concat('_DELETE_SUCCESS'), type.concat('_DELETE_FAILURE')] - } - }); - }); - }); -}); \ No newline at end of file diff --git a/lib/__tests__/init.js b/lib/__tests__/init.js deleted file mode 100644 index 30e2528..0000000 --- a/lib/__tests__/init.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var _chai = require('chai'); - -var _chai2 = _interopRequireDefault(_chai); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -global.expect = _chai2.default.expect; \ No newline at end of file diff --git a/lib/createAPIAction.js b/lib/createAPIAction.js deleted file mode 100644 index 87a2d5b..0000000 --- a/lib/createAPIAction.js +++ /dev/null @@ -1,104 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.default = createAPIAction; - -function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } - -/** - * Returns the endpoint based on HTTP Verb - * Contains a specific case for GET BY ID because - * the verb is the same as a GET ALL -**/ -function getEndpoint(method, endpoint, params) { - var _params = _toArray(params); - - var firstParam = _params[0]; - - var others = _params.slice(1); - - if (typeof endpoint === 'function') { - return endpoint(firstParam); - } - - switch (method) { - case 'DELETE': - case 'PUT': - return endpoint + '/' + firstParam; - default: - return endpoint; - } -} - -/** - * in some cases, the payload is the first argument (POST) - * in other cases, the first argument is the ID and the second - * is the actual post data - * We need to check and return accordingly. - * - * In an error case, we just return the error. -**/ -function getPayload(method, endpoint, params) { - var _params2 = _toArray(params); - - var firstParam = _params2[0]; - - var others = _params2.slice(1); - - if (firstParam instanceof Error) { - return firstParam; - } - - if (typeof endpoint === 'function') { - return firstParam || {}; - } - - switch (method) { - case 'POST': - return firstParam; - case 'PUT': - return others[0]; - default: - return {}; - } -} - -function createAPIAction(type, method, endpoint, actionCreator, metaCreator) { - return function () { - for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) { - params[_key] = arguments[_key]; - } - - var firstParam = params[0]; - var others = params.slice(1); - - - var finalEndpoint = getEndpoint(method, endpoint, params); - - var action = { - type: type, - payload: getPayload(method, endpoint, params) - }; - - if (action.payload instanceof Error) { - // Handle FSA errors where the payload is an Error object. Set error. - action.error = true; - action.payload = firstParam; - } - - if (typeof metaCreator === 'function') { - action.meta = metaCreator(action.payload); - } - - action.meta = Object.assign({}, action.meta, { - api: true, - endpoint: finalEndpoint, - method: method, - types: [type.concat('_' + method.toUpperCase() + '_REQUEST'), type.concat('_' + method.toUpperCase() + '_SUCCESS'), type.concat('_' + method.toUpperCase() + '_FAILURE')] - }); - - return action; - }; -} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index f1755db..0000000 --- a/lib/index.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.createAPIAction = undefined; - -var _createAPIAction = require('./createAPIAction'); - -var _createAPIAction2 = _interopRequireDefault(_createAPIAction); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -exports.createAPIAction = _createAPIAction2.default; \ No newline at end of file