Skip to content

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. Call await client.login() once before calling account-dependent helpers.
  • createDAVClient — async factory that resolves auth headers and (when defaultAccountType is set) runs account discovery immediately, returning bound helper methods.

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;
};
  • serverUrl required — Base DAV server URL (for example https://caldav.icloud.com or https://example.com/dav.php/).
  • credentials required — DAVCredentials object matching your authMethod.
  • authMethod — Defaults to 'Basic'.
    • 'Basic': Sends HTTP Basic credentials. Automatically upgrades to HTTP Digest if the server answers with a Digest-only 401 challenge and WebCrypto is available.
    • 'Digest': Performs RFC 7616 HTTP Digest challenge-response per request (or sends legacy credentials.digestString as-is). See Digest authentication.
    • 'Oauth': Resolves and refreshes OAuth 2.0 Bearer tokens via getOauthHeaders, sharing one refresh across concurrent requests and mutating credentials in place with updated tokens and expiration.
    • 'Bearer': Sends Authorization: Bearer <accessToken> directly.
    • 'Custom': Calls authFunction(credentials) on authentication to produce request headers.
  • authFunction — Required when authMethod is 'Custom'.
  • defaultAccountType — 'caldav' or 'carddav'.
    • In DAVClient, defaults to 'caldav' and is used by client.login().
    • In createDAVClient, account discovery only runs automatically if defaultAccountType is provided.
  • fetchOptions — Default RequestInit options forwarded to every request. Client-level fetchOptions.headers override default authHeaders, while per-call headers and per-call fetchOptions.headers take precedence over client defaults.
  • fetch — Custom Fetch API implementation (for Electron IPC, proxies, tests, or non-standard runtimes).
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();

Authenticates the client and populates client.account via createAccount.

  • options.loadCollections — When true, discovers calendars or address books during login.
  • options.loadObjects — When true, also fetches all objects in each discovered collection (implies loadCollections).

client.calendarMultiGet and client.addressBookMultiGet are bound in the constructor so they can be passed directly as collection.objectMultiGet to smartCollectionSync / smartCollectionSyncDetailed.

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();

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: