UNPKG

redux-grim

Version:

Generator for asynchronous Redux endpoint actions and reducers

3 lines (2 loc) 18.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self)["redux-grim"]={})}(this,(function(t){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function n(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function o(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?n(Object(o),!0).forEach((function(e){r(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function i(t,e){return c(t)||function(t,e){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var r=[],n=!0,o=!1,i=void 0;try{for(var a,c=t[Symbol.iterator]();!(n=(a=c.next()).done)&&(r.push(a.value),!e||r.length!==e);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==c.return||c.return()}finally{if(o)throw i}}return r}(t,e)||s()}function a(t){return function(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}}(t)||u(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function c(t){if(Array.isArray(t))return t}function u(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function s(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function f(t,e){t.on=function(r,n){if(!r in e)throw"invalid hook ".concat(r);var o=e[r];return e[r]=o?function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),i=1;i<e;i++)r[i-1]=arguments[i];return n.apply(void 0,[o.apply(void 0,[t].concat(r))].concat(r))}:n,t}}var l=function(t){return"".concat(t).concat(".start")},p=function(t){return"".concat(t).concat(".success")},y=function(t){return"".concat(t).concat(".error")},h=function(t){return"".concat(t).concat(".set")},d=function(t){return t.endsWith(".success")},v=function(t){return t.endsWith(".set")},b=["put","patch","post"],m=/\(([^)]+)\)/g,g=/\[([^)]+)\]/g,w={start:function(t,e,r,n){return t},success:function(t,e,r,n){return t},error:function(t,e,r,n){return t},all:function(t,e,r,n){return t}},O={apiFetch:function(t,e,r){return fetch(e,{method:t.toUpperCase(),body:JSON.stringify(r)})}};function j(t,e){for(var r,n=new Set;r=m.exec(e);){var o=r[1],i=o.indexOf(".");n.add(-1===i?o:o.substr(0,o.indexOf(".")))}return function(t,e){return b.includes(t)||"delete"===t&&e.includes("[")}(t,e)&&n.add("body"),n.size?a(n):[]}function A(t,e){var r=function(t){var e=t.replace(m,"'+params.$1+'").replace("[]","").replace(g,"'+params.body.$1+'");return new Function("params","return '"+e+"';")}(t);return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];var i=e.reduce((function(t,e,r){return t[e]=n[r],t}),{});return{url:r(i),params:i,restArgs:n.slice(e.length)}}}function P(t,r,n,o,i){var a=n.indexOf("[")>-1||["post","put","patch"].includes(r);o.length<i.length&&console.warn("For api call ".concat(t,", ").concat(r,", ").concat(n,",\n Expected parameters: ").concat(i.join(", "),"\n Actual parameters: ").concat(o.join(", "),"\n ")),i.forEach((function(c,u){var s=e(o[u]);a&&u===i.length-1?"object"!==s&&console.warn("".concat(t,", ").concat(r,", ").concat(n,": Expected parameter ").concat(c," to be an object. Actual value: ").concat(o[u]," ").concat(s)):"string"!==s&&"number"!==s&&console.warn("".concat(t,", ").concat(r,", ").concat(n,": Expected parameter ").concat(c," to be a string or number. Actual value: ").concat(o[u]))}))}var T=function(t,e){return t(e={exports:{}},e.exports),e.exports}((function(t,e){var r;r=function t(e){var r,n="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element"),o=60103,i={use_static:!1};function a(t){var e=Object.getPrototypeOf(t);return e?Object.create(e):{}}function c(t,e,r){Object.defineProperty(t,e,{enumerable:!1,configurable:!1,writable:!1,value:r})}function u(t,e){c(t,e,(function(){throw new b("The "+e+" method cannot be invoked on an Immutable data structure.")}))}"object"!=typeof(r=e)||Array.isArray(r)||null===r||void 0!==e.use_static&&(i.use_static=Boolean(e.use_static));var s="__immutable_invariants_hold";function f(t){return"object"!=typeof t||null===t||Boolean(Object.getOwnPropertyDescriptor(t,s))}function l(t,e){return t===e||t!=t&&e!=e}function p(t){return!(null===t||"object"!=typeof t||Array.isArray(t)||t instanceof Date)}var y=["setPrototypeOf"],h=y.concat(["push","pop","sort","splice","shift","unshift","reverse"]),d=["keys"].concat(["map","filter","slice","concat","reduce","reduceRight"]),v=y.concat(["setDate","setFullYear","setHours","setMilliseconds","setMinutes","setMonth","setSeconds","setTime","setUTCDate","setUTCFullYear","setUTCHours","setUTCMilliseconds","setUTCMinutes","setUTCMonth","setUTCSeconds","setYear"]);function b(t){this.name="MyError",this.message=t,this.stack=(new Error).stack}function m(t,e){for(var r in c(t,s,!0),e)e.hasOwnProperty(r)&&u(t,e[r]);return Object.freeze(t),t}function g(t,e){var r=t[e];c(t,e,(function(){return q(r.apply(t,arguments))}))}function w(t,e,r){var n=r&&r.deep;if(t in this&&(n&&this[t]!==e&&p(e)&&p(this[t])&&(e=q.merge(this[t],e,{deep:!0,mode:"replace"})),l(this[t],e)))return this;var o=x.call(this);return o[t]=q(e),A(o)}b.prototype=new Error,b.prototype.constructor=Error;var O=q([]);function j(t,e,r){var n=t[0];if(1===t.length)return w.call(this,n,e,r);var o,i=t.slice(1),a=this[n];if("object"==typeof a&&null!==a)o=q.setIn(a,i,e);else{var c=i[0];o=""!==c&&isFinite(c)?j.call(O,i,e):U.call(R,i,e)}if(n in this&&a===o)return this;var u=x.call(this);return u[n]=o,A(u)}function A(t){for(var e in d)d.hasOwnProperty(e)&&g(t,d[e]);i.use_static||(c(t,"flatMap",T),c(t,"asObject",I),c(t,"asMutable",x),c(t,"set",w),c(t,"setIn",j),c(t,"update",z),c(t,"updateIn",B),c(t,"getIn",N));for(var r=0,n=t.length;r<n;r++)t[r]=q(t[r]);return m(t,h)}function P(){return new Date(this.getTime())}function T(t){if(0===arguments.length)return this;var e,r=[],n=this.length;for(e=0;e<n;e++){var o=t(this[e],e,this);Array.isArray(o)?r.push.apply(r,o):r.push(o)}return A(r)}function S(t){if(void 0===t&&0===arguments.length)return this;if("function"!=typeof t){var e=Array.isArray(t)?t.slice():Array.prototype.slice.call(arguments);e.forEach((function(t,e,r){"number"==typeof t&&(r[e]=t.toString())})),t=function(t,r){return-1!==e.indexOf(r)}}var r=a(this);for(var n in this)this.hasOwnProperty(n)&&!1===t(this[n],n)&&(r[n]=this[n]);return Y(r)}function x(t){var e,r,n=[];if(t&&t.deep)for(e=0,r=this.length;e<r;e++)n.push(E(this[e]));else for(e=0,r=this.length;e<r;e++)n.push(this[e]);return n}function I(t){"function"!=typeof t&&(t=function(t){return t});var e,r={},n=this.length;for(e=0;e<n;e++){var o=t(this[e],e,this),i=o[0],a=o[1];r[i]=a}return Y(r)}function E(t){return!t||"object"!=typeof t||!Object.getOwnPropertyDescriptor(t,s)||t instanceof Date?t:q.asMutable(t,{deep:!0})}function k(t,e){for(var r in t)Object.getOwnPropertyDescriptor(t,r)&&(e[r]=t[r]);return e}function D(t,e){if(0===arguments.length)return this;if(null===t||"object"!=typeof t)throw new TypeError("Immutable#merge can only be invoked with objects or arrays, not "+JSON.stringify(t));var r,n,o=Array.isArray(t),i=e&&e.deep,c=e&&e.mode||"merge",u=e&&e.merger;function s(t,n,o){var c,s=q(n[o]),f=u&&u(t[o],s,e),y=t[o];void 0===r&&void 0===f&&t.hasOwnProperty(o)&&l(s,y)||l(y,c=f||(i&&p(y)&&p(s)?q.merge(y,s,e):s))&&t.hasOwnProperty(o)||(void 0===r&&(r=k(t,a(t))),r[o]=c)}if(o)for(var f=0,y=t.length;f<y;f++){var h=t[f];for(n in h)h.hasOwnProperty(n)&&s(void 0!==r?r:this,h,n)}else{for(n in t)Object.getOwnPropertyDescriptor(t,n)&&s(this,t,n);"replace"===c&&function(t,e){for(var n in t)e.hasOwnProperty(n)||(void 0===r&&(r=k(t,a(t))),delete r[n])}(this,t)}return void 0===r?this:Y(r)}function M(t,e){var r=e&&e.deep;if(0===arguments.length)return this;if(null===t||"object"!=typeof t)throw new TypeError("Immutable#replace can only be invoked with objects or arrays, not "+JSON.stringify(t));return q.merge(this,t,{deep:r,mode:"replace"})}var _,F,C,R=q({});function U(t,e,r){if(!Array.isArray(t)||0===t.length)throw new TypeError('The first argument to Immutable#setIn must be an array containing at least one "key" string.');var n=t[0];if(1===t.length)return $.call(this,n,e,r);var o,i=t.slice(1),c=this[n];if(o=this.hasOwnProperty(n)&&"object"==typeof c&&null!==c?q.setIn(c,i,e):U.call(R,i,e),this.hasOwnProperty(n)&&c===o)return this;var u=k(this,a(this));return u[n]=o,Y(u)}function $(t,e,r){var n=r&&r.deep;if(this.hasOwnProperty(t)&&(n&&this[t]!==e&&p(e)&&p(this[t])&&(e=q.merge(this[t],e,{deep:!0,mode:"replace"})),l(this[t],e)))return this;var o=k(this,a(this));return o[t]=q(e),Y(o)}function z(t,e){var r=Array.prototype.slice.call(arguments,2),n=this[t];return q.set(this,t,e.apply(n,[n].concat(r)))}function K(t,e){for(var r=0,n=e.length;null!=t&&r<n;r++)t=t[e[r]];return r&&r==n?t:void 0}function B(t,e){var r=Array.prototype.slice.call(arguments,2),n=K(this,t);return q.setIn(this,t,e.apply(n,[n].concat(r)))}function N(t,e){var r=K(this,t);return void 0===r?e:r}function H(t){var e,r=a(this);if(t&&t.deep)for(e in this)this.hasOwnProperty(e)&&(r[e]=E(this[e]));else for(e in this)this.hasOwnProperty(e)&&(r[e]=this[e]);return r}function J(){return{}}function Y(t){return i.use_static||(c(t,"merge",D),c(t,"replace",M),c(t,"without",S),c(t,"asMutable",H),c(t,"set",$),c(t,"setIn",U),c(t,"update",z),c(t,"updateIn",B),c(t,"getIn",N)),m(t,y)}function q(t,e,r){if(f(t)||function(t){return"object"==typeof t&&null!==t&&(t.$$typeof===o||t.$$typeof===n)}(t)||function(t){return"undefined"!=typeof File&&t instanceof File}(t)||function(t){return"undefined"!=typeof Blob&&t instanceof Blob}(t)||function(t){return t instanceof Error}(t))return t;if(function(t){return"object"==typeof t&&"function"==typeof t.then}(t))return t.then(q);if(Array.isArray(t))return A(t.slice());if(t instanceof Date)return a=new Date(t.getTime()),i.use_static||c(a,"asMutable",P),m(a,v);var a,u=e&&e.prototype,s=(u&&u!==Object.prototype?function(){return Object.create(u)}:J)();if(null==r&&(r=64),r<=0)throw new b("Attempt to construct Immutable from a deeply nested object was detected. Have you tried to wrap an object with circular references (e.g. React element)? See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");for(var l in r-=1,t)Object.getOwnPropertyDescriptor(t,l)&&(s[l]=q(t[l],void 0,r));return Y(s)}function W(t){return function(){var e=[].slice.call(arguments),r=e.shift();return t.apply(r,e)}}function G(t,e){return function(){var r=[].slice.call(arguments),n=r.shift();return Array.isArray(n)?e.apply(n,r):t.apply(n,r)}}return q.from=q,q.isImmutable=f,q.ImmutableError=b,q.merge=W(D),q.replace=W(M),q.without=W(S),q.asMutable=(_=H,F=x,C=P,function(){var t=[].slice.call(arguments),e=t.shift();return Array.isArray(e)?F.apply(e,t):e instanceof Date?C.apply(e,t):_.apply(e,t)}),q.set=G($,w),q.setIn=G(U,j),q.update=W(z),q.updateIn=W(B),q.getIn=W(N),q.flatMap=W(T),q.asObject=W(I),i.use_static||(q.static=t({use_static:!0})),Object.freeze(q),q}(),t.exports=r})),S=T.static;T.Immutable;var x,I="id",E="idProp",k="to",D="nestedProps";function M(t,e){for(var r=t[e],n=r&&e;r&&r[k];){if(r[E]||r[D])throw"'".concat(k,"' aliases cannot be used with '").concat(E,"' and '").concat(D,"' properties");n=r[k],r=t[r[k]]}return{entityType:n,rule:r}}var _,F,C,R=(r(x={entityType:"string"},E,"string?"),r(x,k,"string?"),r(x,D,"string{}?"),x),U=/(\{\})?(\??)$/,$=function(t,r){return t.every((function(t){var n=new Set([].concat(a(Object.keys(R)),a(Object.keys(t))));return Array.from(n).every((function(n){var o=r[n];if(void 0!==o){var i=function(t){return c(t)||u(t)||s()}(o.split(U)),a=i[0],f=i.slice(1),l=f.indexOf("?")>-1,p=f.indexOf("{}")>-1,y=t[n],h=e(y);return!(!l||void 0!==y)||(p?"object"===h&&Object.values(y).every((function(t){return e(t)===a})):void 0!==y&&e(h)===a)}}))}))};function z(t){if(_&&t===F&&t.length===C)return _;if(!$(t,R))throw"Error: rules do not match schema";return _=t.reduce((function(t,e){return t[e.entityType]=e,t}),{}),F=t,C=t.length,_}t.createSelector=function(t,r,n,c){var u=z(t);if(!u[n])throw"Key not found ".concat(n);var s,f,l=a(new Set(function t(e,r){var n=M(e,r),o=n.rule,i=n.entityType,c=i&&[i];if(o&&o[D])for(var u=0,s=Object.values(o[D]);u<s.length;u++){var f=s[u];c=[].concat(a(c),a(t(e,f)))}return c}(u,n))),p=[function(t){var r=c(t);return r&&"object"===e(r)&&"data"in r?r.data:r}].concat(a(l.map((function(t){return function(e){return r(e)[t]}}))));return function(t){var a,c,l=p.map((function(e){return e(t)}));return(a=l)===(c=s)||a&&c&&a.length===c.length&&a.every((function(t,e){return t===c[e]}))||(s=l,f=S.from(function t(r,n,a,c){if(!a)return a;var u=M(r,n),s=u.rule,f=u.entityType;if(Array.isArray(a))return a.map((function(e){return t(r,f,e,c)}));if("object"===e(a))return Object.entries(a).reduce((function(e,n){var o=i(n,2),a=o[0],u=o[1];return e[a]=t(r,f,u,c),e}),{});var l=c[f]&&c[f][a];if(!l)return a;var p=o({},l);if(s[D])for(var y=0,h=Object.entries(s[D]);y<h.length;y++){var d=i(h[y],2),v=d[0],b=d[1];p[v]=t(r,b,p[v],c)}return p}(u,n,l[0],r(t)))),f}},t.getNormalizerMiddleware=function(t,r){var n=z(t);return function(){return function(t){return function(a){if(d(a.type)||v(a.type)){var c=a.payload,u=a.meta,s=u&&u.entityType;s&&c&&n[s]&&(u.entities={},a.payload=function t(r,n,a,c){if(!r[n])throw"Unrecognised entityType: ".concat(n);var u=M(r,n),s=u.rule,f=u.entityType;if(Array.isArray(a))return a.map((function(e){return t(r,f,e,c)}));if(!a||"object"!==e(a))return a;var l=a[s[E]||I];if(!l)return Object.entries(a).reduce((function(e,n){var o=i(n,2),a=o[0],u=o[1];return e[a]=t(r,f,u,c),e}),{});var p=o({},a);if(c[f]=c[f]||{},c[f][l]=p,s[D])for(var y=0,h=Object.entries(s[D]);y<h.length;y++){var d=i(h[y],2),v=d[0],b=d[1];p[v]=t(r,b,a[v],c)}return l}(n,s,c,u.entities),r&&r(s,c))}return t(a)}}}},t.makeActionCreator=function(t,e,r){var n,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},c=j(e,r);i.debug&&function(t,e,r,n){console.log("Created action ".concat(t,", ").concat(r,", ").concat(e,"(").concat(n.join(", "),")"))}(t,e,r,c);var u=o({},w),s=o({},O),h=A(r,c),d=function(){for(var o=arguments.length,f=new Array(o),d=0;d<o;d++)f[d]=arguments[d];return function(o){var d,v,b,m,g,w,O,j,A,T;return regeneratorRuntime.async((function(S){for(;;)switch(S.prev=S.next){case 0:if(i.debug&&P(t,e,r,f,c),d=h.apply(void 0,f),v=d.url,b=d.params,m=d.restArgs,g={type:l(t),meta:{entityType:t,method:e}},g=u.start(g,b,m,i),g=u.all(g,b,m,i),o(g),S.prev=6,void 0!==n&&(w="function"==typeof n?n.apply(void 0,f):n,(O=void 0===w?void 0:{body:w})&&console.info("Mocking ".concat(e," ").concat(r),w)),S.t0=O,S.t0){S.next=13;break}return S.next=12,regeneratorRuntime.awrap(s.apiFetch.apply(s,[e,v,b.body].concat(a(m))));case 12:S.t0=S.sent;case 13:return O=S.t0,j={type:p(t),payload:O&&O.body,meta:{entityType:t,method:e}},j=u.success(j,b,m,i,O),j=u.all(j,b,m,i,O),A=j.payload,o(j),S.abrupt("return",A);case 22:throw S.prev=22,S.t1=S.catch(6),T={type:y(t),payload:S.t1,error:!0,meta:{entityType:t,method:e}},T=u.error(T,b,m,i,S.t1),T=u.all(T,b,m,i,S.t1),o(T),S.t1;case 29:case"end":return S.stop()}}),null,null,[[6,22]])}};return f(d,u),d.apiFetch=function(t){return s.apiFetch=t,d},d.mock=function(t){return n=t,d},d.unmock=function(){return n=void 0,d},d},t.makeReducer=function(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i={start:function(t,e,r,n){return t},success:function(t,e,r,n){return t},error:function(t,e,r,n){return t},all:function(t,e,r,n){return t},default:function(t,e,r){return t}},a=l(t),c=p(t),u=y(t),s=n.dataKey||"data",h=n.errorKey||"error",d=n.isRequestingKey||"isRequesting",v=n.isErroredKey||"isErrored",b=S.from((r(e={},s,void 0),r(e,h,null),r(e,d,!1),r(e,v,!1),e)),m=function(){var e,f,l,p,y=arguments.length>0&&void 0!==arguments[0]?arguments[0]:b,m=arguments.length>1?arguments[1]:void 0;switch(m.type){case a:return p=o({},y,(r(e={},d,!0),r(e,v,!1),r(e,h,null),e)),p=i.start(p,y,m,n),p=i.all(p,y,m,n),S.from(p);case c:return p=o({},y,(r(f={},s,m.payload),r(f,d,!1),r(f,v,!1),f)),p=i.success(p,y,m,n),p=i.all(p,y,m,n),S.from(p);case u:return p=o({},y,(r(l={},d,!1),r(l,v,!0),r(l,h,m.payload),l)),p=i.error(p,y,m,n),p=i.all(p,y,m,n),S.from(p);case"".concat(t,".set"):var g=["data"];return m.meta.path&&(g=g.concat(m.meta.path)),S.setIn(y,g,m.payload);case"grim.reset":var w=m.payload,O=w.includes,j=w.excludes;if(O){if(O.includes(t))return b}else if(!j||!j.includes(t))return b;default:return p=i.default?i.default(y,m,n):p,S.from(p)}};return f(m,i),m.modifyInitialState=function(t){return b=t(b),m},m},t.normalizationReducer=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=(arguments.length>1?arguments[1]:void 0).meta;if(r)if("delete"===r.method){if(t[r.entityType]&&r.id)return S.set(t,r.entityType,S.without(t[r.entityType],r.id))}else if(r.entities&&"object"===e(r.entities)){var n=t;return Object.keys(r.entities).forEach((function(e){return n=S.set(n,e,S.merge({},[t[e]||{},r.entities[e]]))})),n}return t},t.processRules=z,t.resetAction=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.includes;return{type:"grim.reset",payload:{excludes:t.excludes,includes:e}}},t.setAction=function(t,e,r){return{type:h(t),payload:e,meta:{entityType:t,path:r}}},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=redux-grim.js.map