@meetreeve/module-team 0.0.0-stage → 0.3.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
@@ -1,3 +1,90 @@
1
- # Temporary Holding Version
1
+ # @meetreeve/module-team
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Reeve.Team module (DEV-9998, C2 of epic DEV-9983). Two halves:
4
+
5
+ * **`.` / `./client`**: a framework-agnostic, server-safe typed client,
6
+ `createTeamClient({ fetch })`, over the org-members routes. No React, no
7
+ Next.js.
8
+ * **`./config`**: `TeamMembersPanel`, the operator surface: members with a
9
+ role picker and remove, the invite dialog, pending invitations with
10
+ revoke/resend, and the seat count.
11
+
12
+ Versioned in lockstep with the Python `reeve-module-team` package. Both ship
13
+ the same MAJOR.MINOR.PATCH.
14
+
15
+ | Method | Route |
16
+ | --- | --- |
17
+ | `listMembers(orgId)` | `GET /api/org/members` |
18
+ | `updateMemberRole(orgId, memberId, role)` | `PUT /api/org/members/{id}/role` |
19
+ | `removeMember(orgId, memberId)` | `DELETE /api/org/members/{id}` |
20
+ | `inviteMember(orgId, email, role, spaces?)` | `POST /api/org/invite` (`spaces` sent whenever passed, `[]` included) |
21
+ | `listInvitations(orgId)` | `GET /api/org/invitations` |
22
+ | `revokeInvitation(orgId, invitationId)` | `DELETE /api/org/invitations/{id}` |
23
+ | `resendInvitation(orgId, invitationId)` | `POST /api/org/invitations/{id}/resend` |
24
+
25
+ ## The transport is the host's
26
+
27
+ `createTeamClient` takes a JSON fetcher, `(path, init) => Promise<body>`,
28
+ that resolves the parsed body and rejects on a non-2xx. reeve-frontend's
29
+ `productFetch` already has that shape, and it owns auth, the 401 refresh and
30
+ timeouts. A spawned tenant passes its own.
31
+
32
+ ## The org is never ambient (DEV-8592)
33
+
34
+ Every client method takes the org it acts on and sends it as
35
+ `X-Reeve-Org-Id`. `TeamMembersPanel` requires `orgId` and keys its stateful
36
+ body on it. Switching orgs remounts the panel, so the previous org's
37
+ members, open dialogs and notices are dropped at once. A response for the
38
+ previous org that lands late is discarded.
39
+
40
+ ```tsx
41
+ import { createTeamClient } from "@meetreeve/module-team/client";
42
+ import { TeamMembersPanel } from "@meetreeve/module-team/config";
43
+
44
+ const client = createTeamClient({ fetch: (path, init) => productFetch(path, init) });
45
+
46
+ <TeamMembersPanel
47
+ client={client}
48
+ orgId={activeOrgId}
49
+ inviteRoles={["admin", "member"]} // default: admin, member, viewer
50
+ currentUserEmail={user?.email}
51
+ renderMemberActions={(m) => <BudgetButton member={m} />} // host-only extras
52
+ onNotice={(n) => toast[n.kind](n.message)} // optional; also shown inline
53
+ />;
54
+ ```
55
+
56
+ Spaces (0.2.0, DEV-15988; 0.3.0, DEV-16808): pass
57
+ `loadSpaceOptions={() => Promise<SpaceOption[]>}` and the invite dialog lists
58
+ every space, the default one included (nobody joins a space automatically).
59
+ With 2+ spaces at least one must be checked; with one there is no picker, the
60
+ dialog says "They'll get access to <Space>" and sends `spaces: []`. The invite
61
+ is held until the options load. `can_manage: false` spaces are shown
62
+ disabled. A server refusal (e.g. 422 `spaces_required`) shows inside the
63
+ dialog. `renderInvitationActions={(inv, { reload }) => ...}` is the
64
+ pending-invite twin of `renderMemberActions`; call `reload` after a change
65
+ the row shows (an invitation's `spaces` are listed on its row).
66
+
67
+ `inviteRoles` scopes both the invite dialog and the per-member role picker.
68
+ Owners get no role picker and no remove control. The role picker also offers
69
+ only the changes the backend accepts from the signed-in user, whose role is
70
+ read from the roster row matching `currentUserEmail`: an owner may move a
71
+ member to any offered role, an admin only to roles below admin and only on
72
+ rows ranked below admin, and anyone else gets plain role labels. Without a
73
+ matching `currentUserEmail` every role is a plain label. A refused change
74
+ shows the backend's `detail`. The backend still enforces every rule.
75
+
76
+ The panel has no runtime dependencies beyond `react`/`react-dom`. It is
77
+ styled with Tailwind utility classes over shadcn tokens (`bg-card`,
78
+ `text-muted-foreground`, `border-border`, ...), so the host's Tailwind must
79
+ scan this package.
80
+
81
+ ## Development
82
+
83
+ ```bash
84
+ npm ci && npm test && npm run typecheck && npm run build && npm run check:esm
85
+ ```
86
+
87
+ reeve-frontend aliases `@meetreeve/module-team/{config,client}` to this
88
+ package's source until the first publish. Running `npm ci` here installs a
89
+ second React under `packages/module-team/node_modules`. Move that directory
90
+ aside before you run the root vitest suite.
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Roles the org-members router knows. `billing_manager` is a legacy alias
3
+ * the backend still accepts; no surface offers it from the invite dialog.
4
+ * Rows may still carry a role this union doesn't name, so row fields are
5
+ * typed `string` and the panel renders unknown roles verbatim.
6
+ */
7
+ type TeamRole = "owner" | "admin" | "billing_manager" | "member" | "viewer";
8
+ /** A role the invite dialog and the role picker can hand out (never `owner`). */
9
+ type AssignableRole = Exclude<TeamRole, "owner" | "billing_manager">;
10
+ /** One live member of the org (`OrganizationMember` row). `GET /api/org/members`. */
11
+ interface TeamMember {
12
+ /** OrganizationMember.id — the id every member route takes. */
13
+ id: string;
14
+ user_id: string;
15
+ email: string | null;
16
+ role: string;
17
+ is_default: boolean;
18
+ /** ISO-8601. */
19
+ created_at: string;
20
+ }
21
+ /** One invitation record. `GET /api/org/invitations`. */
22
+ interface Invitation {
23
+ id: string;
24
+ email: string;
25
+ role: string;
26
+ invited_by_email: string | null;
27
+ /** pending | accepted | expired | revoked */
28
+ status: string;
29
+ /** ISO-8601. */
30
+ created_at: string;
31
+ /** ISO-8601. */
32
+ expires_at: string;
33
+ accepted_at: string | null;
34
+ /**
35
+ * Spaces the invitee joins on accept (DEV-15988 / reeve-services DEV-15559).
36
+ * The backend always sends it; optional so a backend that predates it
37
+ * still type-checks as "no pre-set spaces".
38
+ */
39
+ spaces?: InvitationSpace[];
40
+ }
41
+ /** A space role. */
42
+ type SpaceRole = "member" | "admin";
43
+ /** One pre-set space on an invitation (`GET /api/org/invitations` item). */
44
+ interface InvitationSpace {
45
+ id: string;
46
+ name: string;
47
+ role: SpaceRole;
48
+ }
49
+ /** One space an invite (or a pending invitation) should land the invitee in. */
50
+ interface InviteSpace {
51
+ space_id: string;
52
+ role: SpaceRole;
53
+ }
54
+ /**
55
+ * One of the org's spaces, as the invite dialog's Spaces picker lists it
56
+ * (reeve-frontend maps `GET /api/org/rbac/spaces` onto this). Every space is
57
+ * offered, the default one included: nobody joins a space automatically
58
+ * (DEV-16808). `can_manage: false` rows are shown disabled.
59
+ */
60
+ interface SpaceOption {
61
+ id: string;
62
+ name: string;
63
+ kind?: string;
64
+ is_default: boolean;
65
+ can_manage: boolean;
66
+ }
67
+ /** `POST /api/org/invite`. */
68
+ interface InviteMemberResponse {
69
+ message: string;
70
+ email: string;
71
+ role: string;
72
+ invitation_id?: string;
73
+ }
74
+ /**
75
+ * The data-access boundary the panel is built against. Every method takes
76
+ * the org it acts on: there is no ambient "current org" anywhere in this
77
+ * package, so a request can never be issued for an org other than the one
78
+ * the caller named (DEV-8592). `createTeamClient` is the reference
79
+ * implementation; a host may supply its own.
80
+ */
81
+ interface TeamClient {
82
+ listMembers(orgId: string): Promise<TeamMember[]>;
83
+ updateMemberRole(orgId: string, memberId: string, role: string): Promise<void>;
84
+ removeMember(orgId: string, memberId: string): Promise<void>;
85
+ /**
86
+ * `spaces` goes in the body whenever it is passed, `[]` included (a
87
+ * one-space org sends `[]`, DEV-16808); omitted keeps the pre-DEV-15988
88
+ * request.
89
+ */
90
+ inviteMember(orgId: string, email: string, role: string, spaces?: InviteSpace[]): Promise<InviteMemberResponse>;
91
+ listInvitations(orgId: string): Promise<Invitation[]>;
92
+ revokeInvitation(orgId: string, invitationId: string): Promise<void>;
93
+ resendInvitation(orgId: string, invitationId: string): Promise<void>;
94
+ }
95
+
96
+ /**
97
+ * Framework-agnostic Reeve.Team client (DEV-9998, C2 of epic DEV-9983).
98
+ *
99
+ * The server-safe half: no React, no Next.js. The only import is type-only,
100
+ * erased at build time.
101
+ *
102
+ * Contract: reeve-services' org-members router, ported route-for-route from
103
+ * reeve-frontend's `src/lib/api/team.ts`:
104
+ *
105
+ * GET /api/org/members -> TeamMember[]
106
+ * PUT /api/org/members/{id}/role {role} -> 2xx
107
+ * DELETE /api/org/members/{id} -> 2xx
108
+ * POST /api/org/invite {email, role, spaces?} -> InviteMemberResponse
109
+ * GET /api/org/invitations -> Invitation[]
110
+ * DELETE /api/org/invitations/{id} -> 2xx
111
+ * POST /api/org/invitations/{id}/resend -> 2xx (resets the 7-day expiry)
112
+ *
113
+ * Transport is the HOST's: `createTeamClient({ fetch })` takes a JSON
114
+ * fetcher (reeve-frontend passes `productFetch`, which owns auth, 401
115
+ * refresh and timeouts). The org is never ambient. Every method takes the
116
+ * org it acts on and stamps it as `X-Reeve-Org-Id`, the header the backend's
117
+ * tenant resolver reads, so a request is keyed on the org its caller named
118
+ * rather than on whatever org a global pin held when it went out (DEV-8592).
119
+ */
120
+
121
+ /** The request header the backend tenant resolver reads the org assertion from. */
122
+ declare const ORG_HEADER = "X-Reeve-Org-Id";
123
+ /** What the client hands the host fetcher. `path` is relative to the API origin. */
124
+ type TeamRequestInit = {
125
+ method: "GET" | "POST" | "PUT" | "DELETE";
126
+ headers: Record<string, string>;
127
+ /** JSON-encoded, present only on requests that carry a body. */
128
+ body?: string;
129
+ };
130
+ /**
131
+ * A host-supplied JSON fetcher: resolve with the parsed response body (any
132
+ * value for an empty 2xx), reject on a non-2xx. reeve-frontend's
133
+ * `productFetch` has exactly this shape.
134
+ */
135
+ type TeamFetch = (path: string, init: TeamRequestInit) => Promise<unknown>;
136
+ type TeamClientOptions = {
137
+ fetch: TeamFetch;
138
+ };
139
+ declare function createTeamClient({ fetch }: TeamClientOptions): TeamClient;
140
+
141
+ export { type AssignableRole, type Invitation, type InvitationSpace, type InviteMemberResponse, type InviteSpace, ORG_HEADER, type SpaceOption, type SpaceRole, type TeamClient, type TeamClientOptions, type TeamFetch, type TeamMember, type TeamRequestInit, type TeamRole, createTeamClient };
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Roles the org-members router knows. `billing_manager` is a legacy alias
3
+ * the backend still accepts; no surface offers it from the invite dialog.
4
+ * Rows may still carry a role this union doesn't name, so row fields are
5
+ * typed `string` and the panel renders unknown roles verbatim.
6
+ */
7
+ type TeamRole = "owner" | "admin" | "billing_manager" | "member" | "viewer";
8
+ /** A role the invite dialog and the role picker can hand out (never `owner`). */
9
+ type AssignableRole = Exclude<TeamRole, "owner" | "billing_manager">;
10
+ /** One live member of the org (`OrganizationMember` row). `GET /api/org/members`. */
11
+ interface TeamMember {
12
+ /** OrganizationMember.id — the id every member route takes. */
13
+ id: string;
14
+ user_id: string;
15
+ email: string | null;
16
+ role: string;
17
+ is_default: boolean;
18
+ /** ISO-8601. */
19
+ created_at: string;
20
+ }
21
+ /** One invitation record. `GET /api/org/invitations`. */
22
+ interface Invitation {
23
+ id: string;
24
+ email: string;
25
+ role: string;
26
+ invited_by_email: string | null;
27
+ /** pending | accepted | expired | revoked */
28
+ status: string;
29
+ /** ISO-8601. */
30
+ created_at: string;
31
+ /** ISO-8601. */
32
+ expires_at: string;
33
+ accepted_at: string | null;
34
+ /**
35
+ * Spaces the invitee joins on accept (DEV-15988 / reeve-services DEV-15559).
36
+ * The backend always sends it; optional so a backend that predates it
37
+ * still type-checks as "no pre-set spaces".
38
+ */
39
+ spaces?: InvitationSpace[];
40
+ }
41
+ /** A space role. */
42
+ type SpaceRole = "member" | "admin";
43
+ /** One pre-set space on an invitation (`GET /api/org/invitations` item). */
44
+ interface InvitationSpace {
45
+ id: string;
46
+ name: string;
47
+ role: SpaceRole;
48
+ }
49
+ /** One space an invite (or a pending invitation) should land the invitee in. */
50
+ interface InviteSpace {
51
+ space_id: string;
52
+ role: SpaceRole;
53
+ }
54
+ /**
55
+ * One of the org's spaces, as the invite dialog's Spaces picker lists it
56
+ * (reeve-frontend maps `GET /api/org/rbac/spaces` onto this). Every space is
57
+ * offered, the default one included: nobody joins a space automatically
58
+ * (DEV-16808). `can_manage: false` rows are shown disabled.
59
+ */
60
+ interface SpaceOption {
61
+ id: string;
62
+ name: string;
63
+ kind?: string;
64
+ is_default: boolean;
65
+ can_manage: boolean;
66
+ }
67
+ /** `POST /api/org/invite`. */
68
+ interface InviteMemberResponse {
69
+ message: string;
70
+ email: string;
71
+ role: string;
72
+ invitation_id?: string;
73
+ }
74
+ /**
75
+ * The data-access boundary the panel is built against. Every method takes
76
+ * the org it acts on: there is no ambient "current org" anywhere in this
77
+ * package, so a request can never be issued for an org other than the one
78
+ * the caller named (DEV-8592). `createTeamClient` is the reference
79
+ * implementation; a host may supply its own.
80
+ */
81
+ interface TeamClient {
82
+ listMembers(orgId: string): Promise<TeamMember[]>;
83
+ updateMemberRole(orgId: string, memberId: string, role: string): Promise<void>;
84
+ removeMember(orgId: string, memberId: string): Promise<void>;
85
+ /**
86
+ * `spaces` goes in the body whenever it is passed, `[]` included (a
87
+ * one-space org sends `[]`, DEV-16808); omitted keeps the pre-DEV-15988
88
+ * request.
89
+ */
90
+ inviteMember(orgId: string, email: string, role: string, spaces?: InviteSpace[]): Promise<InviteMemberResponse>;
91
+ listInvitations(orgId: string): Promise<Invitation[]>;
92
+ revokeInvitation(orgId: string, invitationId: string): Promise<void>;
93
+ resendInvitation(orgId: string, invitationId: string): Promise<void>;
94
+ }
95
+
96
+ /**
97
+ * Framework-agnostic Reeve.Team client (DEV-9998, C2 of epic DEV-9983).
98
+ *
99
+ * The server-safe half: no React, no Next.js. The only import is type-only,
100
+ * erased at build time.
101
+ *
102
+ * Contract: reeve-services' org-members router, ported route-for-route from
103
+ * reeve-frontend's `src/lib/api/team.ts`:
104
+ *
105
+ * GET /api/org/members -> TeamMember[]
106
+ * PUT /api/org/members/{id}/role {role} -> 2xx
107
+ * DELETE /api/org/members/{id} -> 2xx
108
+ * POST /api/org/invite {email, role, spaces?} -> InviteMemberResponse
109
+ * GET /api/org/invitations -> Invitation[]
110
+ * DELETE /api/org/invitations/{id} -> 2xx
111
+ * POST /api/org/invitations/{id}/resend -> 2xx (resets the 7-day expiry)
112
+ *
113
+ * Transport is the HOST's: `createTeamClient({ fetch })` takes a JSON
114
+ * fetcher (reeve-frontend passes `productFetch`, which owns auth, 401
115
+ * refresh and timeouts). The org is never ambient. Every method takes the
116
+ * org it acts on and stamps it as `X-Reeve-Org-Id`, the header the backend's
117
+ * tenant resolver reads, so a request is keyed on the org its caller named
118
+ * rather than on whatever org a global pin held when it went out (DEV-8592).
119
+ */
120
+
121
+ /** The request header the backend tenant resolver reads the org assertion from. */
122
+ declare const ORG_HEADER = "X-Reeve-Org-Id";
123
+ /** What the client hands the host fetcher. `path` is relative to the API origin. */
124
+ type TeamRequestInit = {
125
+ method: "GET" | "POST" | "PUT" | "DELETE";
126
+ headers: Record<string, string>;
127
+ /** JSON-encoded, present only on requests that carry a body. */
128
+ body?: string;
129
+ };
130
+ /**
131
+ * A host-supplied JSON fetcher: resolve with the parsed response body (any
132
+ * value for an empty 2xx), reject on a non-2xx. reeve-frontend's
133
+ * `productFetch` has exactly this shape.
134
+ */
135
+ type TeamFetch = (path: string, init: TeamRequestInit) => Promise<unknown>;
136
+ type TeamClientOptions = {
137
+ fetch: TeamFetch;
138
+ };
139
+ declare function createTeamClient({ fetch }: TeamClientOptions): TeamClient;
140
+
141
+ export { type AssignableRole, type Invitation, type InvitationSpace, type InviteMemberResponse, type InviteSpace, ORG_HEADER, type SpaceOption, type SpaceRole, type TeamClient, type TeamClientOptions, type TeamFetch, type TeamMember, type TeamRequestInit, type TeamRole, createTeamClient };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ ORG_HEADER: () => ORG_HEADER,
24
+ createTeamClient: () => createTeamClient
25
+ });
26
+ module.exports = __toCommonJS(client_exports);
27
+ var ORG_HEADER = "X-Reeve-Org-Id";
28
+ var ORG_ID_SHAPE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
29
+ function orgHeaders(orgId) {
30
+ if (typeof orgId !== "string" || !ORG_ID_SHAPE.test(orgId)) {
31
+ throw new Error(`orgId must be a plausible org id, got ${JSON.stringify(orgId)}`);
32
+ }
33
+ return { [ORG_HEADER]: orgId };
34
+ }
35
+ function segment(value, name) {
36
+ if (typeof value !== "string" || value === "") {
37
+ throw new Error(`${name} must be a non-empty string`);
38
+ }
39
+ return encodeURIComponent(value);
40
+ }
41
+ function hasStringId(row) {
42
+ return typeof row === "object" && row !== null && typeof row.id === "string" && row.id !== "";
43
+ }
44
+ function rows(data, what) {
45
+ if (!Array.isArray(data) || !data.every(hasStringId)) {
46
+ throw new Error(`team: malformed ${what} response`);
47
+ }
48
+ return data;
49
+ }
50
+ function createTeamClient({ fetch }) {
51
+ if (typeof fetch !== "function") {
52
+ throw new Error("createTeamClient: `fetch` must be a function");
53
+ }
54
+ const send = (orgId, method, path, body) => {
55
+ const headers = orgHeaders(orgId);
56
+ const init = { method, headers };
57
+ if (body !== void 0) {
58
+ headers["Content-Type"] = "application/json";
59
+ init.body = JSON.stringify(body);
60
+ }
61
+ return fetch(path, init);
62
+ };
63
+ return {
64
+ listMembers: (orgId) => send(orgId, "GET", "/api/org/members").then((d) => rows(d, "members")),
65
+ updateMemberRole: (orgId, memberId, role) => {
66
+ segment(role, "role");
67
+ return send(orgId, "PUT", `/api/org/members/${segment(memberId, "memberId")}/role`, {
68
+ role
69
+ }).then(() => void 0);
70
+ },
71
+ removeMember: (orgId, memberId) => send(orgId, "DELETE", `/api/org/members/${segment(memberId, "memberId")}`).then(
72
+ () => void 0
73
+ ),
74
+ inviteMember: (orgId, email, role, spaces) => {
75
+ if (typeof email !== "string" || email.trim() === "") {
76
+ throw new Error("email must be a non-empty string");
77
+ }
78
+ segment(role, "role");
79
+ const body = { email: email.trim(), role };
80
+ if (spaces) body.spaces = spaces;
81
+ return send(orgId, "POST", "/api/org/invite", body).then(
82
+ (d) => d
83
+ );
84
+ },
85
+ listInvitations: (orgId) => send(orgId, "GET", "/api/org/invitations").then((d) => rows(d, "invitations")),
86
+ revokeInvitation: (orgId, invitationId) => send(orgId, "DELETE", `/api/org/invitations/${segment(invitationId, "invitationId")}`).then(
87
+ () => void 0
88
+ ),
89
+ resendInvitation: (orgId, invitationId) => send(
90
+ orgId,
91
+ "POST",
92
+ `/api/org/invitations/${segment(invitationId, "invitationId")}/resend`
93
+ ).then(() => void 0)
94
+ };
95
+ }
96
+ // Annotate the CommonJS export names for ESM import in node:
97
+ 0 && (module.exports = {
98
+ ORG_HEADER,
99
+ createTeamClient
100
+ });
@@ -0,0 +1,74 @@
1
+ // src/client.ts
2
+ var ORG_HEADER = "X-Reeve-Org-Id";
3
+ var ORG_ID_SHAPE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
4
+ function orgHeaders(orgId) {
5
+ if (typeof orgId !== "string" || !ORG_ID_SHAPE.test(orgId)) {
6
+ throw new Error(`orgId must be a plausible org id, got ${JSON.stringify(orgId)}`);
7
+ }
8
+ return { [ORG_HEADER]: orgId };
9
+ }
10
+ function segment(value, name) {
11
+ if (typeof value !== "string" || value === "") {
12
+ throw new Error(`${name} must be a non-empty string`);
13
+ }
14
+ return encodeURIComponent(value);
15
+ }
16
+ function hasStringId(row) {
17
+ return typeof row === "object" && row !== null && typeof row.id === "string" && row.id !== "";
18
+ }
19
+ function rows(data, what) {
20
+ if (!Array.isArray(data) || !data.every(hasStringId)) {
21
+ throw new Error(`team: malformed ${what} response`);
22
+ }
23
+ return data;
24
+ }
25
+ function createTeamClient({ fetch }) {
26
+ if (typeof fetch !== "function") {
27
+ throw new Error("createTeamClient: `fetch` must be a function");
28
+ }
29
+ const send = (orgId, method, path, body) => {
30
+ const headers = orgHeaders(orgId);
31
+ const init = { method, headers };
32
+ if (body !== void 0) {
33
+ headers["Content-Type"] = "application/json";
34
+ init.body = JSON.stringify(body);
35
+ }
36
+ return fetch(path, init);
37
+ };
38
+ return {
39
+ listMembers: (orgId) => send(orgId, "GET", "/api/org/members").then((d) => rows(d, "members")),
40
+ updateMemberRole: (orgId, memberId, role) => {
41
+ segment(role, "role");
42
+ return send(orgId, "PUT", `/api/org/members/${segment(memberId, "memberId")}/role`, {
43
+ role
44
+ }).then(() => void 0);
45
+ },
46
+ removeMember: (orgId, memberId) => send(orgId, "DELETE", `/api/org/members/${segment(memberId, "memberId")}`).then(
47
+ () => void 0
48
+ ),
49
+ inviteMember: (orgId, email, role, spaces) => {
50
+ if (typeof email !== "string" || email.trim() === "") {
51
+ throw new Error("email must be a non-empty string");
52
+ }
53
+ segment(role, "role");
54
+ const body = { email: email.trim(), role };
55
+ if (spaces) body.spaces = spaces;
56
+ return send(orgId, "POST", "/api/org/invite", body).then(
57
+ (d) => d
58
+ );
59
+ },
60
+ listInvitations: (orgId) => send(orgId, "GET", "/api/org/invitations").then((d) => rows(d, "invitations")),
61
+ revokeInvitation: (orgId, invitationId) => send(orgId, "DELETE", `/api/org/invitations/${segment(invitationId, "invitationId")}`).then(
62
+ () => void 0
63
+ ),
64
+ resendInvitation: (orgId, invitationId) => send(
65
+ orgId,
66
+ "POST",
67
+ `/api/org/invitations/${segment(invitationId, "invitationId")}/resend`
68
+ ).then(() => void 0)
69
+ };
70
+ }
71
+ export {
72
+ ORG_HEADER,
73
+ createTeamClient
74
+ };