axios-cache-interceptor
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,2 +0,2 @@ | ||
!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,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r: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:()=>s,buildKeyGenerator:()=>E,buildMemoryStorage:()=>S,buildStorage:()=>C,buildWebStorage:()=>R,canStale:()=>w,createCacheResponse:()=>h,createValidateStatus:()=>l,defaultHeaderInterpreter:()=>c,defaultKeyGenerator:()=>O,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>v,isExpired:()=>x,isMethodIn:()=>u,isStorage:()=>b,setupCache:()=>T,testCachePredicate:()=>p,updateCache:()=>m,updateStaleRequest:()=>f});var a=Symbol("cache-parser");function r(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function o(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var n=Number;function i(e){var t=Object.defineProperty({},a,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},a=e.toLowerCase().replace(/\s+/g,"").split(",");for(var r in a){var o,n=a[r].split("=",2);t[n[0]]=null==(o=n[1])||o}return t}(e),s=i["max-age"],c=i["max-stale"],d=i["min-fresh"],l=i["s-maxage"],u=i["stale-if-error"],f=i["stale-while-revalidate"];return o(i.immutable)&&(t.immutable=!0),r(s)&&(t.maxAge=n(s)),r(c)&&(t.maxStale=n(c)),r(d)&&(t.minFresh=n(d)),o(i["must-revalidate"])&&(t.mustRevalidate=!0),o(i["must-understand"])&&(t.mustUnderstand=!0),o(i["no-cache"])&&(t.noCache=!0),o(i["no-store"])&&(t.noStore=!0),o(i["no-transform"])&&(t.noTransform=!0),o(i["only-if-cached"])&&(t.onlyIfCached=!0),o(i.private)&&(t.private=!0),o(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),o(i.public)&&(t.public=!0),r(l)&&(t.sMaxAge=n(l)),r(u)&&(t.staleIfError=n(u)),r(f)&&(t.staleWhileRevalidate=n(f)),t}const s=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",Pragma:"pragma",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),c=e=>{if(!e)return"not enough headers";const t=e[s.CacheControl];if(t){const{noCache:a,noStore:r,maxAge:o,immutable:n,staleWhileRevalidate:c}=i(String(t));if(a||r)return"dont cache";if(n)return{cache:31536e6};if(void 0!==o){const t=e[s.Age];return{cache:t?1e3*(o-Number(t)):1e3*o,stale:void 0!==c?1e3*c:0}}}const a=e[s.Expires];if(a){const e=Date.parse(String(a))-Date.now();return e>=0?{cache:e}:"dont cache"}return"not enough headers"};var d=Symbol();function l(e){return e?t=>e(t)||304===t:e=>e>=200&&e<300||304===e}function u(e="get",t=[]){return e=e.toLowerCase(),t.some((t=>t===e))}function f(e,t){var a;t.headers||(t.headers={});const{etag:r,modifiedSince:o}=t.cache;if(r){const o=!0===r?null===(a=e.data)||void 0===a?void 0:a.headers[s.ETag]:r;o&&(t.headers[s.IfNoneMatch]=o)}o&&(t.headers[s.IfModifiedSince]=!0===o?e.data.headers[s.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function h(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=Object.assign(Object.assign({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){const t=async t=>{var a,r,o,n,i,c,h,g,p,m,v,b,y,w,x,C,S,I,A,j,E,O,T;const R=t.id=e.generateKey(t);if(!1===t.cache)return null===(a=e.debug)||void 0===a||a.call(e,{msg:"Ignoring cache because config.cache === false",data:t}),t;if(t.cache=Object.assign(Object.assign({},e.defaults.cache),t.cache),t.cache.cacheTakeover&&(null!==(r=(I=t.headers)[A=s.CacheControl])&&void 0!==r||(I[A]="no-cache"),null!==(o=(j=t.headers)[E=s.Pragma])&&void 0!==o||(j[E]="no-cache"),null!==(n=(O=t.headers)[T=s.Expires])&&void 0!==n||(O[T]="0")),!u(t.method,t.cache.methods))return null===(i=e.debug)||void 0===i||i.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let M=await e.storage.get(R,t);const k=t.cache.override;e:if("empty"===M.state||"stale"===M.state||k){if(e.waiting[R]&&!k&&(M=await e.storage.get(R,t),"empty"!==M.state)){null===(c=e.debug)||void 0===c||c.call(e,{id:R,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[R]=function(){var e,t,a=new Promise((function(a,r){e=a,t=r}));return a.resolve=e,a.reject=t,a[d]=1,a}(),e.waiting[R].catch((()=>{})),await e.storage.set(R,{state:"loading",previous:k?M.data?"stale":"empty":M.state,data:M.data,createdAt:k&&!M.createdAt?Date.now():M.createdAt},t),"stale"===M.state&&(f(M,t),null===(h=e.debug)||void 0===h||h.call(e,{id:R,msg:"Updated stale request"})),t.validateStatus=l(t.validateStatus),null===(g=e.debug)||void 0===g||g.call(e,{id:R,msg:"Sending request, waiting for response",data:{overrideCache:k,state:M.state}}),("stale"===M.state||M.data)&&await(null===(m=(p=t.cache).hydrate)||void 0===m?void 0:m.call(p,M)),t}let P;if("loading"===M.state){const a=e.waiting[R];if(!a)return await e.storage.remove(R,t),M.data&&await(null===(b=(v=t.cache).hydrate)||void 0===b?void 0:b.call(v,M)),t;null===(y=e.debug)||void 0===y||y.call(e,{id:R,msg:"Detected concurrent request, waiting for it to finish"});try{P=await a}catch(a){return null===(w=e.debug)||void 0===w||w.call(e,{id:R,msg:"Deferred rejected, requesting again",data:a}),M.data&&await(null===(C=(x=t.cache).hydrate)||void 0===C?void 0:C.call(x,M)),t}}else P=M.data;return t.adapter=()=>Promise.resolve({config:t,data:P.data,headers:P.headers,status:P.status,statusText:P.statusText,cached:!0,id:R}),null===(S=e.debug)||void 0===S||S.call(e,{id:R,msg:"Returning cached response"}),t};return{onFulfilled:t,apply:()=>e.interceptors.request.use(t)}}async function p(e,t){var a;if("function"==typeof t)return t(e);const{statusCheck:r,responseMatch:o,containsHeaders:n}=t;if(r&&!await r(e.status)||o&&!await o(e))return!1;if(n)for(const[t,r]of Object.entries(n))if(!await r(null!==(a=e.headers[t.toLowerCase()])&&void 0!==a?a:e.headers[t]))return!1;return!0}async function m(e,t,a){if("function"==typeof a)return a(t);for(const[r,o]of Object.entries(a)){if("delete"===o){await e.remove(r,t.config);continue}const a=await e.get(r,t.config);if("loading"===a.state)continue;const n=await o(a,t);"delete"!==n?"ignore"!==n&&await e.set(r,n,t.config):await e.remove(r,t.config)}}function v(e){const t=async(t,a)=>{var r;await e.storage.remove(t,a),null===(r=e.waiting[t])||void 0===r||r.reject(),delete e.waiting[t]},a=async a=>{var r,o,n,i,c,d,l,f,g,v,b,y,w;if(!a.config)throw null===(r=e.debug)||void 0===r||r.call(e,{msg:"Response interceptor received an unknown response.",data:a}),a;const x=a.id=null!==(o=(w=a.config).id)&&void 0!==o?o:w.id=e.generateKey(a.config);if(null!==(n=a.cached)&&void 0!==n||(a.cached=!1),a.cached)return null===(i=e.debug)||void 0===i||i.call(e,{id:x,msg:"Returned cached response"}),a;const C=a.config,S=C.cache;if(!S)return null===(c=e.debug)||void 0===c||c.call(e,{id:x,msg:"Response with config.cache falsy",data:a}),Object.assign(Object.assign({},a),{cached:!1});if(S.update&&await m(e.storage,a,S.update),!u(C.method,S.methods))return null===(d=e.debug)||void 0===d||d.call(e,{id:x,msg:`Ignored because method (${C.method}) is not in cache.methods (${S.methods})`,data:{config:C,cacheConfig:S}}),a;const I=await e.storage.get(x,C);if("loading"!==I.state)return null===(l=e.debug)||void 0===l||l.call(e,{id:x,msg:"Response not cached and storage isn't loading",data:{cache:I,response:a}}),a;if(!I.data&&!await p(a,S.cachePredicate))return await t(x,C),null===(f=e.debug)||void 0===f||f.call(e,{id:x,msg:"Cache predicate rejected this response"}),a;for(const e of Object.keys(a.headers))e.startsWith("x-axios-cache")&&delete a.headers[e];S.etag&&!0!==S.etag&&(a.headers[s.XAxiosCacheEtag]=S.etag),S.modifiedSince&&(a.headers[s.XAxiosCacheLastModified]=!0===S.modifiedSince?"use-cache-timestamp":S.modifiedSince.toUTCString());let A,j=S.ttl||-1;if(S.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(x,C),null===(g=e.debug)||void 0===g||g.call(e,{id:x,msg:"Cache header interpreted as 'dont cache'",data:{cache:I,response:a,expirationTime:r}}),a;"not enough headers"!==r&&("number"==typeof r?j=r:(j=r.cache,A=r.stale))}const E=h(a,I.data);"function"==typeof j&&(j=await j(a)),S.staleIfError&&(a.headers[s.XAxiosCacheStaleIfError]=String(j)),null===(v=e.debug)||void 0===v||v.call(e,{id:x,msg:"Useful response configuration found",data:{cacheConfig:S,cacheResponse:E}});const O={state:"cached",ttl:j,staleTtl:A,createdAt:Date.now(),data:E},T=e.waiting[x];return T&&(T.resolve(O.data),delete e.waiting[x],null===(b=e.debug)||void 0===b||b.call(e,{id:x,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(x,O,C),null===(y=e.debug)||void 0===y||y.call(e,{id:x,msg:"Response cached",data:{cache:O,response:a}}),a},r=async a=>{var r,o,n,c,d,l,f;const h=a.config,g=h.id,p=h.cache,m=a.response;if(!p||!g)throw null===(r=e.debug)||void 0===r||r.call(e,{msg:"Web request returned an error but cache handling is not enabled",data:{error:a}}),a;if(!u(h.method,p.methods))throw null===(o=e.debug)||void 0===o||o.call(e,{id:g,msg:`Ignored because method (${h.method}) is not in cache.methods (${p.methods})`,data:{config:h,cacheConfig:p}}),a;const v=await e.storage.get(g,h);if("loading"!==v.state||"stale"!==v.previous)throw await t(g,h),null===(n=e.debug)||void 0===n||n.call(e,{id:g,msg:"Caught an error in the request interceptor",data:{cache:v,error:a,config:h}}),a;if(p.staleIfError){const t=String(null==m?void 0:m.headers[s.CacheControl]),r=t&&i(t).staleIfError,o="function"==typeof p.staleIfError?await p.staleIfError(m,v,a):!0===p.staleIfError&&r?1e3*r:p.staleIfError;if(null===(c=e.debug)||void 0===c||c.call(e,{id:g,msg:"Found cache if stale config for rejected response",data:{error:a,config:h,staleIfError:o}}),!0===o||"number"==typeof o&&v.createdAt+o>Date.now())return null===(d=e.waiting[g])||void 0===d||d.resolve(v.data),delete e.waiting[g],await e.storage.set(g,{state:"stale",createdAt:Date.now(),data:v.data},h),null===(l=e.debug)||void 0===l||l.call(e,{id:g,msg:"staleIfError resolved this response with cached data",data:{error:a,config:h,cache:v}}),{cached:!0,config:h,id:g,data:v.data.data,headers:v.data.headers,status:v.data.status,statusText:v.data.statusText}}throw null===(f=e.debug)||void 0===f||f.call(e,{id:g,msg:"Received an unknown error that could not be handled",data:{error:a,config:h}}),a};return{onFulfilled:a,onRejected:r,apply:()=>e.interceptors.response.use(a,r)}}const b=e=>!!e&&!!e["is-storage"];function y(e){const t=e.data.headers;return s.ETag in t||s.LastModified in t||s.XAxiosCacheEtag in t||s.XAxiosCacheLastModified in t}function w(e){return!String(e.data.headers[s.CacheControl]).includes("must-revalidate")&&(!!y(e)||"cached"===e.state&&void 0!==e.staleTtl&&e.createdAt+e.ttl+e.staleTtl<=Date.now())}function x(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function C({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,o)=>{let n=await t(r,o);if(!n)return{state:"empty"};if("empty"===n.state||"loading"===n.state)return n;if("cached"===n.state){if(!x(n))return n;if(!w(n))return await a(r,o),{state:"empty"};n={state:"stale",createdAt:n.createdAt,data:n.data,ttl:void 0!==n.staleTtl?n.staleTtl+n.ttl:void 0},await e(r,n,o)}return x(n)?y(n)?n:(await a(r,o),{state:"empty"}):n}}}function S(e=!1,t=!1){const a=C({set:(e,t)=>{a.data[e]=t},remove:e=>{delete a.data[e]},find:t=>{const r=a.data[t];return e&&void 0!==r?"function"==typeof structuredClone?structuredClone(r):JSON.parse(JSON.stringify(r)):r}});return a.data=Object.create(null),t&&(a.cleaner=setInterval((()=>{const e=Object.keys(a.data);let t,r,o=-1;for(;++o<e.length;)r=e[o],t=a.data[r],"empty"!==t.state?"cached"===t.state&&x(t)&&!w(t)&&a.remove(r):a.remove(r)}),t)),a}function I(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var a=Array.isArray(e)?[]:{},r=Object.keys(e).sort((function(e,t){return e>t?1:-1})),o=r.length;o--;){var n=r[o];a[n]=I(e[n])}return String(e.constructor)+JSON.stringify(a,r)}return t+String(e)}function A(e){e=I(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const j=/^\/|\/$/g;function E(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${A(a)}`}}const O=E((({baseURL:e="",url:t="",method:a="get",params:r,data:o})=>(e&&(e=e.replace(j,"")),t&&(t=t.replace(j,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:o})));function T(e,t={}){var a,r,o,n,i,s;const d=e;if(d.defaults.cache)throw new Error("setupCache() should be called only once");if(d.storage=t.storage||S(),!b(d.storage))throw new Error("Use buildStorage() function");return d.waiting=t.waiting||{},d.generateKey=t.generateKey||O,d.headerInterpreter=t.headerInterpreter||c,d.requestInterceptor=t.requestInterceptor||g(d),d.responseInterceptor=t.responseInterceptor||v(d),d.debug=t.debug,d.defaults.cache={update:t.update||{},ttl:null!==(a=t.ttl)&&void 0!==a?a:3e5,methods:t.methods||["get","head"],cachePredicate:t.cachePredicate||{statusCheck:e=>[200,203,300,301,302,404,405,410,414,501].includes(e)},etag:null===(r=t.etag)||void 0===r||r,modifiedSince:null!==(o=t.modifiedSince)&&void 0!==o?o:!1===t.etag,interpretHeader:null===(n=t.interpretHeader)||void 0===n||n,cacheTakeover:null===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:!1,hydrate:void 0},d.requestInterceptor.apply(),d.responseInterceptor.apply(),d}function R(e,t="axios-cache-"){return C({find:a=>{const r=e.getItem(t+a);return r?JSON.parse(r):void 0},remove:a=>{e.removeItem(t+a)},set:(a,r)=>{const o=()=>e.setItem(t+a,JSON.stringify(r));try{return o()}catch(r){const n=Object.entries(e).filter((e=>e[0].startsWith(t))).map((e=>[e[0],JSON.parse(e[1])]));for(const t of n)"cached"===t[1].state&&x(t[1])&&!w(t[1])&&e.removeItem(t[0]);try{return o()}catch(t){const a=n.sort(((e,t)=>(e[1].createdAt||0)-(t[1].createdAt||0)));for(const t of a){e.removeItem(t[0]);try{return o()}catch(e){}}}e.removeItem(t+a)}}})}return console.error("You are using a development build. Make sure to use the correct build in production\nhttps://axios-cache-interceptor.js.org/guide/getting-started\n\n"),t})())); | ||
!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,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r: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:()=>s,buildKeyGenerator:()=>O,buildMemoryStorage:()=>S,buildStorage:()=>C,buildWebStorage:()=>R,canStale:()=>w,createCacheResponse:()=>h,createValidateStatus:()=>l,defaultHeaderInterpreter:()=>c,defaultKeyGenerator:()=>E,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>v,isExpired:()=>x,isMethodIn:()=>u,isStorage:()=>b,setupCache:()=>T,testCachePredicate:()=>p,updateCache:()=>m,updateStaleRequest:()=>f});var a=Symbol("cache-parser");function r(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function n(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;function i(e){var t=Object.defineProperty({},a,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},a=e.toLowerCase().replace(/\s+/g,"").split(",");for(var r in a){var n,o=a[r].split("=",2);t[o[0]]=null==(n=o[1])||n}return t}(e),s=i["max-age"],c=i["max-stale"],d=i["min-fresh"],l=i["s-maxage"],u=i["stale-if-error"],f=i["stale-while-revalidate"];return n(i.immutable)&&(t.immutable=!0),r(s)&&(t.maxAge=o(s)),r(c)&&(t.maxStale=o(c)),r(d)&&(t.minFresh=o(d)),n(i["must-revalidate"])&&(t.mustRevalidate=!0),n(i["must-understand"])&&(t.mustUnderstand=!0),n(i["no-cache"])&&(t.noCache=!0),n(i["no-store"])&&(t.noStore=!0),n(i["no-transform"])&&(t.noTransform=!0),n(i["only-if-cached"])&&(t.onlyIfCached=!0),n(i.private)&&(t.private=!0),n(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),n(i.public)&&(t.public=!0),r(l)&&(t.sMaxAge=o(l)),r(u)&&(t.staleIfError=o(u)),r(f)&&(t.staleWhileRevalidate=o(f)),t}const s=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",Pragma:"pragma",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),c=e=>{if(!e)return"not enough headers";const t=e[s.CacheControl];if(t){const{noCache:a,noStore:r,maxAge:n,immutable:o,staleWhileRevalidate:c}=i(String(t));if(a||r)return"dont cache";if(o)return{cache:31536e6};if(void 0!==n){const t=e[s.Age];return{cache:t?1e3*(n-Number(t)):1e3*n,stale:void 0!==c?1e3*c:0}}}const a=e[s.Expires];if(a){const e=Date.parse(String(a))-Date.now();return e>=0?{cache:e}:"dont cache"}return"not enough headers"};var d=Symbol();function l(e){return e?t=>e(t)||304===t:e=>e>=200&&e<300||304===e}function u(e="get",t=[]){return e=e.toLowerCase(),t.some((t=>t===e))}function f(e,t){var a;t.headers||(t.headers={});const{etag:r,modifiedSince:n}=t.cache;if(r){const n=!0===r?null===(a=e.data)||void 0===a?void 0:a.headers[s.ETag]:r;n&&(t.headers[s.IfNoneMatch]=n)}n&&(t.headers[s.IfModifiedSince]=!0===n?e.data.headers[s.LastModified]||new Date(e.createdAt).toUTCString():n.toUTCString())}function h(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=Object.assign(Object.assign({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){const t=async t=>{var a,r,n,o,i,c,h,g,p,m,v,b,y,w,x,C,S,I,j,A,O,E,T;const R=t.id=e.generateKey(t);if(!1===t.cache)return null===(a=e.debug)||void 0===a||a.call(e,{msg:"Ignoring cache because config.cache === false",data:t}),t;if(t.cache=Object.assign(Object.assign({},e.defaults.cache),t.cache),t.cache.cacheTakeover&&(null!==(r=(I=t.headers)[j=s.CacheControl])&&void 0!==r||(I[j]="no-cache"),null!==(n=(A=t.headers)[O=s.Pragma])&&void 0!==n||(A[O]="no-cache"),null!==(o=(E=t.headers)[T=s.Expires])&&void 0!==o||(E[T]="0")),!u(t.method,t.cache.methods))return null===(i=e.debug)||void 0===i||i.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let M=await e.storage.get(R,t);const k=t.cache.override;e:if("empty"===M.state||"stale"===M.state||k){if(e.waiting[R]&&!k&&(M=await e.storage.get(R,t),"empty"!==M.state)){null===(c=e.debug)||void 0===c||c.call(e,{id:R,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[R]=function(){var e,t,a=new Promise((function(a,r){e=a,t=r}));return a.resolve=e,a.reject=t,a[d]=1,a}(),e.waiting[R].catch((()=>{})),await e.storage.set(R,{state:"loading",previous:k?M.data?"stale":"empty":M.state,data:M.data,createdAt:k&&!M.createdAt?Date.now():M.createdAt},t),"stale"===M.state&&(f(M,t),null===(h=e.debug)||void 0===h||h.call(e,{id:R,msg:"Updated stale request"})),t.validateStatus=l(t.validateStatus),null===(g=e.debug)||void 0===g||g.call(e,{id:R,msg:"Sending request, waiting for response",data:{overrideCache:k,state:M.state}}),("stale"===M.state||M.data)&&await(null===(m=(p=t.cache).hydrate)||void 0===m?void 0:m.call(p,M)),t}let P;if("loading"===M.state){const a=e.waiting[R];if(!a)return await e.storage.remove(R,t),M.data&&await(null===(b=(v=t.cache).hydrate)||void 0===b?void 0:b.call(v,M)),t;null===(y=e.debug)||void 0===y||y.call(e,{id:R,msg:"Detected concurrent request, waiting for it to finish"});try{P=await a}catch(a){return null===(w=e.debug)||void 0===w||w.call(e,{id:R,msg:"Deferred rejected, requesting again",data:a}),M.data&&await(null===(C=(x=t.cache).hydrate)||void 0===C?void 0:C.call(x,M)),t}}else P=M.data;return t.adapter=()=>Promise.resolve({config:t,data:P.data,headers:P.headers,status:P.status,statusText:P.statusText,cached:!0,id:R}),null===(S=e.debug)||void 0===S||S.call(e,{id:R,msg:"Returning cached response"}),t};return{onFulfilled:t,apply:()=>e.interceptors.request.use(t)}}async function p(e,t){var a;if("function"==typeof t)return t(e);const{statusCheck:r,responseMatch:n,containsHeaders:o}=t;if(r&&!await r(e.status)||n&&!await n(e))return!1;if(o)for(const[t,r]of Object.entries(o))if(!await r(null!==(a=e.headers[t.toLowerCase()])&&void 0!==a?a:e.headers[t]))return!1;return!0}async function m(e,t,a){if("function"==typeof a)return a(t);for(const[r,n]of Object.entries(a)){if("delete"===n){await e.remove(r,t.config);continue}const a=await e.get(r,t.config);if("loading"===a.state)continue;const o=await n(a,t);"delete"!==o?"ignore"!==o&&await e.set(r,o,t.config):await e.remove(r,t.config)}}function v(e){const t=async(t,a)=>{var r;await e.storage.remove(t,a),null===(r=e.waiting[t])||void 0===r||r.reject(),delete e.waiting[t]},a=async a=>{var r,n,o,i,c,d,l,f,g,v,b,y,w;if(!a.config)throw null===(r=e.debug)||void 0===r||r.call(e,{msg:"Response interceptor received an unknown response.",data:a}),a;const x=a.id=null!==(n=(w=a.config).id)&&void 0!==n?n:w.id=e.generateKey(a.config);if(null!==(o=a.cached)&&void 0!==o||(a.cached=!1),a.cached)return null===(i=e.debug)||void 0===i||i.call(e,{id:x,msg:"Returned cached response"}),a;const C=a.config,S=C.cache;if(!S)return null===(c=e.debug)||void 0===c||c.call(e,{id:x,msg:"Response with config.cache falsy",data:a}),Object.assign(Object.assign({},a),{cached:!1});if(S.update&&await m(e.storage,a,S.update),!u(C.method,S.methods))return null===(d=e.debug)||void 0===d||d.call(e,{id:x,msg:`Ignored because method (${C.method}) is not in cache.methods (${S.methods})`,data:{config:C,cacheConfig:S}}),a;const I=await e.storage.get(x,C);if("loading"!==I.state)return null===(l=e.debug)||void 0===l||l.call(e,{id:x,msg:"Response not cached and storage isn't loading",data:{cache:I,response:a}}),a;if(!I.data&&!await p(a,S.cachePredicate))return await t(x,C),null===(f=e.debug)||void 0===f||f.call(e,{id:x,msg:"Cache predicate rejected this response"}),a;for(const e of Object.keys(a.headers))e.startsWith("x-axios-cache")&&delete a.headers[e];S.etag&&!0!==S.etag&&(a.headers[s.XAxiosCacheEtag]=S.etag),S.modifiedSince&&(a.headers[s.XAxiosCacheLastModified]=!0===S.modifiedSince?"use-cache-timestamp":S.modifiedSince.toUTCString());let j,A=S.ttl||-1;if(S.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(x,C),null===(g=e.debug)||void 0===g||g.call(e,{id:x,msg:"Cache header interpreted as 'dont cache'",data:{cache:I,response:a,expirationTime:r}}),a;"not enough headers"!==r&&("number"==typeof r?A=r:(A=r.cache,j=r.stale))}const O=h(a,I.data);"function"==typeof A&&(A=await A(a)),S.staleIfError&&(a.headers[s.XAxiosCacheStaleIfError]=String(A)),null===(v=e.debug)||void 0===v||v.call(e,{id:x,msg:"Useful response configuration found",data:{cacheConfig:S,cacheResponse:O}});const E={state:"cached",ttl:A,staleTtl:j,createdAt:Date.now(),data:O},T=e.waiting[x];return T&&(T.resolve(E.data),delete e.waiting[x],null===(b=e.debug)||void 0===b||b.call(e,{id:x,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(x,E,C),null===(y=e.debug)||void 0===y||y.call(e,{id:x,msg:"Response cached",data:{cache:E,response:a}}),a},r=async a=>{var r,n,o,c,d,l,f;const h=a.config,g=h.id,p=h.cache,m=a.response;if(!p||!g)throw null===(r=e.debug)||void 0===r||r.call(e,{msg:"Web request returned an error but cache handling is not enabled",data:{error:a}}),a;if(!u(h.method,p.methods))throw null===(n=e.debug)||void 0===n||n.call(e,{id:g,msg:`Ignored because method (${h.method}) is not in cache.methods (${p.methods})`,data:{config:h,cacheConfig:p}}),a;const v=await e.storage.get(g,h);if("loading"!==v.state||"stale"!==v.previous)throw await t(g,h),null===(o=e.debug)||void 0===o||o.call(e,{id:g,msg:"Caught an error in the request interceptor",data:{cache:v,error:a,config:h}}),a;if(p.staleIfError){const t=String(null==m?void 0:m.headers[s.CacheControl]),r=t&&i(t).staleIfError,n="function"==typeof p.staleIfError?await p.staleIfError(m,v,a):!0===p.staleIfError&&r?1e3*r:p.staleIfError;if(null===(c=e.debug)||void 0===c||c.call(e,{id:g,msg:"Found cache if stale config for rejected response",data:{error:a,config:h,staleIfError:n}}),!0===n||"number"==typeof n&&v.createdAt+n>Date.now())return null===(d=e.waiting[g])||void 0===d||d.resolve(v.data),delete e.waiting[g],await e.storage.set(g,{state:"stale",createdAt:Date.now(),data:v.data},h),null===(l=e.debug)||void 0===l||l.call(e,{id:g,msg:"staleIfError resolved this response with cached data",data:{error:a,config:h,cache:v}}),{cached:!0,config:h,id:g,data:v.data.data,headers:v.data.headers,status:v.data.status,statusText:v.data.statusText}}throw null===(f=e.debug)||void 0===f||f.call(e,{id:g,msg:"Received an unknown error that could not be handled",data:{error:a,config:h}}),a};return{onFulfilled:a,onRejected:r,apply:()=>e.interceptors.response.use(a,r)}}const b=e=>!!e&&!!e["is-storage"];function y(e){const t=e.data.headers;return s.ETag in t||s.LastModified in t||s.XAxiosCacheEtag in t||s.XAxiosCacheLastModified in t}function w(e){return!String(e.data.headers[s.CacheControl]).includes("must-revalidate")&&(!!y(e)||"cached"===e.state&&void 0!==e.staleTtl&&e.createdAt+e.ttl+e.staleTtl<=Date.now())}function x(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function C({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,n)=>{let o=await t(r,n);if(!o)return{state:"empty"};if("empty"===o.state||"loading"===o.state)return o;if("cached"===o.state){if(!x(o))return o;if(!w(o))return await a(r,n),{state:"empty"};o={state:"stale",createdAt:o.createdAt,data:o.data,ttl:void 0!==o.staleTtl?o.staleTtl+o.ttl:void 0},await e(r,o,n)}return x(o)?y(o)?o:(await a(r,n),{state:"empty"}):o}}}function S(e=!1,t=!1,a=!1){const r=C({set:(e,t)=>{if(a){let e=Object.keys(r.data);if(e.length>=a)for(r.cleanup(),e=Object.keys(r.data);e.length>=a;)delete r.data[e.shift()]}r.data[e]=t},remove:e=>{delete r.data[e]},find:t=>{const a=r.data[t];return e&&void 0!==a?"function"==typeof structuredClone?structuredClone(a):JSON.parse(JSON.stringify(a)):a}});return r.data=Object.create(null),r.cleanup=()=>{const e=Object.keys(r.data);let t,a,n=-1;for(;++n<e.length;)a=e[n],t=r.data[a],"empty"!==t.state?"cached"===t.state&&x(t)&&!w(t)&&r.remove(a):r.remove(a)},t&&(r.cleaner=setInterval(r.cleanup,t)),r}function I(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var a=Array.isArray(e)?[]:{},r=Object.keys(e).sort((function(e,t){return e>t?1:-1})),n=r.length;n--;){var o=r[n];a[o]=I(e[o])}return String(e.constructor)+JSON.stringify(a,r)}return t+String(e)}function j(e){e=I(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const A=/^\/|\/$/g;function O(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${j(a)}`}}const E=O((({baseURL:e="",url:t="",method:a="get",params:r,data:n})=>(e&&(e=e.replace(A,"")),t&&(t=t.replace(A,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:n})));function T(e,t={}){var a,r,n,o,i,s;const d=e;if(d.defaults.cache)throw new Error("setupCache() should be called only once");if(d.storage=t.storage||S(),!b(d.storage))throw new Error("Use buildStorage() function");return d.waiting=t.waiting||{},d.generateKey=t.generateKey||E,d.headerInterpreter=t.headerInterpreter||c,d.requestInterceptor=t.requestInterceptor||g(d),d.responseInterceptor=t.responseInterceptor||v(d),d.debug=t.debug,d.defaults.cache={update:t.update||{},ttl:null!==(a=t.ttl)&&void 0!==a?a:3e5,methods:t.methods||["get","head"],cachePredicate:t.cachePredicate||{statusCheck:e=>[200,203,300,301,302,404,405,410,414,501].includes(e)},etag:null===(r=t.etag)||void 0===r||r,modifiedSince:null!==(n=t.modifiedSince)&&void 0!==n?n:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,cacheTakeover:null===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:!1,hydrate:void 0},d.requestInterceptor.apply(),d.responseInterceptor.apply(),d}function R(e,t="axios-cache-"){return C({find:a=>{const r=e.getItem(t+a);return r?JSON.parse(r):void 0},remove:a=>{e.removeItem(t+a)},set:(a,r)=>{const n=()=>e.setItem(t+a,JSON.stringify(r));try{return n()}catch(r){const o=Object.entries(e).filter((e=>e[0].startsWith(t))).map((e=>[e[0],JSON.parse(e[1])]));for(const t of o)"cached"===t[1].state&&x(t[1])&&!w(t[1])&&e.removeItem(t[0]);try{return n()}catch(t){const a=o.sort(((e,t)=>(e[1].createdAt||0)-(t[1].createdAt||0)));for(const t of a){e.removeItem(t[0]);try{return n()}catch(e){}}}e.removeItem(t+a)}}})}return console.error("You are using a development build. Make sure to use the correct build in production\nhttps://axios-cache-interceptor.js.org/guide/getting-started\n\n"),t})())); | ||
//# sourceMappingURL=index.bundle.js.map |
@@ -1,1 +0,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 a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},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 s},buildKeyGenerator:function(){return M},buildMemoryStorage:function(){return E},buildStorage:function(){return A},buildWebStorage:function(){return L},canStale:function(){return C},createCacheResponse:function(){return g},createValidateStatus:function(){return h},defaultHeaderInterpreter:function(){return i},defaultKeyGenerator:function(){return P},defaultRequestInterceptor:function(){return m},defaultResponseInterceptor:function(){return w},isExpired:function(){return I},isMethodIn:function(){return v},isStorage:function(){return x},setupCache:function(){return k},testCachePredicate:function(){return b},updateCache:function(){return y},updateStaleRequest:function(){return p}});var r=Symbol("cache-parser");function a(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function n(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;function c(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var c=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var a in r){var n,o=r[a].split("=",2);t[o[0]]=null==(n=o[1])||n}return t}(e),s=c["max-age"],i=c["max-stale"],u=c["min-fresh"],d=c["s-maxage"],l=c["stale-if-error"],f=c["stale-while-revalidate"];return n(c.immutable)&&(t.immutable=!0),a(s)&&(t.maxAge=o(s)),a(i)&&(t.maxStale=o(i)),a(u)&&(t.minFresh=o(u)),n(c["must-revalidate"])&&(t.mustRevalidate=!0),n(c["must-understand"])&&(t.mustUnderstand=!0),n(c["no-cache"])&&(t.noCache=!0),n(c["no-store"])&&(t.noStore=!0),n(c["no-transform"])&&(t.noTransform=!0),n(c["only-if-cached"])&&(t.onlyIfCached=!0),n(c.private)&&(t.private=!0),n(c["proxy-revalidate"])&&(t.proxyRevalidate=!0),n(c.public)&&(t.public=!0),a(d)&&(t.sMaxAge=o(d)),a(l)&&(t.staleIfError=o(l)),a(f)&&(t.staleWhileRevalidate=o(f)),t}var s=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",Pragma:"pragma",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),i=function(e){if(!e)return"not enough headers";var t=e[s.CacheControl];if(t){var r=c(String(t)),a=r.noCache,n=r.noStore,o=r.maxAge,i=r.immutable,u=r.staleWhileRevalidate;if(a||n)return"dont cache";if(i)return{cache:31536e6};if(void 0!==o){var d=e[s.Age];return{cache:d?1e3*(o-Number(d)):1e3*o,stale:void 0!==u?1e3*u:0}}}var l=e[s.Expires];if(l){var f=Date.parse(String(l))-Date.now();return f>=0?{cache:f}:"dont cache"}return"not enough headers"};var u=function(){return u=Object.assign||function(e){for(var t,r=1,a=arguments.length;r<a;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},u.apply(this,arguments)};function d(e,t,r,a){return new(r||(r=Promise))((function(n,o){function c(e){try{i(a.next(e))}catch(e){o(e)}}function s(e){try{i(a.throw(e))}catch(e){o(e)}}function i(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(c,s)}i((a=a.apply(e,t||[])).next())}))}function l(e,t){var r,a,n,o,c={label:0,sent:function(){if(1&n[0])throw n[1];return n[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(s){return function(i){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(c=0)),c;)try{if(r=1,a&&(n=2&s[0]?a.return:s[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,s[1])).done)return n;switch(a=0,n&&(s=[2&s[0],n.value]),s[0]){case 0:case 1:n=s;break;case 4:return c.label++,{value:s[1],done:!1};case 5:c.label++,a=s[1],s=[0];continue;case 7:s=c.ops.pop(),c.trys.pop();continue;default:if(!(n=c.trys,(n=n.length>0&&n[n.length-1])||6!==s[0]&&2!==s[0])){c=0;continue}if(3===s[0]&&(!n||s[1]>n[0]&&s[1]<n[3])){c.label=s[1];break}if(6===s[0]&&c.label<n[1]){c.label=n[1],n=s;break}if(n&&c.label<n[2]){c.label=n[2],c.ops.push(s);break}n[2]&&c.ops.pop(),c.trys.pop();continue}s=t.call(e,c)}catch(e){s=[6,e],a=0}finally{r=n=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,i])}}}Object.create;Object.create;var f=Symbol();function h(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function v(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function p(e,t){var r;t.headers||(t.headers={});var a=t.cache,n=a.etag,o=a.modifiedSince;if(n){var c=!0===n?null===(r=e.data)||void 0===r?void 0:r.headers[s.ETag]:n;c&&(t.headers[s.IfNoneMatch]=c)}o&&(t.headers[s.IfModifiedSince]=!0===o?e.data.headers[s.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function g(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=u(u({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function m(e){var t=this,r=function(r){return d(t,void 0,void 0,(function(){var t,a,n,o,c,i,d,g,m,b,y,w,x,S,C,I,A,E,T,O;return l(this,(function(l){switch(l.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=u(u({},e.defaults.cache),r.cache),r.cache.cacheTakeover&&(null!==(i=(C=r.headers)[I=s.CacheControl])&&void 0!==i||(C[I]="no-cache"),null!==(d=(A=r.headers)[E=s.Pragma])&&void 0!==d||(A[E]="no-cache"),null!==(g=(T=r.headers)[O=s.Expires])&&void 0!==g||(T[O]="0")),v(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return a=l.sent(),n=r.cache.override,"empty"===a.state||"stale"===a.state||n?!e.waiting[t]||n?[3,3]:[4,e.storage.get(t,r)]:[3,7];case 2:if("empty"!==(a=l.sent()).state)return[3,7];l.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,a){e=r,t=a}));return r.resolve=e,r.reject=t,r[f]=1,r}(),e.waiting[t].catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:n?a.data?"stale":"empty":a.state,data:a.data,createdAt:n&&!a.createdAt?Date.now():a.createdAt},r)];case 4:return l.sent(),"stale"===a.state&&p(a,r),r.validateStatus=h(r.validateStatus),"stale"===a.state||a.data?[4,null===(b=(m=r.cache).hydrate)||void 0===b?void 0:b.call(m,a)]:[3,6];case 5:l.sent(),l.label=6;case 6:return[2,r];case 7:return"loading"!==a.state?[3,18]:(c=e.waiting[t])?[3,11]:[4,e.storage.remove(t,r)];case 8:return l.sent(),a.data?[4,null===(w=(y=r.cache).hydrate)||void 0===w?void 0:w.call(y,a)]:[3,10];case 9:l.sent(),l.label=10;case 10:return[2,r];case 11:0,l.label=12;case 12:return l.trys.push([12,14,,17]),[4,c];case 13:return o=l.sent(),[3,17];case 14:return l.sent(),a.data?[4,null===(S=(x=r.cache).hydrate)||void 0===S?void 0:S.call(x,a)]:[3,16];case 15:l.sent(),l.label=16;case 16:return[2,r];case 17:return[3,19];case 18:o=a.data,l.label=19;case 19:return r.adapter=function(){return Promise.resolve({config:r,data:o.data,headers:o.headers,status:o.status,statusText:o.statusText,cached:!0,id:t})},[2,r]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function b(e,t){var r;return d(this,void 0,void 0,(function(){var a,n,o,c,s,i,u,d,f,h;return l(this,(function(l){switch(l.label){case 0:return"function"==typeof t?[2,t(e)]:(a=t.statusCheck,n=t.responseMatch,o=t.containsHeaders,(s=a)?[4,a(e.status)]:[3,2]);case 1:s=!l.sent(),l.label=2;case 2:return(c=s)?[3,5]:(i=n)?[4,n(e)]:[3,4];case 3:i=!l.sent(),l.label=4;case 4:c=i,l.label=5;case 5:if(c)return[2,!1];if(!o)return[3,9];u=0,d=Object.entries(o),l.label=6;case 6:return u<d.length?(f=d[u],h=f[0],[4,(0,f[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!l.sent())return[2,!1];l.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function y(e,t,r){return d(this,void 0,void 0,(function(){var a,n,o,c,s,i,u;return l(this,(function(d){switch(d.label){case 0:if("function"==typeof r)return[2,r(t)];a=0,n=Object.entries(r),d.label=1;case 1:return a<n.length?(o=n[a],c=o[0],"delete"!==(s=o[1])?[3,3]:[4,e.remove(c,t.config)]):[3,10];case 2:return d.sent(),[3,9];case 3:return[4,e.get(c,t.config)];case 4:return"loading"===(i=d.sent()).state?[3,9]:[4,s(i,t)];case 5:return"delete"!==(u=d.sent())?[3,7]:[4,e.remove(c,t.config)];case 6:return d.sent(),[3,9];case 7:return"ignore"===u?[3,9]:[4,e.set(c,u,t.config)];case 8:d.sent(),d.label=9;case 9:return a++,[3,1];case 10:return[2]}}))}))}function w(e){var t=this,r=function(r,a){return d(t,void 0,void 0,(function(){var t;return l(this,(function(n){switch(n.label){case 0:return[4,e.storage.remove(r,a)];case 1:return n.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},a=function(a){return d(t,void 0,void 0,(function(){var t,n,o,c,i,d,f,h,p,m,w,x,S,C,I,A,E;return l(this,(function(l){switch(l.label){case 0:return a.config,t=a.id=null!==(I=(E=a.config).id)&&void 0!==I?I:E.id=e.generateKey(a.config),null!==(A=a.cached)&&void 0!==A||(a.cached=!1),a.cached?[2,a]:(n=a.config,(o=n.cache)?o.update?[4,y(e.storage,a,o.update)]:[3,2]:[2,u(u({},a),{cached:!1})]);case 1:l.sent(),l.label=2;case 2:return v(n.method,o.methods)?[4,e.storage.get(t,n)]:[2,a];case 3:return"loading"!==(c=l.sent()).state?[2,a]:(i=!c.data)?[4,b(a,o.cachePredicate)]:[3,5];case 4:i=!l.sent(),l.label=5;case 5:return i?[4,r(t,n)]:[3,7];case 6:return l.sent(),[2,a];case 7:for(d=0,f=Object.keys(a.headers);d<f.length;d++)(h=f[d]).startsWith("x-axios-cache")&&delete a.headers[h];return o.etag&&!0!==o.etag&&(a.headers[s.XAxiosCacheEtag]=o.etag),o.modifiedSince&&(a.headers[s.XAxiosCacheLastModified]=!0===o.modifiedSince?"use-cache-timestamp":o.modifiedSince.toUTCString()),p=o.ttl||-1,o.interpretHeader?"dont cache"!==(w=e.headerInterpreter(a.headers))?[3,9]:[4,r(t,n)]:[3,10];case 8:return l.sent(),[2,a];case 9:"not enough headers"!==w&&("number"==typeof w?p=w:(p=w.cache,m=w.stale)),l.label=10;case 10:return x=g(a,c.data),"function"!=typeof p?[3,12]:[4,p(a)];case 11:p=l.sent(),l.label=12;case 12:return o.staleIfError&&(a.headers[s.XAxiosCacheStaleIfError]=String(p)),S={state:"cached",ttl:p,staleTtl:m,createdAt:Date.now(),data:x},(C=e.waiting[t])&&(C.resolve(S.data),delete e.waiting[t]),[4,e.storage.set(t,S,n)];case 13:return l.sent(),[2,a]}}))}))},n=function(a){return d(t,void 0,void 0,(function(){var t,n,o,i,u,d,f,h,p,g;return l(this,(function(l){switch(l.label){case 0:if(t=a.config,n=t.id,o=t.cache,i=a.response,!o||!n)throw a;if(!v(t.method,o.methods))throw a;return[4,e.storage.get(n,t)];case 1:return"loading"===(u=l.sent()).state&&"stale"===u.previous?[3,3]:[4,r(n,t)];case 2:throw l.sent(),a;case 3:return o.staleIfError?(d=String(null==i?void 0:i.headers[s.CacheControl]),f=d&&c(d).staleIfError,"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(i,u,a)]):[3,8];case 4:return p=l.sent(),[3,6];case 5:p=!0===o.staleIfError&&f?1e3*f:o.staleIfError,l.label=6;case 6:return!0===(h=p)||"number"==typeof h&&u.createdAt+h>Date.now()?(null===(g=e.waiting[n])||void 0===g||g.resolve(u.data),delete e.waiting[n],[4,e.storage.set(n,{state:"stale",createdAt:Date.now(),data:u.data},t)]):[3,8];case 7:return l.sent(),[2,{cached:!0,config:t,id:n,data:u.data.data,headers:u.data.headers,status:u.data.status,statusText:u.data.statusText}];case 8:throw a}}))}))};return{onFulfilled:a,onRejected:n,apply:function(){return e.interceptors.response.use(a,n)}}}var x=function(e){return!!e&&!!e["is-storage"]};function S(e){var t=e.data.headers;return s.ETag in t||s.LastModified in t||s.XAxiosCacheEtag in t||s.XAxiosCacheLastModified in t}function C(e){return!String(e.data.headers[s.CacheControl]).includes("must-revalidate")&&(!!S(e)||"cached"===e.state&&void 0!==e.staleTtl&&e.createdAt+e.ttl+e.staleTtl<=Date.now())}function I(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function A(e){var t,r=this,a=e.set,n=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=a,t.remove=o,t.get=function(e,t){return d(r,void 0,void 0,(function(){var r;return l(this,(function(c){switch(c.label){case 0:return[4,n(e,t)];case 1:return(r=c.sent())?"empty"===r.state||"loading"===r.state?[2,r]:"cached"!==r.state?[3,5]:I(r)?C(r)?[3,3]:[4,o(e,t)]:[2,r]:[2,{state:"empty"}];case 2:return c.sent(),[2,{state:"empty"}];case 3:return r={state:"stale",createdAt:r.createdAt,data:r.data,ttl:void 0!==r.staleTtl?r.staleTtl+r.ttl:void 0},[4,a(e,r,t)];case 4:c.sent(),c.label=5;case 5:return I(r)?S(r)?[2,r]:[4,o(e,t)]:[2,r];case 6:return c.sent(),[2,{state:"empty"}]}}))}))},t}function E(e,t){void 0===e&&(e=!1),void 0===t&&(t=!1);var r=A({set:function(e,t){r.data[e]=t},remove:function(e){delete r.data[e]},find:function(t){var a=r.data[t];return e&&void 0!==a?"function"==typeof structuredClone?structuredClone(a):JSON.parse(JSON.stringify(a)):a}});return r.data=Object.create(null),t&&(r.cleaner=setInterval((function(){for(var e,t,a=Object.keys(r.data),n=-1;++n<a.length;)t=a[n],"empty"!==(e=r.data[t]).state?"cached"===e.state&&I(e)&&!C(e)&&r.remove(t):r.remove(t)}),t)),r}function T(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},a=Object.keys(e).sort((function(e,t){return e>t?1:-1})),n=a.length;n--;){var o=a[n];r[o]=T(e[o])}return String(e.constructor)+JSON.stringify(r,a)}return t+String(e)}function O(e){e=T(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var j=/^\/|\/$/g;function M(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:O(r))}}var P=M((function(e){var t=e.baseURL,r=void 0===t?"":t,a=e.url,n=void 0===a?"":a,o=e.method,c=void 0===o?"get":o,s=e.params,i=e.data;return r&&(r=r.replace(j,"")),n&&(n=n.replace(j,"")),c&&(c=c.toLowerCase()),{url:r+(r&&n?"/":"")+n,params:s,method:c,data:i}}));function k(e,t){var r,a,n,o,c,s;void 0===t&&(t={});var u=e;if(u.defaults.cache)throw new Error("setupCache() should be called only once");if(u.storage=t.storage||E(),!x(u.storage))throw new Error("Use buildStorage() function");return u.waiting=t.waiting||{},u.generateKey=t.generateKey||P,u.headerInterpreter=t.headerInterpreter||i,u.requestInterceptor=t.requestInterceptor||m(u),u.responseInterceptor=t.responseInterceptor||w(u),u.debug=t.debug,u.defaults.cache={update:t.update||{},ttl:null!==(r=t.ttl)&&void 0!==r?r:3e5,methods:t.methods||["get","head"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return[200,203,300,301,302,404,405,410,414,501].includes(e)}},etag:null===(a=t.etag)||void 0===a||a,modifiedSince:null!==(n=t.modifiedSince)&&void 0!==n?n:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,cacheTakeover:null===(c=t.cacheTakeover)||void 0===c||c,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:!1,hydrate:void 0},u.requestInterceptor.apply(),u.responseInterceptor.apply(),u}function L(e,t){return void 0===t&&(t="axios-cache-"),A({find:function(r){var a=e.getItem(t+r);return a?JSON.parse(a):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,a){var n=function(){return e.setItem(t+r,JSON.stringify(a))};try{return n()}catch(a){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),c=0,s=o;c<s.length;c++){var i=s[c];"cached"===i[1].state&&I(i[1])&&!C(i[1])&&e.removeItem(i[0])}try{return n()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,l=u;d<l.length;d++){var f=l[d];e.removeItem(f[0]);try{return n()}catch(e){}}}e.removeItem(t+r)}}})}return t}()})); | ||
!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 a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},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 M},buildMemoryStorage:function(){return O},buildStorage:function(){return A},buildWebStorage:function(){return L},canStale:function(){return C},createCacheResponse:function(){return g},createValidateStatus:function(){return h},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return P},defaultRequestInterceptor:function(){return m},defaultResponseInterceptor:function(){return w},isExpired:function(){return I},isMethodIn:function(){return v},isStorage:function(){return x},setupCache:function(){return k},testCachePredicate:function(){return b},updateCache:function(){return y},updateStaleRequest:function(){return p}});var r=Symbol("cache-parser");function a(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function n(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;function c(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var c=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var a in r){var n,o=r[a].split("=",2);t[o[0]]=null==(n=o[1])||n}return t}(e),i=c["max-age"],s=c["max-stale"],u=c["min-fresh"],d=c["s-maxage"],l=c["stale-if-error"],f=c["stale-while-revalidate"];return n(c.immutable)&&(t.immutable=!0),a(i)&&(t.maxAge=o(i)),a(s)&&(t.maxStale=o(s)),a(u)&&(t.minFresh=o(u)),n(c["must-revalidate"])&&(t.mustRevalidate=!0),n(c["must-understand"])&&(t.mustUnderstand=!0),n(c["no-cache"])&&(t.noCache=!0),n(c["no-store"])&&(t.noStore=!0),n(c["no-transform"])&&(t.noTransform=!0),n(c["only-if-cached"])&&(t.onlyIfCached=!0),n(c.private)&&(t.private=!0),n(c["proxy-revalidate"])&&(t.proxyRevalidate=!0),n(c.public)&&(t.public=!0),a(d)&&(t.sMaxAge=o(d)),a(l)&&(t.staleIfError=o(l)),a(f)&&(t.staleWhileRevalidate=o(f)),t}var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",Pragma:"pragma",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 r=c(String(t)),a=r.noCache,n=r.noStore,o=r.maxAge,s=r.immutable,u=r.staleWhileRevalidate;if(a||n)return"dont cache";if(s)return{cache:31536e6};if(void 0!==o){var d=e[i.Age];return{cache:d?1e3*(o-Number(d)):1e3*o,stale:void 0!==u?1e3*u:0}}}var l=e[i.Expires];if(l){var f=Date.parse(String(l))-Date.now();return f>=0?{cache:f}:"dont cache"}return"not enough headers"};var u=function(){return u=Object.assign||function(e){for(var t,r=1,a=arguments.length;r<a;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},u.apply(this,arguments)};function d(e,t,r,a){return new(r||(r=Promise))((function(n,o){function c(e){try{s(a.next(e))}catch(e){o(e)}}function i(e){try{s(a.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(c,i)}s((a=a.apply(e,t||[])).next())}))}function l(e,t){var r,a,n,o,c={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function i(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,i[0]&&(c=0)),c;)try{if(r=1,a&&(n=2&i[0]?a.return:i[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,i[1])).done)return n;switch(a=0,n&&(i=[2&i[0],n.value]),i[0]){case 0:case 1:n=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,a=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(n=c.trys,(n=n.length>0&&n[n.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!n||i[1]>n[0]&&i[1]<n[3])){c.label=i[1];break}if(6===i[0]&&c.label<n[1]){c.label=n[1],n=i;break}if(n&&c.label<n[2]){c.label=n[2],c.ops.push(i);break}n[2]&&c.ops.pop(),c.trys.pop();continue}i=t.call(e,c)}catch(e){i=[6,e],a=0}finally{r=n=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}Object.create;Object.create;var f=Symbol();function h(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function v(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function p(e,t){var r;t.headers||(t.headers={});var a=t.cache,n=a.etag,o=a.modifiedSince;if(n){var c=!0===n?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:n;c&&(t.headers[i.IfNoneMatch]=c)}o&&(t.headers[i.IfModifiedSince]=!0===o?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function g(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=u(u({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function m(e){var t=this,r=function(r){return d(t,void 0,void 0,(function(){var t,a,n,o,c,s,d,g,m,b,y,w,x,S,C,I,A,O,j,E;return l(this,(function(l){switch(l.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=u(u({},e.defaults.cache),r.cache),r.cache.cacheTakeover&&(null!==(s=(C=r.headers)[I=i.CacheControl])&&void 0!==s||(C[I]="no-cache"),null!==(d=(A=r.headers)[O=i.Pragma])&&void 0!==d||(A[O]="no-cache"),null!==(g=(j=r.headers)[E=i.Expires])&&void 0!==g||(j[E]="0")),v(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return a=l.sent(),n=r.cache.override,"empty"===a.state||"stale"===a.state||n?!e.waiting[t]||n?[3,3]:[4,e.storage.get(t,r)]:[3,7];case 2:if("empty"!==(a=l.sent()).state)return[3,7];l.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,a){e=r,t=a}));return r.resolve=e,r.reject=t,r[f]=1,r}(),e.waiting[t].catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:n?a.data?"stale":"empty":a.state,data:a.data,createdAt:n&&!a.createdAt?Date.now():a.createdAt},r)];case 4:return l.sent(),"stale"===a.state&&p(a,r),r.validateStatus=h(r.validateStatus),"stale"===a.state||a.data?[4,null===(b=(m=r.cache).hydrate)||void 0===b?void 0:b.call(m,a)]:[3,6];case 5:l.sent(),l.label=6;case 6:return[2,r];case 7:return"loading"!==a.state?[3,18]:(c=e.waiting[t])?[3,11]:[4,e.storage.remove(t,r)];case 8:return l.sent(),a.data?[4,null===(w=(y=r.cache).hydrate)||void 0===w?void 0:w.call(y,a)]:[3,10];case 9:l.sent(),l.label=10;case 10:return[2,r];case 11:0,l.label=12;case 12:return l.trys.push([12,14,,17]),[4,c];case 13:return o=l.sent(),[3,17];case 14:return l.sent(),a.data?[4,null===(S=(x=r.cache).hydrate)||void 0===S?void 0:S.call(x,a)]:[3,16];case 15:l.sent(),l.label=16;case 16:return[2,r];case 17:return[3,19];case 18:o=a.data,l.label=19;case 19:return r.adapter=function(){return Promise.resolve({config:r,data:o.data,headers:o.headers,status:o.status,statusText:o.statusText,cached:!0,id:t})},[2,r]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function b(e,t){var r;return d(this,void 0,void 0,(function(){var a,n,o,c,i,s,u,d,f,h;return l(this,(function(l){switch(l.label){case 0:return"function"==typeof t?[2,t(e)]:(a=t.statusCheck,n=t.responseMatch,o=t.containsHeaders,(i=a)?[4,a(e.status)]:[3,2]);case 1:i=!l.sent(),l.label=2;case 2:return(c=i)?[3,5]:(s=n)?[4,n(e)]:[3,4];case 3:s=!l.sent(),l.label=4;case 4:c=s,l.label=5;case 5:if(c)return[2,!1];if(!o)return[3,9];u=0,d=Object.entries(o),l.label=6;case 6:return u<d.length?(f=d[u],h=f[0],[4,(0,f[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!l.sent())return[2,!1];l.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function y(e,t,r){return d(this,void 0,void 0,(function(){var a,n,o,c,i,s,u;return l(this,(function(d){switch(d.label){case 0:if("function"==typeof r)return[2,r(t)];a=0,n=Object.entries(r),d.label=1;case 1:return a<n.length?(o=n[a],c=o[0],"delete"!==(i=o[1])?[3,3]:[4,e.remove(c,t.config)]):[3,10];case 2:return d.sent(),[3,9];case 3:return[4,e.get(c,t.config)];case 4:return"loading"===(s=d.sent()).state?[3,9]:[4,i(s,t)];case 5:return"delete"!==(u=d.sent())?[3,7]:[4,e.remove(c,t.config)];case 6:return d.sent(),[3,9];case 7:return"ignore"===u?[3,9]:[4,e.set(c,u,t.config)];case 8:d.sent(),d.label=9;case 9:return a++,[3,1];case 10:return[2]}}))}))}function w(e){var t=this,r=function(r,a){return d(t,void 0,void 0,(function(){var t;return l(this,(function(n){switch(n.label){case 0:return[4,e.storage.remove(r,a)];case 1:return n.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},a=function(a){return d(t,void 0,void 0,(function(){var t,n,o,c,s,d,f,h,p,m,w,x,S,C,I,A,O;return l(this,(function(l){switch(l.label){case 0:return a.config,t=a.id=null!==(I=(O=a.config).id)&&void 0!==I?I:O.id=e.generateKey(a.config),null!==(A=a.cached)&&void 0!==A||(a.cached=!1),a.cached?[2,a]:(n=a.config,(o=n.cache)?o.update?[4,y(e.storage,a,o.update)]:[3,2]:[2,u(u({},a),{cached:!1})]);case 1:l.sent(),l.label=2;case 2:return v(n.method,o.methods)?[4,e.storage.get(t,n)]:[2,a];case 3:return"loading"!==(c=l.sent()).state?[2,a]:(s=!c.data)?[4,b(a,o.cachePredicate)]:[3,5];case 4:s=!l.sent(),l.label=5;case 5:return s?[4,r(t,n)]:[3,7];case 6:return l.sent(),[2,a];case 7:for(d=0,f=Object.keys(a.headers);d<f.length;d++)(h=f[d]).startsWith("x-axios-cache")&&delete a.headers[h];return o.etag&&!0!==o.etag&&(a.headers[i.XAxiosCacheEtag]=o.etag),o.modifiedSince&&(a.headers[i.XAxiosCacheLastModified]=!0===o.modifiedSince?"use-cache-timestamp":o.modifiedSince.toUTCString()),p=o.ttl||-1,o.interpretHeader?"dont cache"!==(w=e.headerInterpreter(a.headers))?[3,9]:[4,r(t,n)]:[3,10];case 8:return l.sent(),[2,a];case 9:"not enough headers"!==w&&("number"==typeof w?p=w:(p=w.cache,m=w.stale)),l.label=10;case 10:return x=g(a,c.data),"function"!=typeof p?[3,12]:[4,p(a)];case 11:p=l.sent(),l.label=12;case 12:return o.staleIfError&&(a.headers[i.XAxiosCacheStaleIfError]=String(p)),S={state:"cached",ttl:p,staleTtl:m,createdAt:Date.now(),data:x},(C=e.waiting[t])&&(C.resolve(S.data),delete e.waiting[t]),[4,e.storage.set(t,S,n)];case 13:return l.sent(),[2,a]}}))}))},n=function(a){return d(t,void 0,void 0,(function(){var t,n,o,s,u,d,f,h,p,g;return l(this,(function(l){switch(l.label){case 0:if(t=a.config,n=t.id,o=t.cache,s=a.response,!o||!n)throw a;if(!v(t.method,o.methods))throw a;return[4,e.storage.get(n,t)];case 1:return"loading"===(u=l.sent()).state&&"stale"===u.previous?[3,3]:[4,r(n,t)];case 2:throw l.sent(),a;case 3:return o.staleIfError?(d=String(null==s?void 0:s.headers[i.CacheControl]),f=d&&c(d).staleIfError,"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(s,u,a)]):[3,8];case 4:return p=l.sent(),[3,6];case 5:p=!0===o.staleIfError&&f?1e3*f:o.staleIfError,l.label=6;case 6:return!0===(h=p)||"number"==typeof h&&u.createdAt+h>Date.now()?(null===(g=e.waiting[n])||void 0===g||g.resolve(u.data),delete e.waiting[n],[4,e.storage.set(n,{state:"stale",createdAt:Date.now(),data:u.data},t)]):[3,8];case 7:return l.sent(),[2,{cached:!0,config:t,id:n,data:u.data.data,headers:u.data.headers,status:u.data.status,statusText:u.data.statusText}];case 8:throw a}}))}))};return{onFulfilled:a,onRejected:n,apply:function(){return e.interceptors.response.use(a,n)}}}var x=function(e){return!!e&&!!e["is-storage"]};function S(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheLastModified in t}function C(e){return!String(e.data.headers[i.CacheControl]).includes("must-revalidate")&&(!!S(e)||"cached"===e.state&&void 0!==e.staleTtl&&e.createdAt+e.ttl+e.staleTtl<=Date.now())}function I(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function A(e){var t,r=this,a=e.set,n=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=a,t.remove=o,t.get=function(e,t){return d(r,void 0,void 0,(function(){var r;return l(this,(function(c){switch(c.label){case 0:return[4,n(e,t)];case 1:return(r=c.sent())?"empty"===r.state||"loading"===r.state?[2,r]:"cached"!==r.state?[3,5]:I(r)?C(r)?[3,3]:[4,o(e,t)]:[2,r]:[2,{state:"empty"}];case 2:return c.sent(),[2,{state:"empty"}];case 3:return r={state:"stale",createdAt:r.createdAt,data:r.data,ttl:void 0!==r.staleTtl?r.staleTtl+r.ttl:void 0},[4,a(e,r,t)];case 4:c.sent(),c.label=5;case 5:return I(r)?S(r)?[2,r]:[4,o(e,t)]:[2,r];case 6:return c.sent(),[2,{state:"empty"}]}}))}))},t}function O(e,t,r){void 0===e&&(e=!1),void 0===t&&(t=!1),void 0===r&&(r=!1);var a=A({set:function(e,t){if(r){var n=Object.keys(a.data);if(n.length>=r)for(a.cleanup(),n=Object.keys(a.data);n.length>=r;)delete a.data[n.shift()]}a.data[e]=t},remove:function(e){delete a.data[e]},find:function(t){var r=a.data[t];return e&&void 0!==r?"function"==typeof structuredClone?structuredClone(r):JSON.parse(JSON.stringify(r)):r}});return a.data=Object.create(null),a.cleanup=function(){for(var e,t,r=Object.keys(a.data),n=-1;++n<r.length;)t=r[n],"empty"!==(e=a.data[t]).state?"cached"===e.state&&I(e)&&!C(e)&&a.remove(t):a.remove(t)},t&&(a.cleaner=setInterval(a.cleanup,t)),a}function j(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},a=Object.keys(e).sort((function(e,t){return e>t?1:-1})),n=a.length;n--;){var o=a[n];r[o]=j(e[o])}return String(e.constructor)+JSON.stringify(r,a)}return t+String(e)}function E(e){e=j(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var T=/^\/|\/$/g;function M(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 P=M((function(e){var t=e.baseURL,r=void 0===t?"":t,a=e.url,n=void 0===a?"":a,o=e.method,c=void 0===o?"get":o,i=e.params,s=e.data;return r&&(r=r.replace(T,"")),n&&(n=n.replace(T,"")),c&&(c=c.toLowerCase()),{url:r+(r&&n?"/":"")+n,params:i,method:c,data:s}}));function k(e,t){var r,a,n,o,c,i;void 0===t&&(t={});var u=e;if(u.defaults.cache)throw new Error("setupCache() should be called only once");if(u.storage=t.storage||O(),!x(u.storage))throw new Error("Use buildStorage() function");return u.waiting=t.waiting||{},u.generateKey=t.generateKey||P,u.headerInterpreter=t.headerInterpreter||s,u.requestInterceptor=t.requestInterceptor||m(u),u.responseInterceptor=t.responseInterceptor||w(u),u.debug=t.debug,u.defaults.cache={update:t.update||{},ttl:null!==(r=t.ttl)&&void 0!==r?r:3e5,methods:t.methods||["get","head"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return[200,203,300,301,302,404,405,410,414,501].includes(e)}},etag:null===(a=t.etag)||void 0===a||a,modifiedSince:null!==(n=t.modifiedSince)&&void 0!==n?n:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,cacheTakeover:null===(c=t.cacheTakeover)||void 0===c||c,staleIfError:null===(i=t.staleIfError)||void 0===i||i,override:!1,hydrate:void 0},u.requestInterceptor.apply(),u.responseInterceptor.apply(),u}function L(e,t){return void 0===t&&(t="axios-cache-"),A({find:function(r){var a=e.getItem(t+r);return a?JSON.parse(a):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,a){var n=function(){return e.setItem(t+r,JSON.stringify(a))};try{return n()}catch(a){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),c=0,i=o;c<i.length;c++){var s=i[c];"cached"===s[1].state&&I(s[1])&&!C(s[1])&&e.removeItem(s[0])}try{return n()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,l=u;d<l.length;d++){var f=l[d];e.removeItem(f[0]);try{return n()}catch(e){}}}e.removeItem(t+r)}}})}return t}()})); |
@@ -29,4 +29,8 @@ import type { AxiosStorage, StorageValue } from './types'; | ||
* setInterval job of cleaning old entries. If false, the job will not be created. Disabled is default | ||
* | ||
* @param {number | false} maxEntries The maximum number of entries to keep in the storage. Its hard to | ||
* determine the size of the entries, so a smart FIFO order is used to determine eviction. If false, | ||
* no check will be done and you may grow up memory usage. Disabled is default | ||
*/ | ||
export declare function buildMemoryStorage(cloneData?: boolean, cleanupInterval?: number | false): MemoryStorage; | ||
export declare function buildMemoryStorage(cloneData?: boolean, cleanupInterval?: number | false, maxEntries?: number | false): MemoryStorage; | ||
export type MemoryStorage = AxiosStorage & { | ||
@@ -36,3 +40,5 @@ data: Record<string, StorageValue>; | ||
cleaner: ReturnType<typeof setInterval>; | ||
/** Tries to remove any invalid entry from the memory */ | ||
cleanup: () => void; | ||
}; | ||
//# sourceMappingURL=memory.d.ts.map |
{ | ||
"name": "axios-cache-interceptor", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Cache interceptor for axios", | ||
@@ -17,3 +17,3 @@ "keywords": [ | ||
"repository": "https://github.com/arthurfiorette/axios-cache-interceptor.git", | ||
"funding": "https://github.com/ArthurFiorette/axios-cache-interceptor?sponsor=1", | ||
"funding": "https://github.com/arthurfiorette/axios-cache-interceptor?sponsor=1", | ||
"license": "MIT", | ||
@@ -91,3 +91,2 @@ "author": "Arthur Fiorette <npm@arthur.place>", | ||
"format": "prettier --write .", | ||
"preinstall": "npx only-allow pnpm", | ||
"lint": "eslint . --ext .ts", | ||
@@ -94,0 +93,0 @@ "test": "jest --verbose --coverage", |
@@ -38,9 +38,34 @@ import { buildStorage, canStale, isExpired } from './build'; | ||
* setInterval job of cleaning old entries. If false, the job will not be created. Disabled is default | ||
* | ||
* @param {number | false} maxEntries The maximum number of entries to keep in the storage. Its hard to | ||
* determine the size of the entries, so a smart FIFO order is used to determine eviction. If false, | ||
* no check will be done and you may grow up memory usage. Disabled is default | ||
*/ | ||
export function buildMemoryStorage( | ||
cloneData = false, | ||
cleanupInterval: number | false = false | ||
cleanupInterval: number | false = false, | ||
maxEntries: number | false = false | ||
) { | ||
const storage = buildStorage({ | ||
set: (key, value) => { | ||
if (maxEntries) { | ||
let keys = Object.keys(storage.data); | ||
// Tries to cleanup first | ||
if (keys.length >= maxEntries) { | ||
storage.cleanup(); | ||
// Recalculates the keys | ||
keys = Object.keys(storage.data); | ||
// Keeps deleting until there's space | ||
while (keys.length >= maxEntries) { | ||
// There's always at least one key here, otherwise it would not be | ||
// in the loop. | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
delete storage.data[keys.shift()!]; | ||
} | ||
} | ||
} | ||
storage.data[key] = value; | ||
@@ -74,31 +99,33 @@ }, | ||
// "faster" loop is more important than code readability. | ||
if (cleanupInterval) { | ||
storage.cleaner = setInterval(() => { | ||
const keys = Object.keys(storage.data); | ||
storage.cleanup = () => { | ||
const keys = Object.keys(storage.data); | ||
let i = -1, | ||
value: StorageValue, | ||
key: string; | ||
let i = -1, | ||
value: StorageValue, | ||
key: string; | ||
// Looping forward, as older entries are more likely to be expired | ||
// than newer ones. | ||
while (++i < keys.length) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(key = keys[i]!), (value = storage.data[key]!); | ||
// Looping forward, as older entries are more likely to be expired | ||
// than newer ones. | ||
while (++i < keys.length) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(key = keys[i]!), (value = storage.data[key]!); | ||
if (value.state === 'empty') { | ||
// this storage returns void. | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
storage.remove(key); | ||
continue; | ||
} | ||
if (value.state === 'empty') { | ||
// this storage returns void. | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
storage.remove(key); | ||
continue; | ||
} | ||
// If the value is expired and can't be stale, remove it | ||
if (value.state === 'cached' && isExpired(value) && !canStale(value)) { | ||
// this storage returns void. | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
storage.remove(key); | ||
} | ||
// If the value is expired and can't be stale, remove it | ||
if (value.state === 'cached' && isExpired(value) && !canStale(value)) { | ||
// this storage returns void. | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
storage.remove(key); | ||
} | ||
}, cleanupInterval); | ||
} | ||
}; | ||
if (cleanupInterval) { | ||
storage.cleaner = setInterval(storage.cleanup, cleanupInterval); | ||
} | ||
@@ -113,2 +140,4 @@ | ||
cleaner: ReturnType<typeof setInterval>; | ||
/** Tries to remove any invalid entry from the memory */ | ||
cleanup: () => void; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
458327
3256
0