@cyc-seattle/gsuite-sdk 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -24,6 +24,8 @@ const members = await new DirectoryClient(auth).listMembers("team@example.org");
24
24
  const settings = await new GroupSettingsClient(auth).getSettings("team@example.org");
25
25
  ```
26
26
 
27
+ `addMember` and `removeMember` are idempotent: adding someone already in a group answers `"already-member"`, raising their role if it's lower, and taking out someone who isn't there answers `"not-member"`. Taking someone out of a group that doesn't exist throws.
28
+
27
29
  ### Client options
28
30
 
29
31
  `DirectoryClient` and `GroupSettingsClient` take an optional second argument:
@@ -24,11 +24,12 @@ export interface GroupMember {
24
24
  type?: GroupMemberType;
25
25
  }
26
26
  /**
27
- * Result of adding a member to a group. The sync that drives this client is
28
- * add-only and idempotent, so a member who's already there is an expected
29
- * outcome, not a failure — Google reports it as an HTTP 409.
27
+ * Result of adding a member to a group. Adding is idempotent, so a member who's
28
+ * already there is an expected outcome, not a failure — Google reports it as an HTTP 409.
30
29
  */
31
30
  export type AddMemberResult = "added" | "already-member";
31
+ /** Result of taking a member out of a group. Someone who isn't there (HTTP 404) is already out. */
32
+ export type RemoveMemberResult = "removed" | "not-member";
32
33
  /**
33
34
  * Admin SDK Directory API client for groups and their members.
34
35
  *
@@ -67,6 +68,11 @@ export declare class DirectoryClient {
67
68
  * than planned is left alone.
68
69
  */
69
70
  addMember(groupKey: string, email: string, role: GroupRole): Promise<AddMemberResult>;
71
+ /**
72
+ * Takes a member out of a group. Returns "not-member" instead of throwing when they aren't in it
73
+ * (HTTP 404), since the caller wants them out either way. A missing group throws.
74
+ */
75
+ removeMember(groupKey: string, email: string): Promise<RemoveMemberResult>;
70
76
  /** The 409 path of `addMember`: promotes an existing member up to `role` if they're currently
71
77
  * below it, and does nothing otherwise - including when they already outrank it. */
72
78
  private promoteIfBelow;
package/dist/directory.js CHANGED
@@ -140,6 +140,27 @@ export class DirectoryClient {
140
140
  throw error;
141
141
  }
142
142
  }
143
+ /**
144
+ * Takes a member out of a group. Returns "not-member" instead of throwing when they aren't in it
145
+ * (HTTP 404), since the caller wants them out either way. A missing group throws.
146
+ */
147
+ async removeMember(groupKey, email) {
148
+ winston.debug("Removing group member", { groupKey, email });
149
+ try {
150
+ await safeCall(async () => {
151
+ await this.client.members.delete({ groupKey, memberKey: email });
152
+ }, this.pacing);
153
+ winston.info("Removed group member", { groupKey, email });
154
+ return "removed";
155
+ }
156
+ catch (error) {
157
+ if (getHttpStatus(error) === 404 && (await this.getGroup(groupKey)) !== null) {
158
+ winston.debug("Member not in group", { groupKey, email });
159
+ return "not-member";
160
+ }
161
+ throw error;
162
+ }
163
+ }
143
164
  /** The 409 path of `addMember`: promotes an existing member up to `role` if they're currently
144
165
  * below it, and does nothing otherwise - including when they already outrank it. */
145
166
  async promoteIfBelow(groupKey, email, role) {
@@ -77,6 +77,7 @@ export declare class WorkspaceModel {
77
77
  updateMember(groupKey: string, memberKey: string, body: {
78
78
  role?: string | null;
79
79
  }): FakeMember;
80
+ deleteMember(groupKey: string, memberKey: string): void;
80
81
  getSettings(groupUniqueId: string): GroupSettings;
81
82
  patchSettings(groupUniqueId: string, patch: GroupSettings): GroupSettings;
82
83
  private requireGroup;
@@ -174,6 +174,14 @@ export class WorkspaceModel {
174
174
  }
175
175
  return this.toMember(member);
176
176
  }
177
+ deleteMember(groupKey, memberKey) {
178
+ const group = this.requireGroup(groupKey);
179
+ const member = this.requireMember(group, memberKey);
180
+ for (const [key, stored] of group.members) {
181
+ if (stored === member)
182
+ group.members.delete(key);
183
+ }
184
+ }
177
185
  getSettings(groupUniqueId) {
178
186
  return this.toSettings(this.requireSettingsGroup(groupUniqueId));
179
187
  }
@@ -47,6 +47,11 @@ const ROUTES = [
47
47
  path: new RegExp(`^/admin/directory/v1/groups/${SEGMENT}/members/${SEGMENT}$`),
48
48
  handle: (model, [groupKey, memberKey], _, body) => model.updateMember(groupKey, memberKey, asObject(body)),
49
49
  },
50
+ {
51
+ method: "DELETE",
52
+ path: new RegExp(`^/admin/directory/v1/groups/${SEGMENT}/members/${SEGMENT}$`),
53
+ handle: (model, [groupKey, memberKey]) => model.deleteMember(groupKey, memberKey),
54
+ },
50
55
  {
51
56
  method: "GET",
52
57
  path: new RegExp(`^/groups/v1/groups/${SEGMENT}$`),
@@ -116,7 +121,11 @@ async function handle(model, request, response) {
116
121
  }
117
122
  try {
118
123
  const result = route.handle(model, match, url, body);
119
- if (url.pathname.startsWith("/groups/v1/") && url.searchParams.get("alt") !== "json") {
124
+ if (result === undefined) {
125
+ // A delete answers with no body, as Google's does.
126
+ response.writeHead(204).end();
127
+ }
128
+ else if (url.pathname.startsWith("/groups/v1/") && url.searchParams.get("alt") !== "json") {
120
129
  sendAtom(response, result);
121
130
  }
122
131
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyc-seattle/gsuite-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Domain-agnostic wrappers around Google Workspace APIs",
5
5
  "author": "CYC Community Sailing Center",
6
6
  "license": "Apache-2.0",