axios-cache-interceptor
Advanced tools
Comparing version 0.10.4 to 0.10.5
@@ -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",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,i,s,f,g,h;const p=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 is false",data:t}),t;if(t.cache=Object.assign(Object.assign({},e.defaults.cache),t.cache),!u(t.method,t.cache.methods))return null===(r=e.debug)||void 0===r||r.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let m,v=await e.storage.get(p,t);e:if("empty"===v.state||"stale"===v.state){if(e.waiting[p]&&(v=await e.storage.get(p,t),"empty"!==v.state)){null===(n=e.debug)||void 0===n||n.call(e,{id:p,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[p]=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}(),null===(o=e.waiting[p])||void 0===o||o.catch((()=>{})),await e.storage.set(p,{state:"loading",previous:v.state,data:v.data,createdAt:v.createdAt},t),"stale"===v.state&&(l(v,t),null===(i=e.debug)||void 0===i||i.call(e,{id:p,msg:"Updated stale request"})),t.validateStatus=d(t.validateStatus),null===(s=e.debug)||void 0===s||s.call(e,{id:p,msg:"Sending request, waiting for response"}),t}if("loading"===v.state){const a=e.waiting[p];if(!a)return await e.storage.remove(p,t),t;null===(f=e.debug)||void 0===f||f.call(e,{id:p,msg:"Detected concurrent request, waiting for it to finish"});try{m=await a}catch(a){return null===(g=e.debug)||void 0===g||g.call(e,{id:p,msg:"Deferred rejected, requesting again",data:a}),t}}else m=v.data;return t.adapter=()=>Promise.resolve({config:t,data:m.data,headers:m.headers,status:m.status,statusText:m.statusText,cached:!0,id:p}),null===(h=e.debug)||void 0===h||h.call(e,{id:p,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){for(const r in a){const n=a[r];if("delete"===n){await e.remove(r,t.config);continue}const o=await e.get(r,t.config);if("loading"===o.state)continue;const i=await n(o,t);"delete"!==i?"ignore"!==i&&await e.set(r,i,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 === false",data:a}),Object.assign(Object.assign({},a),{cached:!1});const w=a.config,x=await e.storage.get(b,w);if("loading"!==x.state)return null===(c=e.debug)||void 0===c||c.call(e,{id:b,msg:"Response not cached but storage is not 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 in i)e.startsWith("XAxiosCache")&&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 I=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,ttl:S,cacheResponse:I}}),(null==y?void 0:y.update)&&await p(e.storage,a,y.update);const C={state:"cached",ttl:S,createdAt:Date.now(),data:I},A=e.waiting[b];return A&&(A.resolve(C.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,C,w),null===(m=e.debug)||void 0===m||m.call(e,{id:b,msg:"Response cached",data:{cache:C,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 I(e){e=S(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const C=/^\/|\/$/g;function A(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${I(a)}`}}const j=A((({baseURL:e="",url:t="",method:a="get",params:r,data:n})=>(e&&(e=e.replace(C,"")),t&&(t=t.replace(C,"")),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;const c=e;if(c.storage=t.storage||x(),!v(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||j,c.headerInterpreter=t.headerInterpreter||s,c.requestInterceptor=t.requestInterceptor||g(c),c.responseInterceptor=t.responseInterceptor||m(c),c.debug=t.debug,c.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,staleIfError:null===(i=t.staleIfError)||void 0===i||i},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function O(e,t=""){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:()=>E,canStale:()=>b,createCacheResponse:()=>f,createValidateStatus:()=>d,defaultHeaderInterpreter:()=>s,defaultKeyGenerator:()=>j,defaultRequestInterceptor:()=>g,defaultResponseInterceptor:()=>m,isExpired:()=>y,isMethodIn:()=>u,isStorage:()=>v,setupCache:()=>O,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",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,i,s,f,g,h;const p=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),!u(t.method,t.cache.methods))return null===(r=e.debug)||void 0===r||r.call(e,{msg:`Ignored because method (${t.method}) is not in cache.methods (${t.cache.methods})`}),t;let m=await e.storage.get(p,t);const v=t.cache.override;e:if("empty"===m.state||"stale"===m.state||v){if(e.waiting[p]&&!v&&(m=await e.storage.get(p,t),"empty"!==m.state)){null===(n=e.debug)||void 0===n||n.call(e,{id:p,msg:"Waiting list had an deferred for this key, waiting for it to finish"});break e}return e.waiting[p]=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}(),null===(o=e.waiting[p])||void 0===o||o.catch((()=>{})),await e.storage.set(p,{state:"loading",previous:v?m.data?"stale":"empty":m.state,data:m.data,createdAt:v&&!m.createdAt?Date.now():m.createdAt},t),"stale"===m.state&&(l(m,t),null===(i=e.debug)||void 0===i||i.call(e,{id:p,msg:"Updated stale request"})),t.validateStatus=d(t.validateStatus),null===(s=e.debug)||void 0===s||s.call(e,{id:p,msg:"Sending request, waiting for response",data:{overrideCache:v,state:m.state}}),t}let b;if("loading"===m.state){const a=e.waiting[p];if(!a)return await e.storage.remove(p,t),t;null===(f=e.debug)||void 0===f||f.call(e,{id:p,msg:"Detected concurrent request, waiting for it to finish"});try{b=await a}catch(a){return null===(g=e.debug)||void 0===g||g.call(e,{id:p,msg:"Deferred rejected, requesting again",data:a}),t}}else b=m.data;return t.adapter=()=>Promise.resolve({config:t,data:b.data,headers:b.headers,status:b.status,statusText:b.statusText,cached:!0,id:p}),null===(h=e.debug)||void 0===h||h.call(e,{id:p,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){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("loading"!==x.state)return null===(c=e.debug)||void 0===c||c.call(e,{id:b,msg:"Response not cached but storage is not 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 I=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:I}}),(null==y?void 0:y.update)&&await p(e.storage,a,y.update);const C={state:"cached",ttl:S,createdAt:Date.now(),data:I},A=e.waiting[b];return A&&(A.resolve(C.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,C,w),null===(m=e.debug)||void 0===m||m.call(e,{id:b,msg:"Response cached",data:{cache:C,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 I(e){e=S(e);for(var t=5381,a=0;a<e.length;)t=33*t^e.charCodeAt(a++);return t}const C=/^\/|\/$/g;function A(e){return t=>{if(t.id)return t.id;const a=e(t);return"string"==typeof a||"number"==typeof a?`${a}`:`${I(a)}`}}const j=A((({baseURL:e="",url:t="",method:a="get",params:r,data:n})=>(e&&(e=e.replace(C,"")),t&&(t=t.replace(C,"")),a&&(a=a.toLowerCase()),{url:e+(e&&t?"/":"")+t,params:r,method:a,data:n})));function O(e,t={}){var a,r,n,o,i;const c=e;if(c.storage=t.storage||x(),!v(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||j,c.headerInterpreter=t.headerInterpreter||s,c.requestInterceptor=t.requestInterceptor||g(c),c.responseInterceptor=t.responseInterceptor||m(c),c.debug=t.debug,c.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,staleIfError:null===(i=t.staleIfError)||void 0===i||i,override:!1},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function E(e,t=""){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 |
@@ -99,2 +99,13 @@ import type { Method } from 'axios'; | ||
staleIfError: StaleIfErrorPredicate<R, D>; | ||
/** | ||
* This option bypasses the current cache and always make a new http request. This will | ||
* not delete the current cache, it will just replace the cache when the response arrives. | ||
* | ||
* Unlike as `cache: false`, this will not disable the cache, it will just ignore the | ||
* pre-request cache checks before making the request. This way, all post-request | ||
* options are still available and will work as expected. | ||
* | ||
* @default false | ||
*/ | ||
override: boolean; | ||
}; | ||
@@ -101,0 +112,0 @@ export interface CacheInstance { |
@@ -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 l},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return T},defaultRequestInterceptor:function(){return g},defaultResponseInterceptor:function(){return y},isExpired:function(){return S},isMethodIn:function(){return h},isStorage:function(){return x},setupCache:function(){return M},testCachePredicate:function(){return m},updateCache:function(){return b},updateStaleRequest:function(){return v}});var r=Symbol("cache-parser");function n(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function a(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),s=function(e){if(!e)return"not enough headers";var t=e[i.CacheControl];if(t){var s=function(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var n in r){var a,o=r[n].split("=",2);t[o[0]]=null==(a=o[1])||a}return t}(e),s=i["max-age"],c=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],f=i["stale-if-error"],l=i["stale-while-revalidate"];return a(i.immutable)&&(t.immutable=!0),n(s)&&(t.maxAge=o(s)),n(c)&&(t.maxStale=o(c)),n(u)&&(t.minFresh=o(u)),a(i["must-revalidate"])&&(t.mustRevalidate=!0),a(i["must-understand"])&&(t.mustUnderstand=!0),a(i["no-cache"])&&(t.noCache=!0),a(i["no-store"])&&(t.noStore=!0),a(i["no-transform"])&&(t.noTransform=!0),a(i["only-if-cached"])&&(t.onlyIfCached=!0),a(i.private)&&(t.private=!0),a(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),a(i.public)&&(t.public=!0),n(d)&&(t.sMaxAge=o(d)),n(f)&&(t.staleIfError=o(f)),n(l)&&(t.staleWhileRevalidate=o(l)),t}(String(t)),c=s.noCache,u=s.noStore,d=s.mustRevalidate,f=s.maxAge,l=s.immutable;if(c||u)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==f){var h=e[i.Age];return h?1e3*(f-Number(h)):1e3*f}}var v=e[i.Expires];if(v){var p=Date.parse(String(v))-Date.now();return p>=0?p:"dont cache"}return"not enough headers"};var c=function(){return c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},c.apply(this,arguments)};function u(e,t,r,n){return new(r||(r=Promise))((function(a,o){function i(e){try{c(n.next(e))}catch(e){o(e)}}function s(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,s)}c((n=n.apply(e,t||[])).next())}))}function d(e,t){var r,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(a=2&o[0]?n.return:o[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,o[1])).done)return a;switch(n=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,n=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(e,i)}catch(e){o=[6,e],n=0}finally{r=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}Object.create;Object.create;var f=Symbol();function l(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function h(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function v(e,t){var r;t.headers||(t.headers={});var n=t.cache,a=n.etag,o=n.modifiedSince;if(a){var s=!0===a?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:a;s&&(t.headers[i.IfNoneMatch]=s)}o&&(t.headers[i.IfModifiedSince]=!0===o?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function p(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=c(c({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){var t=this,r=function(r){return u(t,void 0,void 0,(function(){var t,n,a,o,i;return d(this,(function(s){switch(s.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=c(c({},e.defaults.cache),r.cache),h(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return"empty"!==(n=s.sent()).state&&"stale"!==n.state?[3,5]:e.waiting[t]?[4,e.storage.get(t,r)]:[3,3];case 2:if("empty"!==(n=s.sent()).state)return[3,5];s.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,n){e=r,t=n}));return r.resolve=e,r.reject=t,r[f]=1,r}(),null===(i=e.waiting[t])||void 0===i||i.catch((function(){})),[4,e.storage.set(t,{state:"loading",previous:n.state,data:n.data,createdAt:n.createdAt},r)];case 4:return s.sent(),"stale"===n.state&&v(n,r),r.validateStatus=l(r.validateStatus),[2,r];case 5:return"loading"!==n.state?[3,12]:(o=e.waiting[t])?[3,7]:[4,e.storage.remove(t,r)];case 6:return s.sent(),[2,r];case 7:0,s.label=8;case 8:return s.trys.push([8,10,,11]),[4,o];case 9:return a=s.sent(),[3,11];case 10:return s.sent(),[2,r];case 11:return[3,13];case 12:a=n.data,s.label=13;case 13:return r.adapter=function(){return Promise.resolve({config:r,data:a.data,headers:a.headers,status:a.status,statusText:a.statusText,cached:!0,id:t})},[2,r]}}))}))};return{onFulfilled:r,apply:function(){return e.interceptors.request.use(r)}}}function m(e,t){var r;return u(this,void 0,void 0,(function(){var n,a,o,i,s,c,u,f,l,h;return d(this,(function(d){switch(d.label){case 0:return"function"==typeof t?[2,t(e)]:(n=t.statusCheck,a=t.responseMatch,o=t.containsHeaders,(s=n)?[4,n(e.status)]:[3,2]);case 1:s=!d.sent(),d.label=2;case 2:return(i=s)?[3,5]:(c=a)?[4,a(e)]:[3,4];case 3:c=!d.sent(),d.label=4;case 4:i=c,d.label=5;case 5:if(i)return[2,!1];if(!o)return[3,9];u=0,f=Object.entries(o),d.label=6;case 6:return u<f.length?(l=f[u],h=l[0],[4,(0,l[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!d.sent())return[2,!1];d.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function b(e,t,r){return u(this,void 0,void 0,(function(){var n,a,o,i,s,c,u;return d(this,(function(d){switch(d.label){case 0:for(a in n=[],r)n.push(a);o=0,d.label=1;case 1:return o<n.length?(i=n[o],"delete"!==(s=r[i])?[3,3]:[4,e.remove(i,t.config)]):[3,10];case 2:return d.sent(),[3,9];case 3:return[4,e.get(i,t.config)];case 4:return"loading"===(c=d.sent()).state?[3,9]:[4,s(c,t)];case 5:return"delete"!==(u=d.sent())?[3,7]:[4,e.remove(i,t.config)];case 6:return d.sent(),[3,9];case 7:return"ignore"===u?[3,9]:[4,e.set(i,u,t.config)];case 8:d.sent(),d.label=9;case 9:return o++,[3,1];case 10:return[2]}}))}))}function y(e){var t=this,r=function(r,n){return u(t,void 0,void 0,(function(){var t;return d(this,(function(a){switch(a.label){case 0:return[4,e.storage.remove(r,n)];case 1:return a.sent(),null===(t=e.waiting[r])||void 0===t||t.reject(),delete e.waiting[r],[2]}}))}))},n=function(n){return u(t,void 0,void 0,(function(){var t,a,o,s,u,f,l,h,v,g,y,x,w,S;return d(this,(function(d){switch(d.label){case 0:return t=n.id=null!==(x=(S=n.config).id)&&void 0!==x?x:S.id=e.generateKey(n.config),null!==(w=n.cached)&&void 0!==w||(n.cached=!1),n.cached?[2,n]:(a=n.config.cache)?(o=n.config,[4,e.storage.get(t,o)]):[2,c(c({},n),{cached:!1})];case 1:return"loading"!==(s=d.sent()).state?[2,n]:(u=!s.data)?[4,m(n,a.cachePredicate)]:[3,3];case 2:u=!d.sent(),d.label=3;case 3:return u?[4,r(t,o)]:[3,5];case 4:return d.sent(),[2,n];case 5:for(f in i)f.startsWith("XAxiosCache")&&delete n.headers[f];return a.etag&&!0!==a.etag&&(n.headers[i.XAxiosCacheEtag]=a.etag),a.modifiedSince&&(n.headers[i.XAxiosCacheLastModified]=!0===a.modifiedSince?"use-cache-timestamp":a.modifiedSince.toUTCString()),l=a.ttl||-1,(null==a?void 0:a.interpretHeader)?"dont cache"!==(h=e.headerInterpreter(n.headers))?[3,7]:[4,r(t,o)]:[3,8];case 6:return d.sent(),[2,n];case 7:l="not enough headers"===h?l:h,d.label=8;case 8:return v=p(n,s.data),"function"!=typeof l?[3,10]:[4,l(n)];case 9:l=d.sent(),d.label=10;case 10:return a.staleIfError&&(n.headers[i.XAxiosCacheStaleIfError]=String(l)),(null==a?void 0:a.update)?[4,b(e.storage,n,a.update)]:[3,12];case 11:d.sent(),d.label=12;case 12:return g={state:"cached",ttl:l,createdAt:Date.now(),data:v},(y=e.waiting[t])&&(y.resolve(g.data),delete e.waiting[t]),[4,e.storage.set(t,g,o)];case 13:return d.sent(),[2,n]}}))}))},a=function(n){return u(t,void 0,void 0,(function(){var t,a,o,i,s,c;return d(this,(function(u){switch(u.label){case 0:if(!(null==(t=n.config)?void 0:t.cache)||!t.id)throw n;return[4,e.storage.get(t.id,t)];case 1:return a=u.sent(),o=t.cache,"loading"===a.state&&"stale"===a.previous?[3,3]:[4,r(t.id,t)];case 2:throw u.sent(),n;case 3:return(null==o?void 0:o.staleIfError)?"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(n.response,a,n)]:[3,8];case 4:return s=u.sent(),[3,6];case 5:s=o.staleIfError,u.label=6;case 6:return!0===(i=s)||"number"==typeof i&&a.createdAt+i>Date.now()?(null===(c=e.waiting[t.id])||void 0===c||c.resolve(a.data),delete e.waiting[t.id],[4,e.storage.set(t.id,{state:"stale",createdAt:Date.now(),data:a.data},t)]):[3,8];case 7:return u.sent(),[2,{cached:!0,config:t,id:t.id,data:a.data.data,headers:a.data.headers,status:a.data.status,statusText:a.data.statusText}];case 8:throw n}}))}))};return{onFulfilled:n,onRejected:a,apply:function(){return e.interceptors.response.use(n,a)}}}var x=function(e){return!!e&&!!e["is-storage"]};function w(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function S(e){return e.createdAt+e.ttl<=Date.now()}function I(e){var t,r=this,n=e.set,a=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=n,t.remove=o,t.get=function(e,t){return u(r,void 0,void 0,(function(){var r,i;return d(this,(function(s){switch(s.label){case 0:return[4,a(e,t)];case 1:return(r=s.sent())?"cached"===r.state&&S(r)?w(r)?(i={state:"stale",createdAt:r.createdAt,data:r.data},[4,n(e,i,t)]):[3,3]:[2,r]:[2,{state:"empty"}];case 2:return s.sent(),[2,i];case 3:return[4,o(e,t)];case 4:return s.sent(),[2,{state:"empty"}]}}))}))},t}function C(e){void 0===e&&(e=!1);var t=I({set:function(e,r){t.data[e]=r},remove:function(e){delete t.data[e]},find:function(r){var n=t.data[r];return e&&void 0!==n?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n}});return t.data=Object.create(null),t}function A(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},n=Object.keys(e).sort((function(e,t){return e>t?1:-1})),a=n.length;a--;){var o=n[a];r[o]=A(e[o])}return String(e.constructor)+JSON.stringify(r,n)}return t+String(e)}function E(e){e=A(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var O=/^\/|\/$/g;function j(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:E(r))}}var T=j((function(e){var t=e.baseURL,r=void 0===t?"":t,n=e.url,a=void 0===n?"":n,o=e.method,i=void 0===o?"get":o,s=e.params,c=e.data;return r&&(r=r.replace(O,"")),a&&(a=a.replace(O,"")),i&&(i=i.toLowerCase()),{url:r+(r&&a?"/":"")+a,params:s,method:i,data:c}}));function M(e,t){var r,n,a,o,i;void 0===t&&(t={});var c=e;if(c.storage=t.storage||C(),!x(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||T,c.headerInterpreter=t.headerInterpreter||s,c.requestInterceptor=t.requestInterceptor||g(c),c.responseInterceptor=t.responseInterceptor||y(c),c.debug=t.debug,c.defaults.cache={update:t.update||{},ttl:null!==(r=t.ttl)&&void 0!==r?r:3e5,methods:t.methods||["get"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return e>=200&&e<400}},etag:null===(n=t.etag)||void 0===n||n,modifiedSince:null!==(a=t.modifiedSince)&&void 0!==a?a:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,staleIfError:null===(i=t.staleIfError)||void 0===i||i},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function P(e,t){return void 0===t&&(t=""),I({find:function(r){var n=e.getItem(t+r);return n?JSON.parse(n):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,n){var a=function(){return e.setItem(t+r,JSON.stringify(n))};try{return a()}catch(n){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),i=0,s=o;i<s.length;i++){var c=s[i];"cached"===c[1].state&&S(c[1])&&!w(c[1])&&e.removeItem(c[0])}try{return a()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,f=u;d<f.length;d++){var l=f[d];e.removeItem(l[0]);try{return a()}catch(e){}}}e.removeItem(t+r)}}})}return t}()})); | ||
!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 E},buildMemoryStorage:function(){return C},buildStorage:function(){return I},buildWebStorage:function(){return P},canStale:function(){return w},createCacheResponse:function(){return p},createValidateStatus:function(){return l},defaultHeaderInterpreter:function(){return s},defaultKeyGenerator:function(){return T},defaultRequestInterceptor:function(){return g},defaultResponseInterceptor:function(){return y},isExpired:function(){return S},isMethodIn:function(){return h},isStorage:function(){return x},setupCache:function(){return M},testCachePredicate:function(){return m},updateCache:function(){return b},updateStaleRequest:function(){return v}});var r=Symbol("cache-parser");function n(e){return("string"==typeof e||"number"==typeof e)&&(e=Number(e))>=0&&e<1/0}function a(e){return!0===e||"number"==typeof e||"string"==typeof e&&"false"!==e}var o=Number;var i=Object.freeze({IfModifiedSince:"if-modified-since",LastModified:"last-modified",IfNoneMatch:"if-none-match",CacheControl:"cache-control",ETag:"etag",Expires:"expires",Age:"age",XAxiosCacheEtag:"x-axios-cache-etag",XAxiosCacheLastModified:"x-axios-cache-last-modified",XAxiosCacheStaleIfError:"x-axios-cache-stale-if-error"}),s=function(e){if(!e)return"not enough headers";var t=e[i.CacheControl];if(t){var s=function(e){var t=Object.defineProperty({},r,{enumerable:!1,value:1});if(!e||"string"!=typeof e)return t;var i=function(e){var t={},r=e.toLowerCase().replace(/\s+/g,"").split(",");for(var n in r){var a,o=r[n].split("=",2);t[o[0]]=null==(a=o[1])||a}return t}(e),s=i["max-age"],c=i["max-stale"],u=i["min-fresh"],d=i["s-maxage"],f=i["stale-if-error"],l=i["stale-while-revalidate"];return a(i.immutable)&&(t.immutable=!0),n(s)&&(t.maxAge=o(s)),n(c)&&(t.maxStale=o(c)),n(u)&&(t.minFresh=o(u)),a(i["must-revalidate"])&&(t.mustRevalidate=!0),a(i["must-understand"])&&(t.mustUnderstand=!0),a(i["no-cache"])&&(t.noCache=!0),a(i["no-store"])&&(t.noStore=!0),a(i["no-transform"])&&(t.noTransform=!0),a(i["only-if-cached"])&&(t.onlyIfCached=!0),a(i.private)&&(t.private=!0),a(i["proxy-revalidate"])&&(t.proxyRevalidate=!0),a(i.public)&&(t.public=!0),n(d)&&(t.sMaxAge=o(d)),n(f)&&(t.staleIfError=o(f)),n(l)&&(t.staleWhileRevalidate=o(l)),t}(String(t)),c=s.noCache,u=s.noStore,d=s.mustRevalidate,f=s.maxAge,l=s.immutable;if(c||u)return"dont cache";if(l)return 31536e6;if(d)return 0;if(void 0!==f){var h=e[i.Age];return h?1e3*(f-Number(h)):1e3*f}}var v=e[i.Expires];if(v){var p=Date.parse(String(v))-Date.now();return p>=0?p:"dont cache"}return"not enough headers"};var c=function(){return c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},c.apply(this,arguments)};function u(e,t,r,n){return new(r||(r=Promise))((function(a,o){function i(e){try{c(n.next(e))}catch(e){o(e)}}function s(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,s)}c((n=n.apply(e,t||[])).next())}))}function d(e,t){var r,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(a=2&o[0]?n.return:o[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,o[1])).done)return a;switch(n=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,n=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(e,i)}catch(e){o=[6,e],n=0}finally{r=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}Object.create;Object.create;var f=Symbol();function l(e){return e?function(t){return e(t)||304===t}:function(e){return e>=200&&e<300||304===e}}function h(e,t){return void 0===e&&(e="get"),void 0===t&&(t=[]),e=e.toLowerCase(),t.some((function(t){return t===e}))}function v(e,t){var r;t.headers||(t.headers={});var n=t.cache,a=n.etag,o=n.modifiedSince;if(a){var s=!0===a?null===(r=e.data)||void 0===r?void 0:r.headers[i.ETag]:a;s&&(t.headers[i.IfNoneMatch]=s)}o&&(t.headers[i.IfModifiedSince]=!0===o?e.data.headers[i.LastModified]||new Date(e.createdAt).toUTCString():o.toUTCString())}function p(e,t){return 304===e.status&&t?(e.cached=!0,e.data=t.data,e.status=t.status,e.statusText=t.statusText,e.headers=c(c({},t.headers),e.headers),t):{data:e.data,status:e.status,statusText:e.statusText,headers:e.headers}}function g(e){var t=this,r=function(r){return u(t,void 0,void 0,(function(){var t,n,a,o,i,s;return d(this,(function(u){switch(u.label){case 0:return t=r.id=e.generateKey(r),!1===r.cache?[2,r]:(r.cache=c(c({},e.defaults.cache),r.cache),h(r.method,r.cache.methods)?[4,e.storage.get(t,r)]:[2,r]);case 1:return n=u.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=u.sent()).state)return[3,5];u.label=3;case 3:return e.waiting[t]=function(){var e,t,r=new Promise((function(r,n){e=r,t=n}));return r.resolve=e,r.reject=t,r[f]=1,r}(),null===(s=e.waiting[t])||void 0===s||s.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 u.sent(),"stale"===n.state&&v(n,r),r.validateStatus=l(r.validateStatus),[2,r];case 5:return"loading"!==n.state?[3,12]:(i=e.waiting[t])?[3,7]:[4,e.storage.remove(t,r)];case 6:return u.sent(),[2,r];case 7:0,u.label=8;case 8:return u.trys.push([8,10,,11]),[4,i];case 9:return o=u.sent(),[3,11];case 10:return u.sent(),[2,r];case 11:return[3,13];case 12:o=n.data,u.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,s,c,u,f,l,h;return d(this,(function(d){switch(d.label){case 0:return"function"==typeof t?[2,t(e)]:(n=t.statusCheck,a=t.responseMatch,o=t.containsHeaders,(s=n)?[4,n(e.status)]:[3,2]);case 1:s=!d.sent(),d.label=2;case 2:return(i=s)?[3,5]:(c=a)?[4,a(e)]:[3,4];case 3:c=!d.sent(),d.label=4;case 4:i=c,d.label=5;case 5:if(i)return[2,!1];if(!o)return[3,9];u=0,f=Object.entries(o),d.label=6;case 6:return u<f.length?(l=f[u],h=l[0],[4,(0,l[1])(null!==(r=e.headers[h.toLowerCase()])&&void 0!==r?r:e.headers[h])]):[3,9];case 7:if(!d.sent())return[2,!1];d.label=8;case 8:return u++,[3,6];case 9:return[2,!0]}}))}))}function b(e,t,r){return u(this,void 0,void 0,(function(){var n,a,o,i,s,c,u;return d(this,(function(d){switch(d.label){case 0:n=0,a=Object.entries(r),d.label=1;case 1:return n<a.length?(o=a[n],i=o[0],"delete"!==(s=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"===(c=d.sent()).state?[3,9]:[4,s(c,t)];case 5:return"delete"!==(u=d.sent())?[3,7]:[4,e.remove(i,t.config)];case 6:return d.sent(),[3,9];case 7:return"ignore"===u?[3,9]:[4,e.set(i,u,t.config)];case 8:d.sent(),d.label=9;case 9:return 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,s,u,f,l,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,c(c({},n),{cached:!1})];case 1:return"loading"!==(s=d.sent()).state?[2,n]:(u=!s.data)?[4,m(n,a.cachePredicate)]:[3,3];case 2:u=!d.sent(),d.label=3;case 3:return u?[4,r(t,o)]:[3,5];case 4:return d.sent(),[2,n];case 5:for(f=0,l=Object.keys(n.headers);f<l.length;f++)(h=l[f]).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,7]:[4,r(t,o)]:[3,8];case 6:return d.sent(),[2,n];case 7:v="not enough headers"===g?v:g,d.label=8;case 8:return y=p(n,s.data),"function"!=typeof v?[3,10]:[4,v(n)];case 9:v=d.sent(),d.label=10;case 10:return a.staleIfError&&(n.headers[i.XAxiosCacheStaleIfError]=String(v)),(null==a?void 0:a.update)?[4,b(e.storage,n,a.update)]:[3,12];case 11:d.sent(),d.label=12;case 12:return 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,s,c;return d(this,(function(u){switch(u.label){case 0:if(!(null==(t=n.config)?void 0:t.cache)||!t.id)throw n;return[4,e.storage.get(t.id,t)];case 1:return a=u.sent(),o=t.cache,"loading"===a.state&&"stale"===a.previous?[3,3]:[4,r(t.id,t)];case 2:throw u.sent(),n;case 3:return(null==o?void 0:o.staleIfError)?"function"!=typeof o.staleIfError?[3,5]:[4,o.staleIfError(n.response,a,n)]:[3,8];case 4:return s=u.sent(),[3,6];case 5:s=o.staleIfError,u.label=6;case 6:return!0===(i=s)||"number"==typeof i&&a.createdAt+i>Date.now()?(null===(c=e.waiting[t.id])||void 0===c||c.resolve(a.data),delete e.waiting[t.id],[4,e.storage.set(t.id,{state:"stale",createdAt:Date.now(),data:a.data},t)]):[3,8];case 7:return u.sent(),[2,{cached:!0,config:t,id:t.id,data:a.data.data,headers:a.data.headers,status:a.data.status,statusText:a.data.statusText}];case 8:throw n}}))}))};return{onFulfilled:n,onRejected:a,apply:function(){return e.interceptors.response.use(n,a)}}}var x=function(e){return!!e&&!!e["is-storage"]};function w(e){var t=e.data.headers;return i.ETag in t||i.LastModified in t||i.XAxiosCacheEtag in t||i.XAxiosCacheStaleIfError in t||i.XAxiosCacheLastModified in t}function S(e){return e.createdAt+e.ttl<=Date.now()}function I(e){var t,r=this,n=e.set,a=e.find,o=e.remove;return(t={})["is-storage"]=1,t.set=n,t.remove=o,t.get=function(e,t){return u(r,void 0,void 0,(function(){var r,i;return d(this,(function(s){switch(s.label){case 0:return[4,a(e,t)];case 1:return(r=s.sent())?"cached"===r.state&&S(r)?w(r)?(i={state:"stale",createdAt:r.createdAt,data:r.data},[4,n(e,i,t)]):[3,3]:[2,r]:[2,{state:"empty"}];case 2:return s.sent(),[2,i];case 3:return[4,o(e,t)];case 4:return s.sent(),[2,{state:"empty"}]}}))}))},t}function C(e){void 0===e&&(e=!1);var t=I({set:function(e,r){t.data[e]=r},remove:function(e){delete t.data[e]},find:function(r){var n=t.data[r];return e&&void 0!==n?"function"==typeof structuredClone?structuredClone(n):JSON.parse(JSON.stringify(n)):n}});return t.data=Object.create(null),t}function A(e){var t=typeof e;if(e&&"object"===t&&!(e instanceof Date||e instanceof RegExp)){for(var r=Array.isArray(e)?[]:{},n=Object.keys(e).sort((function(e,t){return e>t?1:-1})),a=n.length;a--;){var o=n[a];r[o]=A(e[o])}return String(e.constructor)+JSON.stringify(r,n)}return t+String(e)}function O(e){e=A(e);for(var t=5381,r=0;r<e.length;)t=33*t^e.charCodeAt(r++);return t}var j=/^\/|\/$/g;function E(e){return function(t){if(t.id)return t.id;var r=e(t);return"".concat("string"==typeof r||"number"==typeof r?r:O(r))}}var T=E((function(e){var t=e.baseURL,r=void 0===t?"":t,n=e.url,a=void 0===n?"":n,o=e.method,i=void 0===o?"get":o,s=e.params,c=e.data;return r&&(r=r.replace(j,"")),a&&(a=a.replace(j,"")),i&&(i=i.toLowerCase()),{url:r+(r&&a?"/":"")+a,params:s,method:i,data:c}}));function M(e,t){var r,n,a,o,i;void 0===t&&(t={});var c=e;if(c.storage=t.storage||C(),!x(c.storage))throw new Error("Use buildStorage() function");return c.waiting=t.waiting||{},c.generateKey=t.generateKey||T,c.headerInterpreter=t.headerInterpreter||s,c.requestInterceptor=t.requestInterceptor||g(c),c.responseInterceptor=t.responseInterceptor||y(c),c.debug=t.debug,c.defaults.cache={update:t.update||{},ttl:null!==(r=t.ttl)&&void 0!==r?r:3e5,methods:t.methods||["get"],cachePredicate:t.cachePredicate||{statusCheck:function(e){return e>=200&&e<400}},etag:null===(n=t.etag)||void 0===n||n,modifiedSince:null!==(a=t.modifiedSince)&&void 0!==a?a:!1===t.etag,interpretHeader:null===(o=t.interpretHeader)||void 0===o||o,staleIfError:null===(i=t.staleIfError)||void 0===i||i,override:!1},c.requestInterceptor.apply(),c.responseInterceptor.apply(),c}function P(e,t){return void 0===t&&(t=""),I({find:function(r){var n=e.getItem(t+r);return n?JSON.parse(n):void 0},remove:function(r){e.removeItem(t+r)},set:function(r,n){var a=function(){return e.setItem(t+r,JSON.stringify(n))};try{return a()}catch(n){for(var o=Object.entries(e).filter((function(e){return e[0].startsWith(t)})).map((function(e){return[e[0],JSON.parse(e[1])]})),i=0,s=o;i<s.length;i++){var c=s[i];"cached"===c[1].state&&S(c[1])&&!w(c[1])&&e.removeItem(c[0])}try{return a()}catch(t){for(var u=o.sort((function(e,t){return(e[1].createdAt||0)-(t[1].createdAt||0)})),d=0,f=u;d<f.length;d++){var l=f[d];e.removeItem(l[0]);try{return a()}catch(e){}}}e.removeItem(t+r)}}})}return t}()})); |
{ | ||
"name": "axios-cache-interceptor", | ||
"version": "0.10.4", | ||
"version": "0.10.5", | ||
"description": "Cache interceptor for axios", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -112,2 +112,14 @@ import type { Method } from 'axios'; | ||
staleIfError: StaleIfErrorPredicate<R, D>; | ||
/** | ||
* This option bypasses the current cache and always make a new http request. This will | ||
* not delete the current cache, it will just replace the cache when the response arrives. | ||
* | ||
* Unlike as `cache: false`, this will not disable the cache, it will just ignore the | ||
* pre-request cache checks before making the request. This way, all post-request | ||
* options are still available and will work as expected. | ||
* | ||
* @default false | ||
*/ | ||
override: boolean; | ||
}; | ||
@@ -114,0 +126,0 @@ |
@@ -95,3 +95,5 @@ import type { AxiosInstance } from 'axios'; | ||
staleIfError: options.staleIfError ?? true | ||
staleIfError: options.staleIfError ?? true, | ||
override: false | ||
}; | ||
@@ -98,0 +100,0 @@ |
@@ -23,3 +23,3 @@ import { deferred } from 'fast-defer'; | ||
axios.debug?.({ | ||
msg: 'Ignoring cache because config.cache is false', | ||
msg: 'Ignoring cache because config.cache === false', | ||
data: config | ||
@@ -47,5 +47,10 @@ }); | ||
let cache = await axios.storage.get(key, config); | ||
const overrideCache = config.cache.override; | ||
// Not cached, continue the request, and mark it as fetching | ||
emptyOrStale: if (cache.state === 'empty' || cache.state === 'stale') { | ||
ignoreAndRequest: if ( | ||
cache.state === 'empty' || | ||
cache.state === 'stale' || | ||
overrideCache | ||
) { | ||
/** | ||
@@ -56,3 +61,3 @@ * This checks for simultaneous access to a new key. The js event loop jumps on the | ||
*/ | ||
if (axios.waiting[key]) { | ||
if (axios.waiting[key] && !overrideCache) { | ||
cache = (await axios.storage.get(key, config)) as | ||
@@ -77,3 +82,3 @@ | CachedStorageValue | ||
break emptyOrStale; | ||
break ignoreAndRequest; | ||
} | ||
@@ -95,3 +100,10 @@ } | ||
state: 'loading', | ||
previous: cache.state, | ||
previous: overrideCache | ||
? // Simply determine if the request is stale or not | ||
// based if it had previous data or not | ||
cache.data | ||
? 'stale' | ||
: 'empty' | ||
: // Typescript doesn't know that cache.state here can only be 'empty' or 'stale' | ||
(cache.state as 'stale'), | ||
@@ -102,4 +114,7 @@ // Eslint complains a lot :) | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any | ||
createdAt: cache.createdAt as any | ||
// If the cache is empty and asked to override it, use the current timestamp | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
createdAt: | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any) | ||
}, | ||
@@ -125,3 +140,7 @@ config | ||
id: key, | ||
msg: 'Sending request, waiting for response' | ||
msg: 'Sending request, waiting for response', | ||
data: { | ||
overrideCache, | ||
state: cache.state | ||
} | ||
}); | ||
@@ -128,0 +147,0 @@ } |
@@ -57,3 +57,3 @@ import type { CacheProperties } from '..'; | ||
id, | ||
msg: 'Response with config.cache === false', | ||
msg: 'Response with config.cache falsy', | ||
data: response | ||
@@ -86,3 +86,3 @@ }); | ||
if ( | ||
// For 'loading' values (post stale), this check was already run in the past. | ||
// For 'loading' values (previous: stale), this check already ran in the past. | ||
!cache.data && | ||
@@ -103,9 +103,7 @@ !(await testCachePredicate(response, cacheConfig.cachePredicate)) | ||
// avoid remnant headers from remote server to break implementation | ||
for (const header in Header) { | ||
if (!header.startsWith('XAxiosCache')) { | ||
continue; | ||
// Avoid remnant headers from remote server to break implementation | ||
for (const header of Object.keys(response.headers)) { | ||
if (header.startsWith('x-axios-cache')) { | ||
delete response.headers[header]; | ||
} | ||
delete response.headers[header]; | ||
} | ||
@@ -161,3 +159,3 @@ | ||
msg: 'Useful response configuration found', | ||
data: { cacheConfig, ttl, cacheResponse: data } | ||
data: { cacheConfig, cacheResponse: data } | ||
}); | ||
@@ -164,0 +162,0 @@ } |
@@ -11,6 +11,3 @@ import type { CacheAxiosResponse } from '../cache/axios'; | ||
): Promise<void> { | ||
for (const cacheKey in entries) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const updater = entries[cacheKey]!; | ||
for (const [cacheKey, updater] of Object.entries(entries)) { | ||
if (updater === 'delete') { | ||
@@ -17,0 +14,0 @@ await storage.remove(cacheKey, data.config); |
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
350455
2583