UNPKG

superapi-cache

Version:
2 lines 4.86 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superapi-cache",[],t):"object"==typeof exports?exports["superapi-cache"]=t():e["superapi-cache"]=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var u=r[n]={exports:{},id:n,loaded:!1};return e[n].call(u.exports,u,u.exports,t),u.loaded=!0,u.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function u(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];e.store=e.store||c["default"];var t=e.key||u.key;return e.maxAge=e.maxAge||0,e.readCache=e.readCache||o["default"],e.serialize=e.serialize||i["default"],e.clearOnStale=void 0!==e.clearOnStale?e.clearOnStale:!0,e.exclude=e.exclude||{},e.exclude.query=e.exclude.query||!0,e.exclude.paths=e.exclude.paths||[],e.exclude.filter=null,e.log!==!1&&(e.log="function"==typeof e.log?e.log:console.log.bind(console)),function(r,n){var u=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];if((0,d["default"])(r,u,e.exclude))return null;var a=t(r),o=u.cache||{},s=r.method.toLowerCase();if("head"===s)return null;if("get"!==s)return e.store.removeItem(a),null;var i=function(){return n().then(function(t){var n=t.status/100|0;if(2!==n)return t;if(["arraybuffer","blob"].indexOf(t.responseType)>-1)return t;var u=0===e.maxAge?0:Date.now()+e.maxAge,s=void 0!==o.expiration;return s&&(u=Date.now()+o.expiration,e.log("override expiration to use "+u)),e.store.setItem(a,{expires:u,data:e.serialize(r,t)})})};return e.store.getItem(a).then(function(t){return e.readCache(r,e.log)(t)["catch"](function(t){e.clearOnStale&&"cache-stale"===t.reason?e.store.removeItem(a).then(i):i()})})}}Object.defineProperty(t,"__esModule",{value:!0});var a=r(1),o=n(a),s=r(3),i=n(s),l=r(4),c=n(l),f=r(5),d=n(f);u.readCache=o["default"],u.serialize=i["default"],u.key=function(e){return e.url},t["default"]=u,e.exports=t["default"]},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e,t){return function(r){return new Promise(function(n,u){if(!r||!r.data){t("cache-miss",e.url);var o=new Error;return o.reason="cache-miss",o.message="Value not found from cache",u(o)}var s=r.expires,i=r.data;if(0!==s&&s<Date.now()){t("cache-stale",e.url);var o=new Error;return o.reason="cache-stale",o.message="Value is stale",u(o)}e.xhr=(0,a["default"])(i),e.callback=function(r,a){return t("cache-hit",e.url),r?u(r,a):void n(a)},e.emit("end")})}};var u=r(2),a=n(u);e.exports=t["default"]},function(e,t){"use strict";function r(){var e=arguments.length<=0||void 0===arguments[0]?"":arguments[0];return"".trim?e.trim(e):e.replace(/(^\s*|\s*$)/g,"")}function n(){var e=arguments.length<=0||void 0===arguments[0]?"":arguments[0],t=e.split(/\r?\n/),n={},u=void 0,a=void 0,o=void 0,s=void 0;t.pop();for(var i=0,l=t.length;l>i;i+=1)a=t[i],u=a.indexOf(":"),o=a.slice(0,u).toLowerCase(),s=r(a.slice(u+1)),n[o]=s;return n}function u(e){var t=e.body||{},r=n(e.headers);return t.getAllResponseHeaders=function(){return e.headers},t.getResponseHeader=function(e){return r[e]},t}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=u,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){return{headers:t.xhr.getAllResponseHeaders(),body:{responseType:t.xhr.responseType,responseText:t.xhr.responseText,status:t.status,statusText:t.statusText}}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=r,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}();Object.defineProperty(t,"__esModule",{value:!0});var u=function(){function e(){r(this,e),this._store={}}return n(e,[{key:"getItem",value:function(e){return Promise.resolve(this._store[e]||null)}},{key:"setItem",value:function(e,t){return this._store[e]=t,Promise.resolve(t)}},{key:"clear",value:function(){return this._store={},Promise.resolve()}}]),e}();t["default"]=u,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];if(t&&t.use&&t.use.cache===!1)return!0;if("function"==typeof r.filter&&!r.filter(e))return!0;if(r.query&&e.url.match(/\?.*$/))return!0;var n=!1,u=r.paths||[];return u.forEach(function(t){return e.url.match(t)?n=!0:void 0}),n?!0:!1}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=r,e.exports=t["default"]}])}); //# sourceMappingURL=superapi-cache.min.js.map