UNPKG

14.4 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.AxiosCacheInterceptor=t():e.AxiosCacheInterceptor=t()}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={d:function(t,r){for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Header:function(){return i},buildKeyGenerator:function(){return j},buildMemoryStorage:function(){return C},buildStorage:function(){return I},buildWebStorage:function(){return P},canStale:function(){return w},createCacheResponse:function(){return p},createValidateStatus:function(){return l},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return T},defaultRequestInterceptor:function(){return g},defaultResponseInterceptor:function(){return y},isExpired:function(){return S},isMethodIn:function(){return h},isStorage:function(){return x},setupCache:function(){return M},testCachePredicate:function(){return m},updateCache:function(){return b},updateStaleRequest:function(){return v}});var r=Symbol("cache-parser");function n(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function a(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),s=function(e){if(!e)return"not enough headers";var t=e[i.CacheControl];if(t){var s=function(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var n in r){var a,o=r[n].split("=",2);t[o[0]]=null==(a=o[1])||a}return t}(e),s=i["max-age"],c=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],f=i["stale-if-error"],l=i["stale-while-revalidate"];return a(i.immutable)&&(t.immutable=!0),n(s)&&(t.maxAge=o(s)),n(c)&&(t.maxStale=o(c)),n(u)&&(t.minFresh=o(u)),a(i["must-revalidate"])&&(t.mustRevalidate=!0),a(i["must-understand"])&&(t.mustUnderstand=!0),a(i["no-cache"])&&(t.noCache=!0),a(i["no-store"])&&(t.noStore=!0),a(i["no-transform"])&&(t.noTransform=!0),a(i["only-if-cached"])&&(t.onlyIfCached=!0),a(i.private)&&(t.private=!0),a(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),a(i.public)&&(t.public=!0),n(d)&&(t.sMaxAge=o(d)),n(f)&&(t.staleIfError=o(f)),n(l)&&(t.staleWhileRevalidate=o(l)),t}(String(t)),c=s.noCache,u=s.noStore,d=s.mustRevalidate,f=s.maxAge,l=s.immutable;if(c||u)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==f){var h=e[i.Age];return h?1e3*(f-Number(h)):1e3*f}}var v=e[i.Expires];if(v){var p=Date.parse(String(v))-Date.now();return p>=0?p:"dont cache"}return"not enough headers"};var c=function(){return c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},c.apply(this,arguments)};function u(e,t,r,n){return new(r||(r=Promise))((function(a,o){function i(e){try{c(n.next(e))}catch(e){o(e)}}function s(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,s)}c((n=n.apply(e,t||[])).next())}))}function d(e,t){var r,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(a=2&o[0]?n.return:o[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,o[1])).done)return a;switch(n=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,n=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(e,i)}catch(e){o=[6,e],n=0}finally{r=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}Object.create;Object.create;var f=Symbol();function l(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function h(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function v(e,t){var r;t.headers||(t.headers={});var n=t.cache,a=n.etag,o=n.modifiedSince;if(a){var s=!0===a?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:a;s&&(t.headers[i.IfNoneMatch]=s)}o&&(t.headers[i.IfModifiedSince]=!0===o?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function p(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=c(c({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){var t=this,r=function(r){return u(t,void 0,void 0,(function(){var t,n,a,o,i;return d(this,(function(s){switch(s.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=c(c({},e.defaults.cache),r.cache),h(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return"empty"!==(n=s.sent()).state&&"stale"!==n.state?[3,5]:e.waiting[t]?[4,e.storage.get(t,r)]:[3,3];case 2:if("empty"!==(n=s.sent()).state)return[3,5];s.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,n){e=r,t=n}));return r.resolve=e,r.reject=t,r[f]=1,r}(),null===(i=e.waiting[t])||void 0===i||i.catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:n.state,data:n.data,createdAt:n.createdAt},r)];case 4:return s.sent(),"stale"===n.state&&v(n,r),r.validateStatus=l(r.validateStatus),[2,r];case 5:return"loading"!==n.state?[3,12]:(o=e.waiting[t])?[3,7]:[4,e.storage.remove(t,r)];case 6:return s.sent(),[2,r];case 7:0,s.label=8;case 8:return s.trys.push([8,10,,11]),[4,o];case 9:return a=s.sent(),[3,11];case 10:return s.sent(),[2,r];case 11:return[3,13];case 12:a=n.data,s.label=13;case 13:return r.adapter=function(){return Promise.resolve({config:r,data:a.data,headers:a.headers,status:a.status,statusText:a.statusText,cached:!0,id:t})},[2,r]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function m(e,t){var r;return u(this,void 0,void 0,(function(){var n,a,o,i,s,c,u,f,l,h;return d(this,(function(d){switch(d.label){case 0:return"function"==typeof t?[2,t(e)]:(n=t.statusCheck,a=t.responseMatch,o=t.containsHeaders,(s=n)?[4,n(e.status)]:[3,2]);case 1:s=!d.sent(),d.label=2;case 2:return(i=s)?[3,5]:(c=a)?[4,a(e)]:[3,4];case 3:c=!d.sent(),d.label=4;case 4:i=c,d.label=5;case 5:if(i)return[2,!1];if(!o)return[3,9];u=0,f=Object.entries(o),d.label=6;case 6:return u<f.length?(l=f[u],h=l[0],[4,(0,l[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!d.sent())return[2,!1];d.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function b(e,t,r){return u(this,void 0,void 0,(function(){var n,a,o,i,s,c,u;return d(this,(function(d){switch(d.label){case 0:for(a in n=[],r)n.push(a);o=0,d.label=1;case 1:return o<n.length?(i=n[o],"delete"!==(s=r[i])?[3,3]:[4,e.remove(i,t.config)]):[3,10];case 2:return d.sent(),[3,9];case 3:return[4,e.get(i,t.config)];case 4:return"loading"===(c=d.sent()).state?[3,9]:[4,s(c,t)];case 5:return"delete"!==(u=d.sent())?[3,7]:[4,e.remove(i,t.config)];case 6:return d.sent(),[3,9];case 7:return"ignore"===u?[3,9]:[4,e.set(i,u,t.config)];case 8:d.sent(),d.label=9;case 9:return o++,[3,1];case 10:return[2]}}))}))}function y(e){var t=this,r=function(r,n){return u(t,void 0,void 0,(function(){var t;return d(this,(function(a){switch(a.label){case 0:return[4,e.storage.remove(r,n)];case 1:return a.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},n=function(n){return u(t,void 0,void 0,(function(){var t,a,o,s,u,f,l,h,v,g,y,x,w,S;return d(this,(function(d){switch(d.label){case 0:return t=n.id=null!==(x=(S=n.config).id)&&void 0!==x?x:S.id=e.generateKey(n.config),null!==(w=n.cached)&&void 0!==w||(n.cached=!1),n.cached?[2,n]:(a=n.config.cache)?(o=n.config,[4,e.storage.get(t,o)]):[2,c(c({},n),{cached:!1})];case 1:return"loading"!==(s=d.sent()).state?[2,n]:(u=!s.data)?[4,m(n,a.cachePredicate)]:[3,3];case 2:u=!d.sent(),d.label=3;case 3:return u?[4,r(t,o)]:[3,5];case 4:return d.sent(),[2,n];case 5:for(f in i)f.startsWith("XAxiosCache")&&delete n.headers[f];return a.etag&&!0!==a.etag&&(n.headers[i.XAxiosCacheEtag]=a.etag),a.modifiedSince&&(n.headers[i.XAxiosCacheLastModified]=!0===a.modifiedSince?"use-cache-timestamp":a.modifiedSince.toUTCString()),l=a.ttl||-1,(null==a?void 0:a.interpretHeader)?"dont cache"!==(h=e.headerInterpreter(n.headers))?[3,7]:[4,r(t,o)]:[3,8];case 6:return d.sent(),[2,n];case 7:l="not enough headers"===h?l:h,d.label=8;case 8:return v=p(n,s.data),"function"!=typeof l?[3,10]:[4,l(n)];case 9:l=d.sent(),d.label=10;case 10:return a.staleIfError&&(n.headers[i.XAxiosCacheStaleIfError]=String(l)),(null==a?void 0:a.update)?[4,b(e.storage,n,a.update)]:[3,12];case 11:d.sent(),d.label=12;case 12:return g={state:"cached",ttl:l,createdAt:Date.now(),data:v},(y=e.waiting[t])&&(y.resolve(g.data),delete e.waiting[t]),[4,e.storage.set(t,g,o)];case 13:return d.sent(),[2,n]}}))}))},a=function(n){return u(t,void 0,void 0,(function(){var t,a,o,i,s,c;return d(this,(function(u){switch(u.label){case 0:if(!(null==(t=n.config)?void 0:t.cache)||!t.id)throw n;return[4,e.storage.get(t.id,t)];case 1:return a=u.sent(),o=t.cache,"loading"===a.state&&"stale"===a.previous?[3,3]:[4,r(t.id,t)];case 2:throw u.sent(),n;case 3:return(null==o?void 0:o.staleIfError)?"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(n.response,a,n)]:[3,8];case 4:return s=u.sent(),[3,6];case 5:s=o.staleIfError,u.label=6;case 6:return!0===(i=s)||"number"==typeof i&&a.createdAt+i>Date.now()?(null===(c=e.waiting[t.id])||void 0===c||c.resolve(a.data),delete e.waiting[t.id],[4,e.storage.set(t.id,{state:"stale",createdAt:Date.now(),data:a.data},t)]):[3,8];case 7:return u.sent(),[2,{cached:!0,config:t,id:t.id,data:a.data.data,headers:a.data.headers,status:a.data.status,statusText:a.data.statusText}];case 8:throw n}}))}))};return{onFulfilled:n,onRejected:a,apply:function(){return e.interceptors.response.use(n,a)}}}var x=function(e){return!!e&&!!e["is-storage"]};function w(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function S(e){return e.createdAt+e.ttl<=Date.now()}function I(e){var t,r=this,n=e.set,a=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=n,t.remove=o,t.get=function(e,t){return u(r,void 0,void 0,(function(){var r,i;return d(this,(function(s){switch(s.label){case 0:return[4,a(e,t)];case 1:return(r=s.sent())?"cached"===r.state&&S(r)?w(r)?(i={state:"stale",createdAt:r.createdAt,data:r.data},[4,n(e,i,t)]):[3,3]:[2,r]:[2,{state:"empty"}];case 2:return s.sent(),[2,i];case 3:return[4,o(e,t)];case 4:return s.sent(),[2,{state:"empty"}]}}))}))},t}function C(e){void 0===e&&(e=!1);var t=I({set:function(e,r){t.data[e]=r},remove:function(e){delete t.data[e]},find:function(r){var n=t.data[r];return e&&void 0!==n?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n}});return t.data=Object.create(null),t}function A(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},n=Object.keys(e).sort((function(e,t){return e>t?1:-1})),a=n.length;a--;){var o=n[a];r[o]=A(e[o])}return String(e.constructor)+JSON.stringify(r,n)}return t+String(e)}function E(e){e=A(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var O=/^\/|\/$/g;function j(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:E(r))}}var T=j((function(e){var t=e.baseURL,r=void 0===t?"":t,n=e.url,a=void 0===n?"":n,o=e.method,i=void 0===o?"get":o,s=e.params,c=e.data;return r&&(r=r.replace(O,"")),a&&(a=a.replace(O,"")),i&&(i=i.toLowerCase()),{url:r+(r&&a?"/":"")+a,params:s,method:i,data:c}}));function M(e,t){var r,n,a,o,i;void 0===t&&(t={});var c=e;if(c.storage=t.storage||C(),!x(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||T,c.headerInterpreter=t.headerInterpreter||s,c.requestInterceptor=t.requestInterceptor||g(c),c.responseInterceptor=t.responseInterceptor||y(c),c.debug=t.debug,c.defaults.cache={update:t.update||{},ttl:null!==(r=t.ttl)&&void 0!==r?r:3e5,methods:t.methods||["get"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return e>=200&&e<400}},etag:null===(n=t.etag)||void 0===n||n,modifiedSince:null!==(a=t.modifiedSince)&&void 0!==a?a:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,staleIfError:null===(i=t.staleIfError)||void 0===i||i},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function P(e,t){return void 0===t&&(t=""),I({find:function(r){var n=e.getItem(t+r);return n?JSON.parse(n):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,n){var a=function(){return e.setItem(t+r,JSON.stringify(n))};try{return a()}catch(n){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),i=0,s=o;i<s.length;i++){var c=s[i];"cached"===c[1].state&&S(c[1])&&!w(c[1])&&e.removeItem(c[0])}try{return a()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,f=u;d<f.length;d++){var l=f[d];e.removeItem(l[0]);try{return a()}catch(e){}}}e.removeItem(t+r)}}})}return t}()}));
\No newline at end of file