UNPKG

tsdav

Version:

WebDAV, CALDAV, and CARDDAV client for Nodejs and the Browser

2 lines (1 loc) 43.6 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("debug"),r=require("xml-js"),a=require("base-64");exports.DAVNamespace=void 0,(e=exports.DAVNamespace||(exports.DAVNamespace={})).CALENDAR_SERVER="http://calendarserver.org/ns/",e.CALDAV_APPLE="http://apple.com/ns/ical/",e.CALDAV="urn:ietf:params:xml:ns:caldav",e.CARDDAV="urn:ietf:params:xml:ns:carddav",e.DAV="DAV:";const s={[exports.DAVNamespace.CALDAV]:"xmlns:c",[exports.DAVNamespace.CARDDAV]:"xmlns:card",[exports.DAVNamespace.CALENDAR_SERVER]:"xmlns:cs",[exports.DAVNamespace.CALDAV_APPLE]:"xmlns:ca",[exports.DAVNamespace.DAV]:"xmlns:d"};var o,c;exports.DAVNamespaceShort=void 0,(o=exports.DAVNamespaceShort||(exports.DAVNamespaceShort={})).CALDAV="c",o.CARDDAV="card",o.CALENDAR_SERVER="cs",o.CALDAV_APPLE="ca",o.DAV="d",function(e){e.VEVENT="VEVENT",e.VTODO="VTODO",e.VJOURNAL="VJOURNAL",e.VFREEBUSY="VFREEBUSY",e.VTIMEZONE="VTIMEZONE",e.VALARM="VALARM"}(c||(c={}));const n="undefined"!=typeof globalThis&&"function"==typeof globalThis.fetch?globalThis.fetch.bind(globalThis):()=>{throw new Error("tsdav: global fetch is not available in this runtime. Upgrade to Node.js >= 18, run under a browser/Bun/Deno, or install a fetch polyfill on globalThis before importing tsdav. You can also pass a custom `fetch` implementation to `createDAVClient`, `DAVClient`, or individual request helpers.")},i=/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/,d=e=>{if("string"!=typeof e)return e;if(i.test(e)){const t=Number(e);if(!Number.isNaN(t)&&Number.isFinite(t))return t}const t=e.toLowerCase();return"true"===t||"false"!==t&&e},l=e=>{const t=e.trim();return t.endsWith("/")?t.slice(0,-1):t},h=(e,t)=>!e&&!t||!(!e||!t)&&l(e)===l(t),p=(e,t)=>{if(!e&&!t)return!0;if(!e||!t)return!1;const r=l(e),a=l(t);return r.includes(a)||a.includes(r)},u=e=>e.reduce((e,t)=>({...e,[s[t]]:t}),{}),f=e=>Object.entries(e).reduce((e,[t,r])=>r?{...e,[t]:r}:e,{}),v=(e,t)=>{if(!e)return{};if(!t||0===t.length)return e;const r=new Set(t.map(e=>e.toLowerCase()));return Object.fromEntries(Object.entries(e).filter(([e])=>!r.has(e.toLowerCase())))};var m=Object.freeze({__proto__:null,cleanupFalsy:f,conditionalParam:(e,t)=>t?{[e]:t}:{},excludeHeaders:v,getDAVAttribute:u,urlContains:p,urlEquals:h});const A=t("tsdav:request"),y=async e=>{var t;const{url:a,init:s,convertIncoming:o=!0,parseOutgoing:c=!0,fetchOptions:i={},fetch:l}=e,h=null!=l?l:n,{headers:p={},body:u,namespace:v,method:m,attributes:y}=s,O=o?r.js2xml({_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},_attributes:y,...u},{compact:!0,spaces:2,elementNameFn:e=>v&&!/^.+:.+/.test(e)?`${v}:${e}`:e}):u,D={...i};delete D.headers;const V={},x=(e,t)=>{if(null==t)return;const r=e.toLowerCase();Object.keys(V).forEach(e=>{e.toLowerCase()===r&&delete V[e]}),V[e]=t};x("Content-Type","text/xml;charset=UTF-8"),Object.entries(f(p)).forEach(([e,t])=>x(e,t)),Object.entries(i.headers||{}).forEach(([e,t])=>x(e,t));const g=await h(a,{...D,headers:V,body:O,method:m}),w=await g.text();if(!(g.ok&&(null===(t=g.headers.get("content-type"))||void 0===t?void 0:t.includes("xml"))&&c&&w)){const e=4096,t=w.length>e?`${w.slice(0,e)}…`:w;return[{href:g.url,ok:g.ok,status:g.status,statusText:g.statusText,raw:t}]}let C;try{C=r.xml2js(w,{compact:!0,trim:!0,textFn:(e,t)=>{try{const r=t._parent,a=Object.keys(r),s=a[a.length-1],o=r[s];if(o.length>0){o[o.length-1]=d(e)}else r[s]=d(e)}catch(e){A(e.stack)}},elementNameFn:e=>e.replace(/^.+:/,"").replace(/[-_]+(\w?)/g,(e,t)=>t?t.toUpperCase():""),attributesFn:e=>{const t={...e};return delete t.xmlns,t},ignoreDeclaration:!0})}catch(e){return A(`Failed to parse DAV response XML: ${e.message}`),[{href:g.url,ok:g.ok,status:g.status,statusText:g.statusText,raw:w}]}if(!(null==C?void 0:C.multistatus))return[{href:g.url,ok:g.ok,status:g.status,statusText:g.statusText,raw:C}];return(Array.isArray(C.multistatus.response)?C.multistatus.response:[C.multistatus.response]).map(e=>{var t,r;if(!e)return{status:g.status,statusText:g.statusText,ok:g.ok};const a=/^\S+\s(?<status>\d+)\s(?<statusText>.+)$/.exec(e.status),s=(null==a?void 0:a.groups)?Number.parseInt(a.groups.status,10):g.status;return{raw:C,href:e.href,status:s,statusText:null!==(r=null===(t=null==a?void 0:a.groups)||void 0===t?void 0:t.statusText)&&void 0!==r?r:g.statusText,ok:s>=200&&s<300,error:e.error,responsedescription:e.responsedescription,props:(Array.isArray(e.propstat)?e.propstat:[e.propstat]).reduce((e,t)=>({...e,...null==t?void 0:t.prop}),{})}})},O=async e=>{const{url:t,props:r,depth:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;return y({url:t,init:{method:"PROPFIND",headers:v(f({depth:a,...s}),o),namespace:exports.DAVNamespaceShort.DAV,body:{propfind:{_attributes:u([exports.DAVNamespace.CALDAV,exports.DAVNamespace.CALDAV_APPLE,exports.DAVNamespace.CALENDAR_SERVER,exports.DAVNamespace.CARDDAV,exports.DAVNamespace.DAV]),prop:r}}},fetchOptions:c,fetch:n})},D=async e=>{const{url:t,data:r,headers:a,headersToExclude:s,fetchOptions:o={},fetch:c}=e;return(null!=c?c:n)(t,{method:"PUT",body:r,headers:v(a,s),...o})},V=async e=>{const{url:t,data:r,etag:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:i}=e;return(null!=i?i:n)(t,{method:"PUT",body:r,headers:v(f({"If-Match":a,...s}),o),...c})},x=async e=>{const{url:t,headers:r,etag:a,headersToExclude:s,fetchOptions:o={},fetch:c}=e;return(null!=c?c:n)(t,{method:"DELETE",headers:v(f({"If-Match":a,...r}),s),...o})};var g=Object.freeze({__proto__:null,createObject:D,davRequest:y,deleteObject:x,propfind:O,updateObject:V});function w(e,t){const r=e=>t.every(t=>e[t]);return Array.isArray(e)?e.every(e=>r(e)):r(e)}const C=(e,t)=>t.reduce((t,r)=>e[r]?t:`${t.length?`${t},`:""}${r.toString()}`,""),b=t("tsdav:collection"),N=async e=>{const{url:t,body:r,depth:a,defaultNamespace:s=exports.DAVNamespaceShort.DAV,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e,d=await y({url:t,init:{method:"REPORT",headers:v(f({depth:a,...o}),c),namespace:s,body:r},fetchOptions:n,fetch:i}),l=d.find(e=>!e.ok||e.status&&e.status>=400);if(l)throw new Error(`Collection query failed: ${l.status} ${l.statusText}. ${l.raw?`Raw response: ${l.raw}`:""}`);return 1===d.length&&!d[0].raw&&d[0].status&&d[0].status<300?[]:d},T=async e=>{const{url:t,props:r,depth:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;return y({url:t,init:{method:"MKCOL",headers:v(f({depth:a,...s}),o),namespace:exports.DAVNamespaceShort.DAV,body:r?{mkcol:{set:{prop:r}}}:void 0},fetchOptions:c,fetch:n})},S=async e=>{var t,r,a;const{collection:s,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e,d=null===(a=null===(r=null===(t=(await O({url:s.url,props:{[`${exports.DAVNamespaceShort.DAV}:supported-report-set`]:{}},depth:"0",headers:v(o,c),fetchOptions:n,fetch:i}))[0])||void 0===t?void 0:t.props)||void 0===r?void 0:r.supportedReportSet)||void 0===a?void 0:a.supportedReport;if(!d)return[];return(Array.isArray(d)?d:[d]).map(e=>(null==e?void 0:e.report)?Object.keys(e.report)[0]:void 0).filter(e=>"string"==typeof e&&e.length>0)},k=async e=>{var t,r,a;const{collection:s,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e,d=(await O({url:s.url,props:{[`${exports.DAVNamespaceShort.CALENDAR_SERVER}:getctag`]:{}},depth:"0",headers:v(o,c),fetchOptions:n,fetch:i})).filter(e=>p(s.url,e.href))[0];if(!d)throw new Error("Collection does not exist on server");return{isDirty:`${s.ctag}`!=`${null===(t=d.props)||void 0===t?void 0:t.getctag}`,newCtag:null===(a=null===(r=d.props)||void 0===r?void 0:r.getctag)||void 0===a?void 0:a.toString()}},$=e=>{const{url:t,props:r,headers:a,syncLevel:s,syncToken:o,headersToExclude:c,fetchOptions:n,fetch:i}=e;return y({url:t,init:{method:"REPORT",namespace:exports.DAVNamespaceShort.DAV,headers:v({...a},c),body:{"sync-collection":{_attributes:u([exports.DAVNamespace.CALDAV,exports.DAVNamespace.CARDDAV,exports.DAVNamespace.DAV]),"sync-level":s,"sync-token":o,[`${exports.DAVNamespaceShort.DAV}:prop`]:r}}},fetchOptions:n,fetch:i})},E=async e=>{var t,r,a,s,o,c,n,i,d,l,h;const{collection:u,method:f,headers:m,headersToExclude:A,account:y,detailedResult:O,fetchOptions:D={},fetch:V}=e,x=["accountType","homeUrl"];if(!y||!w(y,x)){if(!y)throw new Error("no account for smartCollectionSync");throw new Error(`account must have ${C(y,x)} before smartCollectionSync`)}const g=null!=f?f:(null===(t=u.reports)||void 0===t?void 0:t.includes("syncCollection"))?"webdav":"basic";if(b(`smart collection sync with type ${y.accountType} and method ${g}`),"webdav"===g){const e=await $({url:u.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${"caldav"===y.accountType?exports.DAVNamespaceShort.CALDAV:exports.DAVNamespaceShort.CARDDAV}:${"caldav"===y.accountType?"calendar-data":"address-data"}`]:{},[`${exports.DAVNamespaceShort.DAV}:displayname`]:{}},syncLevel:1,syncToken:u.syncToken,headers:v(m,A),fetchOptions:D,fetch:V}),t=e.filter(e=>{var t;const r="caldav"===y.accountType?".ics":".vcf";return(null===(t=e.href)||void 0===t?void 0:t.slice(-4))===r}),d=t.filter(e=>404!==e.status).map(e=>e.href),l=t.filter(e=>404===e.status).map(e=>e.href),h=(d.length&&null!==(a=await(null===(r=u.objectMultiGet)||void 0===r?void 0:r.call(u,{url:u.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${"caldav"===y.accountType?exports.DAVNamespaceShort.CALDAV:exports.DAVNamespaceShort.CARDDAV}:${"caldav"===y.accountType?"calendar-data":"address-data"}`]:{}},objectUrls:d,depth:"1",headers:v(m,A),fetchOptions:D,fetch:V})))&&void 0!==a?a:[]).map(e=>{var t,r,a,s,o,c,n,i,d,l;return{url:null!==(t=e.href)&&void 0!==t?t:"",etag:null===(r=e.props)||void 0===r?void 0:r.getetag,data:"caldav"===(null==y?void 0:y.accountType)?null!==(o=null===(s=null===(a=e.props)||void 0===a?void 0:a.calendarData)||void 0===s?void 0:s._cdata)&&void 0!==o?o:null===(c=e.props)||void 0===c?void 0:c.calendarData:null!==(d=null===(i=null===(n=e.props)||void 0===n?void 0:n.addressData)||void 0===i?void 0:i._cdata)&&void 0!==d?d:null===(l=e.props)||void 0===l?void 0:l.addressData}}),f=null!==(s=u.objects)&&void 0!==s?s:[],x=h.filter(e=>f.every(t=>!p(t.url,e.url))),g=f.reduce((e,t)=>{const r=h.find(e=>p(e.url,t.url));return r&&r.etag&&r.etag!==t.etag?[...e,r]:e},[]),w=l.map(e=>({url:e,etag:""})),C=f.filter(e=>h.some(t=>p(e.url,t.url)&&t.etag===e.etag));return{...u,objects:O?{created:x,updated:g,deleted:w}:[...C,...x,...g],syncToken:null!==(i=null===(n=null===(c=null===(o=e[0])||void 0===o?void 0:o.raw)||void 0===c?void 0:c.multistatus)||void 0===n?void 0:n.syncToken)&&void 0!==i?i:u.syncToken}}if("basic"===g){const{isDirty:e,newCtag:t}=await k({collection:u,headers:v(m,A),fetchOptions:D,fetch:V});if(!e)return O?{...u,objects:{created:[],updated:[],deleted:[]}}:u;const r=null!==(d=u.objects)&&void 0!==d?d:[],a=null!==(h=await(null===(l=u.fetchObjects)||void 0===l?void 0:l.call(u,{collection:u,headers:v(m,A),fetchOptions:D,fetch:V})))&&void 0!==h?h:[],s=a.filter(e=>r.every(t=>!p(t.url,e.url))),o=r.reduce((e,t)=>{const r=a.find(e=>p(e.url,t.url));return r&&r.etag&&r.etag!==t.etag?[...e,r]:e},[]),c=r.filter(e=>a.every(t=>!p(t.url,e.url))),n=r.filter(e=>a.some(t=>p(e.url,t.url)&&t.etag===e.etag));return{...u,objects:O?{created:s,updated:o,deleted:c}:[...n,...s,...o],ctag:t}}return O?{...u,objects:{created:[],updated:[],deleted:[]}}:u},U=async e=>E({...e,detailedResult:!0});var _=Object.freeze({__proto__:null,collectionQuery:N,isCollectionDirty:k,makeCollection:T,smartCollectionSync:E,smartCollectionSyncDetailed:U,supportedReportSet:S,syncCollection:$});const R=t("tsdav:addressBook"),j=async e=>{const{url:t,props:r,filters:a,depth:s,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e;return N({url:t,body:{"addressbook-query":f({_attributes:u([exports.DAVNamespace.CARDDAV,exports.DAVNamespace.DAV]),[`${exports.DAVNamespaceShort.DAV}:prop`]:r,filter:null!=a?a:{"prop-filter":{_attributes:{name:"FN"}}}})},defaultNamespace:exports.DAVNamespaceShort.CARDDAV,depth:s,headers:v(o,c),fetchOptions:n,fetch:i})},L=async e=>{const{url:t,props:r,objectUrls:a,depth:s,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e;return N({url:t,body:{"addressbook-multiget":f({_attributes:u([exports.DAVNamespace.DAV,exports.DAVNamespace.CARDDAV]),[`${exports.DAVNamespaceShort.DAV}:prop`]:r,[`${exports.DAVNamespaceShort.DAV}:href`]:a})},defaultNamespace:exports.DAVNamespaceShort.CARDDAV,depth:s,headers:v(o,c),fetchOptions:n,fetch:i})},H=async e=>{const{account:t,headers:r,props:a,headersToExclude:s,fetchOptions:o={},fetch:c}=null!=e?e:{},n=["homeUrl","rootUrl"];if(!t||!w(t,n)){if(!t)throw new Error("no account for fetchAddressBooks");throw new Error(`account must have ${C(t,n)} before fetchAddressBooks`)}const i=await O({url:t.homeUrl,props:null!=a?a:{[`${exports.DAVNamespaceShort.DAV}:displayname`]:{},[`${exports.DAVNamespaceShort.CALENDAR_SERVER}:getctag`]:{},[`${exports.DAVNamespaceShort.DAV}:resourcetype`]:{},[`${exports.DAVNamespaceShort.DAV}:sync-token`]:{}},depth:"1",headers:v(r,s),fetchOptions:o,fetch:c});return Promise.all(i.filter(e=>{var t,r;return Object.keys(null!==(r=null===(t=e.props)||void 0===t?void 0:t.resourcetype)&&void 0!==r?r:{}).includes("addressbook")}).map(e=>{var r,a,s,o,c,n,i,d,l,h;const p=null!==(s=null===(a=null===(r=e.props)||void 0===r?void 0:r.displayname)||void 0===a?void 0:a._cdata)&&void 0!==s?s:null===(o=e.props)||void 0===o?void 0:o.displayname;return R(`Found address book named ${"string"==typeof p?p:""},\n props: ${JSON.stringify(e.props)}`),{url:new URL(null!==(c=e.href)&&void 0!==c?c:"",null!==(n=t.rootUrl)&&void 0!==n?n:"").href,ctag:null===(i=e.props)||void 0===i?void 0:i.getctag,displayName:"string"==typeof p?p:"",resourcetype:Object.keys(null!==(l=null===(d=e.props)||void 0===d?void 0:d.resourcetype)&&void 0!==l?l:{}),syncToken:null===(h=e.props)||void 0===h?void 0:h.syncToken}}).map(async e=>({...e,reports:await S({collection:e,headers:v(r,s),fetchOptions:o,fetch:c})})))},B=async e=>{const{addressBook:t,headers:r,objectUrls:a,headersToExclude:s,urlFilter:o=e=>Boolean(e),useMultiGet:c=!0,fetchOptions:n={},fetch:i}=e;R(`Fetching vcards from ${null==t?void 0:t.url}`);const d=["url"];if(!t||!w(t,d)){if(!t)throw new Error("cannot fetchVCards for undefined addressBook");throw new Error(`addressBook must have ${C(t,d)} before fetchVCards`)}const l=(null!=a?a:(await j({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{}},depth:"1",headers:v(r,s),fetchOptions:n,fetch:i})).map(e=>{var t;return null!==(t=e.href)&&void 0!==t?t:""})).map(e=>e.startsWith("http")||!e?e:new URL(e,t.url).href).filter(e=>e&&!h(e,t.url)).filter(o).map(e=>new URL(e).pathname);let p=[];return l.length>0&&(p=c?await L({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${exports.DAVNamespaceShort.CARDDAV}:address-data`]:{}},objectUrls:l,depth:"1",headers:v(r,s),fetchOptions:n,fetch:i}):await j({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${exports.DAVNamespaceShort.CARDDAV}:address-data`]:{}},depth:"1",headers:v(r,s),fetchOptions:n,fetch:i})),p.map(e=>{var r,a,s,o,c,n;return{url:new URL(null!==(r=e.href)&&void 0!==r?r:"",t.url).href,etag:null===(a=e.props)||void 0===a?void 0:a.getetag,data:null!==(c=null===(o=null===(s=e.props)||void 0===s?void 0:s.addressData)||void 0===o?void 0:o._cdata)&&void 0!==c?c:null===(n=e.props)||void 0===n?void 0:n.addressData}})},P=async e=>{const{addressBook:t,vCardString:r,filename:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;return D({url:new URL(a,t.url).href,data:r,headers:v({"content-type":"text/vcard; charset=utf-8","If-None-Match":"*",...s},o),fetchOptions:c,fetch:n})},F=async e=>{const{vCard:t,headers:r,headersToExclude:a,fetchOptions:s={},fetch:o}=e;return V({url:t.url,data:t.data,etag:t.etag,headers:v({"content-type":"text/vcard; charset=utf-8",...r},a),fetchOptions:s,fetch:o})},M=async e=>{const{vCard:t,headers:r,headersToExclude:a,fetchOptions:s={},fetch:o}=e;return x({url:t.url,etag:t.etag,headers:v(r,a),fetchOptions:s,fetch:o})};var I=Object.freeze({__proto__:null,addressBookMultiGet:L,addressBookQuery:j,createVCard:P,deleteVCard:M,fetchAddressBooks:H,fetchVCards:B,updateVCard:F});const z=t("tsdav:calendar"),q=/^\d{4}(-\d\d(-\d\d(T\d\d:\d\d(:\d\d)?(\.\d+)?(([+-]\d\d:\d\d)|Z)?)?)?)?$/i,G=/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(([+-]\d\d:\d\d)|Z)?$/i,Q=e=>{const{start:t,end:r}=e;if(!(q.test(t)&&q.test(r)||G.test(t)&&G.test(r)))throw new Error("invalid timeRange format, not in ISO8601");if(Number.isNaN(new Date(t).getTime())||Number.isNaN(new Date(r).getTime()))throw new Error("invalid timeRange: start or end is not a valid date")},Z=e=>{var t;let r=[];return Array.isArray(e)?r=e.map(e=>{var t;return null===(t=null==e?void 0:e._attributes)||void 0===t?void 0:t.name}):e&&"object"==typeof e&&(r=[null===(t=e._attributes)||void 0===t?void 0:t.name]),r.filter(e=>"string"==typeof e&&e.length>0)},J=async e=>{var t,r;const{account:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e,i=["principalUrl","rootUrl"];if(!w(a,i))throw new Error(`account must have ${C(a,i)} before fetchUserAddresses`);z(`Fetch user addresses from ${a.principalUrl}`);const d=(await O({url:a.principalUrl,props:{[`${exports.DAVNamespaceShort.CALDAV}:calendar-user-address-set`]:{}},depth:"0",headers:v(s,o),fetchOptions:c,fetch:n})).find(e=>p(a.principalUrl,e.href));if(!d||!d.ok)throw new Error("cannot find calendarUserAddresses");const l=null===(r=null===(t=null==d?void 0:d.props)||void 0===t?void 0:t.calendarUserAddressSet)||void 0===r?void 0:r.href;let h=[];Array.isArray(l)?h=l:l&&(h=[l]);const u=h.filter(e=>"string"==typeof e&&e.length>0);return z(`Fetched calendar user addresses ${u}`),u},W=async e=>{const{url:t,props:r,filters:a,timezone:s,depth:o,headers:c,headersToExclude:n,fetchOptions:i={},fetch:d}=e;return N({url:t,body:{"calendar-query":f({_attributes:u([exports.DAVNamespace.CALDAV,exports.DAVNamespace.CALENDAR_SERVER,exports.DAVNamespace.CALDAV_APPLE,exports.DAVNamespace.DAV]),[`${exports.DAVNamespaceShort.DAV}:prop`]:r,filter:a,timezone:s})},defaultNamespace:exports.DAVNamespaceShort.CALDAV,depth:o,headers:v(c,n),fetchOptions:i,fetch:d})},Y=async e=>{const{url:t,props:r,objectUrls:a,filters:s,timezone:o,depth:c,headers:n,headersToExclude:i,fetchOptions:d={},fetch:l}=e;return N({url:t,body:{"calendar-multiget":f({_attributes:u([exports.DAVNamespace.DAV,exports.DAVNamespace.CALDAV]),[`${exports.DAVNamespaceShort.DAV}:prop`]:r,[`${exports.DAVNamespaceShort.DAV}:href`]:a,filter:s,timezone:o})},defaultNamespace:exports.DAVNamespaceShort.CALDAV,depth:c,headers:v(n,i),fetchOptions:d,fetch:l})},K=async e=>{const{url:t,props:r,depth:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;return y({url:t,init:{method:"MKCALENDAR",headers:v(f({depth:a,...s}),o),namespace:exports.DAVNamespaceShort.DAV,body:{[`${exports.DAVNamespaceShort.CALDAV}:mkcalendar`]:{_attributes:u([exports.DAVNamespace.DAV,exports.DAVNamespace.CALDAV,exports.DAVNamespace.CALDAV_APPLE]),set:{prop:r}}}},fetchOptions:c,fetch:n})},X=async e=>{const{headers:t,account:r,props:a,projectedProps:s,headersToExclude:o,fetchOptions:n={},fetch:i}=null!=e?e:{},d=["homeUrl","rootUrl"];if(!r||!w(r,d)){if(!r)throw new Error("no account for fetchCalendars");throw new Error(`account must have ${C(r,d)} before fetchCalendars`)}const l=await O({url:r.homeUrl,props:null!=a?a:{[`${exports.DAVNamespaceShort.CALDAV}:calendar-description`]:{},[`${exports.DAVNamespaceShort.CALDAV}:calendar-timezone`]:{},[`${exports.DAVNamespaceShort.DAV}:displayname`]:{},[`${exports.DAVNamespaceShort.CALDAV_APPLE}:calendar-color`]:{},[`${exports.DAVNamespaceShort.CALENDAR_SERVER}:getctag`]:{},[`${exports.DAVNamespaceShort.DAV}:resourcetype`]:{},[`${exports.DAVNamespaceShort.CALDAV}:supported-calendar-component-set`]:{},[`${exports.DAVNamespaceShort.DAV}:sync-token`]:{}},depth:"1",headers:v(t,o),fetchOptions:n,fetch:i});return Promise.all(l.filter(e=>{var t,r;return Object.keys(null!==(r=null===(t=e.props)||void 0===t?void 0:t.resourcetype)&&void 0!==r?r:{}).includes("calendar")}).filter(e=>{var t,r;const a=Z(null===(r=null===(t=e.props)||void 0===t?void 0:t.supportedCalendarComponentSet)||void 0===r?void 0:r.comp);return 0===a.length||a.some(e=>Object.values(c).includes(e))}).map(e=>{var t,a,o,c,n,i,d,l,h,p,u,f,v,m,A,y;const O=null===(t=e.props)||void 0===t?void 0:t.calendarDescription,D=null===(a=e.props)||void 0===a?void 0:a.calendarTimezone,V=null===(c=null===(o=e.props)||void 0===o?void 0:o.supportedCalendarComponentSet)||void 0===c?void 0:c.comp,x=Object.entries(null!==(n=e.props)&&void 0!==n?n:{}).filter(([e])=>null==s?void 0:s[e]);return{description:"string"==typeof O?O:"",timezone:"string"==typeof D?D:"",url:new URL(null!==(i=e.href)&&void 0!==i?i:"",null!==(d=r.rootUrl)&&void 0!==d?d:"").href,ctag:null===(l=e.props)||void 0===l?void 0:l.getctag,calendarColor:null===(h=e.props)||void 0===h?void 0:h.calendarColor,displayName:null!==(f=null===(u=null===(p=e.props)||void 0===p?void 0:p.displayname)||void 0===u?void 0:u._cdata)&&void 0!==f?f:null===(v=e.props)||void 0===v?void 0:v.displayname,components:Z(V),resourcetype:Object.keys(null!==(A=null===(m=e.props)||void 0===m?void 0:m.resourcetype)&&void 0!==A?A:{}),syncToken:null===(y=e.props)||void 0===y?void 0:y.syncToken,...s&&x.length>0?{projectedProps:Object.fromEntries(x)}:{}}}).map(async e=>({...e,reports:await S({collection:e,headers:v(t,o),fetchOptions:n,fetch:i})})))},ee=async e=>{const{calendar:t,objectUrls:r,filters:a,timeRange:s,headers:o,expand:c,urlFilter:n=e=>Boolean(null==e?void 0:e.includes(".ics")),useMultiGet:i=!0,headersToExclude:d,fetchOptions:l={},fetch:h}=e;s&&Q(s),z(`Fetching calendar objects from ${null==t?void 0:t.url}`);const p=["url"];if(!t||!w(t,p)){if(!t)throw new Error("cannot fetchCalendarObjects for undefined calendar");throw new Error(`calendar must have ${C(t,p)} before fetchCalendarObjects`)}const u=null!=a?a:[{"comp-filter":{_attributes:{name:"VCALENDAR"},"comp-filter":{_attributes:{name:"VEVENT"},...s?{"time-range":{_attributes:{start:`${new Date(s.start).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`,end:`${new Date(s.end).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`}}}:{}}}}];let f=[];const m=(null!=r?r:(f=await W({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},...c&&s?{[`${exports.DAVNamespaceShort.CALDAV}:calendar-data`]:{[`${exports.DAVNamespaceShort.CALDAV}:expand`]:{_attributes:{start:`${new Date(s.start).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`,end:`${new Date(s.end).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`}}}}:{}},filters:u,depth:"1",headers:v(o,d),fetchOptions:l,fetch:h})).map(e=>{var t;return null!==(t=e.href)&&void 0!==t?t:""})).map(e=>e.startsWith("http")||!e?e:new URL(e,t.url).href).filter(n).map(e=>new URL(e).pathname);let A=[];return m.length>0&&(A=c&&!r?f.filter(e=>{var r,a;const s=(null!==(r=e.href)&&void 0!==r?r:"").startsWith("http")?e.href:new URL(null!==(a=e.href)&&void 0!==a?a:"",t.url).href;return n(null!=s?s:"")}):i?await Y({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${exports.DAVNamespaceShort.CALDAV}:calendar-data`]:{...c&&s?{[`${exports.DAVNamespaceShort.CALDAV}:expand`]:{_attributes:{start:`${new Date(s.start).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`,end:`${new Date(s.end).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`}}}:{}}},objectUrls:m,depth:"1",headers:v(o,d),fetchOptions:l,fetch:h}):await W({url:t.url,props:{[`${exports.DAVNamespaceShort.DAV}:getetag`]:{},[`${exports.DAVNamespaceShort.CALDAV}:calendar-data`]:{...c&&s?{[`${exports.DAVNamespaceShort.CALDAV}:expand`]:{_attributes:{start:`${new Date(s.start).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`,end:`${new Date(s.end).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`}}}:{}}},filters:u,depth:"1",headers:v(o,d),fetchOptions:l,fetch:h})),A.map(e=>{var r,a,s,o,c,n;return{url:new URL(null!==(r=e.href)&&void 0!==r?r:"",t.url).href,etag:`${null===(a=e.props)||void 0===a?void 0:a.getetag}`,data:null!==(c=null===(o=null===(s=e.props)||void 0===s?void 0:s.calendarData)||void 0===o?void 0:o._cdata)&&void 0!==c?c:null===(n=e.props)||void 0===n?void 0:n.calendarData}})},te=async e=>{const{calendar:t,iCalString:r,filename:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;return D({url:new URL(a,t.url).href,data:r,headers:v({"content-type":"text/calendar; charset=utf-8","If-None-Match":"*",...s},o),fetchOptions:c,fetch:n})},re=async e=>{const{calendarObject:t,headers:r,headersToExclude:a,fetchOptions:s={},fetch:o}=e;return V({url:t.url,data:t.data,etag:t.etag,headers:v({"content-type":"text/calendar; charset=utf-8",...r},a),fetchOptions:s,fetch:o})},ae=async e=>{const{calendarObject:t,headers:r,headersToExclude:a,fetchOptions:s={},fetch:o}=e;return x({url:t.url,etag:t.etag,headers:v(r,a),fetchOptions:s,fetch:o})},se=async e=>{var t;const{oldCalendars:r,account:a,detailedResult:s,headers:o,headersToExclude:c,fetchOptions:n={},fetch:i}=e;if(!a)throw new Error("Must have account before syncCalendars");const d=null!==(t=null!=r?r:a.calendars)&&void 0!==t?t:[],l=await X({account:a,headers:v(o,c),fetchOptions:n,fetch:i}),h=l.filter(e=>d.every(t=>!p(t.url,e.url)));z(`new calendars: ${h.map(e=>e.displayName)}`);const u=d.reduce((e,t)=>{const r=l.find(e=>p(e.url,t.url));return r&&(r.syncToken&&`${r.syncToken}`!=`${t.syncToken}`||r.ctag&&`${r.ctag}`!=`${t.ctag}`)?[...e,r]:e},[]);z(`updated calendars: ${u.map(e=>e.displayName)}`);const f=await Promise.all(u.map(async e=>await E({collection:{...e,objectMultiGet:Y},method:"webdav",headers:v(o,c),account:a,fetchOptions:n,fetch:i}))),m=d.filter(e=>l.every(t=>!p(t.url,e.url)));z(`deleted calendars: ${m.map(e=>e.displayName)}`);const A=d.filter(e=>l.some(t=>{if(!p(t.url,e.url))return!1;const r=!t.syncToken||`${t.syncToken}`==`${e.syncToken}`,a=!t.ctag||`${t.ctag}`==`${e.ctag}`;return r&&a}));return z(`unchanged calendars: ${A.map(e=>e.displayName)}`),s?{created:h,updated:f,deleted:m}:[...A,...h,...f]},oe=async e=>se({...e,detailedResult:!0}),ce=async e=>{const{url:t,timeRange:r,depth:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e;if(!r)throw new Error("timeRange is required");Q(r);return(await N({url:t,body:{"free-busy-query":f({_attributes:u([exports.DAVNamespace.CALDAV]),[`${exports.DAVNamespaceShort.CALDAV}:time-range`]:{_attributes:{start:`${new Date(r.start).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`,end:`${new Date(r.end).toISOString().slice(0,19).replace(/[-:.]/g,"")}Z`}}})},defaultNamespace:exports.DAVNamespaceShort.CALDAV,depth:a,headers:v(s,o),fetchOptions:c,fetch:n}))[0]};var ne=Object.freeze({__proto__:null,calendarMultiGet:Y,calendarQuery:W,createCalendarObject:te,deleteCalendarObject:ae,fetchCalendarObjects:ee,fetchCalendarUserAddresses:J,fetchCalendars:X,freeBusyQuery:ce,makeCalendar:K,syncCalendars:se,syncCalendarsDetailed:oe,updateCalendarObject:re});const ie=t("tsdav:account"),de=async e=>{var t;ie("Service discovery...");const{account:r,headers:a,headersToExclude:s,fetchOptions:o={},fetch:c}=e,i=null!=c?c:n,d=new URL(r.serverUrl),l=new URL(`/.well-known/${r.accountType}`,d);l.protocol=null!==(t=d.protocol)&&void 0!==t?t:"http";const h=e=>{var t;if(e.status>=300&&e.status<400){const r=e.headers.get("Location");if("string"==typeof r&&r.length){ie(`Service discovery redirected to ${r}`);const e=/^[a-z][a-z0-9+.-]*:/i.test(r),a=new URL(r,d);return a.hostname===l.hostname&&l.port&&!a.port&&(a.port=l.port),e||(a.protocol=null!==(t=d.protocol)&&void 0!==t?t:"http"),a.href}}};try{const e=h(await i(l.href,{...o,method:"PROPFIND",headers:{...v(a,s),"Content-Type":"text/xml;charset=UTF-8"},body:'<?xml version="1.0" encoding="utf-8" ?>\n<d:propfind xmlns:d="DAV:">\n <d:prop>\n <d:resourcetype/>\n </d:prop>\n</d:propfind>',redirect:"manual"}));if(e)return e}catch(e){ie(`Service discovery PROPFIND failed: ${e.stack}`)}try{const e=h(await i(l.href,{...o,method:"GET",headers:v(a,s),redirect:"manual"}));if(e)return e}catch(e){ie(`Service discovery GET failed: ${e.stack}`)}return d.href},le=async e=>{var t,r;const{account:a,headers:s,headersToExclude:o,fetchOptions:c={},fetch:n}=e,i=["rootUrl"];if(!w(a,i))throw new Error(`account must have ${C(a,i)} before fetchPrincipalUrl`);ie(`Fetching principal url from path ${a.rootUrl}`);const[d]=await O({url:a.rootUrl,props:{[`${exports.DAVNamespaceShort.DAV}:current-user-principal`]:{}},depth:"0",headers:v(s,o),fetchOptions:c,fetch:n});if(!d.ok){if(ie(`Fetch principal url failed: ${d.statusText}`),401===d.status)throw new Error(`Invalid credentials: PROPFIND ${a.rootUrl} returned 401 Unauthorized`);throw new Error("cannot find principalUrl")}const l=null===(r=null===(t=d.props)||void 0===t?void 0:t.currentUserPrincipal)||void 0===r?void 0:r.href;if("string"!=typeof l||!l.length)throw ie("Fetch principal url failed: missing current-user-principal href"),new Error("cannot find principalUrl");return ie(`Fetched principal url ${l}`),new URL(l,a.rootUrl).href},he=async e=>{var t,r,a,s;const{account:o,headers:c,headersToExclude:n,fetchOptions:i={},fetch:d}=e,l=["principalUrl","rootUrl"];if(!w(o,l))throw new Error(`account must have ${C(o,l)} before fetchHomeUrl`);ie(`Fetch home url from ${o.principalUrl}`);const h=await O({url:o.principalUrl,props:"caldav"===o.accountType?{[`${exports.DAVNamespaceShort.CALDAV}:calendar-home-set`]:{}}:{[`${exports.DAVNamespaceShort.CARDDAV}:addressbook-home-set`]:{}},depth:"0",headers:v(c,n),fetchOptions:i,fetch:d}),u=h.find(e=>p(o.principalUrl,e.href));if(!u||!u.ok)throw ie(`Fetch home url failed with status ${null==u?void 0:u.statusText} and error ${JSON.stringify(h.map(e=>e.error))}`),new Error("cannot find homeUrl");const f="caldav"===o.accountType?null===(r=null===(t=u.props)||void 0===t?void 0:t.calendarHomeSet)||void 0===r?void 0:r.href:null===(s=null===(a=u.props)||void 0===a?void 0:a.addressbookHomeSet)||void 0===s?void 0:s.href;if("string"!=typeof f||0===f.length)throw ie(`Fetch home url failed: server did not return a ${"caldav"===o.accountType?"calendar-home-set":"addressbook-home-set"} href`),new Error("cannot find homeUrl");const m=new URL(f,o.rootUrl).href;return ie(`Fetched home url ${m}`),m},pe=async e=>{var t,r,a,s;const{account:o,headers:c,loadCollections:n=!1,loadObjects:i=!1,headersToExclude:d,fetchOptions:l={},fetch:h}=e,p={...o},u=null!==(t=o.rootUrl)&&void 0!==t?t:await de({account:o,headers:v(c,d),fetchOptions:l,fetch:h});if(o.rootUrl)p.rootUrl=o.rootUrl;else if(o.principalUrl)p.rootUrl=u;else{let e;for(const t of((e,t)=>{const r=[t,e,new URL("/",e).href];return r.filter((e,t)=>r.indexOf(e)===t)})(o.serverUrl,u))try{const e=await le({account:{...p,rootUrl:t},headers:v(c,d),fetchOptions:l,fetch:h});p.rootUrl=t,p.principalUrl=e;break}catch(t){e=t}if(!p.rootUrl||!p.principalUrl)throw null!=e?e:new Error("cannot find principalUrl")}return p.principalUrl=null!==(a=null!==(r=o.principalUrl)&&void 0!==r?r:p.principalUrl)&&void 0!==a?a:await le({account:p,headers:v(c,d),fetchOptions:l,fetch:h}),p.homeUrl=null!==(s=o.homeUrl)&&void 0!==s?s:await he({account:p,headers:v(c,d),fetchOptions:l,fetch:h}),(n||i)&&("caldav"===o.accountType?p.calendars=await X({headers:v(c,d),account:p,fetchOptions:l,fetch:h}):"carddav"===o.accountType&&(p.addressBooks=await H({headers:v(c,d),account:p,fetchOptions:l,fetch:h}))),i&&("caldav"===o.accountType&&p.calendars?p.calendars=await Promise.all(p.calendars.map(async e=>({...e,objects:await ee({calendar:e,headers:v(c,d),fetchOptions:l,fetch:h})}))):"carddav"===o.accountType&&p.addressBooks&&(p.addressBooks=await Promise.all(p.addressBooks.map(async e=>({...e,objects:await B({addressBook:e,headers:v(c,d),fetchOptions:l,fetch:h})}))))),p};var ue=Object.freeze({__proto__:null,createAccount:pe,fetchHomeUrl:he,fetchPrincipalUrl:le,serviceDiscovery:de});const{encode:fe}=a,ve=t("tsdav:authHelper"),me=(e,t)=>(...r)=>e({...t,...r[0]}),Ae=e=>{var t;return ve(`Basic auth token generated for user "${null!==(t=e.username)&&void 0!==t?t:""}"`),{authorization:`Basic ${fe(`${e.username}:${e.password}`)}`}},ye=e=>({authorization:`Bearer ${e.accessToken}`}),Oe=async(e,t,r)=>{const a=["authorizationCode","redirectUrl","clientId","clientSecret","tokenUrl"];if(!w(e,a))throw new Error(`Oauth credentials missing: ${C(e,a)}`);const s=new URLSearchParams({grant_type:"authorization_code",code:e.authorizationCode,redirect_uri:e.redirectUrl,client_id:e.clientId,client_secret:e.clientSecret});ve(`Fetching oauth tokens from ${e.tokenUrl}`);const o=null!=r?r:n,c=await o(e.tokenUrl,{method:"POST",body:s.toString(),headers:{"content-length":`${s.toString().length}`,"content-type":"application/x-www-form-urlencoded"},...null!=t?t:{}});if(c.ok){return await c.json()}return ve(`Fetch Oauth tokens failed with status ${c.status}`),{}},De=async(e,t,r)=>{const a=["refreshToken","clientId","clientSecret","tokenUrl"];if(!w(e,a))throw new Error(`Oauth credentials missing: ${C(e,a)}`);const s=new URLSearchParams({client_id:e.clientId,client_secret:e.clientSecret,refresh_token:e.refreshToken,grant_type:"refresh_token"}),o=null!=r?r:n,c=await o(e.tokenUrl,{method:"POST",body:s.toString(),headers:{"Content-Type":"application/x-www-form-urlencoded"},...null!=t?t:{}});if(c.ok){return await c.json()}return ve(`Refresh access token failed with status ${c.status}`),{}},Ve=async(e,t,r)=>{var a;ve("Fetching oauth headers");let s={},o=!1;return e.refreshToken?e.refreshToken&&!e.accessToken||Date.now()>(null!==(a=e.expiration)&&void 0!==a?a:0)?(s=await De(e,t,r),o=!0):s={access_token:e.accessToken,refresh_token:e.refreshToken}:(s=await Oe(e,t,r),o=!0),o&&(s.access_token&&(e.accessToken=s.access_token),s.refresh_token&&(e.refreshToken=s.refresh_token),"number"==typeof s.expires_in&&(e.expiration=Date.now()+1e3*s.expires_in)),ve("Oauth tokens obtained"),{tokens:s,headers:s.access_token?{authorization:`Bearer ${s.access_token}`}:{}}};var xe=Object.freeze({__proto__:null,defaultParam:me,fetchOauthTokens:Oe,getBasicAuthHeaders:Ae,getBearerAuthHeaders:ye,getOauthHeaders:Ve,refreshAccessToken:De});const ge=async e=>{var t;const{serverUrl:r,credentials:a,authMethod:s="Basic",defaultAccountType:o,authFunction:c,fetchOptions:n,fetch:i}=e;let d={};switch(s){case"Basic":d=Ae(a);break;case"Bearer":d=ye(a);break;case"Oauth":d=(await Ve(a,void 0,i)).headers;break;case"Digest":d={Authorization:`Digest ${a.digestString}`};break;case"Custom":if(!c)throw new Error("authMethod 'Custom' requires an authFunction to produce request headers");d=null!==(t=await c(a))&&void 0!==t?t:{};break;default:throw new Error("Invalid auth method")}const l=o?await pe({account:{serverUrl:r,credentials:a,accountType:o},headers:d,fetchOptions:n,fetch:i}):void 0,h={headers:d,fetchOptions:n,fetch:i},p={url:r,...h},u={account:l,...h},f=me(D,p),v=me(V,p),m=me(x,p),A=me(O,h),g=me(N,h),w=me(T,h),C=me($,h),b=me(S,h),_=me(k,h),R=me(E,u),I=me(U,u),z=me(W,h),q=me(Y,h),G=me(K,h),Q=me(X,u),Z=me(J,u),ce=me(ee,h),ne=me(te,h),ie=me(re,h),de=me(ae,h),le=me(se,u),he=me(oe,u),ue=me(j,h),fe=me(L,h);return{davRequest:async e=>{const{init:t,fetchOptions:r,fetch:a,...s}=e,{headers:o,...c}=t;return y({...s,init:{...c,headers:{...d,...o}},fetchOptions:null!=r?r:n,fetch:null!=a?a:i})},propfind:A,createAccount:async e=>{const{account:t,headers:s,loadCollections:o,loadObjects:c,fetchOptions:l,fetch:h}=e,p={serverUrl:r,credentials:a,...t};if(!p.accountType)throw new Error("createAccount requires an accountType; pass one via `account.accountType` or set `defaultAccountType` on the client.");return pe({account:p,headers:{...d,...s},loadCollections:o,loadObjects:c,fetchOptions:null!=l?l:n,fetch:null!=h?h:i})},createObject:f,updateObject:v,deleteObject:m,calendarQuery:z,addressBookQuery:ue,collectionQuery:g,makeCollection:w,calendarMultiGet:q,makeCalendar:G,syncCollection:C,supportedReportSet:b,isCollectionDirty:_,smartCollectionSync:R,smartCollectionSyncDetailed:I,fetchCalendars:Q,fetchCalendarUserAddresses:Z,fetchCalendarObjects:ce,createCalendarObject:ne,updateCalendarObject:ie,deleteCalendarObject:de,syncCalendars:le,syncCalendarsDetailed:he,fetchAddressBooks:me(H,u),addressBookMultiGet:fe,fetchVCards:me(B,h),createVCard:me(P,h),updateVCard:me(F,h),deleteVCard:me(M,h)}};class we{constructor(e){var t,r,a;this.serverUrl=e.serverUrl,this.credentials=e.credentials,this.authMethod=null!==(t=e.authMethod)&&void 0!==t?t:"Basic",this.accountType=null!==(r=e.defaultAccountType)&&void 0!==r?r:"caldav",this.authFunction=e.authFunction,this.fetchOptions=null!==(a=e.fetchOptions)&&void 0!==a?a:{},this.fetchOverride=e.fetch}async login(e){switch(this.authMethod){case"Basic":this.authHeaders=Ae(this.credentials);break;case"Bearer":this.authHeaders=ye(this.credentials);break;case"Oauth":this.authHeaders=(await Ve(this.credentials,this.fetchOptions,this.fetchOverride)).headers;break;case"Digest":this.authHeaders={Authorization:`Digest ${this.credentials.digestString}`};break;case"Custom":if(!this.authFunction)throw new Error("authMethod 'Custom' requires an authFunction to produce request headers");this.authHeaders=await this.authFunction(this.credentials);break;default:throw new Error("Invalid auth method")}this.account=this.accountType?await pe({account:{serverUrl:this.serverUrl,credentials:this.credentials,accountType:this.accountType},headers:this.authHeaders,loadCollections:null==e?void 0:e.loadCollections,loadObjects:null==e?void 0:e.loadObjects,fetchOptions:this.fetchOptions,fetch:this.fetchOverride}):void 0}async davRequest(e){const{init:t,fetchOptions:r,fetch:a,...s}=e,{headers:o,...c}=t;return y({...s,init:{...c,headers:{...this.authHeaders,...o}},fetchOptions:null!=r?r:this.fetchOptions,fetch:null!=a?a:this.fetchOverride})}async createObject(...e){return me(D,{url:this.serverUrl,headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async updateObject(...e){return me(V,{url:this.serverUrl,headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async deleteObject(...e){return me(x,{url:this.serverUrl,headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async propfind(...e){return me(O,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async createAccount(e){var t;const{account:r,headers:a,loadCollections:s,loadObjects:o,fetchOptions:c,fetch:n}=e,i=null!==(t=r.accountType)&&void 0!==t?t:this.accountType;if(!i)throw new Error("createAccount requires an accountType; pass one via `account.accountType` or configure `defaultAccountType` on the DAVClient.");return pe({account:{serverUrl:this.serverUrl,credentials:this.credentials,...r,accountType:i},headers:{...this.authHeaders,...a},loadCollections:s,loadObjects:o,fetchOptions:null!=c?c:this.fetchOptions,fetch:null!=n?n:this.fetchOverride})}async collectionQuery(...e){return me(N,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async makeCollection(...e){return me(T,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async syncCollection(...e){return me($,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async supportedReportSet(...e){return me(S,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async isCollectionDirty(...e){return me(k,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async smartCollectionSync(...e){return me(E,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride,account:this.account})(e[0])}async smartCollectionSyncDetailed(e){return me(U,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride,account:this.account})(e)}async calendarQuery(...e){return me(W,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async makeCalendar(...e){return me(K,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async calendarMultiGet(...e){return me(Y,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async fetchCalendars(...e){return me(X,{headers:this.authHeaders,account:this.account,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(null==e?void 0:e[0])}async fetchCalendarUserAddresses(...e){return me(J,{headers:this.authHeaders,account:this.account,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(null==e?void 0:e[0])}async fetchCalendarObjects(...e){return me(ee,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async createCalendarObject(...e){return me(te,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async updateCalendarObject(...e){return me(re,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async deleteCalendarObject(...e){return me(ae,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async syncCalendars(...e){return me(se,{headers:this.authHeaders,account:this.account,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async syncCalendarsDetailed(...e){return me(oe,{headers:this.authHeaders,account:this.account,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async addressBookQuery(...e){return me(j,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async addressBookMultiGet(...e){return me(L,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async fetchAddressBooks(...e){return me(H,{headers:this.authHeaders,account:this.account,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(null==e?void 0:e[0])}async fetchVCards(...e){return me(B,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async createVCard(...e){return me(P,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async updateVCard(...e){return me(F,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}async deleteVCard(...e){return me(M,{headers:this.authHeaders,fetchOptions:this.fetchOptions,fetch:this.fetchOverride})(e[0])}}var Ce=Object.freeze({__proto__:null,DAVClient:we,createDAVClient:ge}),be={DAVNamespace:exports.DAVNamespace,DAVNamespaceShort:exports.DAVNamespaceShort,DAVAttributeMap:s,...Ce,...g,..._,...ue,...I,...ne,...xe,...m};exports.DAVAttributeMap=s,exports.DAVClient=we,exports.addressBookMultiGet=L,exports.addressBookQuery=j,exports.calendarMultiGet=Y,exports.calendarQuery=W,exports.cleanupFalsy=f,exports.collectionQuery=N,exports.createAccount=pe,exports.createCalendarObject=te,exports.createDAVClient=ge,exports.createObject=D,exports.createVCard=P,exports.davRequest=y,exports.default=be,exports.deleteCalendarObject=ae,exports.deleteObject=x,exports.deleteVCard=M,exports.fetchAddressBooks=H,exports.fetchCalendarObjects=ee,exports.fetchCalendarUserAddresses=J,exports.fetchCalendars=X,exports.fetchOauthTokens=Oe,exports.fetchVCards=B,exports.freeBusyQuery=ce,exports.getBasicAuthHeaders=Ae,exports.getBearerAuthHeaders=ye,exports.getDAVAttribute=u,exports.getOauthHeaders=Ve,exports.isCollectionDirty=k,exports.makeCalendar=K,exports.propfind=O,exports.refreshAccessToken=De,exports.smartCollectionSync=E,exports.smartCollectionSyncDetailed=U,exports.supportedReportSet=S,exports.syncCalendars=se,exports.syncCalendarsDetailed=oe,exports.syncCollection=$,exports.updateCalendarObject=re,exports.updateObject=V,exports.updateVCard=F,exports.urlContains=p,exports.urlEquals=h;