DAVClient & createDAVClient
tsdav provides two ways to create a pre-configured client that shares serverUrl, authentication, discovered account, fetchOptions, and custom fetch across requests:
DAVClient— class-based API. Callawait client.login()once before calling account-dependent helpers.createDAVClient— async factory that resolves auth headers and (whendefaultAccountTypeis set) runs account discovery immediately, returning bound helper methods.
Constructor / Factory Options
Section titled “Constructor / Factory Options”Both new DAVClient(params) and await createDAVClient(params) accept the same parameter object:
type DAVClientParams = { serverUrl: string; credentials: DAVCredentials; authMethod?: 'Basic' | 'Oauth' | 'Digest' | 'Custom' | 'Bearer'; authFunction?: (credentials: DAVCredentials) => Promise<Record<string, string>>; defaultAccountType?: 'caldav' | 'carddav'; fetchOptions?: RequestInit; fetch?: typeof globalThis.fetch;};serverUrlrequired — Base DAV server URL (for examplehttps://caldav.icloud.comorhttps://example.com/dav.php/).credentialsrequired — DAVCredentials object matching yourauthMethod.authMethod— Defaults to'Basic'.'Basic': Sends HTTP Basic credentials. Automatically upgrades to HTTP Digest if the server answers with a Digest-only401challenge and WebCrypto is available.'Digest': Performs RFC 7616 HTTP Digest challenge-response per request (or sends legacycredentials.digestStringas-is). See Digest authentication.'Oauth': Resolves and refreshes OAuth 2.0 Bearer tokens viagetOauthHeaders, sharing one refresh across concurrent requests and mutatingcredentialsin place with updated tokens andexpiration.'Bearer': SendsAuthorization: Bearer <accessToken>directly.'Custom': CallsauthFunction(credentials)on authentication to produce request headers.
authFunction— Required whenauthMethodis'Custom'.defaultAccountType—'caldav'or'carddav'.- In
DAVClient, defaults to'caldav'and is used byclient.login(). - In
createDAVClient, account discovery only runs automatically ifdefaultAccountTypeis provided.
- In
fetchOptions— DefaultRequestInitoptions forwarded to every request. Client-levelfetchOptions.headersoverride defaultauthHeaders, while per-callheadersand per-callfetchOptions.headerstake precedence over client defaults.fetch— Custom Fetch API implementation (for Electron IPC, proxies, tests, or non-standard runtimes).
Class-Based API (DAVClient)
Section titled “Class-Based API (DAVClient)”import { DAVClient } from 'tsdav';
const client = new DAVClient({ serverUrl: 'https://caldav.icloud.com', credentials: { username: 'YOUR_APPLE_ID', password: 'YOUR_APP_SPECIFIC_PASSWORD', }, authMethod: 'Basic', defaultAccountType: 'caldav',});
await client.login();
const calendars = await client.fetchCalendars();client.login(options?)
Section titled “client.login(options?)”Authenticates the client and populates client.account via createAccount.
options.loadCollections— Whentrue, discovers calendars or address books during login.options.loadObjects— Whentrue, also fetches all objects in each discovered collection (impliesloadCollections).
Bound Callbacks
Section titled “Bound Callbacks”client.calendarMultiGet and client.addressBookMultiGet are bound in the constructor so they can be passed directly as collection.objectMultiGet to smartCollectionSync / smartCollectionSyncDetailed.
Functional Factory (createDAVClient)
Section titled “Functional Factory (createDAVClient)”import { createDAVClient } from 'tsdav';
const client = await createDAVClient({ serverUrl: 'https://caldav.icloud.com', credentials: { username: 'YOUR_APPLE_ID', password: 'YOUR_APP_SPECIFIC_PASSWORD', }, authMethod: 'Basic', defaultAccountType: 'caldav',});
const calendars = await client.fetchCalendars();Available Client Methods
Section titled “Available Client Methods”Both DAVClient instances and the object returned by createDAVClient expose all high-level and low-level DAV methods with shared auth, account, fetchOptions, and fetch defaults:
- WebDAV: davRequest, propfind, createAccount, createObject, updateObject, deleteObject
- Collections: makeCollection, collectionQuery, syncCollection, supportedReportSet, isCollectionDirty, smartCollectionSync,
smartCollectionSyncDetailed - CalDAV: makeCalendar, fetchCalendars, fetchCalendarUserAddresses, fetchCalendarObjects, createCalendarObject, updateCalendarObject, deleteCalendarObject, calendarQuery, calendarMultiGet, syncCalendars,
syncCalendarsDetailed, freeBusyQuery - CardDAV: fetchAddressBooks, fetchVCards, createVCard, updateVCard, deleteVCard, addressBookQuery, addressBookMultiGet