type-r2
Version:
Serializable, validated, and observable data layer for modern JS applications
3 lines (2 loc) • 3.66 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).proxyIO={})}(this,function(t){"use strict";function s(t,s,u,c){return new(u=u||Promise)(function(n,e){function r(t){try{i(c.next(t))}catch(t){e(t)}}function o(t){try{i(c.throw(t))}catch(t){e(t)}}function i(t){var e;t.done?n(t.value):((e=t.value)instanceof u?e:new u(function(t){t(e)})).then(r,o)}i((c=c.apply(t,s||[])).next())})}function u(r,o){var i,s,u,c={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]},a={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function t(n){return function(t){var e=[n,t];if(i)throw new TypeError("Generator is already executing.");for(;c=a&&e[a=0]?0:c;)try{if(i=1,s&&(u=2&e[0]?s.return:e[0]?s.throw||((u=s.return)&&u.call(s),0):s.next)&&!(u=u.call(s,e[1])).done)return u;switch(s=0,(e=u?[2&e[0],u.value]:e)[0]){case 0:case 1:u=e;break;case 4:return c.label++,{value:e[1],done:!1};case 5:c.label++,s=e[1],e=[0];continue;case 7:e=c.ops.pop(),c.trys.pop();continue;default:if(!(u=0<(u=c.trys).length&&u[u.length-1])&&(6===e[0]||2===e[0])){c=0;continue}if(3===e[0]&&(!u||e[1]>u[0]&&e[1]<u[3]))c.label=e[1];else if(6===e[0]&&c.label<u[1])c.label=u[1],u=e;else{if(!(u&&c.label<u[2])){u[2]&&c.ops.pop(),c.trys.pop();continue}c.label=u[2],c.ops.push(e)}}e=o.call(r,c)}catch(t){e=[6,t],s=0}finally{i=u=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}}var c={parse:!0,strict:!0};Object.defineProperty(e.prototype,"endpoint",{get:function(){return this.Record.prototype._endpoint},enumerable:!1,configurable:!0}),e.prototype.subscribe=function(e,n){return s(this,void 0,void 0,function(){return u(this,function(t){return[2,this.endpoint.subscribe(e,n)]})})},e.prototype.unsubscribe=function(t,e){this.endpoint.unsubscribe(t,e)},e.prototype.list=function(n){return s(this,void 0,void 0,function(){var e;return u(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()]}})})},e.prototype.update=function(r,o,i){return s(this,void 0,void 0,function(){var e,n;return u(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]}})})},e.prototype.create=function(r,o){return s(this,void 0,void 0,function(){var e,n;return u(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]}})})},e.prototype.read=function(n,r){return s(this,void 0,void 0,function(){var e;return u(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()]}})})},e.prototype.destroy=function(e,n){return s(this,void 0,void 0,function(){return u(this,function(t){return[2,this.endpoint.destroy(e,n)]})})};var n=e;function e(t,e){void 0===e&&(e={});var n=this,r=(this.options={},this.Record=t,e.createAttrs&&(this.options.createAttrs=e.createAttrs.split(/\s+/)),e.updateAttrs&&(this.options.updateAttrs=e.updateAttrs.split(/\s+/)),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)})})}function a(t,e,n){if(n)for(var r=e.toJSON(),o=0,i=n;o<i.length;o++){var s=i[o];t[s]=r[s]}}t.ProxyEndpoint=n,t.proxyIO=function(t,e){return new n(t,e=void 0===e?{}:e)}});
//# sourceMappingURL=index.js.map