0

List Event Instances

by
Published today

List the occurrences of a recurring event.

Script gcal Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
/**
4
 * List Event Instances
5
 * List the occurrences of a recurring event, each with its own instance id (for Update Event Instance / Update Following Instances). Bound the range with RFC 3339 `time_min`/`time_max` (an unbounded series has no end). Pass the returned `nextPageToken` as `page_token` for the next page.
6
 */
7
export async function main(
8
  auth: RT.Gcal,
9
  recurring_event_id: string,
10
  calendar_id: string = "primary",
11
  time_min: string | undefined,
12
  time_max: string | undefined,
13
  show_deleted: boolean = false,
14
  max_results: number = 250,
15
  page_token: string | undefined
16
) {
17
  const url = new URL(
18
    `https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendar_id || "primary")}/events/${recurring_event_id}/instances`
19
  )
20
  url.searchParams.append("showDeleted", String(!!show_deleted))
21
  url.searchParams.append("maxResults", String(max_results || 250))
22
  for (const [k, v] of [
23
    ["timeMin", time_min],
24
    ["timeMax", time_max],
25
    ["pageToken", page_token],
26
  ]) {
27
    if (v != null && v !== "") url.searchParams.append(k!, v)
28
  }
29

30
  const response = await fetch(url, {
31
    headers: {
32
      Authorization: `Bearer ${auth.token}`,
33
      Accept: "application/json",
34
    },
35
  })
36
  if (!response.ok) {
37
    throw new Error(`${response.status} ${await response.text()}`)
38
  }
39

40
  return await response.json()
41
}
42