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) • 659 B
JavaScript
var l=(y,f)=>{let{refreshRequest:r,onRetry:R,statusCodes:h=[401],shouldRetry:u,retryTimes:a=1}=f??{};if(!r||typeof r!=="function")throw new Error("axios-token-refresh requires `refreshRequest` to be a function that returns a promise.");let n=[];return y.interceptors.response.use((e)=>e,async(e)=>{let{config:t,response:p}=e;if((u?u(e):h.includes(p?.status))&&!t?.__isRetryRequest){t.__isRetryRequest=!0;let o=0,c=async()=>{if(o>=a)throw new Error(e);o++;try{let s=await R?.(t)??t;return await r(s)}catch(s){if(o<a)return c();throw s}},i=c();n.push(i);try{return await i}finally{n.splice(n.indexOf(i),1)}}return Promise.reject(e)})},q=l;export{q as default};