import t from"auto-bind";import e,{isAxiosError as s}from"axios";import a from"axios-better-stacktrace";import{isNil as r,merge as o}from"lodash";import{Mutex as i,E_CANCELED as n}from"async-mutex";import{consola as u}from"consola";import{isBefore as c,subMinutes as h}from"date-fns";import{jwtDecode as f}from"jwt-decode";function l(t,e,s,a){Object.defineProperty(t,e,{get:s,set:a,enumerable:!0,configurable:!0})}l({},"BaseApi",()=>d);class d{constructor(e){this.apiClient=e,t(this)}getApiClient(){return this.apiClient}}var p,g={};l(g,"ContentType",()=>m),l(g,"BaseClient",()=>x);var m=((p={}).Json="application/json",p.FormData="multipart/form-data",p.UrlEncoded="application/x-www-form-urlencoded",p.Text="text/plain",p);class x{constructor(s){this.config=s,t(this),this.storage=s.storage??sessionStorage,this.axiosInstance=e.create({...s.axiosConfig,baseURL:s.baseUrl,headers:{"Content-Type":"application/json",...s.axiosConfig?.headers}}),a(this.axiosInstance)}stringifyFormItem(t){return"object"==typeof t&&null!==t?JSON.stringify(t):`${t}`}createFormData(t){return t instanceof FormData?t:Object.keys(t).reduce((e,s)=>{let a=t[s];for(let t of a instanceof Array?a:[a]){let a=t instanceof Blob||t instanceof File;e.append(s,a?t:this.stringifyFormItem(t))}return e},new FormData)}async request(t){let{contentType:e,data:a}=t;r(a)||("multipart/form-data"===e&&"object"==typeof a&&(t.data=this.createFormData(a)),"text/plain"===e&&"string"!=typeof a&&(t.data=JSON.stringify(a)));let i=o(t.secure??this.config.defaultSecure??!0?await this.prepareAuthRequestConfig():{},e?{headers:{"Content-Type":e}}:{},t);try{let{data:t,status:e,statusText:s}=await this.axiosInstance.request(i);return{success:!0,data:t,status:e,statusText:s}}catch(a){let e={success:!1,status:-1,statusText:String(a),error:a};if(s(a)&&(this.config.onAxiosError?.(a),e.data=a.response?.data,e.status=a.response?.status??e.status,e.statusText=a.response?.statusText??e.statusText),this.config.onError?.(a),!this.config.handleError&&!t.handleError)throw a;return e}}get(t,e){return this.request({...e,method:"GET",url:t})}delete(t,e){return this.request({...e,method:"DELETE",url:t})}head(t,e){return this.request({...e,method:"HEAD",url:t})}options(t,e){return this.request({...e,method:"OPTIONS",url:t})}post(t,e,s){return this.request({...s,method:"POST",url:t,data:e})}put(t,e,s){return this.request({...s,method:"PUT",url:t,data:e})}patch(t,e,s){return this.request({...s,method:"PATCH",url:t,data:e})}getAxios(){return this.axiosInstance}setAuthData(t){this.storage.setItem(this.config.authDataStorageKey||"auth",JSON.stringify(t))}getAuthData(){let t=this.storage.getItem(this.config.authDataStorageKey||"auth");return t?JSON.parse(t):null}getConfig(){return this.config}}l({},"JwtClient",()=>T);class T extends x{constructor(t){super(t),this.config=t,this.lock=new i}getConfig(){return super.getConfig()}setRenewTokenCallback(t){this.renewTokenCallback=t}async prepareAuthRequestConfig(){if(this.config.axiosConfig?.withCredentials)return{};let{accessToken:t}=this.getAuthData()||{};if(!t)return u.warn("No access token found. Please set auth data after authorization."),{};if(!1!==this.config.autoRenewToken&&this.isTokenExpired(t))try{await this.lock.runExclusive(async()=>{this.config.debug&&u.debug("Token expired. Renewing…"),t=(await this.renewToken()).accessToken,this.lock.cancel()})}catch(e){if(e===n)t=this.getAuthData()?.accessToken;else throw e}return{headers:{Authorization:`${this.config.authSchema??"Bearer"} ${t}`}}}isTokenExpired(t){let e=f(t);return!e.exp||c(h(1e3*e.exp,1),Date.now())}async renewToken(){let t=this.config.onRenewToken??this.renewTokenCallback;if(!t)throw Error("No renew token callback provided");let e=await t();return this.setAuthData(e),e}}export{d as BaseApi,m as ContentType,x as BaseClient,T as JwtClient};
//# sourceMappingURL=index.esm.js.map
