type-r
Version:
Serializable, validated, and observable data layer for modern JS applications
3 lines (2 loc) • 7.96 kB
JavaScript
!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=t||self).restfulIO={},t.Nested)}(this,function(t,i){"use strict";var n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)};var c=function(){return(c=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 a(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&(r[n[o]]=t[n[o]])}return r}function l(i,u,s,c){return new(s||(s=Promise))(function(t,e){function r(t){try{o(c.next(t))}catch(t){e(t)}}function n(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(r,n)}o((c=c.apply(i,u||[])).next())})}function f(r,n){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=n.call(r,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 p=function(){function t(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,{})}}return t.prototype.resolve=function(r){var n=this;return i.createIOPromise(function(t,e){setTimeout(function(){return t(r)},n.delay)})},t.prototype.reject=function(r){var n=this;return i.createIOPromise(function(t,e){setTimeout(function(){return e(r)},n.delay)})},t.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]++)},t.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})},t.prototype.update=function(t,e,r){return this.items[t]=e,this.resolve({})},t.prototype.read=function(t,e){var r=this.items[t];return r?this.resolve(r):this.reject("Not found")},t.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")},t.prototype.list=function(t){var e=this;return this.resolve(this.index.slice(1).map(function(t){return e.items[t]}))},t.prototype.subscribe=function(t){},t.prototype.unsubscribe=function(t){},t}();function e(t,e){return new r(t,e)}var r=function(){function s(t,e){void 0===e&&(e={});var r,n,o=e.mockData,i=e.simulateDelay,u=void 0===i?1e3:i,s=a(e,["mockData","simulateDelay"]);this.url=t,this.fetchOptions=s,this.memoryIO=o?(void 0===(r=o)&&(r=[]),void 0===(n=u)&&(n=50),new p(r,n)):null}return s.prototype.create=function(t,e,r){var n=this.collectionUrl(r,e);return this.memoryIO?this.simulateIO("create","POST",n,arguments):this.request("POST",n,e,t)},s.prototype.update=function(t,e,r,n){var o=this.objectUrl(n,t,r);return this.memoryIO?this.simulateIO("update","PUT",o,arguments):this.request("PUT",o,r,e)},s.prototype.read=function(t,e,r){var n=this.objectUrl(r,t,e);return this.memoryIO?this.simulateIO("read","GET",n,arguments):this.request("GET",n,e)},s.prototype.destroy=function(t,e,r){var n=this.objectUrl(r,t,e);return this.memoryIO?this.simulateIO("destroy","DELETE",n,arguments):this.request("DELETE",n,e)},s.prototype.list=function(t,e){var r=this.collectionUrl(e,t);return this.memoryIO?this.simulateIO("list","GET",r,arguments):this.request("GET",r,t)},s.prototype.subscribe=function(t){},s.prototype.unsubscribe=function(t){},s.prototype.simulateIO=function(e,r,n,o){return l(this,void 0,void 0,function(){return f(this,function(t){return i.log(i.isProduction?"error":"info","Type-R:SimulatedIO",r+" "+n),[2,this.memoryIO[e].apply(this.memoryIO,o)]})})},s.prototype.isRelativeUrl=function(t){return 0===t.indexOf("./")},s.prototype.removeTrailingSlash=function(t){return"/"===t.charAt(t.length-1)?t.substr(0,t.length-1):t},s.prototype.getRootUrl=function(t){var e=this.url;if(this.isRelativeUrl(e)){var r=t.getOwner(),n=r.getEndpoint().getUrl(r);return this.removeTrailingSlash(n)+"/"+e.substr(2)}return e},s.prototype.getUrl=function(t){var e=this.getRootUrl(t);return t.isNew()?e:this.removeTrailingSlash(e)+"/"+t.id},s.prototype.objectUrl=function(t,e,r){return o(this.getUrl(t),r.params)},s.prototype.collectionUrl=function(t,e){return o(this.getRootUrl(t),e.params)},s.prototype.buildRequestOptions=function(t,e,r){var n=c({},s.defaultFetchOptions,this.fetchOptions,e),o=n.headers,i=a(n,["headers"]),u=c({method:t,headers:c({"Content-Type":"application/json"},o)},i);return r&&(u.body=JSON.stringify(r)),u},s.prototype.request=function(t,e,r,n){var o=r.options;return fetch(e,this.buildRequestOptions(t,o,n)).then(function(t){if(t.ok)return t.json();throw new Error(t.statusText)})},s.defaultFetchOptions={cache:"no-cache",credentials:"same-origin",mode:"cors",redirect:"error"},s}();function o(t,e){var r=encodeURIComponent;return e?t+"?"+Object.keys(e).map(function(t){return r(t)+"="+r(e[t])}).join("&"):t}function s(t){throw new ReferenceError("Method "+t+" is not supported. modelFetchIO supports only model.fetch().")}var u=function(u){function t(t,e,r){void 0===r&&(r={});var n=r.mockData,o=a(r,["mockData"]),i=u.call(this,"",n?c({mockData:[n]},o):o)||this;return i.method=t,i.constructUrl=e,i}return function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(t,u),t.prototype.list=function(){return l(this,void 0,void 0,function(){return f(this,function(t){return s("collection.fetch()"),[2]})})},t.prototype.destroy=function(){return l(this,void 0,void 0,function(){return f(this,function(t){return s("model.destroy()"),[2]})})},t.prototype.create=function(){return l(this,void 0,void 0,function(){return f(this,function(t){return s("model.save()"),[2]})})},t.prototype.update=function(){return l(this,void 0,void 0,function(){return f(this,function(t){return s("model.save()"),[2]})})},t.prototype.read=function(t,e,r){return l(this,void 0,void 0,function(){return f(this,function(t){switch(t.label){case 0:return this.url=this.constructUrl(e.params,r),this.memoryIO?(i.log(i.isProduction?"error":"info","Type-R:SimulatedIO","GET "+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)]}})})},t=function(t,e,r,n){var o,i=arguments.length,u=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(t,e,r,n);else for(var s=t.length-1;0<=s;s--)(o=t[s])&&(u=(i<3?o(u):3<i?o(e,r,u):o(e,r))||u);return 3<i&&u&&Object.defineProperty(e,r,u),u}([i.define],t)}(r);t.create=e,t.restfulIO=e,t.RestfulEndpoint=r,t.fetchModelIO=function(t,e,r){return new u(t,e,r)},Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=index.js.map