Skip to content

Commit 721ce72

Browse files
committed
fix(clerk-react,clerk-js,types): Crate of API feedback fixes
1 parent adbca01 commit 721ce72

14 files changed

+84
-70
lines changed

packages/clerk-js/src/core/clerk.ts

-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
Clerk as ClerkInterface,
88
ClerkOptions,
99
ClientResource,
10-
CreateOrganizationInvitationParams,
1110
CreateOrganizationParams,
1211
EnvironmentResource,
1312
HandleMagicLinkVerificationParams,
@@ -65,7 +64,6 @@ import {
6564
MagicLinkError,
6665
MagicLinkErrorCode,
6766
Organization,
68-
OrganizationInvitation,
6967
} from './resources/internal';
7068

7169
export type ClerkCoreBroadcastChannelEvent = { type: 'signout' };
@@ -632,13 +630,6 @@ export default class Clerk implements ClerkInterface {
632630
this.#fapiClient.onAfterResponse(callback);
633631
}
634632

635-
__unstable_inviteMember = async (
636-
organizationId: string,
637-
params: CreateOrganizationInvitationParams,
638-
) => {
639-
return await OrganizationInvitation.create(organizationId, params);
640-
};
641-
642633
#loadInBrowser = async (): Promise<void> => {
643634
this.#authService = new AuthenticationService(this);
644635

