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