axios-cache-interceptor
Advanced tools
Comparing version 1.0.0 to 1.0.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:()=>i,buildKeyGenerator:()=>A,buildMemoryStorage:()=>x,buildStorage:()=>w,buildWebStorage:()=>O,canStale:()=>b,createCacheResponse:()=>f,createValidateStatus:()=>d,defaultHeaderInterpreter:()=>s,defaultKeyGenerator:()=>j,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>m,isExpired:()=>y,isMethodIn:()=>u,isStorage:()=>v,setupCache:()=>E,testCachePredicate:()=>h,updateCache:()=>p,updateStaleRequest:()=>l});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;const 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=e=>{if(!e)return"not enough headers";const t=e[i.CacheControl];if(t){const{noCache:s,noStore:c,mustRevalidate:d,maxAge:u,immutable:l}=function(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"],u=i["s-maxage"],l=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(u)&&(t.sMaxAge=o(u)),r(l)&&(t.staleIfError=o(l)),r(f)&&(t.staleWhileRevalidate=o(f)),t}(String(t));if(s||c)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==u){const t=e[i.Age];return t?1e3*(u-Number(t)):1e3*u}}const s=e[i.Expires];if(s){const e=Date.parse(String(s))-Date.now();return e>=0?e:"dont cache"}return"not enough headers"};var c=Symbol();function d(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 l(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[i.ETag]:r;n&&(t.headers[i.IfNoneMatch]=n)}n&&(t.headers[i.IfModifiedSince]=!0===n?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():n.toUTCString())}function f(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,s,f,g,h,p,m,v,b,y,w,x,S,C,I;const A=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=t.headers)&&void 0!==r||(t.headers={}),null!==(n=(y=t.headers)[w=i.CacheControl])&&void 0!==n||(y[w]="no-cache"),null!==(o=(x=t.headers)[S=i.Pragma])&&void 0!==o||(x[S]="no-cache"),null!==(s=(C=t.headers)[I=i.Expires])&&void 0!==s||(C[I]="0")),!u(t.method,t.cache.methods))return null===(f=e.debug)||void 0===f||f.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let j=await e.storage.get(A,t);const E=t.cache.override;e:if("empty"===j.state||"stale"===j.state||E){if(e.waiting[A]&&!E&&(j=await e.storage.get(A,t),"empty"!==j.state)){null===(g=e.debug)||void 0===g||g.call(e,{id:A,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[A]=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].catch((()=>{})),await e.storage.set(A,{state:"loading",previous:E?j.data?"stale":"empty":j.state,data:j.data,createdAt:E&&!j.createdAt?Date.now():j.createdAt},t),"stale"===j.state&&(l(j,t),null===(h=e.debug)||void 0===h||h.call(e,{id:A,msg:"Updated stale request"})),t.validateStatus=d(t.validateStatus),null===(p=e.debug)||void 0===p||p.call(e,{id:A,msg:"Sending request, waiting for response",data:{overrideCache:E,state:j.state}}),t}let O;if("loading"===j.state){const a=e.waiting[A];if(!a)return await e.storage.remove(A,t),t;null===(m=e.debug)||void 0===m||m.call(e,{id:A,msg:"Detected concurrent request, waiting for it to finish"});try{O=await a}catch(a){return null===(v=e.debug)||void 0===v||v.call(e,{id:A,msg:"Deferred rejected, requesting again",data:a}),t}}else O=j.data;return t.adapter=()=>Promise.resolve({config:t,data:O.data,headers:O.headers,status:O.status,statusText:O.statusText,cached:!0,id:A}),null===(b=e.debug)||void 0===b||b.call(e,{id:A,msg:"Returning cached response"}),t};return{onFulfilled:t,apply:()=>e.interceptors.request.use(t)}}async function h(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 p(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 m(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,s,c,d,u,l,g,m,v;const b=a.id=null!==(r=(v=a.config).id)&&void 0!==r?r:v.id=e.generateKey(a.config);if(null!==(n=a.cached)&&void 0!==n||(a.cached=!1),a.cached)return null===(o=e.debug)||void 0===o||o.call(e,{id:b,msg:"Returned cached response"}),a;const y=a.config.cache;if(!y)return null===(s=e.debug)||void 0===s||s.call(e,{id:b,msg:"Response with config.cache falsy",data:a}),Object.assign(Object.assign({},a),{cached:!1});const w=a.config,x=await e.storage.get(b,w);if((null==y?void 0:y.update)&&await p(e.storage,a,y.update),"loading"!==x.state)return null===(c=e.debug)||void 0===c||c.call(e,{id:b,msg:"Response not cached and storage isn't loading",data:{cache:x,response:a}}),a;if(!x.data&&!await h(a,y.cachePredicate))return await t(b,w),null===(d=e.debug)||void 0===d||d.call(e,{id:b,msg:"Cache predicate rejected this response"}),a;for(const e of Object.keys(a.headers))e.startsWith("x-axios-cache")&&delete a.headers[e];y.etag&&!0!==y.etag&&(a.headers[i.XAxiosCacheEtag]=y.etag),y.modifiedSince&&(a.headers[i.XAxiosCacheLastModified]=!0===y.modifiedSince?"use-cache-timestamp":y.modifiedSince.toUTCString());let S=y.ttl||-1;if(null==y?void 0:y.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(b,w),null===(u=e.debug)||void 0===u||u.call(e,{id:b,msg:"Cache header interpreted as 'dont cache'",data:{cache:x,response:a,expirationTime:r}}),a;S="not enough headers"===r?S:r}const C=f(a,x.data);"function"==typeof S&&(S=await S(a)),y.staleIfError&&(a.headers[i.XAxiosCacheStaleIfError]=String(S)),null===(l=e.debug)||void 0===l||l.call(e,{id:b,msg:"Useful response configuration found",data:{cacheConfig:y,cacheResponse:C}});const I={state:"cached",ttl:S,createdAt:Date.now(),data:C},A=e.waiting[b];return A&&(A.resolve(I.data),delete e.waiting[b],null===(g=e.debug)||void 0===g||g.call(e,{id:b,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(b,I,w),null===(m=e.debug)||void 0===m||m.call(e,{id:b,msg:"Response cached",data:{cache:I,response:a}}),a},r=async a=>{var r,n,o,i,s,c;const d=a.config;if(!(null==d?void 0:d.cache)||!d.id)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;const u=await e.storage.get(d.id,d),l=d.cache;if("loading"!==u.state||"stale"!==u.previous)throw await t(d.id,d),null===(n=e.debug)||void 0===n||n.call(e,{msg:"Caught an error in the request interceptor",data:{error:a,config:d}}),a;if(null==l?void 0:l.staleIfError){const t="function"==typeof l.staleIfError?await l.staleIfError(a.response,u,a):l.staleIfError;if(null===(o=e.debug)||void 0===o||o.call(e,{msg:"Found cache if stale config for rejected response",data:{error:a,config:d,staleIfError:t}}),!0===t||"number"==typeof t&&u.createdAt+t>Date.now())return null===(i=e.waiting[d.id])||void 0===i||i.resolve(u.data),delete e.waiting[d.id],await e.storage.set(d.id,{state:"stale",createdAt:Date.now(),data:u.data},d),null===(s=e.debug)||void 0===s||s.call(e,{msg:"staleIfError resolved this response with cached data",data:{error:a,config:d,cache:u}}),{cached:!0,config:d,id:d.id,data:u.data.data,headers:u.data.headers,status:u.data.status,statusText:u.data.statusText}}throw null===(c=e.debug)||void 0===c||c.call(e,{msg:"Received an unknown error that could not be handled",data:{error:a,config:d}}),a};return{onFulfilled:a,onRejected:r,apply:()=>e.interceptors.response.use(a,r)}}const v=e=>!!e&&!!e["is-storage"];function b(e){const t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function y(e){return e.createdAt+e.ttl<=Date.now()}function w({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,n)=>{const o=await t(r,n);if(!o)return{state:"empty"};if("cached"!==o.state||!y(o))return o;if(b(o)){const t={state:"stale",createdAt:o.createdAt,data:o.data};return await e(r,t,n),t}return await a(r,n),{state:"empty"}}}}function x(e=!1){const t=w({set:(e,a)=>{t.data[e]=a},remove:e=>{delete t.data[e]},find:a=>{const r=t.data[a];return e&&void 0!==r?"function"==typeof structuredClone?structuredClone(r):JSON.parse(JSON.stringify(r)):r}});return t.data=Object.create(null),t}function S(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]=S(e[o])}return String(e.constructor)+JSON.stringify(a,r)}return t+String(e)}function C(e){e=S(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const I=/^\/|\/$/g;function A(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${C(a)}`}}const j=A((({baseURL:e="",url:t="",method:a="get",params:r,data:n})=>(e&&(e=e.replace(I,"")),t&&(t=t.replace(I,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:n})));function E(e,t={}){var a,r,n,o,i,c;const d=e;if(d.storage=t.storage||x(),!v(d.storage))throw new Error("Use buildStorage() function");return d.waiting=t.waiting||{},d.generateKey=t.generateKey||j,d.headerInterpreter=t.headerInterpreter||s,d.requestInterceptor=t.requestInterceptor||g(d),d.responseInterceptor=t.responseInterceptor||m(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"],cachePredicate:t.cachePredicate||{statusCheck:e=>e>=200&&e<400},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===(c=t.staleIfError)||void 0===c||c,override:!1},d.requestInterceptor.apply(),d.responseInterceptor.apply(),d}function O(e,t="axios-cache-"){return w({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&&y(t[1])&&!b(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/#/pages/installing\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:()=>i,buildKeyGenerator:()=>A,buildMemoryStorage:()=>x,buildStorage:()=>w,buildWebStorage:()=>O,canStale:()=>b,createCacheResponse:()=>f,createValidateStatus:()=>d,defaultHeaderInterpreter:()=>s,defaultKeyGenerator:()=>j,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>m,isExpired:()=>y,isMethodIn:()=>u,isStorage:()=>v,setupCache:()=>E,testCachePredicate:()=>h,updateCache:()=>p,updateStaleRequest:()=>l});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;const 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=e=>{if(!e)return"not enough headers";const t=e[i.CacheControl];if(t){const{noCache:s,noStore:c,mustRevalidate:d,maxAge:u,immutable:l}=function(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"],u=i["s-maxage"],l=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(u)&&(t.sMaxAge=o(u)),r(l)&&(t.staleIfError=o(l)),r(f)&&(t.staleWhileRevalidate=o(f)),t}(String(t));if(s||c)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==u){const t=e[i.Age];return t?1e3*(u-Number(t)):1e3*u}}const s=e[i.Expires];if(s){const e=Date.parse(String(s))-Date.now();return e>=0?e:"dont cache"}return"not enough headers"};var c=Symbol();function d(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 l(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[i.ETag]:r;n&&(t.headers[i.IfNoneMatch]=n)}n&&(t.headers[i.IfModifiedSince]=!0===n?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():n.toUTCString())}function f(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,s,f,g,h,p,m,v,b,y,w,x,S,C;const I=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=(b=t.headers)[y=i.CacheControl])&&void 0!==r||(b[y]="no-cache"),null!==(n=(w=t.headers)[x=i.Pragma])&&void 0!==n||(w[x]="no-cache"),null!==(o=(S=t.headers)[C=i.Expires])&&void 0!==o||(S[C]="0")),!u(t.method,t.cache.methods))return null===(s=e.debug)||void 0===s||s.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let A=await e.storage.get(I,t);const j=t.cache.override;e:if("empty"===A.state||"stale"===A.state||j){if(e.waiting[I]&&!j&&(A=await e.storage.get(I,t),"empty"!==A.state)){null===(f=e.debug)||void 0===f||f.call(e,{id:I,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[I]=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[I].catch((()=>{})),await e.storage.set(I,{state:"loading",previous:j?A.data?"stale":"empty":A.state,data:A.data,createdAt:j&&!A.createdAt?Date.now():A.createdAt},t),"stale"===A.state&&(l(A,t),null===(g=e.debug)||void 0===g||g.call(e,{id:I,msg:"Updated stale request"})),t.validateStatus=d(t.validateStatus),null===(h=e.debug)||void 0===h||h.call(e,{id:I,msg:"Sending request, waiting for response",data:{overrideCache:j,state:A.state}}),t}let E;if("loading"===A.state){const a=e.waiting[I];if(!a)return await e.storage.remove(I,t),t;null===(p=e.debug)||void 0===p||p.call(e,{id:I,msg:"Detected concurrent request, waiting for it to finish"});try{E=await a}catch(a){return null===(m=e.debug)||void 0===m||m.call(e,{id:I,msg:"Deferred rejected, requesting again",data:a}),t}}else E=A.data;return t.adapter=()=>Promise.resolve({config:t,data:E.data,headers:E.headers,status:E.status,statusText:E.statusText,cached:!0,id:I}),null===(v=e.debug)||void 0===v||v.call(e,{id:I,msg:"Returning cached response"}),t};return{onFulfilled:t,apply:()=>e.interceptors.request.use(t)}}async function h(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 p(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 m(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,s,c,d,l,g,m,v,b,y;const w=a.id=null!==(r=(y=a.config).id)&&void 0!==r?r:y.id=e.generateKey(a.config);if(null!==(n=a.cached)&&void 0!==n||(a.cached=!1),a.cached)return null===(o=e.debug)||void 0===o||o.call(e,{id:w,msg:"Returned cached response"}),a;const x=a.config,S=x.cache;if(!S)return null===(s=e.debug)||void 0===s||s.call(e,{id:w,msg:"Response with config.cache falsy",data:a}),Object.assign(Object.assign({},a),{cached:!1});if(S.update&&await p(e.storage,a,S.update),!u(x.method,S.methods))return null===(c=e.debug)||void 0===c||c.call(e,{id:w,msg:`Ignored because method (${x.method}) is not in cache.methods (${S.methods})`,data:{config:x,cacheConfig:S}}),a;const C=await e.storage.get(w,x);if("loading"!==C.state)return null===(d=e.debug)||void 0===d||d.call(e,{id:w,msg:"Response not cached and storage isn't loading",data:{cache:C,response:a}}),a;if(!C.data&&!await h(a,S.cachePredicate))return await t(w,x),null===(l=e.debug)||void 0===l||l.call(e,{id:w,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[i.XAxiosCacheEtag]=S.etag),S.modifiedSince&&(a.headers[i.XAxiosCacheLastModified]=!0===S.modifiedSince?"use-cache-timestamp":S.modifiedSince.toUTCString());let I=S.ttl||-1;if(S.interpretHeader){const r=e.headerInterpreter(a.headers);if("dont cache"===r)return await t(w,x),null===(g=e.debug)||void 0===g||g.call(e,{id:w,msg:"Cache header interpreted as 'dont cache'",data:{cache:C,response:a,expirationTime:r}}),a;I="not enough headers"===r?I:r}const A=f(a,C.data);"function"==typeof I&&(I=await I(a)),S.staleIfError&&(a.headers[i.XAxiosCacheStaleIfError]=String(I)),null===(m=e.debug)||void 0===m||m.call(e,{id:w,msg:"Useful response configuration found",data:{cacheConfig:S,cacheResponse:A}});const j={state:"cached",ttl:I,createdAt:Date.now(),data:A},E=e.waiting[w];return E&&(E.resolve(j.data),delete e.waiting[w],null===(v=e.debug)||void 0===v||v.call(e,{id:w,msg:"Found waiting deferred(s) and resolved them"})),await e.storage.set(w,j,x),null===(b=e.debug)||void 0===b||b.call(e,{id:w,msg:"Response cached",data:{cache:j,response:a}}),a},r=async a=>{var r,n,o,i,s,c,d;const l=a.config,f=l.id,g=l.cache;if(!g||!f)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(l.method,g.methods))throw null===(n=e.debug)||void 0===n||n.call(e,{id:f,msg:`Ignored because method (${l.method}) is not in cache.methods (${g.methods})`,data:{config:l,cacheConfig:g}}),a;const h=await e.storage.get(f,l);if("loading"!==h.state||"stale"!==h.previous)throw await t(f,l),null===(o=e.debug)||void 0===o||o.call(e,{id:f,msg:"Caught an error in the request interceptor",data:{error:a,config:l}}),a;if(g.staleIfError){const t="function"==typeof g.staleIfError?await g.staleIfError(a.response,h,a):g.staleIfError;if(null===(i=e.debug)||void 0===i||i.call(e,{id:f,msg:"Found cache if stale config for rejected response",data:{error:a,config:l,staleIfError:t}}),!0===t||"number"==typeof t&&h.createdAt+t>Date.now())return null===(s=e.waiting[f])||void 0===s||s.resolve(h.data),delete e.waiting[f],await e.storage.set(f,{state:"stale",createdAt:Date.now(),data:h.data},l),null===(c=e.debug)||void 0===c||c.call(e,{id:f,msg:"staleIfError resolved this response with cached data",data:{error:a,config:l,cache:h}}),{cached:!0,config:l,id:f,data:h.data.data,headers:h.data.headers,status:h.data.status,statusText:h.data.statusText}}throw null===(d=e.debug)||void 0===d||d.call(e,{id:f,msg:"Received an unknown error that could not be handled",data:{error:a,config:l}}),a};return{onFulfilled:a,onRejected:r,apply:()=>e.interceptors.response.use(a,r)}}const v=e=>!!e&&!!e["is-storage"];function b(e){const t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function y(e){return e.createdAt+e.ttl<=Date.now()}function w({set:e,find:t,remove:a}){return{"is-storage":1,set:e,remove:a,get:async(r,n)=>{const o=await t(r,n);if(!o)return{state:"empty"};if("cached"!==o.state||!y(o))return o;if(b(o)){const t={state:"stale",createdAt:o.createdAt,data:o.data};return await e(r,t,n),t}return await a(r,n),{state:"empty"}}}}function x(e=!1){const t=w({set:(e,a)=>{t.data[e]=a},remove:e=>{delete t.data[e]},find:a=>{const r=t.data[a];return e&&void 0!==r?"function"==typeof structuredClone?structuredClone(r):JSON.parse(JSON.stringify(r)):r}});return t.data=Object.create(null),t}function S(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]=S(e[o])}return String(e.constructor)+JSON.stringify(a,r)}return t+String(e)}function C(e){e=S(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const I=/^\/|\/$/g;function A(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${C(a)}`}}const j=A((({baseURL:e="",url:t="",method:a="get",params:r,data:n})=>(e&&(e=e.replace(I,"")),t&&(t=t.replace(I,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:n})));function E(e,t={}){var a,r,n,o,i,c;const d=e;if(d.defaults.cache)throw new Error("setupCache() should be called only once");if(d.storage=t.storage||x(),!v(d.storage))throw new Error("Use buildStorage() function");return d.waiting=t.waiting||{},d.generateKey=t.generateKey||j,d.headerInterpreter=t.headerInterpreter||s,d.requestInterceptor=t.requestInterceptor||g(d),d.responseInterceptor=t.responseInterceptor||m(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"],cachePredicate:t.cachePredicate||{statusCheck:e=>e>=200&&e<400},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===(c=t.staleIfError)||void 0===c||c,override:!1},d.requestInterceptor.apply(),d.responseInterceptor.apply(),d}function O(e,t="axios-cache-"){return w({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&&y(t[1])&&!b(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/#/pages/installing\n\n"),t})())); | ||
//# sourceMappingURL=index.bundle.js.map |
@@ -1,2 +0,2 @@ | ||
import type { AxiosInstance, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import type { AxiosInstance, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders } from 'axios'; | ||
import type { CacheInstance, CacheProperties } from './cache'; | ||
@@ -35,2 +35,6 @@ /** | ||
}; | ||
/** Cached version of type {@link InternalAxiosRequestConfig} */ | ||
export type InternalCacheRequestConfig<R = any, D = any> = CacheRequestConfig<R, D> & { | ||
headers: AxiosResponseHeaders; | ||
}; | ||
/** | ||
@@ -62,3 +66,3 @@ * Same as the AxiosInstance but with CacheRequestConfig as a config type and | ||
interceptors: { | ||
request: AxiosInterceptorManager<CacheRequestConfig>; | ||
request: AxiosInterceptorManager<InternalCacheRequestConfig>; | ||
response: AxiosInterceptorManager<CacheAxiosResponse>; | ||
@@ -65,0 +69,0 @@ }; |
import type { AxiosRequestHeaders } from 'axios'; | ||
import type { CacheAxiosResponse } from '../cache/axios'; | ||
export type InterpreterResult = 'dont cache' | 'not enough headers' | number; | ||
@@ -11,3 +12,3 @@ /** | ||
*/ | ||
export type HeadersInterpreter = (headers?: Record<string, string | undefined>) => InterpreterResult; | ||
export type HeadersInterpreter = (headers?: CacheAxiosResponse['headers']) => InterpreterResult; | ||
/** | ||
@@ -14,0 +15,0 @@ * Interpret a single string header |
@@ -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 n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Header:function(){return i},buildKeyGenerator:function(){return j},buildMemoryStorage:function(){return C},buildStorage:function(){return I},buildWebStorage:function(){return P},canStale:function(){return w},createCacheResponse:function(){return p},createValidateStatus:function(){return f},defaultHeaderInterpreter:function(){return c},defaultKeyGenerator:function(){return T},defaultRequestInterceptor:function(){return g},defaultResponseInterceptor:function(){return y},isExpired:function(){return S},isMethodIn:function(){return h},isStorage:function(){return x},setupCache:function(){return M},testCachePredicate:function(){return m},updateCache:function(){return b},updateStaleRequest:function(){return v}});var r=Symbol("cache-parser");function n(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function a(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",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=function(e){if(!e)return"not enough headers";var t=e[i.CacheControl];if(t){var c=function(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var n in r){var a,o=r[n].split("=",2);t[o[0]]=null==(a=o[1])||a}return t}(e),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 a(i.immutable)&&(t.immutable=!0),n(c)&&(t.maxAge=o(c)),n(s)&&(t.maxStale=o(s)),n(u)&&(t.minFresh=o(u)),a(i["must-revalidate"])&&(t.mustRevalidate=!0),a(i["must-understand"])&&(t.mustUnderstand=!0),a(i["no-cache"])&&(t.noCache=!0),a(i["no-store"])&&(t.noStore=!0),a(i["no-transform"])&&(t.noTransform=!0),a(i["only-if-cached"])&&(t.onlyIfCached=!0),a(i.private)&&(t.private=!0),a(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),a(i.public)&&(t.public=!0),n(d)&&(t.sMaxAge=o(d)),n(l)&&(t.staleIfError=o(l)),n(f)&&(t.staleWhileRevalidate=o(f)),t}(String(t)),s=c.noCache,u=c.noStore,d=c.mustRevalidate,l=c.maxAge,f=c.immutable;if(s||u)return"dont cache";if(f)return 31536e6;if(d)return 0;if(void 0!==l){var h=e[i.Age];return h?1e3*(l-Number(h)):1e3*l}}var v=e[i.Expires];if(v){var p=Date.parse(String(v))-Date.now();return p>=0?p:"dont cache"}return"not enough headers"};var s=function(){return s=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},s.apply(this,arguments)};function u(e,t,r,n){return new(r||(r=Promise))((function(a,o){function i(e){try{s(n.next(e))}catch(e){o(e)}}function c(e){try{s(n.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,c)}s((n=n.apply(e,t||[])).next())}))}function d(e,t){var r,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next: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,n&&(a=2&c[0]?n.return:c[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,c[1])).done)return a;switch(n=0,a&&(c=[2&c[0],a.value]),c[0]){case 0:case 1:a=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!a||c[1]>a[0]&&c[1]<a[3])){i.label=c[1];break}if(6===c[0]&&i.label<a[1]){i.label=a[1],a=c;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(c);break}a[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=a=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}Object.create;Object.create;var l=Symbol();function f(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function h(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function v(e,t){var r;t.headers||(t.headers={});var n=t.cache,a=n.etag,o=n.modifiedSince;if(a){var c=!0===a?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:a;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 p(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=s(s({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){var t=this,r=function(r){return u(t,void 0,void 0,(function(){var t,n,a,o,c,u,p,g,m,b,y,x,w,S,I;return d(this,(function(d){switch(d.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=s(s({},e.defaults.cache),r.cache),r.cache.cacheTakeover&&(null!==(u=r.headers)&&void 0!==u||(r.headers={}),null!==(p=(b=r.headers)[y=i.CacheControl])&&void 0!==p||(b[y]="no-cache"),null!==(g=(x=r.headers)[w=i.Pragma])&&void 0!==g||(x[w]="no-cache"),null!==(m=(S=r.headers)[I=i.Expires])&&void 0!==m||(S[I]="0")),h(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return n=d.sent(),a=r.cache.override,"empty"===n.state||"stale"===n.state||a?!e.waiting[t]||a?[3,3]:[4,e.storage.get(t,r)]:[3,5];case 2:if("empty"!==(n=d.sent()).state)return[3,5];d.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,n){e=r,t=n}));return r.resolve=e,r.reject=t,r[l]=1,r}(),e.waiting[t].catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:a?n.data?"stale":"empty":n.state,data:n.data,createdAt:a&&!n.createdAt?Date.now():n.createdAt},r)];case 4:return d.sent(),"stale"===n.state&&v(n,r),r.validateStatus=f(r.validateStatus),[2,r];case 5:return"loading"!==n.state?[3,12]:(c=e.waiting[t])?[3,7]:[4,e.storage.remove(t,r)];case 6:return d.sent(),[2,r];case 7:0,d.label=8;case 8:return d.trys.push([8,10,,11]),[4,c];case 9:return o=d.sent(),[3,11];case 10:return d.sent(),[2,r];case 11:return[3,13];case 12:o=n.data,d.label=13;case 13: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 m(e,t){var r;return u(this,void 0,void 0,(function(){var n,a,o,i,c,s,u,l,f,h;return d(this,(function(d){switch(d.label){case 0:return"function"==typeof t?[2,t(e)]:(n=t.statusCheck,a=t.responseMatch,o=t.containsHeaders,(c=n)?[4,n(e.status)]:[3,2]);case 1:c=!d.sent(),d.label=2;case 2:return(i=c)?[3,5]:(s=a)?[4,a(e)]:[3,4];case 3:s=!d.sent(),d.label=4;case 4:i=s,d.label=5;case 5:if(i)return[2,!1];if(!o)return[3,9];u=0,l=Object.entries(o),d.label=6;case 6:return u<l.length?(f=l[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(!d.sent())return[2,!1];d.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function b(e,t,r){return u(this,void 0,void 0,(function(){var n,a,o,i,c,s,u;return d(this,(function(d){switch(d.label){case 0:if("function"==typeof r)return[2,r(t)];n=0,a=Object.entries(r),d.label=1;case 1:return n<a.length?(o=a[n],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 n++,[3,1];case 10:return[2]}}))}))}function y(e){var t=this,r=function(r,n){return u(t,void 0,void 0,(function(){var t;return d(this,(function(a){switch(a.label){case 0:return[4,e.storage.remove(r,n)];case 1:return a.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},n=function(n){return u(t,void 0,void 0,(function(){var t,a,o,c,u,l,f,h,v,g,y,x,w,S,I,C;return d(this,(function(d){switch(d.label){case 0:return t=n.id=null!==(S=(C=n.config).id)&&void 0!==S?S:C.id=e.generateKey(n.config),null!==(I=n.cached)&&void 0!==I||(n.cached=!1),n.cached?[2,n]:(a=n.config.cache)?(o=n.config,[4,e.storage.get(t,o)]):[2,s(s({},n),{cached:!1})];case 1:return c=d.sent(),(null==a?void 0:a.update)?[4,b(e.storage,n,a.update)]:[3,3];case 2:d.sent(),d.label=3;case 3:return"loading"!==c.state?[2,n]:(u=!c.data)?[4,m(n,a.cachePredicate)]:[3,5];case 4:u=!d.sent(),d.label=5;case 5:return u?[4,r(t,o)]:[3,7];case 6:return d.sent(),[2,n];case 7:for(l=0,f=Object.keys(n.headers);l<f.length;l++)(h=f[l]).startsWith("x-axios-cache")&&delete n.headers[h];return a.etag&&!0!==a.etag&&(n.headers[i.XAxiosCacheEtag]=a.etag),a.modifiedSince&&(n.headers[i.XAxiosCacheLastModified]=!0===a.modifiedSince?"use-cache-timestamp":a.modifiedSince.toUTCString()),v=a.ttl||-1,(null==a?void 0:a.interpretHeader)?"dont cache"!==(g=e.headerInterpreter(n.headers))?[3,9]:[4,r(t,o)]:[3,10];case 8:return d.sent(),[2,n];case 9:v="not enough headers"===g?v:g,d.label=10;case 10:return y=p(n,c.data),"function"!=typeof v?[3,12]:[4,v(n)];case 11:v=d.sent(),d.label=12;case 12:return a.staleIfError&&(n.headers[i.XAxiosCacheStaleIfError]=String(v)),x={state:"cached",ttl:v,createdAt:Date.now(),data:y},(w=e.waiting[t])&&(w.resolve(x.data),delete e.waiting[t]),[4,e.storage.set(t,x,o)];case 13:return d.sent(),[2,n]}}))}))},a=function(n){return u(t,void 0,void 0,(function(){var t,a,o,i,c,s;return d(this,(function(u){switch(u.label){case 0:if(!(null==(t=n.config)?void 0:t.cache)||!t.id)throw n;return[4,e.storage.get(t.id,t)];case 1:return a=u.sent(),o=t.cache,"loading"===a.state&&"stale"===a.previous?[3,3]:[4,r(t.id,t)];case 2:throw u.sent(),n;case 3:return(null==o?void 0:o.staleIfError)?"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(n.response,a,n)]:[3,8];case 4:return c=u.sent(),[3,6];case 5:c=o.staleIfError,u.label=6;case 6:return!0===(i=c)||"number"==typeof i&&a.createdAt+i>Date.now()?(null===(s=e.waiting[t.id])||void 0===s||s.resolve(a.data),delete e.waiting[t.id],[4,e.storage.set(t.id,{state:"stale",createdAt:Date.now(),data:a.data},t)]):[3,8];case 7:return u.sent(),[2,{cached:!0,config:t,id:t.id,data:a.data.data,headers:a.data.headers,status:a.data.status,statusText:a.data.statusText}];case 8:throw n}}))}))};return{onFulfilled:n,onRejected:a,apply:function(){return e.interceptors.response.use(n,a)}}}var x=function(e){return!!e&&!!e["is-storage"]};function w(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function S(e){return e.createdAt+e.ttl<=Date.now()}function I(e){var t,r=this,n=e.set,a=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=n,t.remove=o,t.get=function(e,t){return u(r,void 0,void 0,(function(){var r,i;return d(this,(function(c){switch(c.label){case 0:return[4,a(e,t)];case 1:return(r=c.sent())?"cached"===r.state&&S(r)?w(r)?(i={state:"stale",createdAt:r.createdAt,data:r.data},[4,n(e,i,t)]):[3,3]:[2,r]:[2,{state:"empty"}];case 2:return c.sent(),[2,i];case 3:return[4,o(e,t)];case 4:return c.sent(),[2,{state:"empty"}]}}))}))},t}function C(e){void 0===e&&(e=!1);var t=I({set:function(e,r){t.data[e]=r},remove:function(e){delete t.data[e]},find:function(r){var n=t.data[r];return e&&void 0!==n?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n}});return t.data=Object.create(null),t}function A(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},n=Object.keys(e).sort((function(e,t){return e>t?1:-1})),a=n.length;a--;){var o=n[a];r[o]=A(e[o])}return String(e.constructor)+JSON.stringify(r,n)}return t+String(e)}function E(e){e=A(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var O=/^\/|\/$/g;function j(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:E(r))}}var T=j((function(e){var t=e.baseURL,r=void 0===t?"":t,n=e.url,a=void 0===n?"":n,o=e.method,i=void 0===o?"get":o,c=e.params,s=e.data;return r&&(r=r.replace(O,"")),a&&(a=a.replace(O,"")),i&&(i=i.toLowerCase()),{url:r+(r&&a?"/":"")+a,params:c,method:i,data:s}}));function M(e,t){var r,n,a,o,i,s;void 0===t&&(t={});var u=e;if(u.storage=t.storage||C(),!x(u.storage))throw new Error("Use buildStorage() function");return u.waiting=t.waiting||{},u.generateKey=t.generateKey||T,u.headerInterpreter=t.headerInterpreter||c,u.requestInterceptor=t.requestInterceptor||g(u),u.responseInterceptor=t.responseInterceptor||y(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"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return e>=200&&e<400}},etag:null===(n=t.etag)||void 0===n||n,modifiedSince:null!==(a=t.modifiedSince)&&void 0!==a?a:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,cacheTakeover:null===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:!1},u.requestInterceptor.apply(),u.responseInterceptor.apply(),u}function P(e,t){return void 0===t&&(t="axios-cache-"),I({find:function(r){var n=e.getItem(t+r);return n?JSON.parse(n):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,n){var a=function(){return e.setItem(t+r,JSON.stringify(n))};try{return a()}catch(n){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),i=0,c=o;i<c.length;i++){var s=c[i];"cached"===s[1].state&&S(s[1])&&!w(s[1])&&e.removeItem(s[0])}try{return a()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,l=u;d<l.length;d++){var f=l[d];e.removeItem(f[0]);try{return a()}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 n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Header:function(){return i},buildKeyGenerator:function(){return j},buildMemoryStorage:function(){return I},buildStorage:function(){return C},buildWebStorage:function(){return P},canStale:function(){return x},createCacheResponse:function(){return p},createValidateStatus:function(){return l},defaultHeaderInterpreter:function(){return c},defaultKeyGenerator:function(){return T},defaultRequestInterceptor:function(){return g},defaultResponseInterceptor:function(){return y},isExpired:function(){return S},isMethodIn:function(){return h},isStorage:function(){return w},setupCache:function(){return M},testCachePredicate:function(){return m},updateCache:function(){return b},updateStaleRequest:function(){return v}});var r=Symbol("cache-parser");function n(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function a(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",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=function(e){if(!e)return"not enough headers";var t=e[i.CacheControl];if(t){var c=function(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var n in r){var a,o=r[n].split("=",2);t[o[0]]=null==(a=o[1])||a}return t}(e),c=i["max-age"],s=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],f=i["stale-if-error"],l=i["stale-while-revalidate"];return a(i.immutable)&&(t.immutable=!0),n(c)&&(t.maxAge=o(c)),n(s)&&(t.maxStale=o(s)),n(u)&&(t.minFresh=o(u)),a(i["must-revalidate"])&&(t.mustRevalidate=!0),a(i["must-understand"])&&(t.mustUnderstand=!0),a(i["no-cache"])&&(t.noCache=!0),a(i["no-store"])&&(t.noStore=!0),a(i["no-transform"])&&(t.noTransform=!0),a(i["only-if-cached"])&&(t.onlyIfCached=!0),a(i.private)&&(t.private=!0),a(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),a(i.public)&&(t.public=!0),n(d)&&(t.sMaxAge=o(d)),n(f)&&(t.staleIfError=o(f)),n(l)&&(t.staleWhileRevalidate=o(l)),t}(String(t)),s=c.noCache,u=c.noStore,d=c.mustRevalidate,f=c.maxAge,l=c.immutable;if(s||u)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==f){var h=e[i.Age];return h?1e3*(f-Number(h)):1e3*f}}var v=e[i.Expires];if(v){var p=Date.parse(String(v))-Date.now();return p>=0?p:"dont cache"}return"not enough headers"};var s=function(){return s=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},s.apply(this,arguments)};function u(e,t,r,n){return new(r||(r=Promise))((function(a,o){function i(e){try{s(n.next(e))}catch(e){o(e)}}function c(e){try{s(n.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,c)}s((n=n.apply(e,t||[])).next())}))}function d(e,t){var r,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next: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,n&&(a=2&c[0]?n.return:c[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,c[1])).done)return a;switch(n=0,a&&(c=[2&c[0],a.value]),c[0]){case 0:case 1:a=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!a||c[1]>a[0]&&c[1]<a[3])){i.label=c[1];break}if(6===c[0]&&i.label<a[1]){i.label=a[1],a=c;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(c);break}a[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=a=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}Object.create;Object.create;var f=Symbol();function l(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function h(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function v(e,t){var r;t.headers||(t.headers={});var n=t.cache,a=n.etag,o=n.modifiedSince;if(a){var c=!0===a?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:a;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 p(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=s(s({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){var t=this,r=function(r){return u(t,void 0,void 0,(function(){var t,n,a,o,c,u,p,g,m,b,y,w,x,S;return d(this,(function(d){switch(d.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=s(s({},e.defaults.cache),r.cache),r.cache.cacheTakeover&&(null!==(u=(m=r.headers)[b=i.CacheControl])&&void 0!==u||(m[b]="no-cache"),null!==(p=(y=r.headers)[w=i.Pragma])&&void 0!==p||(y[w]="no-cache"),null!==(g=(x=r.headers)[S=i.Expires])&&void 0!==g||(x[S]="0")),h(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return n=d.sent(),a=r.cache.override,"empty"===n.state||"stale"===n.state||a?!e.waiting[t]||a?[3,3]:[4,e.storage.get(t,r)]:[3,5];case 2:if("empty"!==(n=d.sent()).state)return[3,5];d.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,n){e=r,t=n}));return r.resolve=e,r.reject=t,r[f]=1,r}(),e.waiting[t].catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:a?n.data?"stale":"empty":n.state,data:n.data,createdAt:a&&!n.createdAt?Date.now():n.createdAt},r)];case 4:return d.sent(),"stale"===n.state&&v(n,r),r.validateStatus=l(r.validateStatus),[2,r];case 5:return"loading"!==n.state?[3,12]:(c=e.waiting[t])?[3,7]:[4,e.storage.remove(t,r)];case 6:return d.sent(),[2,r];case 7:0,d.label=8;case 8:return d.trys.push([8,10,,11]),[4,c];case 9:return o=d.sent(),[3,11];case 10:return d.sent(),[2,r];case 11:return[3,13];case 12:o=n.data,d.label=13;case 13: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 m(e,t){var r;return u(this,void 0,void 0,(function(){var n,a,o,i,c,s,u,f,l,h;return d(this,(function(d){switch(d.label){case 0:return"function"==typeof t?[2,t(e)]:(n=t.statusCheck,a=t.responseMatch,o=t.containsHeaders,(c=n)?[4,n(e.status)]:[3,2]);case 1:c=!d.sent(),d.label=2;case 2:return(i=c)?[3,5]:(s=a)?[4,a(e)]:[3,4];case 3:s=!d.sent(),d.label=4;case 4:i=s,d.label=5;case 5:if(i)return[2,!1];if(!o)return[3,9];u=0,f=Object.entries(o),d.label=6;case 6:return u<f.length?(l=f[u],h=l[0],[4,(0,l[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!d.sent())return[2,!1];d.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function b(e,t,r){return u(this,void 0,void 0,(function(){var n,a,o,i,c,s,u;return d(this,(function(d){switch(d.label){case 0:if("function"==typeof r)return[2,r(t)];n=0,a=Object.entries(r),d.label=1;case 1:return n<a.length?(o=a[n],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 n++,[3,1];case 10:return[2]}}))}))}function y(e){var t=this,r=function(r,n){return u(t,void 0,void 0,(function(){var t;return d(this,(function(a){switch(a.label){case 0:return[4,e.storage.remove(r,n)];case 1:return a.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},n=function(n){return u(t,void 0,void 0,(function(){var t,a,o,c,u,f,l,v,g,y,w,x,S,C,I,A;return d(this,(function(d){switch(d.label){case 0:return t=n.id=null!==(C=(A=n.config).id)&&void 0!==C?C:A.id=e.generateKey(n.config),null!==(I=n.cached)&&void 0!==I||(n.cached=!1),n.cached?[2,n]:(a=n.config,(o=a.cache)?o.update?[4,b(e.storage,n,o.update)]:[3,2]:[2,s(s({},n),{cached:!1})]);case 1:d.sent(),d.label=2;case 2:return h(a.method,o.methods)?[4,e.storage.get(t,a)]:[2,n];case 3:return"loading"!==(c=d.sent()).state?[2,n]:(u=!c.data)?[4,m(n,o.cachePredicate)]:[3,5];case 4:u=!d.sent(),d.label=5;case 5:return u?[4,r(t,a)]:[3,7];case 6:return d.sent(),[2,n];case 7:for(f=0,l=Object.keys(n.headers);f<l.length;f++)(v=l[f]).startsWith("x-axios-cache")&&delete n.headers[v];return o.etag&&!0!==o.etag&&(n.headers[i.XAxiosCacheEtag]=o.etag),o.modifiedSince&&(n.headers[i.XAxiosCacheLastModified]=!0===o.modifiedSince?"use-cache-timestamp":o.modifiedSince.toUTCString()),g=o.ttl||-1,o.interpretHeader?"dont cache"!==(y=e.headerInterpreter(n.headers))?[3,9]:[4,r(t,a)]:[3,10];case 8:return d.sent(),[2,n];case 9:g="not enough headers"===y?g:y,d.label=10;case 10:return w=p(n,c.data),"function"!=typeof g?[3,12]:[4,g(n)];case 11:g=d.sent(),d.label=12;case 12:return o.staleIfError&&(n.headers[i.XAxiosCacheStaleIfError]=String(g)),x={state:"cached",ttl:g,createdAt:Date.now(),data:w},(S=e.waiting[t])&&(S.resolve(x.data),delete e.waiting[t]),[4,e.storage.set(t,x,a)];case 13:return d.sent(),[2,n]}}))}))},a=function(n){return u(t,void 0,void 0,(function(){var t,a,o,i,c,s,u;return d(this,(function(d){switch(d.label){case 0:if(t=n.config,a=t.id,!(o=t.cache)||!a)throw n;if(!h(t.method,o.methods))throw n;return[4,e.storage.get(a,t)];case 1:return"loading"===(i=d.sent()).state&&"stale"===i.previous?[3,3]:[4,r(a,t)];case 2:throw d.sent(),n;case 3:return o.staleIfError?"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(n.response,i,n)]:[3,8];case 4:return s=d.sent(),[3,6];case 5:s=o.staleIfError,d.label=6;case 6:return!0===(c=s)||"number"==typeof c&&i.createdAt+c>Date.now()?(null===(u=e.waiting[a])||void 0===u||u.resolve(i.data),delete e.waiting[a],[4,e.storage.set(a,{state:"stale",createdAt:Date.now(),data:i.data},t)]):[3,8];case 7:return d.sent(),[2,{cached:!0,config:t,id:a,data:i.data.data,headers:i.data.headers,status:i.data.status,statusText:i.data.statusText}];case 8:throw n}}))}))};return{onFulfilled:n,onRejected:a,apply:function(){return e.interceptors.response.use(n,a)}}}var w=function(e){return!!e&&!!e["is-storage"]};function x(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function S(e){return e.createdAt+e.ttl<=Date.now()}function C(e){var t,r=this,n=e.set,a=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=n,t.remove=o,t.get=function(e,t){return u(r,void 0,void 0,(function(){var r,i;return d(this,(function(c){switch(c.label){case 0:return[4,a(e,t)];case 1:return(r=c.sent())?"cached"===r.state&&S(r)?x(r)?(i={state:"stale",createdAt:r.createdAt,data:r.data},[4,n(e,i,t)]):[3,3]:[2,r]:[2,{state:"empty"}];case 2:return c.sent(),[2,i];case 3:return[4,o(e,t)];case 4:return c.sent(),[2,{state:"empty"}]}}))}))},t}function I(e){void 0===e&&(e=!1);var t=C({set:function(e,r){t.data[e]=r},remove:function(e){delete t.data[e]},find:function(r){var n=t.data[r];return e&&void 0!==n?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n}});return t.data=Object.create(null),t}function A(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},n=Object.keys(e).sort((function(e,t){return e>t?1:-1})),a=n.length;a--;){var o=n[a];r[o]=A(e[o])}return String(e.constructor)+JSON.stringify(r,n)}return t+String(e)}function E(e){e=A(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var O=/^\/|\/$/g;function j(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:E(r))}}var T=j((function(e){var t=e.baseURL,r=void 0===t?"":t,n=e.url,a=void 0===n?"":n,o=e.method,i=void 0===o?"get":o,c=e.params,s=e.data;return r&&(r=r.replace(O,"")),a&&(a=a.replace(O,"")),i&&(i=i.toLowerCase()),{url:r+(r&&a?"/":"")+a,params:c,method:i,data:s}}));function M(e,t){var r,n,a,o,i,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||I(),!w(u.storage))throw new Error("Use buildStorage() function");return u.waiting=t.waiting||{},u.generateKey=t.generateKey||T,u.headerInterpreter=t.headerInterpreter||c,u.requestInterceptor=t.requestInterceptor||g(u),u.responseInterceptor=t.responseInterceptor||y(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"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return e>=200&&e<400}},etag:null===(n=t.etag)||void 0===n||n,modifiedSince:null!==(a=t.modifiedSince)&&void 0!==a?a:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,cacheTakeover:null===(i=t.cacheTakeover)||void 0===i||i,staleIfError:null===(s=t.staleIfError)||void 0===s||s,override:!1},u.requestInterceptor.apply(),u.responseInterceptor.apply(),u}function P(e,t){return void 0===t&&(t="axios-cache-"),C({find:function(r){var n=e.getItem(t+r);return n?JSON.parse(n):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,n){var a=function(){return e.setItem(t+r,JSON.stringify(n))};try{return a()}catch(n){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),i=0,c=o;i<c.length;i++){var s=c[i];"cached"===s[1].state&&S(s[1])&&!x(s[1])&&e.removeItem(s[0])}try{return a()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,f=u;d<f.length;d++){var l=f[d];e.removeItem(l[0]);try{return a()}catch(e){}}}e.removeItem(t+r)}}})}return t}()})); |
@@ -1,2 +0,2 @@ | ||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { CacheAxiosResponse, InternalCacheRequestConfig } from '../cache/axios'; | ||
/** See {@link AxiosInterceptorManager} */ | ||
@@ -9,4 +9,4 @@ export interface AxiosInterceptor<T> { | ||
} | ||
export type RequestInterceptor = AxiosInterceptor<CacheRequestConfig<unknown, unknown>>; | ||
export type RequestInterceptor = AxiosInterceptor<InternalCacheRequestConfig<unknown, unknown>>; | ||
export type ResponseInterceptor = AxiosInterceptor<CacheAxiosResponse<unknown, unknown>>; | ||
//# sourceMappingURL=build.d.ts.map |
import type { AxiosCacheInstance } from '../cache/axios'; | ||
export declare function defaultRequestInterceptor(axios: AxiosCacheInstance): { | ||
onFulfilled: (value: import("../cache/axios").CacheRequestConfig<unknown, unknown>) => import("../cache/axios").CacheRequestConfig<unknown, unknown> | Promise<import("../cache/axios").CacheRequestConfig<unknown, unknown>>; | ||
onFulfilled: (value: import("../cache/axios").InternalCacheRequestConfig<unknown, unknown>) => import("../cache/axios").InternalCacheRequestConfig<unknown, unknown> | Promise<import("../cache/axios").InternalCacheRequestConfig<unknown, unknown>>; | ||
apply: () => number; | ||
}; | ||
//# sourceMappingURL=request.d.ts.map |
@@ -1,6 +0,6 @@ | ||
import type { CacheRequestConfig } from '../cache/axios'; | ||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { MaybePromise } from '../util/types'; | ||
export type CachedResponse = { | ||
data?: unknown; | ||
headers: Record<string, string | undefined>; | ||
headers: CacheAxiosResponse['headers']; | ||
status: number; | ||
@@ -7,0 +7,0 @@ statusText: string; |
@@ -15,3 +15,3 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
*/ | ||
containsHeaders?: Record<string, (header?: string) => MaybePromise<boolean>>; | ||
containsHeaders?: Record<string, (header?: CacheAxiosResponse['headers'][string]) => MaybePromise<boolean>>; | ||
/** Check if the response matches this predicate. */ | ||
@@ -18,0 +18,0 @@ responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>; |
{ | ||
"name": "axios-cache-interceptor", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Cache interceptor for axios", | ||
@@ -62,8 +62,8 @@ "license": "MIT", | ||
"@types/webpack": "^5.28.0", | ||
"@typescript-eslint/eslint-plugin": "^5.34.0", | ||
"@typescript-eslint/parser": "^5.14.0", | ||
"@typescript-eslint/eslint-plugin": "^5.50.0", | ||
"@typescript-eslint/parser": "^5.50.0", | ||
"auto-changelog": "^2.4.0", | ||
"axios": "^1.0.0", | ||
"axios": "^1.3.1", | ||
"es-check": "^7.0.0", | ||
"eslint": "^8.15.0", | ||
"eslint": "^8.33.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
@@ -79,4 +79,4 @@ "eslint-plugin-prettier": "^4.0.0", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.5.4", | ||
"vitepress": "^1.0.0-alpha.29", | ||
"typescript": "^4.9.5", | ||
"vitepress": "^1.0.0-alpha.45", | ||
"webpack": "^5.66.0", | ||
@@ -83,0 +83,0 @@ "webpack-cli": "^5.0.1" |
@@ -6,3 +6,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
AxiosRequestConfig, | ||
AxiosResponse | ||
AxiosResponse, | ||
AxiosResponseHeaders, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
InternalAxiosRequestConfig | ||
} from 'axios'; | ||
@@ -48,2 +51,7 @@ import type { CacheInstance, CacheProperties } from './cache'; | ||
/** Cached version of type {@link InternalAxiosRequestConfig} */ | ||
export type InternalCacheRequestConfig<R = any, D = any> = CacheRequestConfig<R, D> & { | ||
headers: AxiosResponseHeaders; | ||
}; | ||
/** | ||
@@ -84,3 +92,3 @@ * Same as the AxiosInstance but with CacheRequestConfig as a config type and | ||
interceptors: { | ||
request: AxiosInterceptorManager<CacheRequestConfig>; | ||
request: AxiosInterceptorManager<InternalCacheRequestConfig>; | ||
response: AxiosInterceptorManager<CacheAxiosResponse>; | ||
@@ -87,0 +95,0 @@ }; |
@@ -55,2 +55,6 @@ import type { AxiosInstance } from 'axios'; | ||
if (axiosCache.defaults.cache) { | ||
throw new Error('setupCache() should be called only once'); | ||
} | ||
axiosCache.storage = options.storage || buildMemoryStorage(); | ||
@@ -57,0 +61,0 @@ |
@@ -8,3 +8,3 @@ import { parse } from 'cache-parser'; | ||
const cacheControl = headers[Header.CacheControl]; | ||
const cacheControl: unknown = headers[Header.CacheControl]; | ||
@@ -33,3 +33,3 @@ if (cacheControl) { | ||
if (maxAge !== undefined) { | ||
const age = headers[Header.Age]; | ||
const age: unknown = headers[Header.Age]; | ||
@@ -44,3 +44,3 @@ if (!age) { | ||
const expires = headers[Header.Expires]; | ||
const expires: unknown = headers[Header.Expires]; | ||
@@ -47,0 +47,0 @@ if (expires) { |
import type { AxiosRequestHeaders } from 'axios'; | ||
import type { CacheAxiosResponse } from '../cache/axios'; | ||
@@ -14,3 +15,3 @@ export type InterpreterResult = 'dont cache' | 'not enough headers' | number; | ||
export type HeadersInterpreter = ( | ||
headers?: Record<string, string | undefined> | ||
headers?: CacheAxiosResponse['headers'] | ||
) => InterpreterResult; | ||
@@ -17,0 +18,0 @@ |
@@ -1,2 +0,2 @@ | ||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { CacheAxiosResponse, InternalCacheRequestConfig } from '../cache/axios'; | ||
@@ -13,3 +13,5 @@ /** See {@link AxiosInterceptorManager} */ | ||
export type RequestInterceptor = AxiosInterceptor<CacheRequestConfig<unknown, unknown>>; | ||
export type RequestInterceptor = AxiosInterceptor< | ||
InternalCacheRequestConfig<unknown, unknown> | ||
>; | ||
export type ResponseInterceptor = AxiosInterceptor<CacheAxiosResponse<unknown, unknown>>; |
@@ -40,3 +40,2 @@ import { deferred } from 'fast-defer'; | ||
if (config.cache.cacheTakeover) { | ||
config.headers ??= {}; | ||
config.headers[Header.CacheControl] ??= 'no-cache'; | ||
@@ -43,0 +42,0 @@ config.headers[Header.Pragma] ??= 'no-cache'; |
@@ -0,1 +1,2 @@ | ||
import type { AxiosResponseHeaders } from 'axios'; | ||
import type { | ||
@@ -12,3 +13,3 @@ AxiosCacheInstance, | ||
import type { ResponseInterceptor } from './build'; | ||
import { createCacheResponse } from './util'; | ||
import { createCacheResponse, isMethodIn } from './util'; | ||
@@ -49,4 +50,5 @@ export function defaultResponseInterceptor( | ||
const config = response.config; | ||
// Request interceptor merges defaults with per request configuration | ||
const cacheConfig = response.config.cache as CacheProperties; | ||
const cacheConfig = config.cache as CacheProperties; | ||
@@ -67,10 +69,21 @@ // Skip cache: either false or weird behavior | ||
const config = response.config; | ||
const cache = await axios.storage.get(id, config); | ||
// Update other entries before updating himself | ||
if (cacheConfig?.update) { | ||
if (cacheConfig.update) { | ||
await updateCache(axios.storage, response, cacheConfig.update); | ||
} | ||
if (!isMethodIn(config.method, cacheConfig.methods)) { | ||
if (__ACI_DEV__) { | ||
axios.debug?.({ | ||
id, | ||
msg: `Ignored because method (${config.method}) is not in cache.methods (${cacheConfig.methods})`, | ||
data: { config, cacheConfig } | ||
}); | ||
} | ||
return response; | ||
} | ||
const cache = await axios.storage.get(id, config); | ||
if ( | ||
@@ -129,3 +142,3 @@ // If the request interceptor had a problem or it wasn't cached | ||
if (cacheConfig?.interpretHeader) { | ||
if (cacheConfig.interpretHeader) { | ||
const expirationTime = axios.headerInterpreter(response.headers); | ||
@@ -208,6 +221,8 @@ | ||
const onRejected: ResponseInterceptor['onRejected'] = async (error) => { | ||
const config = error.config as CacheRequestConfig; | ||
const config = error.config as CacheRequestConfig & { headers: AxiosResponseHeaders }; | ||
const id = config.id; | ||
const cacheConfig = config.cache as CacheProperties; | ||
// config.cache should always exists, at least from global config merge. | ||
if (!config?.cache || !config.id) { | ||
// config.cache should always exist, at least from global config merge. | ||
if (!cacheConfig || !id) { | ||
if (__ACI_DEV__) { | ||
@@ -223,5 +238,16 @@ axios.debug?.({ | ||
const cache = await axios.storage.get(config.id, config); | ||
const cacheConfig = config.cache; | ||
if (!isMethodIn(config.method, cacheConfig.methods)) { | ||
if (__ACI_DEV__) { | ||
axios.debug?.({ | ||
id, | ||
msg: `Ignored because method (${config.method}) is not in cache.methods (${cacheConfig.methods})`, | ||
data: { config, cacheConfig } | ||
}); | ||
} | ||
throw error; | ||
} | ||
const cache = await axios.storage.get(id, config); | ||
if ( | ||
@@ -232,6 +258,7 @@ // This will only not be loading if the interceptor broke | ||
) { | ||
await rejectResponse(config.id, config); | ||
await rejectResponse(id, config); | ||
if (__ACI_DEV__) { | ||
axios.debug?.({ | ||
id, | ||
msg: 'Caught an error in the request interceptor', | ||
@@ -245,3 +272,3 @@ data: { error, config } | ||
if (cacheConfig?.staleIfError) { | ||
if (cacheConfig.staleIfError) { | ||
const staleIfError = | ||
@@ -258,2 +285,3 @@ typeof cacheConfig.staleIfError === 'function' | ||
axios.debug?.({ | ||
id, | ||
msg: 'Found cache if stale config for rejected response', | ||
@@ -270,8 +298,8 @@ data: { error, config, staleIfError } | ||
// Resolve all other requests waiting for this response | ||
axios.waiting[config.id]?.resolve(cache.data); | ||
delete axios.waiting[config.id]; | ||
axios.waiting[id]?.resolve(cache.data); | ||
delete axios.waiting[id]; | ||
// re-mark the cache as stale | ||
await axios.storage.set( | ||
config.id, | ||
id, | ||
{ | ||
@@ -287,2 +315,3 @@ state: 'stale', | ||
axios.debug?.({ | ||
id, | ||
msg: 'staleIfError resolved this response with cached data', | ||
@@ -296,3 +325,3 @@ data: { error, config, cache } | ||
config, | ||
id: config.id, | ||
id, | ||
data: cache.data.data, | ||
@@ -308,2 +337,3 @@ headers: cache.data.headers, | ||
axios.debug?.({ | ||
id, | ||
msg: 'Received an unknown error that could not be handled', | ||
@@ -310,0 +340,0 @@ data: { error, config } |
@@ -45,4 +45,8 @@ import type { Method } from 'axios'; | ||
if (etag) { | ||
const etagValue = etag === true ? cache.data?.headers[Header.ETag] : etag; | ||
etagValue && (config.headers[Header.IfNoneMatch] = etagValue); | ||
const etagValue = | ||
etag === true ? (cache.data?.headers[Header.ETag] as unknown) : etag; | ||
if (etagValue) { | ||
config.headers[Header.IfNoneMatch] = etagValue; | ||
} | ||
} | ||
@@ -54,3 +58,3 @@ | ||
? // If last-modified is not present, use the createdAt timestamp | ||
cache.data.headers[Header.LastModified] || | ||
(cache.data.headers[Header.LastModified] as unknown) || | ||
new Date(cache.createdAt).toUTCString() | ||
@@ -57,0 +61,0 @@ : modifiedSince.toUTCString(); |
@@ -1,2 +0,2 @@ | ||
import type { CacheRequestConfig } from '../cache/axios'; | ||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import type { MaybePromise } from '../util/types'; | ||
@@ -6,3 +6,3 @@ | ||
data?: unknown; | ||
headers: Record<string, string | undefined>; | ||
headers: CacheAxiosResponse['headers']; | ||
status: number; | ||
@@ -9,0 +9,0 @@ statusText: string; |
import type { CacheAxiosResponse } from '../cache/axios'; | ||
import type { CachePredicate } from './types'; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import type { CachePredicate, CachePredicateObject } from './types'; | ||
@@ -27,2 +28,5 @@ /** Tests an response against a {@link CachePredicateObject}. */ | ||
// Axios response headers are in lowercase, but check both just in case. | ||
// FIXME: https://github.com/axios/axios/pull/5525 | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
response.headers[header.toLowerCase()] ?? response.headers[header] | ||
@@ -29,0 +33,0 @@ )) |
@@ -25,3 +25,6 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
*/ | ||
containsHeaders?: Record<string, (header?: string) => MaybePromise<boolean>>; | ||
containsHeaders?: Record< | ||
string, | ||
(header?: CacheAxiosResponse['headers'][string]) => MaybePromise<boolean> | ||
>; | ||
@@ -28,0 +31,0 @@ /** Check if the response matches this predicate. */ |
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
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
369650
2763