axios-cache-interceptor
Advanced tools
Comparing version 1.3.3 to 1.4.0
@@ -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:()=>O,buildMemoryStorage:()=>C,buildStorage:()=>x,buildWebStorage:()=>R,canStale:()=>w,createCacheResponse:()=>h,createValidateStatus:()=>u,defaultHeaderInterpreter:()=>d,defaultKeyGenerator:()=>T,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>v,isExpired:()=>S,isMethodIn:()=>l,isStorage:()=>b,setupCache:()=>E,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"],d=i["max-stale"],c=i["min-fresh"],u=i["s-maxage"],l=i["stale-if-error"],f=i["stale-while-revalidate"];return o(i.immutable)&&(t.immutable=!0),r(s)&&(t.maxAge=n(s)),r(d)&&(t.maxStale=n(d)),r(c)&&(t.minFresh=n(c)),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(u)&&(t.sMaxAge=n(u)),r(l)&&(t.staleIfError=n(l)),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"}),d=e=>{if(!e)return"not enough headers";const t=e[s.CacheControl];if(t){const{noCache:a,noStore:r,maxAge:o,maxStale:n,immutable:d,staleWhileRevalidate:c}=i(String(t));if(a||r)return"dont cache";if(d)return{cache:31536e6};if(void 0!==o){const t=e[s.Age];return{cache:t?1e3*(o-Number(t)):1e3*o,stale:void 0!==n?1e3*n:void 0!==c?1e3*c:void 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 c=Symbol();function u(e){return e?t=>e(t)||304===t:e=>e>=200&&e<300||304===e}function l(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 a=>{var r,o,n,i,d,h,g,p,m,v,b,y,w,S,x;if(a.id=e.generateKey(a),!1===a.cache)return e.debug({msg:"Ignoring cache because config.cache === false",data:a}),a;if(a.cache=Object.assign(Object.assign({},e.defaults.cache),a.cache),a.cache.cacheTakeover&&(null!==(r=(v=a.headers)[b=s.CacheControl])&&void 0!==r||(v[b]="no-cache"),null!==(o=(y=a.headers)[w=s.Pragma])&&void 0!==o||(y[w]="no-cache"),null!==(n=(S=a.headers)[x=s.Expires])&&void 0!==n||(S[x]="0")),!l(a.method,a.cache.methods))return e.debug({msg:`Ignored because method (${a.method}) is not in cache.methods (${a.cache.methods})`}),a;let C=await e.storage.get(a.id,a);const I=a.cache.override;e:if("empty"===C.state||"stale"===C.state||I){if(e.waiting[a.id]&&!I&&(C=await e.storage.get(a.id,a),"empty"!==C.state)){e.debug({id:a.id,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[a.id]=function(){var e,t,a=new Promise((function(a,r){e=a,t=r}));return a.resolve=e,a.reject=t,a[c]=1,a}(),e.waiting[a.id].catch((()=>{})),await e.storage.set(a.id,{state:"loading",previous:I?C.data?"stale":"empty":C.state,data:C.data,createdAt:I&&!C.createdAt?Date.now():C.createdAt},a),"stale"===C.state&&(f(C,a),e.debug({id:a.id,msg:"Updated stale request"})),a.validateStatus=u(a.validateStatus),e.debug({id:a.id,msg:"Sending request, waiting for response",data:{overrideCache:I,state:C.state}}),("stale"===C.state||C.data)&&await(null===(d=(i=a.cache).hydrate)||void 0===d?void 0:d.call(i,C)),a}let j;if("loading"===C.state){const r=e.waiting[a.id];if(!r)return C.data&&await(null===(g=(h=a.cache).hydrate)||void 0===g?void 0:g.call(h,C)),a;e.debug({id:a.id,msg:"Detected concurrent request, waiting for it to finish"});try{j=await r}catch(r){return e.debug({id:a.id,msg:"Deferred rejected, requesting again",data:r}),C.data&&await(null===(m=(p=a.cache).hydrate)||void 0===m?void 0:m.call(p,C)),t(a)}}else j=C.data;return a.adapter=function(){return Promise.resolve({config:a,data:j.data,headers:j.headers,status:j.status,statusText:j.statusText,cached:!0,id:a.id})},e.debug({id:a.id,msg:"Returning cached response"}),a};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;if(!(null==a?void 0:a.config))throw e.debug({msg:"Response interceptor received an unknown response.",data:a}),a;a.id=a.config.id,null!==(r=a.cached)&&void 0!==r||(a.cached=!1);const o=a.config,n=o.cache;if(a.cached)return e.debug({id:a.id,msg:"Returned cached response"}),a;if(!n)return e.debug({id:a.id,msg:"Response with config.cache falsy",data:a}),a.cached=!1,a;if(n.update&&await m(e.storage,a,n.update),!l(o.method,n.methods))return e.debug({id:a.id,msg:`Ignored because method (${o.method}) is not in cache.methods (${n.methods})`,data:{config:o,cacheConfig:n}}),a;const i=await e.storage.get(a.id,o);if("loading"!==i.state)return e.debug({id:a.id,msg:"Response not cached and storage isn't loading",data:{cache:i,response:a}}),a;if(!i.data&&!await p(a,n.cachePredicate))return await t(a.id,o),e.debug({id:a.id,msg:"Cache predicate rejected this response"}),a;for(const e of Object.keys(a.headers))e.startsWith("x-axios-cache")&&delete a.headers[e];n.etag&&!0!==n.etag&&(a.headers[s.XAxiosCacheEtag]=n.etag),n.modifiedSince&&(a.headers[s.XAxiosCacheLastModified]=!0===n.modifiedSince?"use-cache-timestamp":n.modifiedSince.toUTCString());let d,c=n.ttl||-1;if(n.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(a.id,o),e.debug({id:a.id,msg:"Cache header interpreted as 'dont cache'",data:{cache:i,response:a,expirationTime:r}}),a;"not enough headers"!==r&&("number"==typeof r?c=r:(c=r.cache,d=r.stale))}const u=h(a,i.data);"function"==typeof c&&(c=await c(a)),n.staleIfError&&(a.headers[s.XAxiosCacheStaleIfError]=String(c)),e.debug({id:a.id,msg:"Useful response configuration found",data:{cacheConfig:n,cacheResponse:u}});const f={state:"cached",ttl:c,staleTtl:d,createdAt:Date.now(),data:u},g=e.waiting[a.id];return g&&(g.resolve(f.data),delete e.waiting[a.id],e.debug({id:a.id,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(a.id,f,o),e.debug({id:a.id,msg:"Response cached",data:{cache:f,response:a}}),a},r=async a=>{var r;if(!a.isAxiosError||!a.config)throw e.debug({msg:"FATAL: Received an non axios error in the rejected response interceptor, ignoring.",data:a}),a;const o=a.config,n=o.id,d=o.cache,c=a.response;if(!d||!n)throw e.debug({msg:"Web request returned an error but cache handling is not enabled",data:{error:a}}),a;if(!l(o.method,d.methods))throw e.debug({id:n,msg:`Ignored because method (${o.method}) is not in cache.methods (${d.methods})`,data:{config:o,cacheConfig:d}}),await t(n,o),a;const u=await e.storage.get(n,o);if("loading"!==u.state||"stale"!==u.previous)throw e.debug({id:n,msg:"Caught an error in the request interceptor",data:{cache:u,error:a,config:o}}),await t(n,o),a;if(d.staleIfError){const t=String(null==c?void 0:c.headers[s.CacheControl]),l=t&&i(t).staleIfError,f="function"==typeof d.staleIfError?await d.staleIfError(c,u,a):!0===d.staleIfError&&l?1e3*l:d.staleIfError;if(e.debug({id:n,msg:"Found cache if stale config for rejected response",data:{error:a,config:o,staleIfError:f}}),!0===f||"number"==typeof f&&u.createdAt+f>Date.now())return null===(r=e.waiting[n])||void 0===r||r.resolve(u.data),delete e.waiting[n],await e.storage.set(n,{state:"stale",createdAt:Date.now(),data:u.data},o),e.debug({id:n,msg:"staleIfError resolved this response with cached data",data:{error:a,config:o,cache:u}}),{cached:!0,config:o,id:n,data:u.data.data,headers:u.data.headers,status:u.data.status,statusText:u.data.statusText}}throw e.debug({id:n,msg:"Received an unknown error that could not be handled",data:{error:a,config:o}}),await t(n,o),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&&Math.abs(Date.now()-(e.createdAt+e.ttl))<=e.staleTtl)}function S(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function x({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(!S(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 S(n)?y(n)?n:(await a(r,o),{state:"empty"}):n}}}function C(e=!1,t=!1,a=!1){const r=x({set:(t,o)=>{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[t]="double"===e?"function"==typeof structuredClone?structuredClone(o):JSON.parse(JSON.stringify(o)):o},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,o=-1;for(;++o<e.length;)a=e[o],t=r.data[a],"empty"!==t.state?"cached"===t.state&&S(t)&&!w(t)&&r.remove(a):r.remove(a)},t&&(r.cleaner=setInterval(r.cleanup,t)),r}function I(e,t){return e>t?1:-1}function j(e,t){var a=5381;if("object"==typeof e&&null!==e&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){t||(t=new WeakSet);for(var r=Object.keys(e).sort(I),o=0;o<r.length;o++){var n=r[o],i=e[n];if(a=33*a^j(n,t),"object"==typeof i&&null!==i&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){if(t.has(i))continue;t.add(i)}a=33*a^j(i,t)}return 33*a^j(e.constructor,t)}var s=typeof e;try{e instanceof Date?s+=e.getTime():s+=String(e)}catch(t){s+=String(Object.assign({},e))}for(var d=0;d<s.length;d++)a=33*a^s.charCodeAt(d);return a}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 T=O((({baseURL:e,url:t,method:a,params:r,data:o})=>(e=void 0!==e?e.replace(A,""):"",t=void 0!==t?t.replace(A,""):"",{url:e+(e&&t?"/":"")+t,params:r,method:a=void 0!==a?a.toLowerCase():"get",data:o})));function E(e,t={}){var a,r,o,n,i,s,c,u;const l=e;if(l.defaults.cache)throw new Error("setupCache() should be called only once");if(l.storage=t.storage||C(),!b(l.storage))throw new Error("Use buildStorage() function");return l.waiting=t.waiting||{},l.generateKey=t.generateKey||T,l.headerInterpreter=t.headerInterpreter||d,l.requestInterceptor=t.requestInterceptor||g(l),l.responseInterceptor=t.responseInterceptor||v(l),l.debug=t.debug||function(){},l.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:null!==(c=t.override)&&void 0!==c&&c,hydrate:null!==(u=t.hydrate)&&void 0!==u?u:void 0},l.requestInterceptor.apply(),l.responseInterceptor.apply(),l}function R(e,t="axios-cache-"){return x({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&&S(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:()=>C,buildStorage:()=>x,buildWebStorage:()=>R,canStale:()=>w,createCacheResponse:()=>h,createValidateStatus:()=>u,defaultHeaderInterpreter:()=>d,defaultKeyGenerator:()=>E,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>v,isExpired:()=>S,isMethodIn:()=>l,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 i=Number;function n(e){var t=Object.defineProperty({},a,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var n=function(e){var t={},a=e.toLowerCase().replace(/\s+/g,"").split(",");for(var r in a){var o,i=a[r].split("=",2);t[i[0]]=null==(o=i[1])||o}return t}(e),s=n["max-age"],d=n["max-stale"],c=n["min-fresh"],u=n["s-maxage"],l=n["stale-if-error"],f=n["stale-while-revalidate"];return o(n.immutable)&&(t.immutable=!0),r(s)&&(t.maxAge=i(s)),r(d)&&(t.maxStale=i(d)),r(c)&&(t.minFresh=i(c)),o(n["must-revalidate"])&&(t.mustRevalidate=!0),o(n["must-understand"])&&(t.mustUnderstand=!0),o(n["no-cache"])&&(t.noCache=!0),o(n["no-store"])&&(t.noStore=!0),o(n["no-transform"])&&(t.noTransform=!0),o(n["only-if-cached"])&&(t.onlyIfCached=!0),o(n.private)&&(t.private=!0),o(n["proxy-revalidate"])&&(t.proxyRevalidate=!0),o(n.public)&&(t.public=!0),r(u)&&(t.sMaxAge=i(u)),r(l)&&(t.staleIfError=i(l)),r(f)&&(t.staleWhileRevalidate=i(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"}),d=e=>{if(!e)return"not enough headers";const t=e[s.CacheControl];if(t){const{noCache:a,noStore:r,maxAge:o,maxStale:i,immutable:d,staleWhileRevalidate:c}=n(String(t));if(a||r)return"dont cache";if(d)return{cache:31536e6};if(void 0!==o){const t=e[s.Age];return{cache:t?1e3*(o-Number(t)):1e3*o,stale:void 0!==i?1e3*i:void 0!==c?1e3*c:void 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 c=Symbol();function u(e){return e?t=>e(t)||304===t:e=>e>=200&&e<300||304===e}function l(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 a=>{var r,o,i,n,d,h,g,p,m,v,b,y,w,S,x;if(a.id=e.generateKey(a),!1===a.cache)return e.debug({id:a.id,msg:"Ignoring cache because config.cache === false",data:a}),a;if(a.cache=Object.assign(Object.assign({},e.defaults.cache),a.cache),"object"==typeof a.cache.cachePredicate&&a.cache.cachePredicate.ignoreUrls&&a.url)for(const t of a.cache.cachePredicate.ignoreUrls)if(t instanceof RegExp?(t.lastIndex=0,t.test(a.url)):a.url.includes(t))return e.debug({id:a.id,msg:`Ignored because url (${a.url}) matches ignoreUrls (${a.cache.cachePredicate.ignoreUrls})`,data:{url:a.url,cachePredicate:a.cache.cachePredicate}}),a;if(a.cache.cacheTakeover&&(null!==(r=(v=a.headers)[b=s.CacheControl])&&void 0!==r||(v[b]="no-cache"),null!==(o=(y=a.headers)[w=s.Pragma])&&void 0!==o||(y[w]="no-cache"),null!==(i=(S=a.headers)[x=s.Expires])&&void 0!==i||(S[x]="0")),!l(a.method,a.cache.methods))return e.debug({id:a.id,msg:`Ignored because method (${a.method}) is not in cache.methods (${a.cache.methods})`}),a;let C=await e.storage.get(a.id,a);const I=a.cache.override;e:if("empty"===C.state||"stale"===C.state||I){if(e.waiting[a.id]&&!I&&(C=await e.storage.get(a.id,a),"empty"!==C.state)){e.debug({id:a.id,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[a.id]=function(){var e,t,a=new Promise((function(a,r){e=a,t=r}));return a.resolve=e,a.reject=t,a[c]=1,a}(),e.waiting[a.id].catch((()=>{})),await e.storage.set(a.id,{state:"loading",previous:I?C.data?"stale":"empty":C.state,data:C.data,createdAt:I&&!C.createdAt?Date.now():C.createdAt},a),"stale"===C.state&&(f(C,a),e.debug({id:a.id,msg:"Updated stale request"})),a.validateStatus=u(a.validateStatus),e.debug({id:a.id,msg:"Sending request, waiting for response",data:{overrideCache:I,state:C.state}}),("stale"===C.state||C.data)&&await(null===(d=(n=a.cache).hydrate)||void 0===d?void 0:d.call(n,C)),a}let j;if("loading"===C.state){const r=e.waiting[a.id];if(!r)return C.data&&await(null===(g=(h=a.cache).hydrate)||void 0===g?void 0:g.call(h,C)),a;e.debug({id:a.id,msg:"Detected concurrent request, waiting for it to finish"});try{j=await r}catch(r){return e.debug({id:a.id,msg:"Deferred rejected, requesting again",data:r}),C.data&&await(null===(m=(p=a.cache).hydrate)||void 0===m?void 0:m.call(p,C)),t(a)}}else j=C.data;return a.adapter=function(){return Promise.resolve({config:a,data:j.data,headers:j.headers,status:j.status,statusText:j.statusText,cached:!0,id:a.id})},e.debug({id:a.id,msg:"Returning cached response"}),a};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:i}=t;if(r&&!await r(e.status)||o&&!await o(e))return!1;if(i)for(const[t,r]of Object.entries(i))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 i=await o(a,t);"delete"!==i?"ignore"!==i&&await e.set(r,i,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;if(!(null==a?void 0:a.config))throw e.debug({msg:"Response interceptor received an unknown response.",data:a}),a;a.id=a.config.id,null!==(r=a.cached)&&void 0!==r||(a.cached=!1);const o=a.config,i=o.cache;if(a.cached)return e.debug({id:a.id,msg:"Returned cached response"}),a;if(!i)return e.debug({id:a.id,msg:"Response with config.cache falsy",data:a}),a.cached=!1,a;if(i.update&&await m(e.storage,a,i.update),!l(o.method,i.methods))return e.debug({id:a.id,msg:`Ignored because method (${o.method}) is not in cache.methods (${i.methods})`,data:{config:o,cacheConfig:i}}),a;const n=await e.storage.get(a.id,o);if("loading"!==n.state)return e.debug({id:a.id,msg:"Response not cached and storage isn't loading",data:{cache:n,response:a}}),a;if(!n.data&&!await p(a,i.cachePredicate))return await t(a.id,o),e.debug({id:a.id,msg:"Cache predicate rejected this response"}),a;for(const e of Object.keys(a.headers))e.startsWith("x-axios-cache")&&delete a.headers[e];i.etag&&!0!==i.etag&&(a.headers[s.XAxiosCacheEtag]=i.etag),i.modifiedSince&&(a.headers[s.XAxiosCacheLastModified]=!0===i.modifiedSince?"use-cache-timestamp":i.modifiedSince.toUTCString());let d,c=i.ttl||-1;if(i.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(a.id,o),e.debug({id:a.id,msg:"Cache header interpreted as 'dont cache'",data:{cache:n,response:a,expirationTime:r}}),a;"not enough headers"!==r&&("number"==typeof r?c=r:(c=r.cache,d=r.stale))}const u=h(a,n.data);"function"==typeof c&&(c=await c(a)),i.staleIfError&&(a.headers[s.XAxiosCacheStaleIfError]=String(c)),e.debug({id:a.id,msg:"Useful response configuration found",data:{cacheConfig:i,cacheResponse:u}});const f={state:"cached",ttl:c,staleTtl:d,createdAt:Date.now(),data:u},g=e.waiting[a.id];return g&&(g.resolve(f.data),delete e.waiting[a.id],e.debug({id:a.id,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(a.id,f,o),e.debug({id:a.id,msg:"Response cached",data:{cache:f,response:a}}),a},r=async a=>{var r;if(!a.isAxiosError||!a.config)throw e.debug({msg:"FATAL: Received an non axios error in the rejected response interceptor, ignoring.",data:a}),a;const o=a.config,i=o.id,d=o.cache,c=a.response;if(!d||!i)throw e.debug({msg:"Web request returned an error but cache handling is not enabled",data:{error:a}}),a;if(!l(o.method,d.methods))throw e.debug({id:i,msg:`Ignored because method (${o.method}) is not in cache.methods (${d.methods})`,data:{config:o,cacheConfig:d}}),await t(i,o),a;const u=await e.storage.get(i,o);if("loading"!==u.state||"stale"!==u.previous)throw e.debug({id:i,msg:"Caught an error in the request interceptor",data:{cache:u,error:a,config:o}}),await t(i,o),a;if(d.staleIfError){const t=String(null==c?void 0:c.headers[s.CacheControl]),l=t&&n(t).staleIfError,f="function"==typeof d.staleIfError?await d.staleIfError(c,u,a):!0===d.staleIfError&&l?1e3*l:d.staleIfError;if(e.debug({id:i,msg:"Found cache if stale config for rejected response",data:{error:a,config:o,staleIfError:f}}),!0===f||"number"==typeof f&&u.createdAt+f>Date.now())return null===(r=e.waiting[i])||void 0===r||r.resolve(u.data),delete e.waiting[i],await e.storage.set(i,{state:"stale",createdAt:Date.now(),data:u.data},o),e.debug({id:i,msg:"staleIfError resolved this response with cached data",data:{error:a,config:o,cache:u}}),{cached:!0,config:o,id:i,data:u.data.data,headers:u.data.headers,status:u.data.status,statusText:u.data.statusText}}throw e.debug({id:i,msg:"Received an unknown error that could not be handled",data:{error:a,config:o}}),await t(i,o),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&&Math.abs(Date.now()-(e.createdAt+e.ttl))<=e.staleTtl)}function S(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function x({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,o)=>{let i=await t(r,o);if(!i)return{state:"empty"};if("empty"===i.state||"loading"===i.state)return i;if("cached"===i.state){if(!S(i))return i;if(!w(i))return await a(r,o),{state:"empty"};i={state:"stale",createdAt:i.createdAt,data:i.data,ttl:void 0!==i.staleTtl?i.staleTtl+i.ttl:void 0},await e(r,i,o)}return S(i)?y(i)?i:(await a(r,o),{state:"empty"}):i}}}function C(e=!1,t=!1,a=!1){const r=x({set:(t,o)=>{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[t]="double"===e?"function"==typeof structuredClone?structuredClone(o):JSON.parse(JSON.stringify(o)):o},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,o=-1;for(;++o<e.length;)a=e[o],t=r.data[a],"empty"!==t.state?"cached"===t.state&&S(t)&&!w(t)&&r.remove(a):r.remove(a)},t&&(r.cleaner=setInterval(r.cleanup,t)),r}function I(e,t){return e>t?1:-1}function j(e,t){var a=5381;if("object"==typeof e&&null!==e&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){t||(t=new WeakSet);for(var r=Object.keys(e).sort(I),o=0;o<r.length;o++){var i=r[o],n=e[i];if(a=33*a^j(i,t),"object"==typeof n&&null!==n&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){if(t.has(n))continue;t.add(n)}a=33*a^j(n,t)}return 33*a^j(e.constructor,t)}var s=typeof e;try{e instanceof Date?s+=e.getTime():s+=String(e)}catch(t){s+=String(Object.assign({},e))}for(var d=0;d<s.length;d++)a=33*a^s.charCodeAt(d);return a}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,params:r,data:o})=>(e=void 0!==e?e.replace(A,""):"",t=void 0!==t?t.replace(A,""):"",{url:e+(e&&t?"/":"")+t,params:r,method:a=void 0!==a?a.toLowerCase():"get",data:o})));function T(e,t={}){var a,r,o,i,n,s,c,u;const l=e;if(l.defaults.cache)throw new Error("setupCache() should be called only once");if(l.storage=t.storage||C(),!b(l.storage))throw new Error("Use buildStorage() function");return l.waiting=t.waiting||{},l.generateKey=t.generateKey||E,l.headerInterpreter=t.headerInterpreter||d,l.requestInterceptor=t.requestInterceptor||g(l),l.responseInterceptor=t.responseInterceptor||v(l),l.debug=t.debug||function(){},l.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===(i=t.interpretHeader)||void 0===i||i,cacheTakeover:null===(n=t.cacheTakeover)||void 0===n||n,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:null!==(c=t.override)&&void 0!==c&&c,hydrate:null!==(u=t.hydrate)&&void 0!==u?u:void 0},l.requestInterceptor.apply(),l.responseInterceptor.apply(),l}function R(e,t="axios-cache-"){return x({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 i=Object.entries(e).filter((e=>e[0].startsWith(t))).map((e=>[e[0],JSON.parse(e[1])]));for(const t of i)"cached"===t[1].state&&S(t[1])&&!w(t[1])&&e.removeItem(t[0]);try{return o()}catch(t){const a=i.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})())); | ||
//# 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 c},buildKeyGenerator:function(){return M},buildMemoryStorage:function(){return O},buildStorage:function(){return A},buildWebStorage:function(){return N},canStale:function(){return C},createCacheResponse:function(){return g},createValidateStatus:function(){return h},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return k},defaultRequestInterceptor:function(){return m},defaultResponseInterceptor:function(){return S},isExpired:function(){return I},isMethodIn:function(){return v},isStorage:function(){return w},setupCache:function(){return P},testCachePredicate:function(){return y},updateCache:function(){return b},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 i(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 a in r){var n,o=r[a].split("=",2);t[o[0]]=null==(n=o[1])||n}return t}(e),c=i["max-age"],s=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],l=i["stale-if-error"],f=i["stale-while-revalidate"];return n(i.immutable)&&(t.immutable=!0),a(c)&&(t.maxAge=o(c)),a(s)&&(t.maxStale=o(s)),a(u)&&(t.minFresh=o(u)),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),a(d)&&(t.sMaxAge=o(d)),a(l)&&(t.staleIfError=o(l)),a(f)&&(t.staleWhileRevalidate=o(f)),t}var c=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[c.CacheControl];if(t){var r=i(String(t)),a=r.noCache,n=r.noStore,o=r.maxAge,s=r.maxStale,u=r.immutable,d=r.staleWhileRevalidate;if(a||n)return"dont cache";if(u)return{cache:31536e6};if(void 0!==o){var l=e[c.Age];return{cache:l?1e3*(o-Number(l)):1e3*o,stale:void 0!==s?1e3*s:void 0!==d?1e3*d:void 0}}}var f=e[c.Expires];if(f){var h=Date.parse(String(f))-Date.now();return h>=0?{cache:h}:"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 i(e){try{s(a.next(e))}catch(e){o(e)}}function c(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(i,c)}s((a=a.apply(e,t||[])).next())}))}function l(e,t){var r,a,n,o,i={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(i=0)),i;)try{if(r=1,a&&(n=2&c[0]?a.return:c[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,c[1])).done)return n;switch(a=0,n&&(c=[2&c[0],n.value]),c[0]){case 0:case 1:n=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,a=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(n=i.trys,(n=n.length>0&&n[n.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!n||c[1]>n[0]&&c[1]<n[3])){i.label=c[1];break}if(6===c[0]&&i.label<n[1]){i.label=n[1],n=c;break}if(n&&i.label<n[2]){i.label=n[2],i.ops.push(c);break}n[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],a=0}finally{r=n=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}Object.create;Object.create;"function"==typeof SuppressedError&&SuppressedError;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 i=!0===n?null===(r=e.data)||void 0===r?void 0:r.headers[c.ETag]:n;i&&(t.headers[c.IfNoneMatch]=i)}o&&(t.headers[c.IfModifiedSince]=!0===o?e.data.headers[c.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(a){return d(t,void 0,void 0,(function(){var t,n,o,i,s,d,g,m,y,b,S,w,x,C,I,A,O,j,E;return l(this,(function(l){switch(l.label){case 0:return a.id=e.generateKey(a),!1===a.cache?[2,a]:(a.cache=u(u({},e.defaults.cache),a.cache),a.cache.cacheTakeover&&(null!==(s=(C=a.headers)[I=c.CacheControl])&&void 0!==s||(C[I]="no-cache"),null!==(d=(A=a.headers)[O=c.Pragma])&&void 0!==d||(A[O]="no-cache"),null!==(g=(j=a.headers)[E=c.Expires])&&void 0!==g||(j[E]="0")),v(a.method,a.cache.methods)?[4,e.storage.get(a.id,a)]:[2,a]);case 1:return t=l.sent(),n=a.cache.override,"empty"===t.state||"stale"===t.state||n?!e.waiting[a.id]||n?[3,3]:[4,e.storage.get(a.id,a)]:[3,7];case 2:if("empty"!==(t=l.sent()).state)return[3,7];l.label=3;case 3:return e.waiting[a.id]=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[a.id].catch((function(){})),[4,e.storage.set(a.id,{state:"loading",previous:n?t.data?"stale":"empty":t.state,data:t.data,createdAt:n&&!t.createdAt?Date.now():t.createdAt},a)];case 4:return l.sent(),"stale"===t.state&&p(t,a),a.validateStatus=h(a.validateStatus),"stale"===t.state||t.data?[4,null===(y=(m=a.cache).hydrate)||void 0===y?void 0:y.call(m,t)]:[3,6];case 5:l.sent(),l.label=6;case 6:return[2,a];case 7:return"loading"!==t.state?[3,17]:(i=e.waiting[a.id])?[3,10]:t.data?[4,null===(S=(b=a.cache).hydrate)||void 0===S?void 0:S.call(b,t)]:[3,9];case 8:l.sent(),l.label=9;case 9:return[2,a];case 10:0,l.label=11;case 11:return l.trys.push([11,13,,16]),[4,i];case 12:return o=l.sent(),[3,16];case 13:return l.sent(),t.data?[4,null===(x=(w=a.cache).hydrate)||void 0===x?void 0:x.call(w,t)]:[3,15];case 14:l.sent(),l.label=15;case 15:return[2,r(a)];case 16:return[3,18];case 17:o=t.data,l.label=18;case 18:return a.adapter=function(){return Promise.resolve({config:a,data:o.data,headers:o.headers,status:o.status,statusText:o.statusText,cached:!0,id:a.id})},[2,a]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function y(e,t){var r;return d(this,void 0,void 0,(function(){var a,n,o,i,c,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,(c=a)?[4,a(e.status)]:[3,2]);case 1:c=!l.sent(),l.label=2;case 2:return(i=c)?[3,5]:(s=n)?[4,n(e)]:[3,4];case 3:s=!l.sent(),l.label=4;case 4:i=s,l.label=5;case 5:if(i)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 b(e,t,r){return d(this,void 0,void 0,(function(){var a,n,o,i,c,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],i=o[0],"delete"!==(c=o[1])?[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"===(s=d.sent()).state?[3,9]:[4,c(s,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 a++,[3,1];case 10:return[2]}}))}))}function S(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,i,s,u,d,f,h,p,m,S,w,x;return l(this,(function(l){switch(l.label){case 0:if(!(null==a?void 0:a.config))throw a;return a.id=a.config.id,null!==(x=a.cached)&&void 0!==x||(a.cached=!1),t=a.config,n=t.cache,a.cached?[2,a]:n?n.update?[4,b(e.storage,a,n.update)]:[3,2]:(a.cached=!1,[2,a]);case 1:l.sent(),l.label=2;case 2:return v(t.method,n.methods)?[4,e.storage.get(a.id,t)]:[2,a];case 3:return"loading"!==(o=l.sent()).state?[2,a]:(i=!o.data)?[4,y(a,n.cachePredicate)]:[3,5];case 4:i=!l.sent(),l.label=5;case 5:return i?[4,r(a.id,t)]:[3,7];case 6:return l.sent(),[2,a];case 7:for(s=0,u=Object.keys(a.headers);s<u.length;s++)(d=u[s]).startsWith("x-axios-cache")&&delete a.headers[d];return n.etag&&!0!==n.etag&&(a.headers[c.XAxiosCacheEtag]=n.etag),n.modifiedSince&&(a.headers[c.XAxiosCacheLastModified]=!0===n.modifiedSince?"use-cache-timestamp":n.modifiedSince.toUTCString()),f=n.ttl||-1,n.interpretHeader?"dont cache"!==(p=e.headerInterpreter(a.headers))?[3,9]:[4,r(a.id,t)]:[3,10];case 8:return l.sent(),[2,a];case 9:"not enough headers"!==p&&("number"==typeof p?f=p:(f=p.cache,h=p.stale)),l.label=10;case 10:return m=g(a,o.data),"function"!=typeof f?[3,12]:[4,f(a)];case 11:f=l.sent(),l.label=12;case 12:return n.staleIfError&&(a.headers[c.XAxiosCacheStaleIfError]=String(f)),S={state:"cached",ttl:f,staleTtl:h,createdAt:Date.now(),data:m},(w=e.waiting[a.id])&&(w.resolve(S.data),delete e.waiting[a.id]),[4,e.storage.set(a.id,S,t)];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(!a.isAxiosError||!a.config)throw a;if(t=a.config,n=t.id,o=t.cache,s=a.response,!o||!n)throw a;return v(t.method,o.methods)?[3,2]:[4,r(n,t)];case 1:throw l.sent(),a;case 2:return[4,e.storage.get(n,t)];case 3:return"loading"===(u=l.sent()).state&&"stale"===u.previous?[3,5]:[4,r(n,t)];case 4:throw l.sent(),a;case 5:return o.staleIfError?(d=String(null==s?void 0:s.headers[c.CacheControl]),f=d&&i(d).staleIfError,"function"!=typeof o.staleIfError?[3,7]:[4,o.staleIfError(s,u,a)]):[3,10];case 6:return p=l.sent(),[3,8];case 7:p=!0===o.staleIfError&&f?1e3*f:o.staleIfError,l.label=8;case 8: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,10];case 9: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 10:return[4,r(n,t)];case 11:throw l.sent(),a}}))}))};return{onFulfilled:a,onRejected:n,apply:function(){return e.interceptors.response.use(a,n)}}}var w=function(e){return!!e&&!!e["is-storage"]};function x(e){var t=e.data.headers;return c.ETag in t||c.LastModified in t||c.XAxiosCacheEtag in t||c.XAxiosCacheLastModified in t}function C(e){return!String(e.data.headers[c.CacheControl]).includes("must-revalidate")&&(!!x(e)||"cached"===e.state&&void 0!==e.staleTtl&&Math.abs(Date.now()-(e.createdAt+e.ttl))<=e.staleTtl)}function I(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function A(e){var t=this,r=e.set,a=e.find,n=e.remove;return{"is-storage":1,set:r,remove:n,get:function(e,o){return d(t,void 0,void 0,(function(){var t;return l(this,(function(i){switch(i.label){case 0:return[4,a(e,o)];case 1:return(t=i.sent())?"empty"===t.state||"loading"===t.state?[2,t]:"cached"!==t.state?[3,5]:I(t)?C(t)?[3,3]:[4,n(e,o)]:[2,t]:[2,{state:"empty"}];case 2:return i.sent(),[2,{state:"empty"}];case 3:return t={state:"stale",createdAt:t.createdAt,data:t.data,ttl:void 0!==t.staleTtl?t.staleTtl+t.ttl:void 0},[4,r(e,t,o)];case 4:i.sent(),i.label=5;case 5:return I(t)?x(t)?[2,t]:[4,n(e,o)]:[2,t];case 6:return i.sent(),[2,{state:"empty"}]}}))}))}}}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(t,n){if(r){var o=Object.keys(a.data);if(o.length>=r)for(a.cleanup(),o=Object.keys(a.data);o.length>=r;)delete a.data[o.shift()]}a.data[t]="double"===e?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n},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,t){return e>t?1:-1}function E(e,t){var r=5381;if("object"==typeof e&&null!==e&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){t||(t=new WeakSet);for(var a=Object.keys(e).sort(j),n=0;n<a.length;n++){var o=a[n],i=e[o];if(r=33*r^E(o,t),"object"==typeof i&&null!==i&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){if(t.has(i))continue;t.add(i)}r=33*r^E(i,t)}return 33*r^E(e.constructor,t)}var c=typeof e;try{e instanceof Date?c+=e.getTime():c+=String(e)}catch(t){c+=String(Object.assign({},e))}for(var s=0;s<c.length;s++)r=33*r^c.charCodeAt(s);return r}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 k=M((function(e){var t=e.baseURL,r=e.url,a=e.method,n=e.params,o=e.data;return t=void 0!==t?t.replace(T,""):"",r=void 0!==r?r.replace(T,""):"",{url:t+(t&&r?"/":"")+r,params:n,method:a=void 0!==a?a.toLowerCase():"get",data:o}}));function P(e,t){var r,a,n,o,i,c,u,d;void 0===t&&(t={});var l=e;if(l.defaults.cache)throw new Error("setupCache() should be called only once");if(l.storage=t.storage||O(),!w(l.storage))throw new Error("Use buildStorage() function");return l.waiting=t.waiting||{},l.generateKey=t.generateKey||k,l.headerInterpreter=t.headerInterpreter||s,l.requestInterceptor=t.requestInterceptor||m(l),l.responseInterceptor=t.responseInterceptor||S(l),l.debug=t.debug||function(){},l.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===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(c=t.staleIfError)||void 0===c||c,override:null!==(u=t.override)&&void 0!==u&&u,hydrate:null!==(d=t.hydrate)&&void 0!==d?d:void 0},l.requestInterceptor.apply(),l.responseInterceptor.apply(),l}function N(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])]})),i=0,c=o;i<c.length;i++){var s=c[i];"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}()})); | ||
!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 c},buildKeyGenerator:function(){return P},buildMemoryStorage:function(){return j},buildStorage:function(){return A},buildWebStorage:function(){return N},canStale:function(){return C},createCacheResponse:function(){return g},createValidateStatus:function(){return h},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return M},defaultRequestInterceptor:function(){return m},defaultResponseInterceptor:function(){return S},isExpired:function(){return I},isMethodIn:function(){return v},isStorage:function(){return w},setupCache:function(){return k},testCachePredicate:function(){return y},updateCache:function(){return b},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 i(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 a in r){var n,o=r[a].split("=",2);t[o[0]]=null==(n=o[1])||n}return t}(e),c=i["max-age"],s=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],l=i["stale-if-error"],f=i["stale-while-revalidate"];return n(i.immutable)&&(t.immutable=!0),a(c)&&(t.maxAge=o(c)),a(s)&&(t.maxStale=o(s)),a(u)&&(t.minFresh=o(u)),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),a(d)&&(t.sMaxAge=o(d)),a(l)&&(t.staleIfError=o(l)),a(f)&&(t.staleWhileRevalidate=o(f)),t}var c=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[c.CacheControl];if(t){var r=i(String(t)),a=r.noCache,n=r.noStore,o=r.maxAge,s=r.maxStale,u=r.immutable,d=r.staleWhileRevalidate;if(a||n)return"dont cache";if(u)return{cache:31536e6};if(void 0!==o){var l=e[c.Age];return{cache:l?1e3*(o-Number(l)):1e3*o,stale:void 0!==s?1e3*s:void 0!==d?1e3*d:void 0}}}var f=e[c.Expires];if(f){var h=Date.parse(String(f))-Date.now();return h>=0?{cache:h}:"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 i(e){try{s(a.next(e))}catch(e){o(e)}}function c(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(i,c)}s((a=a.apply(e,t||[])).next())}))}function l(e,t){var r,a,n,o,i={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(i=0)),i;)try{if(r=1,a&&(n=2&c[0]?a.return:c[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,c[1])).done)return n;switch(a=0,n&&(c=[2&c[0],n.value]),c[0]){case 0:case 1:n=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,a=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(n=i.trys,(n=n.length>0&&n[n.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!n||c[1]>n[0]&&c[1]<n[3])){i.label=c[1];break}if(6===c[0]&&i.label<n[1]){i.label=n[1],n=c;break}if(n&&i.label<n[2]){i.label=n[2],i.ops.push(c);break}n[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],a=0}finally{r=n=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}Object.create;Object.create;"function"==typeof SuppressedError&&SuppressedError;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 i=!0===n?null===(r=e.data)||void 0===r?void 0:r.headers[c.ETag]:n;i&&(t.headers[c.IfNoneMatch]=i)}o&&(t.headers[c.IfModifiedSince]=!0===o?e.data.headers[c.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(a){return d(t,void 0,void 0,(function(){var t,n,o,i,s,d,g,m,y,b,S,w,x,C,I,A,j,O,E,T,P,M;return l(this,(function(l){switch(l.label){case 0:if(a.id=e.generateKey(a),!1===a.cache)return[2,a];if(a.cache=u(u({},e.defaults.cache),a.cache),"object"==typeof a.cache.cachePredicate&&a.cache.cachePredicate.ignoreUrls&&a.url)for(t=0,n=a.cache.cachePredicate.ignoreUrls;t<n.length;t++)if((o=n[t])instanceof RegExp?(o.lastIndex=0,o.test(a.url)):a.url.includes(o))return[2,a];return a.cache.cacheTakeover&&(null!==(m=(j=a.headers)[O=c.CacheControl])&&void 0!==m||(j[O]="no-cache"),null!==(y=(E=a.headers)[T=c.Pragma])&&void 0!==y||(E[T]="no-cache"),null!==(b=(P=a.headers)[M=c.Expires])&&void 0!==b||(P[M]="0")),v(a.method,a.cache.methods)?[4,e.storage.get(a.id,a)]:[2,a];case 1:return i=l.sent(),s=a.cache.override,"empty"===i.state||"stale"===i.state||s?!e.waiting[a.id]||s?[3,3]:[4,e.storage.get(a.id,a)]:[3,7];case 2:if("empty"!==(i=l.sent()).state)return[3,7];l.label=3;case 3:return e.waiting[a.id]=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[a.id].catch((function(){})),[4,e.storage.set(a.id,{state:"loading",previous:s?i.data?"stale":"empty":i.state,data:i.data,createdAt:s&&!i.createdAt?Date.now():i.createdAt},a)];case 4:return l.sent(),"stale"===i.state&&p(i,a),a.validateStatus=h(a.validateStatus),"stale"===i.state||i.data?[4,null===(w=(S=a.cache).hydrate)||void 0===w?void 0:w.call(S,i)]:[3,6];case 5:l.sent(),l.label=6;case 6:return[2,a];case 7:return"loading"!==i.state?[3,17]:(g=e.waiting[a.id])?[3,10]:i.data?[4,null===(C=(x=a.cache).hydrate)||void 0===C?void 0:C.call(x,i)]:[3,9];case 8:l.sent(),l.label=9;case 9:return[2,a];case 10:0,l.label=11;case 11:return l.trys.push([11,13,,16]),[4,g];case 12:return d=l.sent(),[3,16];case 13:return l.sent(),i.data?[4,null===(A=(I=a.cache).hydrate)||void 0===A?void 0:A.call(I,i)]:[3,15];case 14:l.sent(),l.label=15;case 15:return[2,r(a)];case 16:return[3,18];case 17:d=i.data,l.label=18;case 18:return a.adapter=function(){return Promise.resolve({config:a,data:d.data,headers:d.headers,status:d.status,statusText:d.statusText,cached:!0,id:a.id})},[2,a]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function y(e,t){var r;return d(this,void 0,void 0,(function(){var a,n,o,i,c,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,(c=a)?[4,a(e.status)]:[3,2]);case 1:c=!l.sent(),l.label=2;case 2:return(i=c)?[3,5]:(s=n)?[4,n(e)]:[3,4];case 3:s=!l.sent(),l.label=4;case 4:i=s,l.label=5;case 5:if(i)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 b(e,t,r){return d(this,void 0,void 0,(function(){var a,n,o,i,c,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],i=o[0],"delete"!==(c=o[1])?[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"===(s=d.sent()).state?[3,9]:[4,c(s,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 a++,[3,1];case 10:return[2]}}))}))}function S(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,i,s,u,d,f,h,p,m,S,w,x;return l(this,(function(l){switch(l.label){case 0:if(!(null==a?void 0:a.config))throw a;return a.id=a.config.id,null!==(x=a.cached)&&void 0!==x||(a.cached=!1),t=a.config,n=t.cache,a.cached?[2,a]:n?n.update?[4,b(e.storage,a,n.update)]:[3,2]:(a.cached=!1,[2,a]);case 1:l.sent(),l.label=2;case 2:return v(t.method,n.methods)?[4,e.storage.get(a.id,t)]:[2,a];case 3:return"loading"!==(o=l.sent()).state?[2,a]:(i=!o.data)?[4,y(a,n.cachePredicate)]:[3,5];case 4:i=!l.sent(),l.label=5;case 5:return i?[4,r(a.id,t)]:[3,7];case 6:return l.sent(),[2,a];case 7:for(s=0,u=Object.keys(a.headers);s<u.length;s++)(d=u[s]).startsWith("x-axios-cache")&&delete a.headers[d];return n.etag&&!0!==n.etag&&(a.headers[c.XAxiosCacheEtag]=n.etag),n.modifiedSince&&(a.headers[c.XAxiosCacheLastModified]=!0===n.modifiedSince?"use-cache-timestamp":n.modifiedSince.toUTCString()),f=n.ttl||-1,n.interpretHeader?"dont cache"!==(p=e.headerInterpreter(a.headers))?[3,9]:[4,r(a.id,t)]:[3,10];case 8:return l.sent(),[2,a];case 9:"not enough headers"!==p&&("number"==typeof p?f=p:(f=p.cache,h=p.stale)),l.label=10;case 10:return m=g(a,o.data),"function"!=typeof f?[3,12]:[4,f(a)];case 11:f=l.sent(),l.label=12;case 12:return n.staleIfError&&(a.headers[c.XAxiosCacheStaleIfError]=String(f)),S={state:"cached",ttl:f,staleTtl:h,createdAt:Date.now(),data:m},(w=e.waiting[a.id])&&(w.resolve(S.data),delete e.waiting[a.id]),[4,e.storage.set(a.id,S,t)];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(!a.isAxiosError||!a.config)throw a;if(t=a.config,n=t.id,o=t.cache,s=a.response,!o||!n)throw a;return v(t.method,o.methods)?[3,2]:[4,r(n,t)];case 1:throw l.sent(),a;case 2:return[4,e.storage.get(n,t)];case 3:return"loading"===(u=l.sent()).state&&"stale"===u.previous?[3,5]:[4,r(n,t)];case 4:throw l.sent(),a;case 5:return o.staleIfError?(d=String(null==s?void 0:s.headers[c.CacheControl]),f=d&&i(d).staleIfError,"function"!=typeof o.staleIfError?[3,7]:[4,o.staleIfError(s,u,a)]):[3,10];case 6:return p=l.sent(),[3,8];case 7:p=!0===o.staleIfError&&f?1e3*f:o.staleIfError,l.label=8;case 8: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,10];case 9: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 10:return[4,r(n,t)];case 11:throw l.sent(),a}}))}))};return{onFulfilled:a,onRejected:n,apply:function(){return e.interceptors.response.use(a,n)}}}var w=function(e){return!!e&&!!e["is-storage"]};function x(e){var t=e.data.headers;return c.ETag in t||c.LastModified in t||c.XAxiosCacheEtag in t||c.XAxiosCacheLastModified in t}function C(e){return!String(e.data.headers[c.CacheControl]).includes("must-revalidate")&&(!!x(e)||"cached"===e.state&&void 0!==e.staleTtl&&Math.abs(Date.now()-(e.createdAt+e.ttl))<=e.staleTtl)}function I(e){return void 0!==e.ttl&&e.createdAt+e.ttl<=Date.now()}function A(e){var t=this,r=e.set,a=e.find,n=e.remove;return{"is-storage":1,set:r,remove:n,get:function(e,o){return d(t,void 0,void 0,(function(){var t;return l(this,(function(i){switch(i.label){case 0:return[4,a(e,o)];case 1:return(t=i.sent())?"empty"===t.state||"loading"===t.state?[2,t]:"cached"!==t.state?[3,5]:I(t)?C(t)?[3,3]:[4,n(e,o)]:[2,t]:[2,{state:"empty"}];case 2:return i.sent(),[2,{state:"empty"}];case 3:return t={state:"stale",createdAt:t.createdAt,data:t.data,ttl:void 0!==t.staleTtl?t.staleTtl+t.ttl:void 0},[4,r(e,t,o)];case 4:i.sent(),i.label=5;case 5:return I(t)?x(t)?[2,t]:[4,n(e,o)]:[2,t];case 6:return i.sent(),[2,{state:"empty"}]}}))}))}}}function j(e,t,r){void 0===e&&(e=!1),void 0===t&&(t=!1),void 0===r&&(r=!1);var a=A({set:function(t,n){if(r){var o=Object.keys(a.data);if(o.length>=r)for(a.cleanup(),o=Object.keys(a.data);o.length>=r;)delete a.data[o.shift()]}a.data[t]="double"===e?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n},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 O(e,t){return e>t?1:-1}function E(e,t){var r=5381;if("object"==typeof e&&null!==e&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){t||(t=new WeakSet);for(var a=Object.keys(e).sort(O),n=0;n<a.length;n++){var o=a[n],i=e[o];if(r=33*r^E(o,t),"object"==typeof i&&null!==i&&(e.toString===Object.prototype.toString||e.toString===Array.prototype.toString)){if(t.has(i))continue;t.add(i)}r=33*r^E(i,t)}return 33*r^E(e.constructor,t)}var c=typeof e;try{e instanceof Date?c+=e.getTime():c+=String(e)}catch(t){c+=String(Object.assign({},e))}for(var s=0;s<c.length;s++)r=33*r^c.charCodeAt(s);return r}var T=/^\/|\/$/g;function P(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 M=P((function(e){var t=e.baseURL,r=e.url,a=e.method,n=e.params,o=e.data;return t=void 0!==t?t.replace(T,""):"",r=void 0!==r?r.replace(T,""):"",{url:t+(t&&r?"/":"")+r,params:n,method:a=void 0!==a?a.toLowerCase():"get",data:o}}));function k(e,t){var r,a,n,o,i,c,u,d;void 0===t&&(t={});var l=e;if(l.defaults.cache)throw new Error("setupCache() should be called only once");if(l.storage=t.storage||j(),!w(l.storage))throw new Error("Use buildStorage() function");return l.waiting=t.waiting||{},l.generateKey=t.generateKey||M,l.headerInterpreter=t.headerInterpreter||s,l.requestInterceptor=t.requestInterceptor||m(l),l.responseInterceptor=t.responseInterceptor||S(l),l.debug=t.debug||function(){},l.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===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(c=t.staleIfError)||void 0===c||c,override:null!==(u=t.override)&&void 0!==u&&u,hydrate:null!==(d=t.hydrate)&&void 0!==d?d:void 0},l.requestInterceptor.apply(),l.responseInterceptor.apply(),l}function N(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])]})),i=0,c=o;i<c.length;i++){var s=c[i];"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}()})); |
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { CachedStorageValue, LoadingStorageValue, StorageValue } from '../storage/types'; | ||
export type CachePredicate<R = unknown, D = unknown> = Exclude<CachePredicateObject<R, D> | CachePredicateObject<R, D>['responseMatch'], undefined>; | ||
export type CachePredicate<R = unknown, D = unknown> = NonNullable<CachePredicateObject<R, D> | CachePredicateObject<R, D>['responseMatch']>; | ||
export interface CachePredicateObject<R = unknown, D = unknown> { | ||
@@ -18,2 +18,9 @@ /** Matches if this function returned true. */ | ||
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>; | ||
/** | ||
* Ignores the request if their url matches any provided urls and/or regexes. | ||
* | ||
* - It checks against the `request.url` property, `baseURL` is not considered. | ||
* - When only `baseURL` is specified, this property is ignored. | ||
*/ | ||
ignoreUrls?: (RegExp | string)[]; | ||
} | ||
@@ -20,0 +27,0 @@ /** |
{ | ||
"name": "axios-cache-interceptor", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "Cache interceptor for axios", | ||
@@ -63,3 +63,3 @@ "keywords": [ | ||
"typescript": "5.3.3", | ||
"vitepress": "1.0.0-rc.31", | ||
"vitepress": "1.0.0-rc.32", | ||
"webpack": "5.89.0", | ||
@@ -66,0 +66,0 @@ "webpack-cli": "5.1.4" |
@@ -15,2 +15,3 @@ import { deferred } from 'fast-defer'; | ||
axios.debug({ | ||
id: config.id, | ||
msg: 'Ignoring cache because config.cache === false', | ||
@@ -27,2 +28,31 @@ data: config | ||
if ( | ||
typeof config.cache.cachePredicate === 'object' && | ||
config.cache.cachePredicate.ignoreUrls && | ||
config.url | ||
) { | ||
for (const url of config.cache.cachePredicate.ignoreUrls) { | ||
if ( | ||
url instanceof RegExp | ||
? // Handles stateful regexes | ||
// biome-ignore lint: reduces the number of checks | ||
((url.lastIndex = 0), url.test(config.url)) | ||
: config.url.includes(url) | ||
) { | ||
if (__ACI_DEV__) { | ||
axios.debug({ | ||
id: config.id, | ||
msg: `Ignored because url (${config.url}) matches ignoreUrls (${config.cache.cachePredicate.ignoreUrls})`, | ||
data: { | ||
url: config.url, | ||
cachePredicate: config.cache.cachePredicate | ||
} | ||
}); | ||
} | ||
return config; | ||
} | ||
} | ||
} | ||
// Applies sufficient headers to prevent other cache systems to work along with this one | ||
@@ -41,2 +71,3 @@ // | ||
axios.debug({ | ||
id: config.id, | ||
msg: `Ignored because method (${config.method}) is not in cache.methods (${config.cache.methods})` | ||
@@ -43,0 +74,0 @@ }); |
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { CachedStorageValue, LoadingStorageValue, StorageValue } from '../storage/types'; | ||
export type CachePredicate<R = unknown, D = unknown> = Exclude< | ||
CachePredicateObject<R, D> | CachePredicateObject<R, D>['responseMatch'], | ||
undefined | ||
export type CachePredicate<R = unknown, D = unknown> = NonNullable< | ||
CachePredicateObject<R, D> | CachePredicateObject<R, D>['responseMatch'] | ||
>; | ||
@@ -28,2 +27,10 @@ | ||
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>; | ||
/** | ||
* Ignores the request if their url matches any provided urls and/or regexes. | ||
* | ||
* - It checks against the `request.url` property, `baseURL` is not considered. | ||
* - When only `baseURL` is specified, this property is ignored. | ||
*/ | ||
ignoreUrls?: (RegExp | string)[]; | ||
} | ||
@@ -30,0 +37,0 @@ |
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
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
470190
3307