UNPKG

type-r2

Version:

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

3 lines (2 loc) 8.09 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("type-r")):"function"==typeof define&&define.amd?define(["exports","type-r"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).restfulIO={},t.Nested)}(this,function(t,s){"use strict";var o=function(t,e){return(o=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(t,e){t.__proto__=e}:function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}))(t,e)};var u=function(){return(u=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function c(t,e){var r={};for(o in t)Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,o=Object.getOwnPropertySymbols(t);n<o.length;n++)e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]]);return r}function l(t,s,u,c){return new(u=u||Promise)(function(r,e){function n(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?r(t.value):((e=t.value)instanceof u?e:new u(function(t){t(e)})).then(n,o)}i((c=c.apply(t,s||[])).next())})}function a(n,o){var i,s,u,c={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]},l={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function t(r){return function(t){var e=[r,t];if(i)throw new TypeError("Generator is already executing.");for(;c=l&&e[l=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(n,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}}}}e.prototype.resolve=function(r){var n=this;return s.createIOPromise(function(t,e){setTimeout(function(){return t(r)},n.delay)})},e.prototype.reject=function(r){var n=this;return s.createIOPromise(function(t,e){setTimeout(function(){return e(r)},n.delay)})},e.prototype.generateId=function(t){var e=Number(t);return isNaN(e)||(this.index[0]=Math.max(this.index[0],e)),t||String(this.index[0]++)},e.prototype.create=function(t,e){var r=t.id=this.generateId(t.id);return this.index.push(r),this.items[r]=t,this.resolve({id:r})},e.prototype.update=function(t,e,r){return this.items[t]=e,this.resolve({})},e.prototype.read=function(t,e){t=this.items[t];return t?this.resolve(t):this.reject("Not found")},e.prototype.destroy=function(e,t){return this.items[e]?(delete this.items[e],this.index=this.index.filter(function(t){return t!==e}),this.resolve({})):this.reject("Not found")},e.prototype.list=function(t){var e=this;return this.resolve(this.index.slice(1).map(function(t){return e.items[t]}))},e.prototype.subscribe=function(t){},e.prototype.unsubscribe=function(t){};var i=e;function e(t,e){this.delay=e,this.index=[0],this.items={};for(var r=0,n=t;r<n.length;r++){var o=n[r];this.create(o,{})}}function r(t,e){return new n(t,e)}p.prototype.create=function(t,e,r){r=this.collectionUrl(r,e);return this.memoryIO?this.simulateIO("create","POST",r,arguments):this.request("POST",r,e,t)},p.prototype.update=function(t,e,r,n){n=this.objectUrl(n,t,r);return this.memoryIO?this.simulateIO("update","PUT",n,arguments):this.request("PUT",n,r,e)},p.prototype.read=function(t,e,r){r=this.objectUrl(r,t,e);return this.memoryIO?this.simulateIO("read","GET",r,arguments):this.request("GET",r,e)},p.prototype.destroy=function(t,e,r){r=this.objectUrl(r,t,e);return this.memoryIO?this.simulateIO("destroy","DELETE",r,arguments):this.request("DELETE",r,e)},p.prototype.list=function(t,e){e=this.collectionUrl(e,t);return this.memoryIO?this.simulateIO("list","GET",e,arguments):this.request("GET",e,t)},p.prototype.subscribe=function(t){},p.prototype.unsubscribe=function(t){},p.prototype.simulateIO=function(e,r,n,o){return l(this,void 0,void 0,function(){return a(this,function(t){return s.log(s.isProduction?"error":"info","Type-R:SimulatedIO","".concat(r," ").concat(n)),[2,this.memoryIO[e].apply(this.memoryIO,o)]})})},p.prototype.isRelativeUrl=function(t){return 0===t.indexOf("./")},p.prototype.removeTrailingSlash=function(t){return"/"===t.charAt(t.length-1)?t.substr(0,t.length-1):t},p.prototype.getRootUrl=function(t){var e=this.url;return this.isRelativeUrl(e)?(t=(t=t.getOwner()).getEndpoint().getUrl(t),this.removeTrailingSlash(t)+"/"+e.substr(2)):e},p.prototype.getUrl=function(t){var e=this.getRootUrl(t);return t.isNew()?e:this.removeTrailingSlash(e)+"/"+t.id},p.prototype.objectUrl=function(t,e,r){return f(this.getUrl(t),r.params)},p.prototype.collectionUrl=function(t,e){return f(this.getRootUrl(t),e.params)},p.prototype.buildRequestOptions=function(t,e,r){var e=u(u(u({},p.defaultFetchOptions),this.fetchOptions),e),n=e.headers,e=c(e,["headers"]),t=u({method:t,headers:u({"Content-Type":"application/json"},n)},e);return r&&(t.body=JSON.stringify(r)),t},p.prototype.request=function(t,e,r,n){r=r.options;return fetch(e,this.buildRequestOptions(t,r,n)).then(function(t){if(t.ok)return t.json();throw new Error(t.statusText)})},p.defaultFetchOptions={cache:"no-cache",credentials:"same-origin",mode:"cors",redirect:"error"};var n=p;function p(t,e){var r=(e=void 0===e?{}:e).mockData,n=void 0===(n=e.simulateDelay)?1e3:n,e=c(e,["mockData","simulateDelay"]);this.url=t,this.fetchOptions=e,this.memoryIO=r?new i(t=void 0===(t=r)?[]:t,e=void 0===(e=n)?50:e):null}function f(t,e){var r=encodeURIComponent;return e?t+"?"+Object.keys(e).map(function(t){return r(t)+"="+r(e[t])}).join("&"):t}function h(t){throw new ReferenceError("Method ".concat(t," is not supported. modelFetchIO supports only model.fetch()."))}var d=function(i){var t=n,e=i;if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}function n(t,e,r){var n=this,o=(r=void 0===r?{}:r).mockData,r=c(r,["mockData"]);return(n=i.call(this,"",o?u({mockData:[o]},r):r)||this).method=t,n.constructUrl=e,n}return o(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r),n.prototype.list=function(){return l(this,void 0,void 0,function(){return a(this,function(t){return h("collection.fetch()"),[2]})})},n.prototype.destroy=function(){return l(this,void 0,void 0,function(){return a(this,function(t){return h("model.destroy()"),[2]})})},n.prototype.create=function(){return l(this,void 0,void 0,function(){return a(this,function(t){return h("model.save()"),[2]})})},n.prototype.update=function(){return l(this,void 0,void 0,function(){return a(this,function(t){return h("model.save()"),[2]})})},n.prototype.read=function(t,e,r){return l(this,void 0,void 0,function(){return a(this,function(t){switch(t.label){case 0:return(this.url=this.constructUrl(e.params,r),this.memoryIO)?(s.log(s.isProduction?"error":"info","Type-R:SimulatedIO","GET ".concat(this.url)),[4,this.memoryIO.list(e)]):[3,2];case 1:return[2,t.sent()[0]];case 2:return[2,this.request(this.method,this.getRootUrl(r),e)]}})})},function(t,e,r,n){var o,i=arguments.length,s=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,r,n);else for(var u=t.length-1;0<=u;u--)(o=t[u])&&(s=(i<3?o(s):3<i?o(e,r,s):o(e,r))||s);return 3<i&&s&&Object.defineProperty(e,r,s),s}([s.define],n)}(n);t.RestfulEndpoint=n,t.create=r,t.fetchModelIO=function(t,e,r){return new d(t,e,r)},t.restfulIO=r}); //# sourceMappingURL=index.js.map