Comparing version 0.6.2 to 0.7.0
@@ -438,9 +438,9 @@ (function (global, factory) { | ||
/** | ||
* This is a middleware manager, which is used to register, | ||
* get and run middlewares over actions and responses | ||
* This is a middleware manager, which is used to get get and run middlewares | ||
* over actions and responses | ||
* | ||
* A middleware is a middleware, and it should follow the signature bellow | ||
* A request middleware should follow the signature below | ||
* store => done => (action, error, response) => done(action, error, response); | ||
* A middleware must have a applyPoint property. The available apply points are: | ||
* 'onRequest', 'onResponse' | ||
* A middleware must have an applyPoint property. The available apply points are: | ||
* 'onRequest' and 'onResponse' | ||
*/ | ||
@@ -450,36 +450,33 @@ | ||
var globalMiddlewares = {}; | ||
var notEmpty = function notEmpty(item) { | ||
return !!item; | ||
}; | ||
function reset() { | ||
Object.keys(globalMiddlewares).forEach(function (key) { | ||
delete globalMiddlewares[key]; | ||
function validateMiddleware(middlewares) { | ||
middlewares.forEach(function (middleware) { | ||
if (typeof middleware !== 'function') { | ||
throw new Error('All middlewares should be functions: [' + middlewares + ']'); | ||
} | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
var invalid = 'Invalid applyPoint: ' + middleware.applyPoint + ', provided with middleware: ' + middleware.name + '.'; | ||
var available = 'The apply points available are: ' + applyPoints.join(', '); | ||
throw new Error(invalid + ' ' + available); | ||
} | ||
}); | ||
} | ||
function register(name, middleware) { | ||
var _Object$assign; | ||
var withApplyPoint = function withApplyPoint(applyPoint) { | ||
return function (middleware) { | ||
return middleware.applyPoint === applyPoint; | ||
}; | ||
}; | ||
if (globalMiddlewares[name]) { | ||
throw new Error('Called register with middleware: ' + name + ' more than once'); | ||
} | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
var invalid = 'Invalid applyPoint: ' + middleware.applyPoint + ', provided with middleware: ' + name + '.'; | ||
var available = 'The apply points available are: ' + applyPoints.join(', '); | ||
throw new Error(invalid + ' ' + available); | ||
} | ||
Object.assign(globalMiddlewares, (_Object$assign = {}, _Object$assign[name] = middleware, _Object$assign)); | ||
} | ||
var get$1 = function get(middlewareNames) { | ||
var get$1 = function get(middlewares) { | ||
return function (applyPoint) { | ||
var middlewares = middlewareNames.map(function (name) { | ||
return globalMiddlewares[name]; | ||
}).filter(function (middleware) { | ||
return middleware.applyPoint === applyPoint; | ||
}); | ||
var applyMiddlewares = middlewares.filter(withApplyPoint(applyPoint)); | ||
return function (store) { | ||
return function (done) { | ||
var chain = middlewares.map(function (middleware) { | ||
var chain = applyMiddlewares.map(function (middleware) { | ||
return middleware(store); | ||
@@ -493,10 +490,2 @@ }); | ||
function validateMiddleware(middlewares) { | ||
middlewares.forEach(function (middlewareName) { | ||
if (!globalMiddlewares[middlewareName]) { | ||
throw new Error('Request Middleware ' + middlewareName + ' not registered. Perhaps you forgot to import its file'); | ||
} | ||
}); | ||
} | ||
function getRequestMiddlewares(middlewares) { | ||
@@ -506,11 +495,10 @@ if (!Array.isArray(middlewares)) { | ||
} | ||
validateMiddleware(middlewares); | ||
return get$1(middlewares); | ||
var actualMiddlewares = middlewares.filter(notEmpty); | ||
validateMiddleware(actualMiddlewares); | ||
return get$1(actualMiddlewares); | ||
} | ||
var requestMiddlewares = { | ||
globalMiddlewares: globalMiddlewares, | ||
register: register, | ||
getRequestMiddlewares: getRequestMiddlewares, | ||
reset: reset | ||
getRequestMiddlewares: getRequestMiddlewares | ||
}; | ||
@@ -660,23 +648,2 @@ | ||
/* | ||
After using creators to generate types and creators of different types (Api, Fsa), you may would | ||
like to merge them together, in order to have only one object types and only one object creators | ||
*/ | ||
function mergeTypesAndCreators$1() { | ||
for (var _len = arguments.length, typesAndCreators = Array(_len), _key = 0; _key < _len; _key++) { | ||
typesAndCreators[_key] = arguments[_key]; | ||
} | ||
return typesAndCreators.reduce(function (acc, next, index) { | ||
if (!next.types || !next.creators) { | ||
throw new Error("argument of index: " + index + " is invalid.\n mergeTypesAndCreators is expecting all arguments to be objects with types and creators"); | ||
} | ||
return { | ||
types: _extends({}, acc.types, next.types), | ||
creators: _extends({}, acc.creators, next.creators) | ||
}; | ||
}, { types: {}, creators: {} }); | ||
} | ||
exports.createActions = createActions; | ||
@@ -686,3 +653,2 @@ exports.createApiActions = createActions; | ||
exports.createReducers = createReducers; | ||
exports.mergeTypesAndCreators = mergeTypesAndCreators$1; | ||
exports.middlewares = requestMiddlewares; | ||
@@ -689,0 +655,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e.ReduxArc={})}(this,function(e){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t=Object.assign||function(e){for(var r=1;arguments.length>r;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},n=function(e,r){var t={};for(var n in e)0>r.indexOf(n)&&Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},o=function(e){return"string"==typeof e},i=function(e){return 2===e.length&&e.every(o)},a=function(e){return e.replace(/([A-Z])/g,"_$1").toUpperCase()},u=function(e){return[e+"_REQUEST",e+"_RESPONSE"]};function f(e,n){var o=null!==e&&"object"===(void 0===e?"undefined":r(e))?e:{},i=e.payload,a=e.meta,u=e.error;function f(e,r,o){var f={type:n},c=t({},a,r);Object.keys(c).length&&(f.meta=c);var l=void 0!==e?e:i;void 0!==l&&(f.payload=l);var d=void 0!==o?o:u;return void 0!==d&&(f.error=d),f}return Object.assign(f,o),Object.defineProperty(f,"name",{value:n+" action creator",writable:!1}),f}var c=function(e,r){return"function"==typeof e?e(r):e};function l(e,o){var i=e.payload,a=e.url,f=e.meta,l=n(e,["payload","url","meta"]),d=u(o);function s(e,n){var o,u,s=c(a,n),p=t({},f||{},n||{},l);return{type:d,payload:void 0!==e?e:i,meta:t({},p,{url:(o=s,u=p,o.replace(/(:)([A-Za-z0-9]*)/g,function(e,t,n){var i=r(u[n]);if("string"!==i&&"number"!==i)throw Error("Param "+n+" from url "+o+", not found in params object");return u[n]}))})}}return Object.assign(s,e),Object.defineProperty(s,"name",{value:o+" apiActionCreator",writable:!1}),s}var d={};function s(e,r){return Object.keys(e).reduce(function(n,o){var i,a,u=e[o]||d,c=(a=u)&&a.url?l:f;return t({},n,((i={})[o]=c(u,r[o]),i))},{})}function p(e){var r=u(e);return{REQUEST:r[0],RESPONSE:r[1]}}function y(e,r){return Object.keys(r).reduce(function(n,o){var i,u,f=(u=e[o])&&u.url?p:function(e){return e};return t({},n,((i={})[a(o)]=f(r[o]),i))},{})}function v(e,r){var n=a(e);!function(e,r){if(Object.keys(r).forEach(function(t){var n=r[t]||{},o=e+"_"+a(t);if(n.url&&"string"!=typeof n.url&&"function"!=typeof n.url)throw Error("Invalid url, "+n.url+", provided for "+o+", it should be a string or a function that returns a string");if(n.url&&("string"!=typeof n.method||!n.method.length))throw Error("Invalid method, "+n.method+", provided for "+o+", it should be a string");if(n.modifier&&"function"!=typeof n.modifier)throw Error("Invalid modifier handler, "+n.modifier+", provided for "+o+", it should be a function")}),!e||"string"!=typeof e)throw Error("Invalid namespace provided: "+e+", it should be a string")}(n,r);var o,i=(o=n,Object.keys(r).reduce(function(e,r){var n;return t({},e,((n={})[r]=o+"_"+a(r),n))},{}));return{creators:s(r,i),types:y(r,i)}}var b,h="object"==typeof global&&global&&global.Object===Object&&global,m="object"==typeof self&&self&&self.Object===Object&&self;h||m||Function("return this")(),Function.prototype.toString.call(Object);b="undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof module?module:Function("return this")();var g,w;"function"==typeof(w=b.Symbol)?w.observable?g=w.observable:(g=w("observable"),w.observable=g):g="@@observable";function j(){for(var e=arguments.length,r=Array(e),t=0;e>t;t++)r[t]=arguments[t];return 0===r.length?function(e){return e}:1===r.length?r[0]:r.reduce(function(e,r){return function(){return e(r.apply(void 0,arguments))}})}var O=["onRequest","onResponse"],E={};var A=function(e){return function(r){var t=e.map(function(e){return E[e]}).filter(function(e){return e.applyPoint===r});return function(e){return function(r){var n=t.map(function(r){return r(e)});return j.apply(void 0,n)(r)}}}};function P(e){return Array.isArray(e)?(e.forEach(function(e){if(!E[e])throw Error("Request Middleware "+e+" not registered. Perhaps you forgot to import its file")}),A(e)):A([])}var S={globalMiddlewares:E,register:function(e,r){var t;if(E[e])throw Error("Called register with middleware: "+e+" more than once");if(0>O.indexOf(r.applyPoint)){var n="Invalid applyPoint: "+r.applyPoint+", provided with middleware: "+e+".",o="The apply points available are: "+O.join(", ");throw Error(n+" "+o)}Object.assign(E,((t={})[e]=r,t))},getRequestMiddlewares:P,reset:function(){Object.keys(E).forEach(function(e){delete E[e]})}};function R(e){if(Object.keys(e).some(function(r,t){return!r||"undefined"===r||"function"!=typeof e[r]}))throw Error("All keys should be defined values should be present:\n"+function(e){if(!e)return"";var r=[];return Object.keys(e).forEach(function(t){var n;r.push("\t"+t+": "+(null===(n=e[t])?"null":void 0===n?"undefined":"string"==typeof n?"'"+n+"'":n)+",")}),"{\t\n"+r.join("\n")+"\n}"}(e))}e.createActions=v,e.createApiActions=v,e.createAsyncMiddleware=function(e){if("function"!=typeof e)throw Error("You must provide a asyncTask function to createAsyncMiddleware, with the following signature: \n done => (action, error, response) => done(action, error, response)");return function(n){return function(o){return function(a){var u=a.type,f=a.meta;if(!Array.isArray(u))return o(a);if(!i(u))throw Error("Expected type to be an array of two strings, request and response.");if(!f||"object"!==(void 0===f?"undefined":r(f)))throw Error("Expected meta to be an object");var c,l,d,s=P(a.meta.middlewares),p=a.type,y=p[0],v=p[1],b=[s("onRequest"),(c=y,l=e,function(e){return function(r){return function(n){e.dispatch({type:c,meta:n.meta,payload:n.payload});var o=t({payload:n.payload},n.meta);return l(e)(function(e,t){return r(n,e,t)})(o)}}}),s("onResponse")].map(function(e){return e(n)}),h=(d=v,function(e){return function(r,n,o){var i={type:d,meta:r.meta,payload:o};if(n){var a=t({},i,{error:!0,payload:n});return e.dispatch(a),n}return e.dispatch(i),o}})(n);return j.apply(void 0,b)(h)(a)}}}},e.createReducers=function(e,r){return R(r),function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:e,n=arguments[1];if(!n)return t;var o=r[n.type];return o?o(t,n):t}},e.mergeTypesAndCreators=function(){for(var e=arguments.length,r=Array(e),n=0;e>n;n++)r[n]=arguments[n];return r.reduce(function(e,r,n){if(!r.types||!r.creators)throw Error("argument of index: "+n+" is invalid.\n mergeTypesAndCreators is expecting all arguments to be objects with types and creators");return{types:t({},e.types,r.types),creators:t({},e.creators,r.creators)}},{types:{},creators:{}})},e.middlewares=S,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(r.ReduxArc={})}(this,function(r){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},t=Object.assign||function(r){for(var e=1;arguments.length>e;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r},n=function(r,e){var t={};for(var n in r)0>e.indexOf(n)&&Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n]);return t},o=function(r){return"string"==typeof r},i=function(r){return 2===r.length&&r.every(o)},u=function(r){return r.replace(/([A-Z])/g,"_$1").toUpperCase()},a=function(r){return[r+"_REQUEST",r+"_RESPONSE"]};function f(r,n){var o=null!==r&&"object"===(void 0===r?"undefined":e(r))?r:{},i=r.payload,u=r.meta,a=r.error;function f(r,e,o){var f={type:n},c=t({},u,e);Object.keys(c).length&&(f.meta=c);var l=void 0!==r?r:i;void 0!==l&&(f.payload=l);var d=void 0!==o?o:a;return void 0!==d&&(f.error=d),f}return Object.assign(f,o),Object.defineProperty(f,"name",{value:n+" action creator",writable:!1}),f}var c=function(r,e){return"function"==typeof r?r(e):r};function l(r,o){var i=r.payload,u=r.url,f=r.meta,l=n(r,["payload","url","meta"]),d=a(o);function p(r,n){var o,a,p=c(u,n),s=t({},f||{},n||{},l);return{type:d,payload:void 0!==r?r:i,meta:t({},s,{url:(o=p,a=s,o.replace(/(:)([A-Za-z0-9]*)/g,function(r,t,n){var i=e(a[n]);if("string"!==i&&"number"!==i)throw Error("Param "+n+" from url "+o+", not found in params object");return a[n]}))})}}return Object.assign(p,r),Object.defineProperty(p,"name",{value:o+" apiActionCreator",writable:!1}),p}var d={};function p(r,e){return Object.keys(r).reduce(function(n,o){var i,u,a=r[o]||d,c=(u=a)&&u.url?l:f;return t({},n,((i={})[o]=c(a,e[o]),i))},{})}function s(r){var e=a(r);return{REQUEST:e[0],RESPONSE:e[1]}}function y(r,e){return Object.keys(e).reduce(function(n,o){var i,a,f=(a=r[o])&&a.url?s:function(r){return r};return t({},n,((i={})[u(o)]=f(e[o]),i))},{})}function v(r,e){var n=u(r);!function(r,e){if(Object.keys(e).forEach(function(t){var n=e[t]||{},o=r+"_"+u(t);if(n.url&&"string"!=typeof n.url&&"function"!=typeof n.url)throw Error("Invalid url, "+n.url+", provided for "+o+", it should be a string or a function that returns a string");if(n.url&&("string"!=typeof n.method||!n.method.length))throw Error("Invalid method, "+n.method+", provided for "+o+", it should be a string");if(n.modifier&&"function"!=typeof n.modifier)throw Error("Invalid modifier handler, "+n.modifier+", provided for "+o+", it should be a function")}),!r||"string"!=typeof r)throw Error("Invalid namespace provided: "+r+", it should be a string")}(n,e);var o,i=(o=n,Object.keys(e).reduce(function(r,e){var n;return t({},r,((n={})[e]=o+"_"+u(e),n))},{}));return{creators:p(e,i),types:y(e,i)}}var b,m="object"==typeof global&&global&&global.Object===Object&&global,h="object"==typeof self&&self&&self.Object===Object&&self;m||h||Function("return this")(),Function.prototype.toString.call(Object);b="undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof module?module:Function("return this")();var g,w;"function"==typeof(w=b.Symbol)?w.observable?g=w.observable:(g=w("observable"),w.observable=g):g="@@observable";function j(){for(var r=arguments.length,e=Array(r),t=0;r>t;t++)e[t]=arguments[t];return 0===e.length?function(r){return r}:1===e.length?e[0]:e.reduce(function(r,e){return function(){return r(e.apply(void 0,arguments))}})}var O=["onRequest","onResponse"],E=function(r){return!!r};var A=function(r){return function(e){var t,n=r.filter((t=e,function(r){return r.applyPoint===t}));return function(r){return function(e){var t=n.map(function(e){return e(r)});return j.apply(void 0,t)(e)}}}};function S(r){if(!Array.isArray(r))return A([]);var e,t=r.filter(E);return(e=t).forEach(function(r){if("function"!=typeof r)throw Error("All middlewares should be functions: ["+e+"]");if(0>O.indexOf(r.applyPoint)){var t="Invalid applyPoint: "+r.applyPoint+", provided with middleware: "+r.name+".",n="The apply points available are: "+O.join(", ");throw Error(t+" "+n)}}),A(t)}var P={getRequestMiddlewares:S};function R(r){if(Object.keys(r).some(function(e,t){return!e||"undefined"===e||"function"!=typeof r[e]}))throw Error("All keys should be defined values should be present:\n"+function(r){if(!r)return"";var e=[];return Object.keys(r).forEach(function(t){var n;e.push("\t"+t+": "+(null===(n=r[t])?"null":void 0===n?"undefined":"string"==typeof n?"'"+n+"'":n)+",")}),"{\t\n"+e.join("\n")+"\n}"}(r))}r.createActions=v,r.createApiActions=v,r.createAsyncMiddleware=function(r){if("function"!=typeof r)throw Error("You must provide a asyncTask function to createAsyncMiddleware, with the following signature: \n done => (action, error, response) => done(action, error, response)");return function(n){return function(o){return function(u){var a=u.type,f=u.meta;if(!Array.isArray(a))return o(u);if(!i(a))throw Error("Expected type to be an array of two strings, request and response.");if(!f||"object"!==(void 0===f?"undefined":e(f)))throw Error("Expected meta to be an object");var c,l,d,p=S(u.meta.middlewares),s=u.type,y=s[0],v=s[1],b=[p("onRequest"),(c=y,l=r,function(r){return function(e){return function(n){r.dispatch({type:c,meta:n.meta,payload:n.payload});var o=t({payload:n.payload},n.meta);return l(r)(function(r,t){return e(n,r,t)})(o)}}}),p("onResponse")].map(function(r){return r(n)}),m=(d=v,function(r){return function(e,n,o){var i={type:d,meta:e.meta,payload:o};if(n){var u=t({},i,{error:!0,payload:n});return r.dispatch(u),n}return r.dispatch(i),o}})(n);return j.apply(void 0,b)(m)(u)}}}},r.createReducers=function(r,e){return R(e),function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:r,n=arguments[1];if(!n)return t;var o=e[n.type];return o?o(t,n):t}},r.middlewares=P,Object.defineProperty(r,"__esModule",{value:!0})}); |
'use strict'; | ||
exports.__esModule = true; | ||
exports.middlewares = exports.mergeTypesAndCreators = exports.createReducers = exports.createAsyncMiddleware = exports.createApiActions = exports.createActions = undefined; | ||
exports.middlewares = exports.createReducers = exports.createAsyncMiddleware = exports.createApiActions = exports.createActions = undefined; | ||
@@ -18,6 +18,2 @@ var _createActions = require('./createActions'); | ||
var _mergeTypesAndCreators = require('./mergeTypesAndCreators'); | ||
var _mergeTypesAndCreators2 = _interopRequireDefault(_mergeTypesAndCreators); | ||
var _requestMiddlewares = require('./requestMiddlewares'); | ||
@@ -33,3 +29,2 @@ | ||
exports.createReducers = _createReducers2.default; | ||
exports.mergeTypesAndCreators = _mergeTypesAndCreators2.default; | ||
exports.middlewares = _requestMiddlewares2.default; |
'use strict'; | ||
exports.__esModule = true; | ||
exports.globalMiddlewares = undefined; | ||
exports.reset = reset; | ||
exports.register = register; | ||
exports.validateMiddleware = validateMiddleware; | ||
@@ -13,9 +10,9 @@ exports.getRequestMiddlewares = getRequestMiddlewares; | ||
/** | ||
* This is a middleware manager, which is used to register, | ||
* get and run middlewares over actions and responses | ||
* This is a middleware manager, which is used to get get and run middlewares | ||
* over actions and responses | ||
* | ||
* A middleware is a middleware, and it should follow the signature bellow | ||
* A request middleware should follow the signature below | ||
* store => done => (action, error, response) => done(action, error, response); | ||
* A middleware must have a applyPoint property. The available apply points are: | ||
* 'onRequest', 'onResponse' | ||
* A middleware must have an applyPoint property. The available apply points are: | ||
* 'onRequest' and 'onResponse' | ||
*/ | ||
@@ -25,36 +22,33 @@ | ||
var globalMiddlewares = exports.globalMiddlewares = {}; | ||
function reset() { | ||
Object.keys(globalMiddlewares).forEach(function (key) { | ||
delete globalMiddlewares[key]; | ||
}); | ||
var notEmpty = function notEmpty(item) { | ||
return !!item; | ||
}; | ||
function register(name, middleware) { | ||
var _Object$assign; | ||
function validateMiddleware(middlewares) { | ||
middlewares.forEach(function (middleware) { | ||
if (typeof middleware !== 'function') { | ||
throw new Error('All middlewares should be functions: [' + middlewares + ']'); | ||
} | ||
if (globalMiddlewares[name]) { | ||
throw new Error('Called register with middleware: ' + name + ' more than once'); | ||
} | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
var invalid = 'Invalid applyPoint: ' + middleware.applyPoint + ', provided with middleware: ' + name + '.'; | ||
var available = 'The apply points available are: ' + applyPoints.join(', '); | ||
throw new Error(invalid + ' ' + available); | ||
} | ||
Object.assign(globalMiddlewares, (_Object$assign = {}, _Object$assign[name] = middleware, _Object$assign)); | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
var invalid = 'Invalid applyPoint: ' + middleware.applyPoint + ', provided with middleware: ' + middleware.name + '.'; | ||
var available = 'The apply points available are: ' + applyPoints.join(', '); | ||
throw new Error(invalid + ' ' + available); | ||
} | ||
}); | ||
} | ||
var get = function get(middlewareNames) { | ||
var withApplyPoint = function withApplyPoint(applyPoint) { | ||
return function (middleware) { | ||
return middleware.applyPoint === applyPoint; | ||
}; | ||
}; | ||
var get = function get(middlewares) { | ||
return function (applyPoint) { | ||
var middlewares = middlewareNames.map(function (name) { | ||
return globalMiddlewares[name]; | ||
}).filter(function (middleware) { | ||
return middleware.applyPoint === applyPoint; | ||
}); | ||
var applyMiddlewares = middlewares.filter(withApplyPoint(applyPoint)); | ||
return function (store) { | ||
return function (done) { | ||
var chain = middlewares.map(function (middleware) { | ||
var chain = applyMiddlewares.map(function (middleware) { | ||
return middleware(store); | ||
@@ -68,10 +62,2 @@ }); | ||
function validateMiddleware(middlewares) { | ||
middlewares.forEach(function (middlewareName) { | ||
if (!globalMiddlewares[middlewareName]) { | ||
throw new Error('Request Middleware ' + middlewareName + ' not registered. Perhaps you forgot to import its file'); | ||
} | ||
}); | ||
} | ||
function getRequestMiddlewares(middlewares) { | ||
@@ -81,11 +67,10 @@ if (!Array.isArray(middlewares)) { | ||
} | ||
validateMiddleware(middlewares); | ||
return get(middlewares); | ||
var actualMiddlewares = middlewares.filter(notEmpty); | ||
validateMiddleware(actualMiddlewares); | ||
return get(actualMiddlewares); | ||
}; | ||
exports.default = { | ||
globalMiddlewares: globalMiddlewares, | ||
register: register, | ||
getRequestMiddlewares: getRequestMiddlewares, | ||
reset: reset | ||
getRequestMiddlewares: getRequestMiddlewares | ||
}; |
{ | ||
"name": "redux-arc", | ||
"version": "0.6.2", | ||
"version": "0.7.0", | ||
"description": "Abstraction layer to simplify rest api calls in redux", | ||
@@ -40,28 +40,28 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.0.1", | ||
"babel-jest": "^21.2.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"cross-env": "^5.1.0", | ||
"eslint": "^4.9.0", | ||
"eslint-config-react-app": "^2.0.1", | ||
"eslint-plugin-flowtype": "^2.39.1", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-react": "^7.4.0", | ||
"gitbook-cli": "^2.3.2", | ||
"glob": "^7.1.1", | ||
"jest": "^21.2.1", | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.0", | ||
"babel-eslint": "8.2.2", | ||
"babel-jest": "22.4.1", | ||
"babel-plugin-external-helpers": "6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"babel-preset-env": "1.6.1", | ||
"babel-register": "6.26.0", | ||
"cross-env": "5.1.0", | ||
"eslint": "4.9.0", | ||
"eslint-config-react-app": "2.0.1", | ||
"eslint-plugin-flowtype": "2.39.1", | ||
"eslint-plugin-import": "2.2.0", | ||
"eslint-plugin-jsx-a11y": "5.1.1", | ||
"eslint-plugin-react": "7.4.0", | ||
"gitbook-cli": "2.3.2", | ||
"glob": "7.1.1", | ||
"jest": "22.4.2", | ||
"prettier": "1.8.2", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.51.8", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"rollup-plugin-replace": "^2.0.0", | ||
"rollup-plugin-uglify": "^2.0.1", | ||
"redux": "^3.7.1" | ||
"rimraf": "2.6.2", | ||
"rollup": "0.51.8", | ||
"rollup-plugin-babel": "3.0.2", | ||
"rollup-plugin-node-resolve": "3.0.0", | ||
"rollup-plugin-replace": "2.0.0", | ||
"rollup-plugin-uglify": "2.0.1", | ||
"redux": "3.7.1" | ||
}, | ||
@@ -68,0 +68,0 @@ "authors": [ |
<img src="https://github.com/viniciusdacal/redux-arc/blob/master/arc-64.png?raw=true" height="64" /> | ||
Create scalable, no-boilerplate redux Apps! | ||
@@ -88,2 +87,6 @@ | ||
## Demo Project | ||
Take a look at the demo project using Arc to build a Contacts CRUD: [github.com/redux-arc/redux-arc-demo](https://github.com/redux-arc/redux-arc-demo) | ||
# Getting started | ||
@@ -342,3 +345,6 @@ | ||
axios[method](url, params).then((error, response) => done(error, response)); | ||
axios[method](url, params).then( | ||
response => done(null, response), | ||
error => done(error, null), | ||
); | ||
}; | ||
@@ -345,0 +351,0 @@ |
import createActions from './createActions'; | ||
import createAsyncMiddleware from './createAsyncMiddleware'; | ||
import createReducers from './createReducers'; | ||
import mergeTypesAndCreators from './mergeTypesAndCreators'; | ||
import middlewares from './requestMiddlewares'; | ||
@@ -12,4 +11,3 @@ | ||
createReducers, | ||
mergeTypesAndCreators, | ||
middlewares, | ||
}; |
import { compose } from 'redux'; | ||
/** | ||
* This is a middleware manager, which is used to register, | ||
* get and run middlewares over actions and responses | ||
* This is a middleware manager, which is used to get get and run middlewares | ||
* over actions and responses | ||
* | ||
* A middleware is a middleware, and it should follow the signature bellow | ||
* A request middleware should follow the signature below | ||
* store => done => (action, error, response) => done(action, error, response); | ||
* A middleware must have a applyPoint property. The available apply points are: | ||
* 'onRequest', 'onResponse' | ||
* A middleware must have an applyPoint property. The available apply points are: | ||
* 'onRequest' and 'onResponse' | ||
*/ | ||
@@ -15,30 +15,25 @@ | ||
export const globalMiddlewares = {}; | ||
const notEmpty = item => !!item; | ||
export function reset() { | ||
Object.keys(globalMiddlewares).forEach((key) => { | ||
delete globalMiddlewares[key]; | ||
export function validateMiddleware(middlewares) { | ||
middlewares.forEach(middleware => { | ||
if (typeof middleware !== 'function') { | ||
throw new Error(`All middlewares should be functions: [${middlewares}]`); | ||
} | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
const invalid = `Invalid applyPoint: ${middleware.applyPoint}, provided with middleware: ${middleware.name}.`; | ||
const available = `The apply points available are: ${applyPoints.join(', ')}`; | ||
throw new Error(`${invalid} ${available}`); | ||
} | ||
}); | ||
}; | ||
} | ||
export function register(name, middleware) { | ||
if (globalMiddlewares[name]) { | ||
throw new Error(`Called register with middleware: ${name} more than once`); | ||
} | ||
const withApplyPoint = applyPoint => middleware => middleware.applyPoint === applyPoint; | ||
if (applyPoints.indexOf(middleware.applyPoint) < 0) { | ||
const invalid = `Invalid applyPoint: ${middleware.applyPoint}, provided with middleware: ${name}.`; | ||
const available = `The apply points available are: ${applyPoints.join(', ')}`; | ||
throw new Error(`${invalid} ${available}`); | ||
} | ||
Object.assign(globalMiddlewares, { [name]: middleware }) | ||
} | ||
const get = middlewares => (applyPoint) => { | ||
const applyMiddlewares = middlewares.filter(withApplyPoint(applyPoint)); | ||
const get = middlewareNames => (applyPoint) => { | ||
const middlewares = middlewareNames | ||
.map(name => globalMiddlewares[name]) | ||
.filter(middleware => middleware.applyPoint === applyPoint); | ||
return store => done => { | ||
const chain = middlewares.map(middleware => middleware(store)); | ||
const chain = applyMiddlewares.map(middleware => middleware(store)); | ||
return compose(...chain)(done); | ||
@@ -48,10 +43,2 @@ }; | ||
export function validateMiddleware(middlewares) { | ||
middlewares.forEach(middlewareName => { | ||
if (!globalMiddlewares[middlewareName]) { | ||
throw new Error(`Request Middleware ${middlewareName} not registered. Perhaps you forgot to import its file`) | ||
} | ||
}); | ||
} | ||
export function getRequestMiddlewares(middlewares) { | ||
@@ -61,11 +48,10 @@ if (!Array.isArray(middlewares)) { | ||
} | ||
validateMiddleware(middlewares); | ||
return get(middlewares); | ||
const actualMiddlewares = middlewares.filter(notEmpty); | ||
validateMiddleware(actualMiddlewares); | ||
return get(actualMiddlewares); | ||
}; | ||
export default { | ||
globalMiddlewares, | ||
register, | ||
getRequestMiddlewares, | ||
reset, | ||
}; |
457
125954
35
1319