forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.calendar.mjs
39 lines (32 loc) · 1005 Bytes
/
next.calendar.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
import {
BASE_CALENDAR_URL,
SHARED_CALENDAR_KEY,
} from './next.calendar.constants.mjs';
/**
*
* @param {string} calendarId
* @param {number} maxResults
* @returns {Promise<Array<import('./types').CalendarEvent>>}
*/
export const getCalendarEvents = async (calendarId = '', maxResults = 20) => {
const currentDate = new Date();
const nextWeekDate = new Date();
nextWeekDate.setDate(currentDate.getDate() + 7);
const calendarQueryParams = new URLSearchParams({
calendarId,
maxResults,
singleEvents: true,
timeZone: 'Etc/Utc',
key: SHARED_CALENDAR_KEY,
timeMax: nextWeekDate.toISOString(),
timeMin: currentDate.toISOString(),
});
const calendarQueryUrl = new URL(`${BASE_CALENDAR_URL}${calendarId}/events`);
calendarQueryParams.forEach((value, key) =>
calendarQueryUrl.searchParams.append(key, value)
);
return fetch(calendarQueryUrl.toString())
.then(response => response.json())
.then(calendar => calendar.items);
};