Adding a New Calendar Provider
Relevant source files
The following files were used as context for generating this wiki page:
- packages/api/src/providers/calendars/interfaces.ts
- packages/api/src/providers/calendars/google-calendar.ts
- packages/api/src/providers/calendars/microsoft-calendar.ts
- packages/api/src/schemas/events.ts
- packages/google-calendar/src/client.ts
- packages/google-calendar/src/resources/users/me/calendar-list.ts
The Analog backend is designed with a provider-based architecture to support various calendar services like Google Calendar and Microsoft Calendar. This modular approach allows for the seamless integration of new calendar services by creating a new "provider" class that conforms to a standardized interface.
This document outlines the architecture and the steps required to implement a new calendar provider. The core task is to create a class that implements the CalendarProvider interface, which acts as a bridge between the external calendar service's API and Analog's internal data models and business logic.
Core Architecture
The system relies on a central CalendarProvider interface that defines a contract for all interactions with an external calendar service. Concrete implementations, such as GoogleCalendarProvider and MicrosoftCalendarProvider, handle the specific API calls and data transformations for their respective services.
This diagram shows the relationship between the CalendarProvider interface and its concrete implementations.
The CalendarProvider Interface
To add a new calendar service, you must create a class that implements the CalendarProvider interface. This interface standardizes all calendar-related operations within the Analog system.
Sources: packages/api/src/providers/calendars/interfaces.ts:24-101
Properties
| Property | Type | Description |
|---|---|---|
providerId | string | A unique identifier for the provider (e.g., "google", "microsoft"). |
accountId | string | The ID of the user account associated with this provider instance. |
Sources: packages/api/src/providers/calendars/interfaces.ts:25-26, packages/api/src/providers/calendars/google-calendar.ts:21, packages/api/src/providers/calendars/microsoft-calendar.ts:33
Methods
The following table details the methods that must be implemented.
| Method | Description |
|---|---|
calendars() | Fetches all calendars for the user's account. |
createCalendar() | Creates a new calendar. |
updateCalendar() | Updates an existing calendar's metadata. |
deleteCalendar() | Deletes a calendar. |
events() | Fetches events for a specific calendar within a given time range (timeMin, timeMax). |
event() | Fetches a single event by its ID. |
createEvent() | Creates a new event in a calendar. |
updateEvent() | Updates an existing event. |
deleteEvent() | Deletes an event. |
moveEvent() | Moves an event from a source calendar to a destination calendar. |
responseToEvent() | Updates the user's response status (e.g., accepted, declined) for an event. |
freeBusy() | Retrieves the free/busy schedule information for a list of calendars. |
Sources: packages/api/src/providers/calendars/interfaces.ts:28-100
Implementing a New Provider
1. Class Structure and Initialization
Create a new class that implements CalendarProvider. The constructor should accept an accessToken for authenticating with the external service's API and the accountId. It should also initialize the API client/SDK for the target service.
Example: GoogleCalendarProvider
// packages/api/src/providers/calendars/google-calendar.ts:20-28
export class GoogleCalendarProvider implements CalendarProvider {
public readonly providerId = "google" as const;
public readonly accountId: string;
private client: GoogleCalendar;
constructor({ accessToken, accountId }: GoogleCalendarProviderOptions) {
this.accountId = accountId;
this.client = new GoogleCalendar({
accessToken,
});
}
// ... method implementations
}
Example: MicrosoftCalendarProvider
// packages/api/src/providers/calendars/microsoft-calendar.ts:32-41
export class MicrosoftCalendarProvider implements CalendarProvider {
public readonly providerId = "microsoft" as const;
public readonly accountId: string;
private graphClient: Client;
constructor({ accessToken, accountId }: MicrosoftCalendarProviderOptions) {
this.accountId = accountId;
this.graphClient = Client.initWithMiddleware({
authProvider: {
getAccessToken: async () => accessToken,
},
});
}
// ... method implementations
}
The GoogleCalendar client is a custom-built client within the Analog monorepo, which is initialized directly with the access token. The Microsoft provider uses the official @microsoft/microsoft-graph-client and configures it with an auth provider middleware.
Sources: packages/api/src/providers/calendars/google-calendar.ts:20-28, packages/api/src/providers/calendars/microsoft-calendar.ts:32-41, packages/google-calendar/src/client.ts:145-154
2. Data Transformation
A key responsibility of the provider is to translate data between the external service's format and Analog's internal data models. The internal models are defined in files like packages/api/src/schemas/events.ts.
This involves two main flows:
- Parsing: Converting raw API responses from the external service into Analog's
CalendarandCalendarEventobjects. - Formatting: Converting Analog's input objects (e.g.,
CreateEventInput) into the format expected by the external service's API.
The following diagram illustrates the data flow when fetching events.
Helper functions are used extensively for this mapping. For example, parseGoogleCalendarEvent takes a raw Google Calendar event and transforms it into an Analog CalendarEvent. Conversely, toMicrosoftEvent takes an Analog UpdateEventInput and converts it into a MicrosoftEvent object for an update request.
Sources: packages/api/src/providers/calendars/google-calendar.ts:44-48, packages/api/src/providers/calendars/microsoft-calendar.ts:161, packages/api/src/schemas/events.ts:233-261
3. Method Implementation and Error Handling
Each method from the CalendarProvider interface must be implemented. These methods will wrap calls to the external service's SDK. It is best practice to wrap each implementation in a centralized error handler to ensure consistent error logging and propagation. Both existing providers use a withErrorHandler private method for this purpose.
This sequence diagram shows a typical flow for creating an event.
4. Handling Provider-Specific Logic
Some operations require provider-specific logic. For instance, updating a user's response to an event invitation is handled differently by Google and Microsoft.
- Google Calendar: The attendee's status is updated within the main event object, and the entire event is updated via a
PATCHorPUTrequest to the events endpoint. - Microsoft Graph: The event is first updated with a
PATCHrequest, and then a separatePOSTrequest is made to a specific response endpoint (e.g.,/me/events/{id}/accept).
Your new provider must account for such nuances in its implementation of the updateEvent and responseToEvent methods.
Sources: packages/api/src/providers/calendars/google-calendar.ts:120-169, packages/api/src/providers/calendars/microsoft-calendar.ts:163-190
Conclusion
Adding a new calendar provider to Analog is a structured process centered around the CalendarProvider interface. The key steps are to create a new class implementing this interface, initialize the appropriate SDK with an access token, implement all required methods to wrap the external API calls, and write the necessary data transformation logic to map data between the provider's models and Analog's internal schemas. Consistent error handling and attention to provider-specific API behaviors are crucial for a robust implementation.