import{r as e}from"./chunk-Bdh3yLIe.js";import{t}from"./client-types-CJfvFcC7.js";import{t as n}from"./memory-BobYC_NI.js";import{t as r}from"./fetch-base-skT9NKps.js";var i=e({FetchAdapter:()=>c}),a=Object.defineProperty,o=(e,t,n)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,s=(e,t,n)=>o(e,typeof t==`symbol`?t:t+``,n),c=class extends r{constructor(e){if(super({...e,refreshToken:!0}),s(this,`username`),s(this,`password`),s(this,`tokenStore`),s(this,`getTokenKey`),s(this,`getToken`,async e=>{let{refresh:n=!1}=e??{},r=null;if(n||(r=await this.tokenStore.getToken(this.getTokenKey())),!r){let e=await fetch(`${this.baseUrl}/sessions`,{method:`POST`,headers:{"Content-Type":`application/json`,Authorization:`Basic ${Buffer.from(`${this.username}:${this.password}`).toString(`base64`)}`}});if(!e.ok){let n=await e.json();throw new t(n.messages[0].code,n.messages[0].message)}if(r=e.headers.get(`X-FM-Data-Access-Token`),!r)throw Error(`Could not get token`);this.tokenStore.setToken(this.getTokenKey(),r)}return r}),s(this,`disconnect`,async()=>{let e=await this.tokenStore.getToken(this.getTokenKey());e&&(await this.request({url:`/sessions/${e}`,method:`DELETE`,fetchOptions:{headers:{Authorization:`Bearer ${e}`,"Content-Type":`application/json`}}}),this.tokenStore.clearToken(this.getTokenKey()))}),this.username=e.auth.username,this.password=e.auth.password,this.tokenStore=e.tokenStore??n(),this.getTokenKey=e.tokenStore?.getKey??(()=>`${e.server}/${e.db}`),this.username===``)throw Error(`Username is required`);if(this.password===``)throw Error(`Password is required`)}};export{i as n,c as t};
//# sourceMappingURL=fetch-COmDkOJN.js.map