import{t as e}from"./client-types-CJfvFcC7.js";var t=Object.defineProperty,n=(e,n,r)=>n in e?t(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,r=(e,t,r)=>n(e,typeof t==`symbol`?t:t+``,r),i=class{constructor(t){if(r(this,`server`),r(this,`db`),r(this,`refreshToken`),r(this,`baseUrl`),r(this,`getToken`,e=>Promise.reject(Error(`getToken method not implemented by Fetch Adapter`))),r(this,`request`,async t=>{let{query:n,body:r,method:i=`GET`,retry:a=!1,fetchOptions:o={}}=t,s=new URL(`${this.baseUrl}${t.url}`);if(n){let{_sort:e,...t}=n,r=Object.fromEntries(Object.entries(t).filter(([,e])=>e!=null)),i=new URLSearchParams(r);if(n.portalRanges&&typeof n.portalRanges==`object`)for(let[e,t]of Object.entries(n.portalRanges))t&&(t.offset&&t.offset>0&&i.set(`_offset.${e}`,t.offset.toString()),t.limit&&i.set(`_limit.${e}`,t.limit.toString()));e&&i.set(`_sort`,JSON.stringify(e)),i.delete(`portalRanges`),s.search=i.toString()}if(r&&`portalRanges`in r){for(let[e,t]of Object.entries(r.portalRanges))t&&(t.offset&&t.offset>0&&s.searchParams.set(`_offset.${e}`,t.offset.toString()),t.limit&&s.searchParams.set(`_limit.${e}`,t.limit.toString()));r.portalRanges=void 0}let c=new AbortController,l=null;t.timeout&&(l=setTimeout(()=>c.abort(),t.timeout));let u=await this.getToken({refresh:a}),d=new Headers(o?.headers);d.set(`Authorization`,`Bearer ${u}`),r instanceof FormData||d.set(`Content-Type`,`application/json`);let f;f=r instanceof FormData?r:r?JSON.stringify(r):void 0;let p=await fetch(s.toString(),{...o,method:i,body:f,headers:d,signal:c.signal});l&&clearTimeout(l);let m;try{m=await p.json()}catch{m={}}if(!p.ok){if((m?.messages)?.[0].code===`952`&&!a&&this.refreshToken)return this.request({...t,retry:!0});throw new e((m?.messages)?.[0].code??`500`,`Filemaker Data API failed with (${p.status}): ${JSON.stringify(m,null,2)}`)}return m.response}),r(this,`list`,async e=>{let{data:t,layout:n}=e;return await this.request({url:`/layouts/${n}/records`,query:t,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`get`,async e=>{let{data:t,layout:n}=e;return await this.request({url:`/layouts/${n}/records/${t.recordId}`,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`find`,async e=>{let{data:t,layout:n}=e;return await this.request({url:`/layouts/${n}/_find`,body:t,method:`POST`,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`create`,async e=>{let{data:t,layout:n}=e;return await this.request({url:`/layouts/${n}/records`,body:t,method:`POST`,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`update`,async e=>{let{data:{recordId:t,...n},layout:r}=e;return await this.request({url:`/layouts/${r}/records/${t}`,body:n,method:`PATCH`,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`delete`,async e=>{let{data:t,layout:n}=e;return await this.request({url:`/layouts/${n}/records/${t.recordId}`,method:`DELETE`,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`layoutMetadata`,async e=>await this.request({url:`/layouts/${e.layout}`,fetchOptions:e.fetch,timeout:e.timeout})),r(this,`executeScript`,async e=>{let{script:t,scriptParam:n,layout:r}=e;return await this.request({url:`/layouts/${r}/script/${t}`,query:n?{"script.param":n}:void 0,fetchOptions:e.fetch,timeout:e.timeout})}),r(this,`layouts`,async e=>await this.request({url:`/layouts`,fetchOptions:e?.fetch,timeout:e?.timeout})),r(this,`scripts`,async e=>await this.request({url:`/scripts`,fetchOptions:e?.fetch,timeout:e?.timeout})),r(this,`containerUpload`,async e=>{let t=`/layouts/${e.layout}/records/${e.data.recordId}/containers/${e.data.containerFieldName}`;e.data.repetition&&(t+=`/${e.data.repetition}`);let n=new FormData;n.append(`upload`,e.data.file),await this.request({url:t,method:`POST`,body:n,timeout:e.timeout,fetchOptions:e.fetch})}),r(this,`globals`,async e=>await this.request({url:`/globals`,method:`PATCH`,body:{globalFields:e.globalFields},fetchOptions:e?.fetch,timeout:e?.timeout})),this.server=t.server,this.db=t.db,this.refreshToken=t.refreshToken??!1,this.baseUrl=new URL(`${this.server}/fmi/data/vLatest/databases/${this.db}`),this.db===``)throw Error(`Database name is required`)}};export{i as t};
//# sourceMappingURL=fetch-base-skT9NKps.js.map