1 |
|
2 | type Calendly = { |
3 | token: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Calendly, |
8 | user: string | undefined, |
9 | organization: string | undefined, |
10 | invitee_email: string | undefined, |
11 | status: 'active' | 'canceled' | undefined, |
12 | sort: string | undefined, |
13 | min_start_time: string | undefined, |
14 | max_start_time: string | undefined, |
15 | page_token: string | undefined, |
16 | count: string | undefined, |
17 | group: string | undefined |
18 | ) { |
19 | const url = new URL(`https://api.calendly.com/scheduled_events`) |
20 |
|
21 | for (const [k, v] of [ |
22 | ['user', user], |
23 | ['organization', organization], |
24 | ['invitee_email', invitee_email], |
25 | ['status', status], |
26 | ['sort', sort], |
27 | ['min_start_time', min_start_time], |
28 | ['max_start_time', max_start_time], |
29 | ['page_token', page_token], |
30 | ['count', count], |
31 | ['group', group] |
32 | ]) { |
33 | if (v !== undefined && v !== '' && k !== undefined) { |
34 | url.searchParams.append(k, v) |
35 | } |
36 | } |
37 |
|
38 | const response = await fetch(url, { |
39 | method: 'GET', |
40 | headers: { |
41 | Authorization: 'Bearer ' + auth.token |
42 | }, |
43 | body: undefined |
44 | }) |
45 |
|
46 | if (!response.ok) { |
47 | const text = await response.text() |
48 | throw new Error(`${response.status} ${text}`) |
49 | } |
50 |
|
51 | return await response.json() |
52 | } |
53 |
|