@remcostoeten/fync
Version:
A unified TypeScript library for easy access to popular APIs (GitHub, Spotify, GitLab, etc.)
141 lines (140 loc) • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
createCalendarService: true
};
exports.createCalendarService = createCalendarService;
var _calendarClient = require("./services/calendar-client");
var _types = require("./types");
Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _types[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _types[key];
}
});
});
function createCalendarService(config) {
const client = (0, _calendarClient.createCalendarClient)({
accessToken: config.accessToken,
cache: config.cache,
cacheTTL: config.cacheTTL
});
async function getCalendars(params) {
const response = await client.users.me.calendarList.get({
params
});
return response.items;
}
async function getCalendar(calendarId) {
return client.calendars[calendarId].get();
}
async function getEvents(calendarId = "primary", params) {
const transformedParams = params ? {
...params,
...(params.eventTypes && {
eventTypes: params.eventTypes.join(",")
})
} : undefined;
const response = await client.calendars[calendarId].events.get({
params: transformedParams
});
return response.items;
}
async function getEvent(calendarId, eventId) {
return client.calendars[calendarId].events[eventId].get();
}
async function getUpcomingEvents(calendarId = "primary", maxResults = 10) {
const now = new Date().toISOString();
return getEvents(calendarId, {
timeMin: now,
maxResults,
singleEvents: true,
orderBy: "startTime"
});
}
async function getEventsInDateRange(calendarId = "primary", startDate, endDate) {
return getEvents(calendarId, {
timeMin: startDate.toISOString(),
timeMax: endDate.toISOString(),
singleEvents: true,
orderBy: "startTime"
});
}
async function getTodaysEvents(calendarId = "primary") {
const today = new Date();
const startOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate());
const endOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
return getEventsInDateRange(calendarId, startOfDay, endOfDay);
}
async function searchEvents(query, calendarId = "primary", maxResults = 25) {
return getEvents(calendarId, {
q: query,
maxResults,
singleEvents: true,
orderBy: "startTime"
});
}
async function getColors() {
return client.colors.get();
}
async function getFreeBusy(params) {
const transformedParams = {
...params,
items: JSON.stringify(params.items)
};
return client.freebusy.query.get({
params: transformedParams
});
}
async function isTimeSlotBusy(calendarId, startTime, endTime) {
const freeBusyResult = await getFreeBusy({
timeMin: startTime.toISOString(),
timeMax: endTime.toISOString(),
items: [{
id: calendarId
}]
});
const calendarBusy = freeBusyResult.calendars[calendarId]?.busy || [];
return calendarBusy.length > 0;
}
async function getAllCalendarEvents(maxResults) {
const calendars = await getCalendars();
const results = [];
for (const calendar of calendars) {
if (calendar.accessRole === "reader" || calendar.accessRole === "writer" || calendar.accessRole === "owner") {
try {
const events = await getEvents(calendar.id, {
maxResults,
singleEvents: true,
orderBy: "startTime"
});
results.push({
calendar,
events
});
} catch {}
}
}
return results;
}
return {
getCalendars,
getCalendar,
getEvents,
getEvent,
getUpcomingEvents,
getEventsInDateRange,
getTodaysEvents,
searchEvents,
getColors,
getFreeBusy,
isTimeSlotBusy,
getAllCalendarEvents
};
}