restfulapi-sdk
Version:
standard restful api sdk with cache
1 lines • 7.56 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.APISDK=t():e.APISDK=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="/dist/",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),a=(n(2),n(4)),s=function(e){var t={},n=function r(e,t){for(var n in e)"object"===i(e[n])?(t[n]={},r(e[n],t[n])):"string"==typeof e[n]&&(t[n]=e[n])};return n(e,t),t},u=function(){function e(t,n){r(this,e),this.http=new a(n);var i=s(t);return this.walk(i),i}return o(e,[{key:"walk",value:function(e){var t=this;for(var n in e)if("object"===i(e[n]))this.walk(e[n]);else if("string"==typeof e[n]){var r=e[n];e[n]=function(e){return t.http.getObj(e)}(r)}}}]),e}();u.all=n(5).all,u.httpCode=a.httpCode,u.removeCache=a.removeCache,e.exports=u},function(e,t,n){(function(e){"use strict";function t(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}function n(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":"undefined"==typeof e?"undefined":t(e)}var r=function(e){return new Date(e).getDate()},i={init:function(e){this.limit=e.overdue?null:e.limit||3600,this.overdue=e.overdue||1,this.prefix=(e.prefix||"cache")+"_"},deal:function(e,t,n){var i=n?sessionStorage:localStorage,o=this.limit,a=this.overdue,s=this.prefix,u=i.getItem(s+e),c=Date.now();u?(u=JSON.parse(u),o?c-u.createAt>1e3*o?t(!0,u.data):t(!1,u.data):a&&(r(Date.now())-r(u.createAt)>a-1?t(!0,u.data):t(!1,u.data))):t(!0,null)},save:function(e,t,n){var r=n?sessionStorage:localStorage,i=this.prefix,o={createAt:Date.now(),data:t};r.setItem(i+e,JSON.stringify(o))},remove:function(e,t){"string"!=typeof e&&(t=e,e=null);var n=t?sessionStorage:localStorage,r=this.prefix;if(e)n.removeItem(r+e);else for(var i in n)0===i.indexOf(r)&&n.removeItem(i)}};"object"===n(e)&&(e.exports=i)}).call(t,n(3)(e))},function(e,t){"use strict";e.exports=function(e){return e.webpackPolyfill||(e.deprecate=function(){},e.paths=[],e.children=[],e.webpackPolyfill=1),e}},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=n(2),a=n(5),s=function(e){try{e=JSON.parse(e)}catch(t){}return e},u=function(e){var t="";for(var n in e)t+=t?"&"+n+"="+encodeURI(e[n]):n+"="+encodeURI(e[n]);return t},c=function(e,t){t=JSON.parse(JSON.stringify(t));var n=e.match(/:\w+/g);n&&n.forEach(function(n){var r=n.replace(":","");null!=t[r]?(e=e.replace(new RegExp(n,"g"),t[r].toString()),delete t[r]):e=e.replace(new RegExp(n+"/?","g"),"")});var r=u(t);return e+(r?"?"+r:"")},f=function(){function e(t){r(this,e),"undefined"==typeof t||t.cache?(this.config=t||{},this.config.urlPrefix=t.urlPrefix||"",o.init({limit:this.config.overdue||3600,overdue:this.config.overdueDay||null,prefix:this.config.cachePrefix||"api"}),this.config.isSession=t.isSession||!1,this.config.needCache=!0):(this.config={},this.config.urlPrefix=t.urlPrefix||"",this.config.needCache=!1)}return i(e,[{key:"getObj",value:function(e){var t=this;if(!e)return null;var n={get:this.getInitMethod("GET",e),post:this.getInitMethod("POST",e),put:this.getInitMethod("PUT",e),del:this.getInitMethod("DELETE",e),patch:this.getInitMethod("PATCH",e),options:this.getInitMethod("OPTIONS",e)};return this.config.needCache&&(n.cache=function(){for(var n=arguments.length,r=Array(n),i=0;n>i;i++)r[i]=arguments[i];return new a(t.getCacheFunc.bind(t),e,r)}),n}},{key:"getInitMethod",value:function(e,t){var n=this;return function(){for(var r=arguments.length,i=Array(r),o=0;r>o;o++)i[o]=arguments[o];return new a(n.getSendFunc.bind(n),e,t,i)}}},{key:"getSendFunc",value:function(t,n,r,i){var a=this.config,u=a.needCache,f=a.isSession,l=a.urlPrefix,h=new XMLHttpRequest,p="";h.onreadystatechange=function(){if(4===h.readyState)if(e.codeCallback&&e.codeCallback(h.status),h.status>=200&&h.status<300){var n=s(h.responseText);u&&"get"===t&&o.save(p,n,f),i(!0,n,h.status)}else i(!1,s(h.responseText),h.status)};var g=function(){h.open(t,p,!0),"get"!==t&&h.setRequestHeader("Content-type","application/json")};switch(r.length){case 0:p=l+c(n,{}),g(),h.send();break;case 1:p=l+c(n,r[0]),g(),h.send();break;case 2:p=l+c(n,r[0]),g(),h.send(JSON.stringify(r[1]))}}},{key:"getCacheFunc",value:function(t,n,r){var i=this,a=this.config,s=a.urlPrefix,u=a.isSession,f="",l=function(){o.deal(f,function(o,a){o||!a?i.getSendFunc("get",t,n,r):setTimeout(function(){e.codeCallback&&e.codeCallback(200),r(!0,a,200)})},u)};switch(n.length){case 0:f=s+c(t,{}),l();break;case 1:case 2:f=s+c(t,n[0]),l()}}}],[{key:"httpCode",value:function(t){"function"==typeof t&&(e.codeCallback=t)}},{key:"removeCache",value:function(e,t){o.remove(e,t)}}]),e}();e.exports=f},function(e,t){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=function(){function e(t){r(this,e),this.list=[],this.status=0,this.data=null,this.progress=-1,this.finish=!1;for(var n=arguments.length,i=Array(n>1?n-1:0),o=1;n>o;o++)i[o-1]=arguments[o];this.runFunc.apply(this,[t].concat(i))}return i(e,[{key:"defer",value:function(e){for(var t=this.list,r=(this.progress,arguments.length),i=Array(r>1?r-1:0),o=1;r>o;o++)i[o-1]=arguments[o];if(i&&(this.data=Array.isArray(i)?i:[i]),e?this.status=0:this.status=1,this.progress++,t[this.progress]){var a=t[this.progress][this.status];this.runFunc.apply(this,[a].concat(n(this.data)))}else this.finish=!0}},{key:"runFunc",value:function(e){for(var t=this,n=arguments.length,r=Array(n>1?n-1:0),i=1;n>i;i++)r[i-1]=arguments[i];e&&e.apply(void 0,r.concat([function(e){for(var n=arguments.length,r=Array(n>1?n-1:0),i=1;n>i;i++)r[i-1]=arguments[i];t.defer.apply(t,[e].concat(r))}]))}},{key:"then",value:function(){for(var e=this,t=arguments.length,n=Array(t),r=0;t>r;r++)n[r]=arguments[r];return this.list.push(n),{then:function(){return e.then.apply(e,arguments),e}}}}],[{key:"all",value:function(t){return new e(function(e){var r=function(r,s,u){s.pop(),r?i.push([u].concat(n(s))):o.push([u].concat(n(s))),a++,a===t.length&&e(!0,i,o)},i=[],o=[],a=0;t.forEach(function(e,t){e.then(function(){for(var e=arguments.length,n=Array(e),i=0;e>i;i++)n[i]=arguments[i];r(!0,n,t)},function(){for(var e=arguments.length,n=Array(e),i=0;e>i;i++)n[i]=arguments[i];r(!1,n,t)})})})}}]),e}();e.exports=o}])});