UNPKG

axios-token-refresh

Version:

A lightweight utility for Axios to handle token refresh logic seamlessly. Automatically retries requests after token expiration with customizable retry logic and status code handling.

2 lines (1 loc) 1.15 kB
var{defineProperty:y,getOwnPropertyNames:w,getOwnPropertyDescriptor:x}=Object,q=Object.prototype.hasOwnProperty;var p=new WeakMap,d=(t)=>{var e=p.get(t),s;if(e)return e;if(e=y({},"__esModule",{value:!0}),t&&typeof t==="object"||typeof t==="function")w(t).map((r)=>!q.call(e,r)&&y(e,r,{get:()=>t[r],enumerable:!(s=x(t,r))||s.enumerable}));return p.set(t,e),e};var A=(t,e)=>{for(var s in e)y(t,s,{get:e[s],enumerable:!0,configurable:!0,set:(r)=>e[s]=()=>r})};var O={};A(O,{default:()=>P});module.exports=d(O);var g=(t,e)=>{let{refreshRequest:s,onRetry:r,statusCodes:l=[401],shouldRetry:f,retryTimes:R=1}=e??{};if(!s||typeof s!=="function")throw new Error("axios-token-refresh requires `refreshRequest` to be a function that returns a promise.");let u=[];return t.interceptors.response.use((n)=>n,async(n)=>{let{config:o,response:m}=n;if((f?f(n):l.includes(m?.status))&&!o?.__isRetryRequest){o.__isRetryRequest=!0;let a=0,h=async()=>{if(a>=R)throw new Error(n);a++;try{let i=await r?.(o)??o;return await s(i)}catch(i){if(a<R)return h();throw i}},c=h();u.push(c);try{return await c}finally{u.splice(u.indexOf(c),1)}}return Promise.reject(n)})},P=g;