UNPKG

type-r

Version:

Serializable, validated, and observable data layer for modern JS applications

3 lines (2 loc) 3.68 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).proxyIO={})}(this,function(t){"use strict";function u(i,u,s,c){return new(s||(s=Promise))(function(t,e){function n(t){try{o(c.next(t))}catch(t){e(t)}}function r(t){try{o(c.throw(t))}catch(t){e(t)}}function o(e){e.done?t(e.value):new s(function(t){t(e.value)}).then(n,r)}o((c=c.apply(i,u||[])).next())})}function s(n,r){var o,i,u,t,s={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,i&&(u=2&e[0]?i.return:e[0]?i.throw||((u=i.return)&&u.call(i),0):i.next)&&!(u=u.call(i,e[1])).done)return u;switch(i=0,u&&(e=[2&e[0],u.value]),e[0]){case 0:case 1:u=e;break;case 4:return s.label++,{value:e[1],done:!1};case 5:s.label++,i=e[1],e=[0];continue;case 7:e=s.ops.pop(),s.trys.pop();continue;default:if(!(u=0<(u=s.trys).length&&u[u.length-1])&&(6===e[0]||2===e[0])){s=0;continue}if(3===e[0]&&(!u||e[1]>u[0]&&e[1]<u[3])){s.label=e[1];break}if(6===e[0]&&s.label<u[1]){s.label=u[1],u=e;break}if(u&&s.label<u[2]){s.label=u[2],s.ops.push(e);break}u[2]&&s.ops.pop(),s.trys.pop();continue}e=r.call(n,s)}catch(t){e=[6,t],i=0}finally{o=u=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var c={parse:!0,strict:!0};var n=function(){function t(t,e){void 0===e&&(e={});var n=this;this.options={},this.Record=t,e.createAttrs&&(this.options.createAttrs=e.createAttrs.split(/\s+/)),e.updateAttrs&&(this.options.updateAttrs=e.updateAttrs.split(/\s+/));var r=Object.getPrototypeOf(this.endpoint);Object.keys(r).forEach(function(t){n[t]||"function"!=typeof r[t]||(n[t]=function(){return r[t].apply(this.endpoint,arguments)})})}return Object.defineProperty(t.prototype,"endpoint",{get:function(){return this.Record.prototype._endpoint},enumerable:!0,configurable:!0}),t.prototype.subscribe=function(e,n){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this.endpoint.subscribe(e,n)]})})},t.prototype.unsubscribe=function(t,e){this.endpoint.unsubscribe(t,e)},t.prototype.list=function(n){return u(this,void 0,void 0,function(){var e;return s(this,function(t){switch(t.label){case 0:return[4,(e=new this.Record.Collection).fetch(n)];case 1:return t.sent(),[2,e.toJSON()]}})})},t.prototype.update=function(r,o,i){return u(this,void 0,void 0,function(){var e,n;return s(this,function(t){switch(t.label){case 0:return o.id=r,[4,(e=this.Record.from(o,c)).save(i)];case 1:return t.sent(),a(n={_cas:e._cas},e,this.options.updateAttrs),[2,n]}})})},t.prototype.create=function(r,o){return u(this,void 0,void 0,function(){var e,n;return s(this,function(t){switch(t.label){case 0:return[4,(e=this.Record.from(r,c)).save(o)];case 1:return t.sent(),a(n={id:e.id,_cas:e._cas,_type:e._type},e,this.options.createAttrs),[2,n]}})})},t.prototype.read=function(n,r){return u(this,void 0,void 0,function(){var e;return s(this,function(t){switch(t.label){case 0:return[4,(e=new this.Record({id:n})).fetch(r)];case 1:return t.sent(),[2,e.toJSON()]}})})},t.prototype.destroy=function(e,n){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this.endpoint.destroy(e,n)]})})},t}();function a(t,e,n){if(n)for(var r=e.toJSON(),o=0,i=n;o<i.length;o++){var u=i[o];t[u]=r[u]}}t.proxyIO=function(t,e){return void 0===e&&(e={}),new n(t,e)},t.ProxyEndpoint=n,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=index.js.map