packages/clerk-js/src/core/resources/Organization.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {
2-
GetMembersParams,
2+
GetMembershipsParams,
33
MembershipRole,
44
OrganizationInvitationJSON,
55
OrganizationJSON,
@@ -56,13 +56,13 @@ export class Organization extends BaseResource implements OrganizationResource {
5656
.catch(() => []);
5757
}
5858

59-
getMembers = async (
60-
getMemberParams?: GetMembersParams,
59+
getMemberships = async (
60+
getMemberhipsParams?: GetMembershipsParams,
6161
): Promise<OrganizationMembership[]> => {
6262
return await BaseResource._fetch({
6363
path: `/organizations/${this.id}/memberships`,
6464
method: 'GET',
65-
search: getMemberParams as any,
65+
search: getMemberhipsParams as any,
6666
})
6767
.then(res => {
6868
const members =
@@ -87,8 +87,8 @@ export class Organization extends BaseResource implements OrganizationResource {
8787
.catch(() => []);
8888
};
8989

90-
inviteUser = async (inviteUserParams: InviteUserParams) => {
91-
return await OrganizationInvitation.create(this.id, inviteUserParams);
90+
inviteMember = async (inviteMemberParams: InviteMemberParams) => {
91+
return await OrganizationInvitation.create(this.id, inviteMemberParams);
9292
};
9393

9494
updateMember = async ({
@@ -105,10 +105,14 @@ export class Organization extends BaseResource implements OrganizationResource {
105105
);
106106
};
107107

108-
removeMember = async (userId: string) => {
109-
return await this._baseDelete({
108+
removeMember = async (userId: string): Promise<OrganizationMembership> => {
109+
return await BaseResource._fetch({
110+
method: 'DELETE',
110111
path: `/organizations/${this.id}/memberships/${userId}`,
111-
});
112+
}).then(
113+
res =>
114+
new OrganizationMembership(res?.response as OrganizationMembershipJSON),
115+
);
112116
};
113117

114118
protected fromJSON(data: OrganizationJSON): this {
@@ -128,7 +132,7 @@ export type GetOrganizationParams = {
128132
offset?: number;
129133
};
130134

131-
export type InviteUserParams = {
135+
export type InviteMemberParams = {
132136
emailAddress: string;
133137
role: MembershipRole;
134138
redirectUrl?: string;

packages/clerk-js/src/core/resources/OrganizationInvitation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class OrganizationInvitation
4444
this.fromJSON(data);
4545
}
4646

47-
revoke = async () => {
47+
revoke = async (): Promise<OrganizationInvitation> => {
4848
return await this._basePost({
4949
path: `/organizations/${this.organizationId}/invitations/${this.id}/revoke`,
5050
});

packages/clerk-js/src/core/resources/OrganizationMembership.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe('OrganizationMembership', () => {
55
const organizationMemberShip = new OrganizationMembership({
66
object: 'organization_membership',
77
id: 'test_id',
8+
organization_id: 'test_org_id',
89
created_at: 12345,
910
updated_at: 5678,
1011
role: 'admin',

packages/clerk-js/src/core/resources/OrganizationMembership.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,43 @@ import {
66
} from '@clerk/types';
77
import { unixEpochToDate } from 'utils/date';
88

9-
export class OrganizationMembership implements OrganizationMembershipResource {
9+
import { BaseResource } from './internal';
10+
11+
export class OrganizationMembership
12+
extends BaseResource
13+
implements OrganizationMembershipResource
14+
{
1015
id!: string;
16+
organizationId!: string;
1117
publicUserData!: PublicUserData;
1218
role!: MembershipRole;
1319
createdAt!: Date;
1420
updatedAt!: Date;
1521

1622
constructor(data: OrganizationMembershipJSON) {
23+
super();
1724
this.fromJSON(data);
1825
}
1926

27+
destroy = async (): Promise<OrganizationMembership> => {
28+
// FIXME: Revise the return type of _baseDelete
29+
return (await this._baseDelete({
30+
path: `/organizations/${this.organizationId}/memberships/${this.publicUserData.userId}`,
31+
})) as unknown as OrganizationMembership;
32+
};
33+
34+
update = async ({
35+
role,
36+
}: UpdateOrganizationMembershipParams): Promise<OrganizationMembership> => {
37+
return await this._basePatch({
38+
path: `/organizations/${this.organizationId}/memberships/${this.publicUserData.userId}`,
39+
body: { role },
40+
});
41+
};
42+
2043
protected fromJSON(data: OrganizationMembershipJSON): this {
2144
this.id = data.id;
45+
this.organizationId = data.organization_id;
2246
this.publicUserData = {
2347
firstName: data.public_user_data.first_name,
2448
lastName: data.public_user_data.last_name,
@@ -32,3 +56,7 @@ export class OrganizationMembership implements OrganizationMembershipResource {
3256
return this;
3357
}
3458
}
59+
60+
export type UpdateOrganizationMembershipParams = {
61+
role: MembershipRole;
62+
};

packages/clerk-js/src/core/resources/__snapshots__/Organization.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ exports[`Organization has the same initial properties 1`] = `
44
Organization {
55
"createdAt": 1970-01-01T00:00:12.345Z,
66
"createdBy": "test_user_id",
7-
"getMembers": [Function],
7+
"getMemberships": [Function],
88
"getPendingInvitations": [Function],
99
"id": "test_id",
1010
"instanceId": "test_instance_id",
11-
"inviteUser": [Function],
11+
"inviteMember": [Function],
1212
"name": "test_name",
1313
"pathRoot": "",
1414
"removeMember": [Function],

packages/clerk-js/src/core/resources/__snapshots__/OrganizationMembership.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
exports[`OrganizationMembership has the same initial properties 1`] = `
44
OrganizationMembership {
55
"createdAt": 1970-01-01T00:00:12.345Z,
6+
"destroy": [Function],
67
"id": "test_id",
8+
"organizationId": "test_org_id",
9+
"pathRoot": "",
710
"publicUserData": Object {
811
"firstName": "test_first_name",
912
"identifier": "test@identifier.gr",
@@ -12,6 +15,7 @@ OrganizationMembership {
1215
"userId": undefined,
1316
},
1417
"role": "admin",
18+
"update": [Function],
1519
"updatedAt": 1970-01-01T00:00:05.678Z,
1620
}
1721
`;

packages/react/src/hooks/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './useOrganization';
21
export * from './useOrganizations';
32
export * from './useMagicLink';

packages/react/src/hooks/useOrganization.ts

-27
This file was deleted.

packages/react/src/isomorphicClerk.ts

-15
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,6 @@ export default class IsomorphicClerk {
237237
}
238238
}
239239

240-
__unstable_inviteMember = async (
241-
organizationId: string,
242-
params: CreateOrganizationInvitationParams,
243-
): Promise<OrganizationInvitationResource | void> => {
244-
const callback = () =>
245-
// @ts-expect-error
246-
this.clerkjs.__unstable_inviteMember(organizationId, params);
247-
if (this.clerkjs && this._loaded) {
248-
return callback() as Promise<OrganizationInvitationResource>;
249-
} else {
250-
// @ts-expect-error
251-
this.premountMethodCalls.set('__unstable_inviteMember', callback);
252-
}
253-
};
254-
255240
setSession = (
256241
session: ActiveSessionResource | string | null,
257242
beforeEmit?: (session: ActiveSessionResource | null) => void | Promise<any>,

packages/types/src/json.ts

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export interface OrganizationJSON extends ClerkResourceJSON {
334334
export interface OrganizationMembershipJSON extends ClerkResourceJSON {
335335
object: 'organization_membership';
336336
id: string;
337+
organization_id: string;
337338
public_user_data: PublicUserDataJSON;
338339
role: MembershipRole;
339340
created_at: number;

packages/types/src/jwt.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { MembershipRole } from '.';
2+
13
export interface JWTClaims {
24
__raw: string;
35
sub: string;
@@ -8,9 +10,11 @@ export interface JWTClaims {
810
iat?: number;
911
nbf?: number;
1012
name?: string;
13+
orgs?: OrganizationsJWTClaim;
1114
[key: string]: unknown;
1215
}
1316

17+
export type OrganizationsJWTClaim = Record<string, MembershipRole>;
1418
// eslint-disable-next-line @typescript-eslint/naming-convention
1519
export interface JWT {
1620
encoded: { header: string; payload: string; signature: string };

packages/types/src/organization.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { MembershipRole, OrganizationMembershipResource } from '.';
1+
import {
2+
MembershipRole,
3+
OrganizationInvitationResource,
4+
OrganizationMembershipResource,
5+
} from '.';
26

37
export interface OrganizationResource {
48
id: string;
@@ -8,12 +12,31 @@ export interface OrganizationResource {
812
createdBy: string;
913
createdAt: Date;
1014
updatedAt: Date;
11-
getMembers: (
12-
params?: GetMembersParams,
15+
getMemberships: (
16+
params?: GetMembershipsParams,
1317
) => Promise<OrganizationMembershipResource[]>;
18+
getPendingInvitations: () => Promise<OrganizationInvitationResource[]>;
19+
inviteMember: (
20+
params: InviteMemberParams,
21+
) => Promise<OrganizationInvitationResource>;
22+
updateMember: (
23+
params: UpdateMembershipParams,
24+
) => Promise<OrganizationMembershipResource>;
25+
removeMember: (userId: string) => Promise<OrganizationMembershipResource>;
1426
}
1527

16-
export interface GetMembersParams {
28+
export interface GetMembershipsParams {
1729
limit?: number;
1830
offset?: number;
1931
}
32+
33+
export interface InviteMemberParams {
34+
emailAddress: string;
35+
role: MembershipRole;
36+
redirectUrl?: string;
37+
}
38+
39+
export interface UpdateMembershipParams {
40+
userId: string;
41+
role: MembershipRole;
42+
}

packages/types/src/organizationMembership.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PublicUserData } from '.';
22

33
export interface OrganizationMembershipResource {
44
id: string;
5+
organizationId: string;
56
publicUserData: PublicUserData;
67
role: MembershipRole;
78
createdAt: Date;

0 commit comments

Comments
 (0)