UNPKG

@cloudflare/redux-grim

Version:

Generator for asynchronous Redux endpoint actions and reducers

3 lines (2 loc) 17.3 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t["redux-grim"]={})}(this,function(t){"use strict";function e(t,e){t.on=function(r,n){if(!r in e)throw"invalid hook "+r;var i=e[r];return e[r]=i?function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),o=1;o<e;o++)r[o-1]=arguments[o];return n.apply(void 0,[i.apply(void 0,[t].concat(r))].concat(r))}:n,t}}var r="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},n=function(t){return function(){var e=t.apply(this,arguments);return new Promise(function(t,r){return function n(i,o){try{var a=e[i](o),u=a.value}catch(t){return void r(t)}if(!a.done)return Promise.resolve(u).then(function(t){n("next",t)},function(t){n("throw",t)});t(u)}("next")})}},i=function(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},a=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var r=[],n=!0,i=!1,o=void 0;try{for(var a,u=t[Symbol.iterator]();!(n=(a=u.next()).done)&&(r.push(a.value),!e||r.length!==e);n=!0);}catch(t){i=!0,o=t}finally{try{!n&&u.return&&u.return()}finally{if(i)throw o}}return r}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),u=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)},c=["put","patch","post"],s=/\(([^)]+)\)/g,f=/\[([^)]+)\]/g,l={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}},p={apiFetch:function(t,e,r){return fetch(e,{method:t.toUpperCase(),body:JSON.stringify(r)})}};function y(t,e){for(var r=new Set,n=void 0;n=s.exec(e);){var i=n[1],o=i.indexOf(".");r.add(-1===o?i:i.substr(0,i.indexOf(".")))}return function(t,e){return c.includes(t)||"delete"===t&&e.includes("[")}(t,e)&&r.add("body"),r.size?[].concat(u(r)):[]}function d(t,e){var r=function(t){var e=t.replace(s,"$${params.$1}").replace("[]","").replace(f,"$${params.body.$1}");return new Function("params","return `"+e+"`;")}(t);return function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];var o=e.reduce(function(t,e,r){return t[e]=n[r],t},{});return{url:r(o),params:o,restArgs:n.slice(e.length)}}}function h(t,e,n,i,o){var a=n.indexOf("[")>-1||["post","put","patch"].includes(e);i.length<o.length&&console.warn("For api call "+t+", "+e+", "+n+",\n Expected parameters: "+o.join(", ")+"\n Actual parameters: "+i.join(", ")+"\n "),o.forEach(function(u,c){var s=r(i[c]);a&&c===o.length-1?"object"!==s&&console.warn(t+", "+e+", "+n+": Expected parameter "+u+" to be an object. Actual value: "+i[c]+" "+s):"string"!==s&&"number"!==s&&console.warn(t+", "+e+", "+n+": Expected parameter "+u+" to be a string or number. Actual value: "+i[c])})}var v,m=(function(t,e){!function(){var e=function t(e){var r,n="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element"),i=60103,o={use_static:!1};function a(t){var e=Object.getPrototypeOf(t);return e?Object.create(e):{}}function u(t,e,r){Object.defineProperty(t,e,{enumerable:!1,configurable:!1,writable:!1,value:r})}function c(t,e){u(t,e,function(){throw new m("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&&(o.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"],d=y.concat(["push","pop","sort","splice","shift","unshift","reverse"]),h=["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 m(t){this.name="MyError",this.message=t,this.stack=(new Error).stack}function b(t,e){for(var r in u(t,s,!0),e)e.hasOwnProperty(r)&&c(t,e[r]);return Object.freeze(t),t}function g(t,e){var r=t[e];u(t,e,function(){return K(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=K.merge(this[t],e,{deep:!0,mode:"replace"})),l(this[t],e)))return this;var i=S.call(this);return i[t]=K(e),A(i)}m.prototype=new Error,m.prototype.constructor=Error;var O=K([]);function j(t,e,r){var n=t[0];if(1===t.length)return w.call(this,n,e,r);var i,o=t.slice(1),a=this[n];if("object"==typeof a&&null!==a)i=K.setIn(a,o,e);else{var u=o[0];i=""!==u&&isFinite(u)?j.call(O,o,e):C.call($,o,e)}if(n in this&&a===i)return this;var c=S.call(this);return c[n]=i,A(c)}function A(t){for(var e in h)h.hasOwnProperty(e)&&g(t,h[e]);o.use_static||(u(t,"flatMap",x),u(t,"asObject",I),u(t,"asMutable",S),u(t,"set",w),u(t,"setIn",j),u(t,"update",z),u(t,"updateIn",B),u(t,"getIn",N));for(var r=0,n=t.length;r<n;r++)t[r]=K(t[r]);return b(t,d)}function T(){return new Date(this.getTime())}function x(t){if(0===arguments.length)return this;var e,r=[],n=this.length;for(e=0;e<n;e++){var i=t(this[e],e,this);Array.isArray(i)?r.push.apply(r,i):r.push(i)}return A(r)}function P(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 S(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 i=t(this[e],e,this),o=i[0],a=i[1];r[o]=a}return Y(r)}function E(t){return!t||"object"!=typeof t||!Object.getOwnPropertyDescriptor(t,s)||t instanceof Date?t:K.asMutable(t,{deep:!0})}function M(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,i=Array.isArray(t),o=e&&e.deep,u=e&&e.mode||"merge",c=e&&e.merger;function s(t,n,i){var u,s=K(n[i]),f=c&&c(t[i],s,e),y=t[i];void 0===r&&void 0===f&&t.hasOwnProperty(i)&&l(s,y)||l(y,u=f||(o&&p(y)&&p(s)?K.merge(y,s,e):s))&&t.hasOwnProperty(i)||(void 0===r&&(r=M(t,a(t))),r[i]=u)}if(i)for(var f=0,y=t.length;f<y;f++){var d=t[f];for(n in d)d.hasOwnProperty(n)&&s(void 0!==r?r:this,d,n)}else{for(n in t)Object.getOwnPropertyDescriptor(t,n)&&s(this,t,n);"replace"===u&&function(t,e){for(var n in t)e.hasOwnProperty(n)||(void 0===r&&(r=M(t,a(t))),delete r[n])}(this,t)}return void 0===r?this:Y(r)}function _(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 K.merge(this,t,{deep:r,mode:"replace"})}var D,F,R,$=K({});function C(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 U.call(this,n,e,r);var i,o=t.slice(1),u=this[n];if(i=this.hasOwnProperty(n)&&"object"==typeof u&&null!==u?K.setIn(u,o,e):C.call($,o,e),this.hasOwnProperty(n)&&u===i)return this;var c=M(this,a(this));return c[n]=i,Y(c)}function U(t,e,r){var n=r&&r.deep;if(this.hasOwnProperty(t)&&(n&&this[t]!==e&&p(e)&&p(this[t])&&(e=K.merge(this[t],e,{deep:!0,mode:"replace"})),l(this[t],e)))return this;var i=M(this,a(this));return i[t]=K(e),Y(i)}function z(t,e){var r=Array.prototype.slice.call(arguments,2),n=this[t];return K.set(this,t,e.apply(n,[n].concat(r)))}function q(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=q(this,t);return K.setIn(this,t,e.apply(n,[n].concat(r)))}function N(t,e){var r=q(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 o.use_static||(u(t,"merge",k),u(t,"replace",_),u(t,"without",P),u(t,"asMutable",H),u(t,"set",U),u(t,"setIn",C),u(t,"update",z),u(t,"updateIn",B),u(t,"getIn",N)),b(t,y)}function K(t,e,r){if(f(t)||function(t){return"object"==typeof t&&null!==t&&(t.$$typeof===i||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(K);if(Array.isArray(t))return A(t.slice());if(t instanceof Date)return a=new Date(t.getTime()),o.use_static||u(a,"asMutable",T),b(a,v);var a,c=e&&e.prototype,s=(c&&c!==Object.prototype?function(){return Object.create(c)}:J)();if(null==r&&(r=64),r<=0)throw new m("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]=K(t[l],void 0,r));return Y(s)}function G(t){return function(){var e=[].slice.call(arguments),r=e.shift();return t.apply(r,e)}}function L(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 K.from=K,K.isImmutable=f,K.ImmutableError=m,K.merge=G(k),K.replace=G(_),K.without=G(P),K.asMutable=(D=H,F=S,R=T,function(){var t=[].slice.call(arguments),e=t.shift();return Array.isArray(e)?F.apply(e,t):e instanceof Date?R.apply(e,t):D.apply(e,t)}),K.set=L(U,w),K.setIn=L(C,j),K.update=G(z),K.updateIn=G(B),K.getIn=G(N),K.flatMap=G(x),K.asObject=G(I),o.use_static||(K.static=t({use_static:!0})),Object.freeze(K),K}();t.exports=e}()}(v={exports:{}},v.exports),v.exports),b=m.static,g=(m.Immutable,b.from({data:void 0,error:null,isRequesting:!1,isErrored:!1})),w="error";var O,j="id",A="idProp",T="to",x="nestedProps";function P(t,e){for(var r=t[e],n=r&&e;r&&r[T];){if(r[A]||r[x])throw"'"+T+"' aliases cannot be used with '"+A+"' and '"+x+"' properties";n=r[T],r=t[r[T]]}return{entityType:n,rule:r}}var S=(i(O={entityType:"string"},A,"string?"),i(O,T,"string?"),i(O,x,"string{}?"),O),I=/(\{\})?(\??)$/,E=function(t,e){return t.every(function(t){var n=new Set([].concat(u(Object.keys(S)),u(Object.keys(t))));return Array.from(n).every(function(n){var i=e[n];if(void 0!==i){var o,a=i.split(I),u=(o=a,Array.isArray(o)?o:Array.from(o)),c=u[0],s=u.slice(1),f=s.indexOf("?")>-1,l=s.indexOf("{}")>-1,p=t[n],y=void 0===p?"undefined":r(p);return!(!f||void 0!==p)||(l?"object"===y&&Object.values(p).every(function(t){return(void 0===t?"undefined":r(t))===c}):void 0!==p&&(void 0===y?"undefined":r(y))===c)}})})},M=void 0,k=void 0,_=void 0;function D(t){if(M&&t===k&&t.length===_)return M;if(!E(t,S))throw"Error: rules do not match schema";return M=t.reduce(function(t,e){return t[e.entityType]=e,t},{}),k=t,_=t.length,M}t.makeActionCreator=function(t,r,i){var a=this,c=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},s=y(r,i);c.debug&&function(t,e,r,n){console.log("Created action "+t+", "+r+", "+e+"("+n.join(", ")+")")}(t,r,i,s);var f=o({},l),v=o({},p),m=d(i,s),b=function(){for(var e=arguments.length,o=Array(e),l=0;l<e;l++)o[l]=arguments[l];return p=n(regeneratorRuntime.mark(function e(n){var l,p,y,d,b,g,w,O,j;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return c.debug&&h(t,r,i,o,s),l=m.apply(void 0,o),p=l.url,y=l.params,d=l.restArgs,b={type:t+".start",meta:{entityType:t,method:r}},b=f.start(b,y,d,c),b=f.all(b,y,d,c),n(b),e.prev=6,e.next=9,v.apiFetch.apply(v,[r,p,y.body].concat(u(d)));case 9:return g=e.sent,w={type:t+".success",payload:g,meta:{entityType:t,method:r}},w=f.success(w,y,d,c,g),w=f.all(w,y,d,c,g),O=w.payload,n(w),e.abrupt("return",O);case 18:throw e.prev=18,e.t0=e.catch(6),j={type:t+".error",payload:e.t0,error:!0,meta:{entityType:t,method:r}},j=f.error(j,y,d,c,e.t0),j=f.all(j,y,d,c,e.t0),n(j),e.t0;case 25:case"end":return e.stop()}},e,a,[[6,18]])})),function(t){return p.apply(this,arguments)};var p};return e(b,f),b.apiFetch=function(t){return v.apiFetch=t,b},b},t.makeReducer=function(t,r){var n={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=r&&r.errorKey||w,u=a===w?g:u=b.from(o({},b.without(g,"error"),i({},a,null))),c=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:u,c=arguments[1],s=void 0;switch(c.type){case t+".start":return s=o({},e,i({isRequesting:!0,isErrored:!1},a,null)),s=n.start(s,e,c,r),s=n.all(s,e,c,r),b.from(s);case t+".success":return s=o({},e,{data:c.payload,isRequesting:!1,isErrored:!1}),s=n.success(s,e,c,r),s=n.all(s,e,c,r),b.from(s);case t+".error":return s=o({},e,i({isRequesting:!1,isErrored:!0},a,c.payload)),s=n.error(s,e,c,r),s=n.all(s,e,c,r),b.from(s);case t+".set":var f=["data"];return c.meta.path&&(f=f.concat(c.meta.path)),b.setIn(e,f,c.payload);case"grim.reset":var l=c.payload,p=l.includes,y=l.excludes;if(p){if(p.includes(t))return u}else if(!y||!y.includes(t))return u;default:return s=n.default?n.default(e,c,r):s,b.from(s)}};return e(c,n),c.modifyInitialState=function(t){return u=t(u),c},c},t.setAction=function(t,e,r){return{type:t+".set",payload:e,meta:{entityType:t,path:r}}},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.createSelector=function(t,e,n,i){var c=D(t);if(!c[n])throw"Key not found "+n;var s=[].concat(u(new Set(function t(e,r){var n=P(e,r),i=n.rule,o=n.entityType,a=o&&[o];if(i&&i[x]){var c=!0,s=!1,f=void 0;try{for(var l,p=Object.values(i[x])[Symbol.iterator]();!(c=(l=p.next()).done);c=!0){var y=l.value;a=[].concat(u(a),u(t(e,y)))}}catch(t){s=!0,f=t}finally{try{!c&&p.return&&p.return()}finally{if(s)throw f}}}return a}(c,n)))),f=[function(t){var e=i(t);return e&&"object"===(void 0===e?"undefined":r(e))&&"data"in e?e.data:e}].concat(u(s.map(function(t){return function(r){return e(r)[t]}}))),l=void 0,p=void 0;return function(t){var i,u,s=f.map(function(e){return e(t)});return(i=s)===(u=l)||i&&u&&i.length===u.length&&i.every(function(t,e){return t===u[e]})||(l=s,p=b.from(function t(e,n,i,u){if(!i)return i;var c=P(e,n),s=c.rule,f=c.entityType;if(Array.isArray(i))return i.map(function(r){return t(e,f,r,u)});if("object"===(void 0===i?"undefined":r(i)))return Object.entries(i).reduce(function(r,n){var i=a(n,2),o=i[0],c=i[1];return r[o]=t(e,f,c,u),r},{});var l=u[f]&&u[f][i];if(!l)return i;var p=o({},l);if(s[x]){var y=!0,d=!1,h=void 0;try{for(var v,m=Object.entries(s[x])[Symbol.iterator]();!(y=(v=m.next()).done);y=!0){var b=v.value,g=a(b,2),w=g[0],O=g[1];p[w]=t(e,O,p[w],u)}}catch(t){d=!0,h=t}finally{try{!y&&m.return&&m.return()}finally{if(d)throw h}}}return p}(c,n,s[0],e(t)))),p}},t.normalizationReducer=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1].meta;if(e&&"delete"===e.method&&e.id)return b.set(t,e.entityType,b.without(t[e.entityType],e.id));if(e&&e.entities&&"object"===r(e.entities)){var n=t;return Object.keys(e.entities).forEach(function(r){return n=b.set(n,r,b.merge({},[t[r],e.entities[r]]))}),n}return t},t.getNormalizerMiddleware=function(t,e){var n=D(t);return function(){return function(t){return function(i){var u=i.payload,c=i.meta,s=c&&c.entityType;return s&&u&&n[s]&&(c.entities={},i.payload=function t(e,n,i,u){if(!e[n])throw"Unrecognised entityType: "+n;var c=P(e,n),s=c.rule,f=c.entityType;if(Array.isArray(i))return i.map(function(r){return t(e,f,r,u)});if(!i||"object"!==(void 0===i?"undefined":r(i)))return i;var l=i[s[A]||j];if(!l)return Object.entries(i).reduce(function(r,n){var i=a(n,2),o=i[0],c=i[1];return r[o]=t(e,f,c,u),r},{});var p=o({},i);if(u[f]=u[f]||{},u[f][l]=p,s[x]){var y=!0,d=!1,h=void 0;try{for(var v,m=Object.entries(s[x])[Symbol.iterator]();!(y=(v=m.next()).done);y=!0){var b=v.value,g=a(b,2),w=g[0],O=g[1];p[w]=t(e,O,i[w],u)}}catch(t){d=!0,h=t}finally{try{!y&&m.return&&m.return()}finally{if(d)throw h}}}return l}(n,s,u,c.entities),e&&e(s,u)),t(i)}}}},t.processRules=D,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=redux-grim.min.js.map