Code Snippet Server
by ngeojiajun
Verified
- src
- services
import { google } from "googleapis";
import { GoogleBaseService } from "./google-base-service.js";
import { calendar_v3 } from "googleapis/build/src/apis/calendar/v3.js";
export class CalendarService extends GoogleBaseService {
private calendar: calendar_v3.Calendar;
constructor() {
super();
this.calendar = google.calendar({
version: "v3",
auth: this.auth.getAuth(),
});
}
async listEvents(
calendarId: string = "primary",
maxResults: number = 10
): Promise<calendar_v3.Schema$Event[]> {
try {
const response = await this.calendar.events.list({
calendarId,
timeMin: new Date().toISOString(),
maxResults,
singleEvents: true,
orderBy: "startTime",
});
return response.data.items || [];
} catch (error) {
console.error("Failed to list calendar events:", error);
throw error;
}
}
async getEvent(
eventId: string,
calendarId: string = "primary"
): Promise<calendar_v3.Schema$Event> {
try {
const response = await this.calendar.events.get({
calendarId,
eventId,
});
return response.data;
} catch (error) {
console.error("Failed to get calendar event:", error);
throw error;
}
}
async listCalendars(): Promise<calendar_v3.Schema$CalendarListEntry[]> {
try {
const response = await this.calendar.calendarList.list();
return response.data.items || [];
} catch (error) {
console.error("Failed to list calendars:", error);
throw error;
}
}
async searchEvents(
query: string,
calendarId: string = "primary",
maxResults: number = 10
): Promise<calendar_v3.Schema$Event[]> {
try {
const response = await this.calendar.events.list({
calendarId,
timeMin: new Date().toISOString(),
maxResults,
singleEvents: true,
orderBy: "startTime",
q: query,
});
return response.data.items || [];
} catch (error) {
console.error("Failed to search calendar events:", error);
throw error;
}
}
}