UNPKG

redux-grim

Version:

Generator for asynchronous Redux endpoint actions and reducers

3 lines (2 loc) 17.8 kB
function t(e){return(t="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})(e)}function e(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function r(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 n(t){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?r(Object(o),!0).forEach((function(r){e(t,r,o[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):r(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function o(t,e){return a(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)||u()}function i(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)||c(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function a(t){if(Array.isArray(t))return t}function c(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function u(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function s(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 f=function(t){return"".concat(t).concat(".start")},l=function(t){return"".concat(t).concat(".success")},p=function(t){return"".concat(t).concat(".error")},y=function(t){return"".concat(t).concat(".set")},h=function(t){return t.endsWith(".success")},d=function(t){return t.endsWith(".set")},v=["put","patch","post"],b=/\(([^)]+)\)/g,m=/\[([^)]+)\]/g,g={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}},w={apiFetch:function(t,e,r){return fetch(e,{method:t.toUpperCase(),body:JSON.stringify(r)})}};function O(t,e){for(var r,n=new Set;r=b.exec(e);){var o=r[1],a=o.indexOf(".");n.add(-1===a?o:o.substr(0,o.indexOf(".")))}return function(t,e){return v.includes(t)||"delete"===t&&e.includes("[")}(t,e)&&n.add("body"),n.size?i(n):[]}function j(t,e){var r=function(t){var e=t.replace(b,"'+params.$1+'").replace("[]","").replace(m,"'+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 A(t,e,r){var o,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},c=O(e,r);a.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=n({},g),y=n({},w),h=j(r,c),d=function(){for(var n=arguments.length,s=new Array(n),d=0;d<n;d++)s[d]=arguments[d];return function(n){var d,v,b,m,g,w,O,j,A,P;return regeneratorRuntime.async((function(S){for(;;)switch(S.prev=S.next){case 0:if(a.debug&&T(t,e,r,s,c),d=h.apply(void 0,s),v=d.url,b=d.params,m=d.restArgs,g={type:f(t),meta:{entityType:t,method:e}},g=u.start(g,b,m,a),g=u.all(g,b,m,a),n(g),S.prev=6,void 0!==o&&(w="function"==typeof o?o.apply(void 0,s):o,(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(y.apiFetch.apply(y,[e,v,b.body].concat(i(m))));case 12:S.t0=S.sent;case 13:return O=S.t0,j={type:l(t),payload:O&&O.body,meta:{entityType:t,method:e}},j=u.success(j,b,m,a,O),j=u.all(j,b,m,a,O),A=j.payload,n(j),S.abrupt("return",A);case 22:throw S.prev=22,S.t1=S.catch(6),P={type:p(t),payload:S.t1,error:!0,meta:{entityType:t,method:e}},P=u.error(P,b,m,a,S.t1),P=u.all(P,b,m,a,S.t1),n(P),S.t1;case 29:case"end":return S.stop()}}),null,null,[[6,22]])}};return s(d,u),d.apiFetch=function(t){return y.apiFetch=t,d},d.mock=function(t){return o=t,d},d.unmock=function(){return o=void 0,d},d}function T(e,r,n,o,i){var a=n.indexOf("[")>-1||["post","put","patch"].includes(r);o.length<i.length&&console.warn("For api call ".concat(e,", ").concat(r,", ").concat(n,",\n Expected parameters: ").concat(i.join(", "),"\n Actual parameters: ").concat(o.join(", "),"\n ")),i.forEach((function(c,u){var s=t(o[u]);a&&u===i.length-1?"object"!==s&&console.warn("".concat(e,", ").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(e,", ").concat(r,", ").concat(n,": Expected parameter ").concat(c," to be a string or number. Actual value: ").concat(o[u]))}))}var P=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=I.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):$.call(U,i,e)}if(n in this&&a===o)return this;var u=I.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",P),c(t,"asObject",x),c(t,"asMutable",I),c(t,"set",w),c(t,"setIn",j),c(t,"update",K),c(t,"updateIn",z),c(t,"getIn",H));for(var r=0,n=t.length;r<n;r++)t[r]=q(t[r]);return m(t,h)}function T(){return new Date(this.getTime())}function P(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 I(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 x(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 D(t,e){for(var r in t)Object.getOwnPropertyDescriptor(t,r)&&(e[r]=t[r]);return e}function k(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=D(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=D(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,U=q({});function $(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 R.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):$.call(U,i,e),this.hasOwnProperty(n)&&c===o)return this;var u=D(this,a(this));return u[n]=o,Y(u)}function R(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=D(this,a(this));return o[t]=q(e),Y(o)}function K(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 B(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 z(t,e){var r=Array.prototype.slice.call(arguments,2),n=B(this,t);return q.setIn(this,t,e.apply(n,[n].concat(r)))}function H(t,e){var r=B(this,t);return void 0===r?e:r}function J(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 N(){return{}}function Y(t){return i.use_static||(c(t,"merge",k),c(t,"replace",M),c(t,"without",S),c(t,"asMutable",J),c(t,"set",R),c(t,"setIn",$),c(t,"update",K),c(t,"updateIn",z),c(t,"getIn",H)),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",T),m(a,v);var a,u=e&&e.prototype,s=(u&&u!==Object.prototype?function(){return Object.create(u)}:N)();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(k),q.replace=W(M),q.without=W(S),q.asMutable=(_=J,F=I,C=T,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(R,w),q.setIn=G($,j),q.update=W(K),q.updateIn=W(z),q.getIn=W(H),q.flatMap=W(P),q.asObject=W(x),i.use_static||(q.static=t({use_static:!0})),Object.freeze(q),q}(),t.exports=r})),S=P.static;P.Immutable;function I(t){var r,o=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=f(t),c=l(t),u=p(t),y=o.dataKey||"data",h=o.errorKey||"error",d=o.isRequestingKey||"isRequesting",v=o.isErroredKey||"isErrored",b=S.from((e(r={},y,void 0),e(r,h,null),e(r,d,!1),e(r,v,!1),r)),m=function(){var r,s,f,l,p=arguments.length>0&&void 0!==arguments[0]?arguments[0]:b,m=arguments.length>1?arguments[1]:void 0;switch(m.type){case a:return l=n({},p,(e(r={},d,!0),e(r,v,!1),e(r,h,null),r)),l=i.start(l,p,m,o),l=i.all(l,p,m,o),S.from(l);case c:return l=n({},p,(e(s={},y,m.payload),e(s,d,!1),e(s,v,!1),s)),l=i.success(l,p,m,o),l=i.all(l,p,m,o),S.from(l);case u:return l=n({},p,(e(f={},d,!1),e(f,v,!0),e(f,h,m.payload),f)),l=i.error(l,p,m,o),l=i.all(l,p,m,o),S.from(l);case"".concat(t,".set"):var g=["data"];return m.meta.path&&(g=g.concat(m.meta.path)),S.setIn(p,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 l=i.default?i.default(p,m,o):l,S.from(l)}};return s(m,i),m.modifyInitialState=function(t){return b=t(b),m},m}function x(t,e,r){return{type:y(t),payload:e,meta:{entityType:t,path:r}}}function E(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.includes;return{type:"grim.reset",payload:{excludes:t.excludes,includes:e}}}var D,k="id",M="idProp",_="to",F="nestedProps";function C(t,e){for(var r=t[e],n=r&&e;r&&r[_];){if(r[M]||r[F])throw"'".concat(_,"' aliases cannot be used with '").concat(M,"' and '").concat(F,"' properties");n=r[_],r=t[r[_]]}return{entityType:n,rule:r}}var U,$,R,K=(e(D={entityType:"string"},M,"string?"),e(D,_,"string?"),e(D,F,"string{}?"),D),B=/(\{\})?(\??)$/,z=function(e,r){return e.every((function(e){var n=new Set([].concat(i(Object.keys(K)),i(Object.keys(e))));return Array.from(n).every((function(n){var o=r[n];if(void 0!==o){var i=function(t){return a(t)||c(t)||u()}(o.split(B)),s=i[0],f=i.slice(1),l=f.indexOf("?")>-1,p=f.indexOf("{}")>-1,y=e[n],h=t(y);return!(!l||void 0!==y)||(p?"object"===h&&Object.values(y).every((function(e){return t(e)===s})):void 0!==y&&t(h)===s)}}))}))};function H(t){if(U&&t===$&&t.length===R)return U;if(!z(t,K))throw"Error: rules do not match schema";return U=t.reduce((function(t,e){return t[e.entityType]=e,t}),{}),$=t,R=t.length,U}function J(e,r,a,c){var u=H(e);if(!u[a])throw"Key not found ".concat(a);var s,f,l=i(new Set(function t(e,r){var n=C(e,r),o=n.rule,a=n.entityType,c=a&&[a];if(o&&o[F])for(var u=0,s=Object.values(o[F]);u<s.length;u++){var f=s[u];c=[].concat(i(c),i(t(e,f)))}return c}(u,a))),p=[function(e){var r=c(e);return r&&"object"===t(r)&&"data"in r?r.data:r}].concat(i(l.map((function(t){return function(e){return r(e)[t]}}))));return function(e){var i,c,l=p.map((function(t){return t(e)}));return(i=l)===(c=s)||i&&c&&i.length===c.length&&i.every((function(t,e){return t===c[e]}))||(s=l,f=S.from(function e(r,i,a,c){if(!a)return a;var u=C(r,i),s=u.rule,f=u.entityType;if(Array.isArray(a))return a.map((function(t){return e(r,f,t,c)}));if("object"===t(a))return Object.entries(a).reduce((function(t,n){var i=o(n,2),a=i[0],u=i[1];return t[a]=e(r,f,u,c),t}),{});var l=c[f]&&c[f][a];if(!l)return a;var p=n({},l);if(s[F])for(var y=0,h=Object.entries(s[F]);y<h.length;y++){var d=o(h[y],2),v=d[0],b=d[1];p[v]=e(r,b,p[v],c)}return p}(u,a,l[0],r(e)))),f}}function N(){var e=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(e[r.entityType]&&r.id)return S.set(e,r.entityType,S.without(e[r.entityType],r.id))}else if(r.entities&&"object"===t(r.entities)){var n=e;return Object.keys(r.entities).forEach((function(t){return n=S.set(n,t,S.merge({},[e[t]||{},r.entities[t]]))})),n}return e}function Y(e,r){var i=H(e);return function(){return function(e){return function(a){if(h(a.type)||d(a.type)){var c=a.payload,u=a.meta,s=u&&u.entityType;s&&c&&i[s]&&(u.entities={},a.payload=function e(r,i,a,c){if(!r[i])throw"Unrecognised entityType: ".concat(i);var u=C(r,i),s=u.rule,f=u.entityType;if(Array.isArray(a))return a.map((function(t){return e(r,f,t,c)}));if(!a||"object"!==t(a))return a;var l=a[s[M]||k];if(!l)return Object.entries(a).reduce((function(t,n){var i=o(n,2),a=i[0],u=i[1];return t[a]=e(r,f,u,c),t}),{});var p=n({},a);if(c[f]=c[f]||{},c[f][l]=p,s[F])for(var y=0,h=Object.entries(s[F]);y<h.length;y++){var d=o(h[y],2),v=d[0],b=d[1];p[v]=e(r,b,a[v],c)}return l}(i,s,c,u.entities),r&&r(s,c))}return e(a)}}}}export{J as createSelector,Y as getNormalizerMiddleware,A as makeActionCreator,I as makeReducer,N as normalizationReducer,H as processRules,E as resetAction,x as setAction}; //# sourceMappingURL=redux-grim.es.min.js.map