@cyc-seattle/gsuite-sdk 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +63 -2
- package/dist/calendar.d.ts +48 -0
- package/dist/calendar.js +157 -0
- package/dist/common.d.ts +34 -0
- package/dist/common.js +85 -0
- package/dist/directory.d.ts +84 -0
- package/dist/directory.js +225 -0
- package/dist/group-settings-templates.d.ts +13 -0
- package/dist/group-settings-templates.js +249 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/spreadsheet.d.ts +111 -0
- package/dist/spreadsheet.js +330 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cyc-seattle/gsuite-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typed wrappers around Google Workspace APIs: Calendar, Sheets, the Admin SDK Directory, and Group Settings.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @cyc-seattle/gsuite-sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Every client takes a `GoogleAuth` instance from `googleapis`. It uses Application Default Credentials (ADC) unless you pass a key. Set `scopes` for the APIs you call.
|
|
10
|
+
|
|
11
|
+
## Spreadsheets
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { SpreadsheetClient, type Row } from "@cyc-seattle/gsuite-sdk";
|
|
15
|
+
import { google } from "googleapis";
|
|
16
|
+
|
|
17
|
+
interface Camp extends Row {
|
|
18
|
+
name: string;
|
|
19
|
+
start: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const auth = new google.auth.GoogleAuth({ scopes: ["https://www.googleapis.com/auth/spreadsheets"] });
|
|
23
|
+
const spreadsheet = await new SpreadsheetClient(auth).loadSpreadsheet("<spreadsheet URL or ID>");
|
|
24
|
+
const worksheet = await spreadsheet.getOrCreateWorksheet<Camp>("Camps", ["name", "start"]);
|
|
25
|
+
const table = await worksheet.getTable();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Calendar
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { CalendarClient } from "@cyc-seattle/gsuite-sdk";
|
|
32
|
+
|
|
33
|
+
const calendar = new CalendarClient(auth).loadCalendar("<calendar ID>");
|
|
34
|
+
const events = await calendar.listEvents();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Directory and Group Settings
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { DirectoryClient, GroupSettingsClient } from "@cyc-seattle/gsuite-sdk";
|
|
41
|
+
|
|
42
|
+
const auth = new google.auth.GoogleAuth({
|
|
43
|
+
scopes: [
|
|
44
|
+
"https://www.googleapis.com/auth/admin.directory.group",
|
|
45
|
+
"https://www.googleapis.com/auth/apps.groups.settings",
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
const members = await new DirectoryClient(auth).listMembers("team@example.org");
|
|
49
|
+
const settings = await new GroupSettingsClient(auth).getSettings("team@example.org");
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Retries
|
|
53
|
+
|
|
54
|
+
`safeCall` wraps a request. It waits between requests and retries transient errors, such as rate limits and 5xx responses.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { safeCall } from "@cyc-seattle/gsuite-sdk";
|
|
58
|
+
|
|
59
|
+
await safeCall(() => row.save());
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Release
|
|
63
|
+
|
|
64
|
+
Every merge to `main` releases from the PR title. The title must be a conventional commit. `fix:` makes a patch release, `feat:` a minor release, and a `!` after the type a major release. Types such as `docs:`, `build:`, and `test:` release nothing. semantic-release publishes to npm and creates the GitHub release.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { calendar_v3 } from "googleapis";
|
|
2
|
+
import { Auth } from "googleapis";
|
|
3
|
+
import { DateTime } from "luxon";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a calendar event
|
|
6
|
+
*/
|
|
7
|
+
export interface CalendarEvent {
|
|
8
|
+
/** Unique identifier for the event */
|
|
9
|
+
id: string;
|
|
10
|
+
title: string;
|
|
11
|
+
startTime: DateTime;
|
|
12
|
+
endTime: DateTime;
|
|
13
|
+
allDay: boolean;
|
|
14
|
+
description?: string;
|
|
15
|
+
location?: string;
|
|
16
|
+
/** Additional metadata */
|
|
17
|
+
metadata?: Record<string, string> | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Google Calendar API client
|
|
21
|
+
*/
|
|
22
|
+
export declare class CalendarClient {
|
|
23
|
+
private client;
|
|
24
|
+
constructor(auth: Auth.GoogleAuth);
|
|
25
|
+
loadCalendar(calendarId: string): Calendar;
|
|
26
|
+
}
|
|
27
|
+
export declare class Calendar {
|
|
28
|
+
private client;
|
|
29
|
+
readonly calendarId: string;
|
|
30
|
+
constructor(client: calendar_v3.Calendar, calendarId: string);
|
|
31
|
+
/**
|
|
32
|
+
* Gets a single event by ID
|
|
33
|
+
* Returns null if the event doesn't exist
|
|
34
|
+
*/
|
|
35
|
+
getEvent(eventId: string): Promise<CalendarEvent | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Lists all events in a calendar within a date range
|
|
38
|
+
*/
|
|
39
|
+
listEvents(timeMin?: DateTime, timeMax?: DateTime): Promise<CalendarEvent[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new event in the calendar
|
|
42
|
+
*/
|
|
43
|
+
createEvent(calendarId: string, event: Omit<CalendarEvent, "id">): Promise<CalendarEvent>;
|
|
44
|
+
/**
|
|
45
|
+
* Updates an existing event in the calendar
|
|
46
|
+
*/
|
|
47
|
+
updateEvent(calendarId: string, event: CalendarEvent): Promise<CalendarEvent>;
|
|
48
|
+
}
|
package/dist/calendar.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { google } from "googleapis";
|
|
2
|
+
import { safeCall } from "./common.js";
|
|
3
|
+
import { DateTime } from "luxon";
|
|
4
|
+
import winston from "winston";
|
|
5
|
+
/**
|
|
6
|
+
* Google Calendar API client
|
|
7
|
+
*/
|
|
8
|
+
export class CalendarClient {
|
|
9
|
+
client;
|
|
10
|
+
constructor(auth) {
|
|
11
|
+
this.client = google.calendar({ version: "v3", auth });
|
|
12
|
+
}
|
|
13
|
+
loadCalendar(calendarId) {
|
|
14
|
+
return new Calendar(this.client, calendarId);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class Calendar {
|
|
18
|
+
client;
|
|
19
|
+
calendarId;
|
|
20
|
+
constructor(client, calendarId) {
|
|
21
|
+
this.client = client;
|
|
22
|
+
this.calendarId = calendarId;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets a single event by ID
|
|
26
|
+
* Returns null if the event doesn't exist
|
|
27
|
+
*/
|
|
28
|
+
async getEvent(eventId) {
|
|
29
|
+
winston.debug("Getting calendar event", { calendar: this.calendarId, eventId });
|
|
30
|
+
try {
|
|
31
|
+
const response = await safeCall(async () => this.client.events.get({
|
|
32
|
+
calendarId: this.calendarId,
|
|
33
|
+
eventId,
|
|
34
|
+
}));
|
|
35
|
+
if (!response.data.id || response.data.status === "cancelled") {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
winston.debug("Found calendar event", { eventId: response.data.id });
|
|
39
|
+
return convertToCalendarEvent(response.data);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// If event doesn't exist, return null instead of throwing
|
|
43
|
+
if (error.code === 404) {
|
|
44
|
+
winston.debug("Event not found", { eventId });
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Lists all events in a calendar within a date range
|
|
52
|
+
*/
|
|
53
|
+
async listEvents(timeMin, timeMax) {
|
|
54
|
+
winston.debug("Listing calendar events", { calendar: this.calendarId, timeMin, timeMax });
|
|
55
|
+
// Typed explicitly, and the optional bounds assigned only when present: given a bare object
|
|
56
|
+
// literal TypeScript resolves `events.list` to its callback-style overload and infers `void`,
|
|
57
|
+
// and under exactOptionalPropertyTypes an explicit `undefined` is not assignable to `timeMin?`.
|
|
58
|
+
const params = {
|
|
59
|
+
calendarId: this.calendarId,
|
|
60
|
+
singleEvents: true,
|
|
61
|
+
orderBy: "startTime",
|
|
62
|
+
};
|
|
63
|
+
const min = timeMin?.toISO();
|
|
64
|
+
if (min) {
|
|
65
|
+
params.timeMin = min;
|
|
66
|
+
}
|
|
67
|
+
const max = timeMax?.toISO();
|
|
68
|
+
if (max) {
|
|
69
|
+
params.timeMax = max;
|
|
70
|
+
}
|
|
71
|
+
const data = await safeCall(async () => {
|
|
72
|
+
const res = await this.client.events.list(params);
|
|
73
|
+
return res.data;
|
|
74
|
+
});
|
|
75
|
+
const events = data.items ?? [];
|
|
76
|
+
winston.info(`Found ${events.length} events in calendar`);
|
|
77
|
+
return events
|
|
78
|
+
.filter((event) => Boolean(event.start && event.id))
|
|
79
|
+
.map((event) => convertToCalendarEvent(event));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new event in the calendar
|
|
83
|
+
*/
|
|
84
|
+
async createEvent(calendarId, event) {
|
|
85
|
+
winston.debug("Creating calendar event", { calendar: this.calendarId, event });
|
|
86
|
+
const response = await safeCall(async () => this.client.events.insert({
|
|
87
|
+
calendarId,
|
|
88
|
+
requestBody: convertToGoogleEvent(event),
|
|
89
|
+
}));
|
|
90
|
+
if (!response.data.id) {
|
|
91
|
+
throw new Error("Failed to create event: no ID returned");
|
|
92
|
+
}
|
|
93
|
+
winston.info("Created calendar event", { eventId: response.data.id });
|
|
94
|
+
return convertToCalendarEvent(response.data);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Updates an existing event in the calendar
|
|
98
|
+
*/
|
|
99
|
+
async updateEvent(calendarId, event) {
|
|
100
|
+
winston.debug("Updating calendar event", { calendar: calendarId, eventId: event.id });
|
|
101
|
+
const response = await safeCall(async () => this.client.events.update({
|
|
102
|
+
calendarId,
|
|
103
|
+
eventId: event.id,
|
|
104
|
+
requestBody: convertToGoogleEvent(event),
|
|
105
|
+
}));
|
|
106
|
+
if (!response.data.id) {
|
|
107
|
+
throw new Error("Failed to update event: no ID returned");
|
|
108
|
+
}
|
|
109
|
+
winston.info("Updated calendar event", { eventId: response.data.id });
|
|
110
|
+
return convertToCalendarEvent(response.data);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function convertToCalendarEvent(event) {
|
|
114
|
+
const allDay = !event.start?.dateTime;
|
|
115
|
+
const startTime = allDay ? DateTime.fromISO(event.start?.date ?? "") : DateTime.fromISO(event.start?.dateTime ?? "");
|
|
116
|
+
const endTime = allDay ? DateTime.fromISO(event.end?.date ?? "") : DateTime.fromISO(event.end?.dateTime ?? "");
|
|
117
|
+
const calEvent = {
|
|
118
|
+
id: event.id,
|
|
119
|
+
title: event.summary ?? "Untitled Event",
|
|
120
|
+
startTime,
|
|
121
|
+
endTime,
|
|
122
|
+
allDay,
|
|
123
|
+
};
|
|
124
|
+
if (event.description) {
|
|
125
|
+
calEvent.description = event.description;
|
|
126
|
+
}
|
|
127
|
+
if (event.location) {
|
|
128
|
+
calEvent.location = event.location;
|
|
129
|
+
}
|
|
130
|
+
if (event.extendedProperties?.private) {
|
|
131
|
+
calEvent.metadata = event.extendedProperties.private;
|
|
132
|
+
}
|
|
133
|
+
return calEvent;
|
|
134
|
+
}
|
|
135
|
+
function convertToGoogleEvent(event) {
|
|
136
|
+
const googleEvent = {
|
|
137
|
+
summary: event.title ?? null,
|
|
138
|
+
description: event.description ?? null,
|
|
139
|
+
location: event.location ?? null,
|
|
140
|
+
};
|
|
141
|
+
if (event.startTime && event.endTime) {
|
|
142
|
+
if (event.allDay) {
|
|
143
|
+
googleEvent.start = { date: event.startTime.toISODate() ?? null };
|
|
144
|
+
googleEvent.end = { date: event.endTime.toISODate() ?? null };
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
googleEvent.start = { dateTime: event.startTime.toISO() ?? null };
|
|
148
|
+
googleEvent.end = { dateTime: event.endTime.toISO() ?? null };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (event.metadata) {
|
|
152
|
+
googleEvent.extendedProperties = {
|
|
153
|
+
private: event.metadata,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return googleEvent;
|
|
157
|
+
}
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the HTTP status code from an error thrown by a Google API client.
|
|
3
|
+
*
|
|
4
|
+
* The two client libraries in use report the status differently:
|
|
5
|
+
* - `google-spreadsheet` uses axios, which exposes it as `error.response.status`.
|
|
6
|
+
* - `googleapis` uses gaxios, which additionally sets a numeric `error.code`
|
|
7
|
+
* (and `error.status` on newer versions).
|
|
8
|
+
*
|
|
9
|
+
* Returns `undefined` when no HTTP status can be determined (e.g. a plain
|
|
10
|
+
* programming error or a network failure without a response).
|
|
11
|
+
*/
|
|
12
|
+
export declare function getHttpStatus(error: unknown): number | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Decides whether a failed Google API request should be retried.
|
|
15
|
+
*
|
|
16
|
+
* Only transient conditions are retryable:
|
|
17
|
+
* - HTTP 429 (rate limited)
|
|
18
|
+
* - HTTP 5xx (server errors)
|
|
19
|
+
*
|
|
20
|
+
* Everything else — most notably 4xx client errors like 401/403 (auth /
|
|
21
|
+
* permission) and 404 (not found) — is a hard failure that will never succeed
|
|
22
|
+
* on retry, so we fail fast instead of waiting out the backoff delay.
|
|
23
|
+
*
|
|
24
|
+
* Errors with no discernible HTTP status (e.g. network errors) are treated as
|
|
25
|
+
* transient and retried, since those are frequently recoverable.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isRetryableError(error: unknown): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Rate-limited wrapper for Google API calls with exponential backoff retry.
|
|
30
|
+
*
|
|
31
|
+
* Retries only transient failures (see {@link isRetryableError}); non-retryable
|
|
32
|
+
* errors are re-thrown immediately rather than incurring the backoff delay.
|
|
33
|
+
*/
|
|
34
|
+
export declare function safeCall<T>(request: () => Promise<T>): Promise<T>;
|
package/dist/common.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
import { backOff } from "exponential-backoff";
|
|
3
|
+
import { setTimeout } from "timers/promises";
|
|
4
|
+
// The default quota on Google Spreadsheet API is 60 requests/minute/user.
|
|
5
|
+
// However, there's a separate read and write quota, so we can safely double
|
|
6
|
+
// this to get a little bit more throughput.
|
|
7
|
+
const requestPerMinute = 60 * 2;
|
|
8
|
+
const millisPerRequest = (60 * 1000) / requestPerMinute;
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the HTTP status code from an error thrown by a Google API client.
|
|
11
|
+
*
|
|
12
|
+
* The two client libraries in use report the status differently:
|
|
13
|
+
* - `google-spreadsheet` uses axios, which exposes it as `error.response.status`.
|
|
14
|
+
* - `googleapis` uses gaxios, which additionally sets a numeric `error.code`
|
|
15
|
+
* (and `error.status` on newer versions).
|
|
16
|
+
*
|
|
17
|
+
* Returns `undefined` when no HTTP status can be determined (e.g. a plain
|
|
18
|
+
* programming error or a network failure without a response).
|
|
19
|
+
*/
|
|
20
|
+
export function getHttpStatus(error) {
|
|
21
|
+
if (typeof error !== "object" || error === null) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const candidate = error;
|
|
25
|
+
const response = candidate.response;
|
|
26
|
+
if (response && typeof response.status === "number") {
|
|
27
|
+
return response.status;
|
|
28
|
+
}
|
|
29
|
+
if (typeof candidate.status === "number") {
|
|
30
|
+
return candidate.status;
|
|
31
|
+
}
|
|
32
|
+
// gaxios sets `code` to the numeric HTTP status (older versions may set it to
|
|
33
|
+
// a string like "ERR_BAD_REQUEST", which we ignore here).
|
|
34
|
+
if (typeof candidate.code === "number") {
|
|
35
|
+
return candidate.code;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Decides whether a failed Google API request should be retried.
|
|
41
|
+
*
|
|
42
|
+
* Only transient conditions are retryable:
|
|
43
|
+
* - HTTP 429 (rate limited)
|
|
44
|
+
* - HTTP 5xx (server errors)
|
|
45
|
+
*
|
|
46
|
+
* Everything else — most notably 4xx client errors like 401/403 (auth /
|
|
47
|
+
* permission) and 404 (not found) — is a hard failure that will never succeed
|
|
48
|
+
* on retry, so we fail fast instead of waiting out the backoff delay.
|
|
49
|
+
*
|
|
50
|
+
* Errors with no discernible HTTP status (e.g. network errors) are treated as
|
|
51
|
+
* transient and retried, since those are frequently recoverable.
|
|
52
|
+
*/
|
|
53
|
+
export function isRetryableError(error) {
|
|
54
|
+
const status = getHttpStatus(error);
|
|
55
|
+
if (status === undefined) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (status === 429) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return status >= 500 && status <= 599;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Rate-limited wrapper for Google API calls with exponential backoff retry.
|
|
65
|
+
*
|
|
66
|
+
* Retries only transient failures (see {@link isRetryableError}); non-retryable
|
|
67
|
+
* errors are re-thrown immediately rather than incurring the backoff delay.
|
|
68
|
+
*/
|
|
69
|
+
export async function safeCall(request) {
|
|
70
|
+
await setTimeout(millisPerRequest);
|
|
71
|
+
return backOff(request, {
|
|
72
|
+
jitter: "full",
|
|
73
|
+
numOfAttempts: 2,
|
|
74
|
+
startingDelay: 60_000,
|
|
75
|
+
retry: (error) => {
|
|
76
|
+
if (!isRetryableError(error)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
winston.warn("Transient error from Google API. Retrying", {
|
|
80
|
+
status: getHttpStatus(error),
|
|
81
|
+
});
|
|
82
|
+
return true;
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Auth, groupssettings_v1 } from "googleapis";
|
|
2
|
+
export type GroupRole = "MEMBER" | "MANAGER" | "OWNER";
|
|
3
|
+
export interface Group {
|
|
4
|
+
id: string;
|
|
5
|
+
email: string;
|
|
6
|
+
name?: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Directory API's `type` on a member: `GROUP` is what tells a nested sub-group apart from a person. */
|
|
10
|
+
export type GroupMemberType = "USER" | "GROUP" | "EXTERNAL" | "CUSTOMER";
|
|
11
|
+
export interface GroupMember {
|
|
12
|
+
email: string;
|
|
13
|
+
role: GroupRole;
|
|
14
|
+
type?: GroupMemberType;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Result of adding a member to a group. The sync that drives this client is
|
|
18
|
+
* add-only and idempotent, so a member who's already there is an expected
|
|
19
|
+
* outcome, not a failure — Google reports it as an HTTP 409.
|
|
20
|
+
*/
|
|
21
|
+
export type AddMemberResult = "added" | "already-member";
|
|
22
|
+
/**
|
|
23
|
+
* Admin SDK Directory API client for groups and their members.
|
|
24
|
+
*
|
|
25
|
+
* A sub-group is added to another group the same way a user is: as a member
|
|
26
|
+
* with the sub-group's email address. There's no separate method for it.
|
|
27
|
+
*/
|
|
28
|
+
export declare class DirectoryClient {
|
|
29
|
+
private client;
|
|
30
|
+
constructor(auth: Auth.GoogleAuth);
|
|
31
|
+
/**
|
|
32
|
+
* Gets a group by email or ID. Returns null if the group doesn't exist.
|
|
33
|
+
*/
|
|
34
|
+
getGroup(groupKey: string): Promise<Group | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Lists every group for the given customer id, following pagination. No default: the
|
|
37
|
+
* `my_customer` alias resolves relative to the *authenticated user's* domain, which doesn't
|
|
38
|
+
* exist for a service account holding a direct admin role rather than impersonating a domain
|
|
39
|
+
* user - that path 404s ("Domain not found") instead of listing anything.
|
|
40
|
+
*/
|
|
41
|
+
listGroups(customer: string): Promise<Group[]>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new group.
|
|
44
|
+
*/
|
|
45
|
+
createGroup(email: string, name?: string, description?: string): Promise<Group>;
|
|
46
|
+
/**
|
|
47
|
+
* Lists all members of a group, following pagination.
|
|
48
|
+
*/
|
|
49
|
+
listMembers(groupKey: string): Promise<GroupMember[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Adds a member to a group with the given role. Returns "already-member"
|
|
52
|
+
* instead of throwing when the member is already in the group (HTTP 409),
|
|
53
|
+
* since the sync that calls this treats that as success - but first promotes
|
|
54
|
+
* them if they're already there at a lower role than intended (e.g. added by
|
|
55
|
+
* hand as a plain MEMBER). Never demotes: a member already at a higher role
|
|
56
|
+
* than planned is left alone.
|
|
57
|
+
*/
|
|
58
|
+
addMember(groupKey: string, email: string, role: GroupRole): Promise<AddMemberResult>;
|
|
59
|
+
/** The 409 path of `addMember`: promotes an existing member up to `role` if they're currently
|
|
60
|
+
* below it, and does nothing otherwise - including when they already outrank it. */
|
|
61
|
+
private promoteIfBelow;
|
|
62
|
+
/**
|
|
63
|
+
* Updates an existing member's role.
|
|
64
|
+
*/
|
|
65
|
+
updateMemberRole(groupKey: string, email: string, role: GroupRole): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
export type GroupSettings = groupssettings_v1.Schema$Groups;
|
|
68
|
+
/**
|
|
69
|
+
* Groups Settings API client. Settings are a separate API from the Directory
|
|
70
|
+
* API's group resource — this covers permission-level fields like who can
|
|
71
|
+
* post or join, not membership.
|
|
72
|
+
*/
|
|
73
|
+
export declare class GroupSettingsClient {
|
|
74
|
+
private client;
|
|
75
|
+
constructor(auth: Auth.GoogleAuth);
|
|
76
|
+
/**
|
|
77
|
+
* Gets a group's settings.
|
|
78
|
+
*/
|
|
79
|
+
getSettings(groupEmail: string): Promise<GroupSettings>;
|
|
80
|
+
/**
|
|
81
|
+
* Patches a group's settings from the given settings object.
|
|
82
|
+
*/
|
|
83
|
+
patchSettings(groupEmail: string, settings: GroupSettings): Promise<GroupSettings>;
|
|
84
|
+
}
|