-
Notifications
You must be signed in to change notification settings - Fork 10
/
peasy.js
1 lines (1 loc) · 11.6 KB
/
peasy.js
1
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.peasy=t():n.peasy=t()}(this,(()=>{return n={891:(n,t,e)=>{var r=e(807),i=e(826),o=function(){"use strict";var n=function(t){if(!(this instanceof n))return new n(t);this.dataProxy=t};return n.extendService=function(t,e){return e.service=t,n.extend(e)},n.extend=function(t){(t=t||{}).params=t.params||["dataProxy"],t.functions=t.functions||{};var e=function(){var e=this;e.arguments=arguments,n.call(this),t.params.forEach((function(n,t){e[n]=e.arguments[t]}))},r=t.service||n;e.prototype=new r;var i=Object.keys(n.prototype);return Object.keys(t.functions).forEach((function(n){-1===i.indexOf(n)&&console.warn("The method: '"+n+"' is not an overridable method of BusinessService"),e.prototype[n]=t.functions[n]})),{createCommand:function t(r){return(r=r||{}).service=e,n.createCommand(r),{createCommand:t,service:e}},service:e}},n.createCommand=function(n){function t(n){return n.charAt(0).toUpperCase()+n.slice(1)}if(!(n=n||{}).name)throw new Error("A value for name must be supplied");if(!n.service)throw new Error("A function for the service argument must be supplied");var e=n.name,o="_on"+t(e)+"Initialization",a="_getRulesFor"+t(e),u="_"+e.replace("Command",""),s="_"+e+"Params",c=n.functions||{},l=n.service;return l.prototype[o]=c._onInitialization||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null):Promise.resolve()},l.prototype[a]=c._getRules||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null,[]):Promise.resolve([])},l.prototype[u]=c._onValidationSuccess||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null):Promise.resolve()},l.prototype[s]=n.params||[],l.prototype[e]=function(){var n=this,t=arguments,e=new r({_onInitialization:function(){var t=n[o].apply(this,arguments);return i.autoWrapInitializationResult(t)},_getRules:function(){var t=n[a].apply(this,arguments);return i.autoWrapRulesResult(t)},_onValidationSuccess:function(){var t=n[u].apply(this,arguments);return i.autoWrapValidationCompleteResult(t)}});return n[s].forEach((function(n,r){e[n]=t[r]})),Object.keys(n).forEach((function(t){e[t]=n[t]})),e.arguments=arguments,e},l},Object.defineProperty(n.prototype,"constructor",{enumerable:!1,value:n}),n.createCommand({name:"getByIdCommand",service:n,params:["id"],functions:{_onValidationSuccess:function(n,t,e){return e?this.dataProxy.getById(this.id,e):this.dataProxy.getById(this.id)}}}),n.createCommand({name:"getAllCommand",service:n,functions:{_onValidationSuccess:function(n,t){return t?this.dataProxy.getAll(t):this.dataProxy.getAll()}}}),n.createCommand({name:"insertCommand",service:n,params:["data"],functions:{_onValidationSuccess:function(n,t,e){return e?this.dataProxy.insert(n,e):this.dataProxy.insert(n)}}}),n.createCommand({name:"updateCommand",service:n,params:["data"],functions:{_onValidationSuccess:function(n,t,e){return e?this.dataProxy.update(n,e):this.dataProxy.update(n)}}}),n.createCommand({name:"destroyCommand",service:n,params:["id"],functions:{_onValidationSuccess:function(n,t,e){return e?this.dataProxy.destroy(n,e):this.dataProxy.destroy(n)}}}),n}();n.exports=o},807:(n,t,e)=>{var r=e(197),i=e(108),o=e(359),a=e(826),u=function(){"use strict";function n(n,t){return function(e){var r=this;return new Promise((function(e,i){n.apply(r,t.concat((function(n,t){if(n)return i(n);e(t)})))}))}}var t=function(n){if(n=n||{},!(this instanceof t))return new t(n.onInitialization,n.getRules,n.onValidationSuccess);this._onInitialization||(this._onInitialization=n._onInitialization||function(n,t){return t?t():Promise.resolve()}),this._getRules||(this._getRules=n._getRules||function(n,t){return t?t(null,[]):Promise.resolve([])}),this._onValidationSuccess||(this._onValidationSuccess=n._onValidationSuccess||function(n,t){return t?t():Promise.resolve()})};return t.prototype={constructor:t,getErrors:function(t,e){var r=this,i=r.arguments||{},u=Object.keys(i).map((function(n){return i[n]})).concat([e]),s=r._getRules.bind(r),c=r._onValidationSuccess.bind(r),l=function(n){return new o(n).validate()};t&&(s=n(s,u),c=n(c,u),l=function(n){return new Promise((function(t,e){new o(n).validate((function(r){if(r)return e(r);t(n)}))}))});var f,d=(f=s.apply(r,u),(f=a.autoWrapRulesResult(f)).then((function(n){return Array.isArray(n)||(n=[n]),n}))).then((function(n){return l(n)})).then((function(n){var t=n.filter((function(n){return!n.valid})).map((function(n){return n.errors}));return[].concat.apply([],t)})).then((function(n){return t?t(null,n):n})).catch((function(n){return t?t(n):Promise.reject(n)}));if(!t)return d},execute:function(t){var e=this,o={},u=e.arguments||{},s=Object.keys(u).map((function(n){return u[n]})).concat([o]),c=e._onInitialization.bind(e),l=e._onValidationSuccess.bind(e);t&&(c=n(c,s),l=n(l,s));var f,d=(f=c.apply(e,s),a.autoWrapInitializationResult(f)).then((function(){return t?new Promise((function(n,t){e.getErrors((function(t,e){return n(e)}),o)})):e.getErrors(null,o)})).then((function(n){if(n.length>0)return function(n){return Promise.resolve(new r(!1,null,n))}(n);try{var t=l.apply(e,s);return(t=a.autoWrapValidationCompleteResult(t)).then((function(n){return Promise.resolve(new r(!0,n,null))})).catch(h)}catch(n){return h(n)}})).then((function(n){return t?t(null,n):n})).catch((function(n){return t?t(n):Promise.reject(n)}));if(!t)return d;function h(n){return n instanceof i?Promise.resolve(new r(!1,null,n.errors)):Promise.reject(n)}}},t.extend=function(n){var e=(n=n||{}).params||[],r=n.functions||{},i=function(){var n=this;n.arguments=arguments,e.forEach((function(t,e){n[t]=n.arguments[e]}))};return(i.prototype=new t)._onInitialization=r._onInitialization||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null):Promise.resolve()},i.prototype._getRules=r._getRules||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null,[]):Promise.resolve([])},i.prototype._onValidationSuccess=r._onValidationSuccess||function(){var n=arguments[Object.keys(arguments).length-1];return n&&"function"==typeof n?n(null):Promise.resolve()},i},t.executeAll=function(n,t){Array.isArray(n)||(n=[n]);var e=n.length;if(e<1)return t?t():Promise.resolve();if(!t)return Promise.all(n.map((function(n){return n.execute()})));var r=0,i=[];function o(n,o){if(n)return t(n,i);r++,i.push(o),r===e&&t(null,i)}n.forEach((function(n){n.execute(o)}))},t}();n.exports=u},168:n=>{var t=function(){"use strict";var n=function(n){};return n.autoPromiseWrap=!1,n}();n.exports=t},197:n=>{var t=function(){"use strict";var n=function(t,e,r){if(!(this instanceof n))return new n(t,e,r);this.success=t,this.value=e,this.errors=r};return n}();n.exports=t},954:(n,t,e)=>{var r=e(891),i=e(807),o=e(197),a=e(758),u=e(108),s=e(168);n.exports={BusinessService:r,Command:i,ExecutionResult:o,Rule:a,ServiceException:u,Configuration:s}},758:(n,t,e)=>{var r=e(359),i=e(826),o=function(){"use strict";var n=function(t){if(!(this instanceof n))return new n;t=t||{},this.association=t.association||null,this.errors=[],this.ifInvalidThenFn=null,this.ifValidThenFn=null,this.ifValidThenGetRulesFn=null,this.validSuccessors=[],this.invalidSuccessors=[],this.valid=!0};return n.getAllRulesFrom=function(n,t){var e={};return t?function(n,t){Array.isArray(n)||(n=[n]);var r=n.length;if(r<1&&t)return t(null,[]);var i=0,o=[];function a(n,e){if(n)return t(n,o);Array.isArray(e)?e.forEach((function(n){o.push(n)})):o.push(e),++i===r&&t(null,o)}n.forEach((function(n){n._getRules(e,a)}))}(n,t):Promise.all(n.map((function(n){return n._getRules(e)}))).then((function(n){return[].concat.apply([],n)}))},n.ifAllValid=function(t){return{thenGetRules:function(e){var r=new n;return r._onValidate=function(n){return n?n():Promise.resolve()},r.validSuccessors=t,r.ifValidThenGetRulesFn=e,r}}},n.extend=function(t){if((t=t||{}).functions=t.functions||{},"function"!=typeof t.functions._onValidate)throw new Error("An onValidate method needs to be supplied to execute!");t.association=t.association||null,t.params=t.params||[];var e=function(){var e=this;e.arguments=arguments,n.call(e,{association:t.association}),t.params.forEach((function(n,t){e[n]=e.arguments[t]}))};return(e.prototype=new n)._onValidate=t.functions._onValidate,e},n.prototype={constructor:n,_invalidate:function(n){var t=this;this.valid=!1,Array.isArray(n)||(n=[n]),n.forEach((function(n){"string"==typeof n?t.errors.push({association:t.association,message:n}):t.errors.push(n)}))},_unInvalidate:function(){this.valid=!0,this.errors=[]},_onValidate:function(n){},validate:function(n){var t=this;t.errors=[];var e=t.arguments||{},o=Object.keys(e).map((function(n){return t.arguments[n]}));if(n)return this._onValidate.apply(t,o.concat((function(t){if(t)return n(t);u(n)})));var a=this._onValidate.apply(t,o);return(a=i.autoWrapValidationResult(a)).then(u);function u(n){if(t.valid){if(t.ifValidThenFn&&t.ifValidThenFn(t),t.validSuccessors.length>0)return function(n,t,e){return e?s(n,t,(function(){if(n.ifValidThenGetRulesFn)return c(n,t,e);e()})):s(n,t).then((function(){if(n.ifValidThenGetRulesFn)return c(n,t)}))}(t,t.validSuccessors,n);if(t.ifValidThenGetRulesFn)return c(t,t.validSuccessors,n)}else if(t.ifInvalidThenFn&&t.ifInvalidThenFn(t),t.invalidSuccessors.length>0)return s(t,t.invalidSuccessors,n);n&&n()}function s(n,t,e){return e?new r(t).validate((function(r){if(r)return e(r);l(n).ifAnyInvalid(t),e()})):new r(t).validate().then((function(){return l(n).ifAnyInvalid(t)}))}function c(n,t,e){var r=t.filter((function(n){return!n.valid}));return r.length>0?e?e():Promise.resolve():e?n.ifValidThenGetRulesFn((function(t,r){return Array.isArray(r)||(r=[r]),s(n,r,e)})):n.ifValidThenGetRulesFn().then((function(t){return Array.isArray(t)||(t=[t]),s(n,t)}))}function l(n){return{ifAnyInvalid:function(t){var e=t.filter((function(n){return!n.valid}));e.length>0?e.forEach((function(t){n._invalidate(t.errors)})):n._unInvalidate()}}}},ifValidThenValidate:function(n){return Array.isArray(n)||(n=[n]),this.validSuccessors=n,this},ifValidThenExecute:function(n){return this.ifValidThenFn=n,this},ifInvalidThenValidate:function(n){return Array.isArray(n)||(n=[n]),this.invalidSuccessors=n,this},ifInvalidThenExecute:function(n){return this.ifInvalidThenFn=n,this},ifValidThenGetRules:function(n){return this.ifValidThenGetRulesFn=n,this}},n}();n.exports=o},359:n=>{var t=function(){"use strict";var n=function(t){if(!(this instanceof n))return new n(t);this.rules=t};return n.prototype.validate=function(n){var t=this,e=t.rules.map((function(n){return n.validate.bind(n)}));n&&(e=e.map((function(n){return t=n,function(){return new Promise((function(n,e){t((function(t,r){if(t)return e(t);n(r)}))}))};var t})));var r=Promise.all(e.map((function(n){return n()}))).then((function(){return n?n(null,t.rules):Promise.resolve(t.rules)})).catch((function(t){return n?n(t):Promise.reject(t)}));if(!n)return r},n}();n.exports=t},108:n=>{var t=function(n){this.message=n,this.errors=[]};t.prototype=new Error,n.exports=t},826:(n,t,e)=>{var r=e(168);function i(n){return!r.autoPromiseWrap||void 0!==n&&"function"==typeof n.then?n:Promise.resolve(n)}var o={autoWrapInitializationResult:function(n){return i(n)},autoWrapRulesResult:function(n){return r.autoPromiseWrap&&(Array.isArray(n)&&(n=Promise.resolve(n)),void 0===n&&(n=Promise.resolve([])),"function"!=typeof n.then&&(n=Promise.resolve(n))),n},autoWrapValidationCompleteResult:function(n){return i(n)},autoWrapValidationResult:function(n){return i(n)},autoWrapRuleValidationCompleteResult:function(n){return i(n)}};n.exports=o}},t={},function e(r){var i=t[r];if(void 0!==i)return i.exports;var o=t[r]={exports:{}};return n[r](o,o.exports,e),o.exports}(954);var n,t}));