Skip to content

Commit

Permalink
chore(clerk-js,types,backend): Remove MemberRole Type (#2388)
Browse files Browse the repository at this point in the history
* chore(clerk-js,types,backend): Remove MemberRole Type

`MemberRole` would always include the old role keys `admin`, `member`, `guest_member`. If developers still depend on them after the introduction of custom roles, the can provide them as their custom types for authorization.

* chore(clerk-js): Change changeset
  • Loading branch information
panteliselef authored Dec 19, 2023
1 parent 72b66e1 commit fe2607b
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 60 deletions.
18 changes: 18 additions & 0 deletions .changeset/orange-files-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@clerk/clerk-js': minor
'@clerk/clerk-react': minor
'@clerk/types': minor
---

Remove MemberRole Type`MemberRole` would always include the old role keys `admin`, `member`, `guest_member`.
If developers still depend on them after the introduction of custom roles, the can provide them as their custom types for authorization.

```ts
// clerk.d.ts
export {}

interface ClerkAuthorization {
permission: '';
role: 'admin' | 'basic_member' | 'guest_member';
}
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
CreateBulkOrganizationInvitationParams,
CreateOrganizationInvitationParams,
MembershipRole,
OrganizationCustomRoleKey,
OrganizationInvitationJSON,
OrganizationInvitationResource,
OrganizationInvitationStatus,
Expand All @@ -16,7 +16,7 @@ export class OrganizationInvitation extends BaseResource implements Organization
organizationId!: string;
publicMetadata: OrganizationInvitationPublicMetadata = {};
status!: OrganizationInvitationStatus;
role!: MembershipRole;
role!: OrganizationCustomRoleKey;
createdAt!: Date;
updatedAt!: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
ClerkPaginatedResponse,
ClerkResourceReloadParams,
GetUserOrganizationMembershipParams,
MembershipRole,
OrganizationCustomRoleKey,
OrganizationMembershipJSON,
OrganizationMembershipResource,
OrganizationPermissionKey,
Expand All @@ -18,7 +18,7 @@ export class OrganizationMembership extends BaseResource implements Organization
publicUserData!: PublicUserData;
organization!: Organization;
permissions: OrganizationPermissionKey[] = [];
role!: MembershipRole;
role!: OrganizationCustomRoleKey;
createdAt!: Date;
updatedAt!: Date;

Expand Down Expand Up @@ -117,7 +117,7 @@ export class OrganizationMembership extends BaseResource implements Organization
}

export type UpdateOrganizationMembershipParams = {
role: MembershipRole;
role: OrganizationCustomRoleKey;
};

export type GetOrganizationMembershipsClass = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
ClerkPaginatedResponse,
GetUserOrganizationInvitationsParams,
MembershipRole,
OrganizationCustomRoleKey,
OrganizationInvitationStatus,
UserOrganizationInvitationJSON,
UserOrganizationInvitationResource,
Expand All @@ -17,7 +17,7 @@ export class UserOrganizationInvitation extends BaseResource implements UserOrga
publicOrganizationData!: UserOrganizationInvitationResource['publicOrganizationData'];
publicMetadata: OrganizationInvitationPublicMetadata = {};
status!: OrganizationInvitationStatus;
role!: MembershipRole;
role!: OrganizationCustomRoleKey;
createdAt!: Date;
updatedAt!: Date;

Expand Down
4 changes: 2 additions & 2 deletions packages/clerk-js/src/ui/common/Gate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSession } from '@clerk/shared/react';
import type { CheckAuthorization, MembershipRole, OrganizationPermissionKey } from '@clerk/types';
import type { CheckAuthorization, OrganizationCustomRoleKey, OrganizationPermissionKey } from '@clerk/types';
import type { ComponentType, PropsWithChildren, ReactNode } from 'react';
import React, { useEffect } from 'react';

Expand All @@ -10,7 +10,7 @@ type GateProps = PropsWithChildren<
(
| {
condition?: never;
role: MembershipRole;
role: OrganizationCustomRoleKey;
permission?: never;
}
| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isClerkAPIResponseError } from '@clerk/shared/error';
import { useOrganization } from '@clerk/shared/react';
import type { ClerkAPIError, MembershipRole } from '@clerk/types';
import type { ClerkAPIError } from '@clerk/types';
import type { FormEvent } from 'react';
import { useState } from 'react';

Expand Down Expand Up @@ -74,7 +74,7 @@ export const InviteMembersForm = (props: InviteMembersFormProps) => {
return organization
.inviteMembers({
emailAddresses: emailAddressField.value.split(','),
role: submittedData.get('role') as MembershipRole,
role: submittedData.get('role') as string,
})
.then(async () => {
await invitations?.revalidate?.();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { MembershipRole } from '@clerk/types';
import React, { useMemo } from 'react';

import type { LocalizationKey } from '../../customizables';
Expand Down Expand Up @@ -121,7 +120,7 @@ export const RowContainer = (props: PropsOfComponent<typeof Tr>) => {

export const RoleSelect = (props: {
roles: { label: string; value: string }[] | undefined;
value: MembershipRole;
value: string;
onChange: (params: string) => unknown;
isDisabled?: boolean;
triggerSx?: ThemableCssProp;
Expand Down
11 changes: 6 additions & 5 deletions packages/clerk-js/src/ui/utils/roleLocalizationKey.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { MembershipRole } from '@clerk/types';

import type { LocalizationKey } from '../localization/localizationKeys';
import { localizationKeys } from '../localization/localizationKeys';

const roleToLocalizationKey: Record<MembershipRole, LocalizationKey> = {
const roleToLocalizationKey: Record<string, LocalizationKey> = {
/**
* These are old role keys. We still need to support localization for those to avoid breaking labels in UI components for old instances.
*/
basic_member: localizationKeys('membershipRole__basicMember'),
guest_member: localizationKeys('membershipRole__guestMember'),
admin: localizationKeys('membershipRole__admin'),
};

export const roleLocalizationKey = (role: MembershipRole | undefined): LocalizationKey | undefined => {
export const roleLocalizationKey = (role: string | undefined): LocalizationKey | undefined => {
if (!role) {
return undefined;
}
return roleToLocalizationKey[role];
};

export const customRoleLocalizationKey = (role: MembershipRole | undefined): LocalizationKey | undefined => {
export const customRoleLocalizationKey = (role: string | undefined): LocalizationKey | undefined => {
if (!role) {
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
ActJWTClaim,
CheckAuthorizationWithCustomPermissions,
GetToken,
MembershipRole,
OrganizationCustomRoleKey,
SignOut,
} from '@clerk/types';
import { useCallback } from 'react';
Expand Down Expand Up @@ -64,7 +64,7 @@ type UseAuthReturn =
sessionId: string;
actor: ActJWTClaim | null;
orgId: string;
orgRole: MembershipRole;
orgRole: OrganizationCustomRoleKey;
orgSlug: string | null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { DisplayThemeJSON } from './json';
import type { LocalizationResource } from './localization';
import type { OAuthProvider, OAuthScope } from './oauth';
import type { OrganizationResource } from './organization';
import type { MembershipRole } from './organizationMembership';
import type { OrganizationCustomRoleKey } from './organizationMembership';
import type { ActiveSessionResource } from './session';
import type { UserResource } from './user';
import type { Autocomplete, DeepPartial, DeepSnakeToCamel } from './utils';
Expand Down Expand Up @@ -935,12 +935,12 @@ export interface HandleEmailLinkVerificationParams {

export type CreateOrganizationInvitationParams = {
emailAddress: string;
role: MembershipRole;
role: OrganizationCustomRoleKey;
};

export type CreateBulkOrganizationInvitationParams = {
emailAddresses: string[];
role: MembershipRole;
role: OrganizationCustomRoleKey;
};

export interface CreateOrganizationParams {
Expand Down
8 changes: 4 additions & 4 deletions packages/types/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ActJWTClaim } from './jwt';
import type { OAuthProvider } from './oauth';
import type { OrganizationDomainVerificationStatus, OrganizationEnrollmentMode } from './organizationDomain';
import type { OrganizationInvitationStatus } from './organizationInvitation';
import type { MembershipRole, OrganizationPermissionKey } from './organizationMembership';
import type { OrganizationCustomRoleKey, OrganizationPermissionKey } from './organizationMembership';
import type { OrganizationSettingsJSON } from './organizationSettings';
import type { OrganizationSuggestionStatus } from './organizationSuggestion';
import type { SamlIdpSlug } from './saml';
Expand Down Expand Up @@ -303,7 +303,7 @@ export interface OrganizationMembershipJSON extends ClerkResourceJSON {
permissions: OrganizationPermissionKey[];
public_metadata: OrganizationMembershipPublicMetadata;
public_user_data: PublicUserDataJSON;
role: MembershipRole;
role: OrganizationCustomRoleKey;
created_at: number;
updated_at: number;
}
Expand All @@ -315,7 +315,7 @@ export interface OrganizationInvitationJSON extends ClerkResourceJSON {
organization_id: string;
public_metadata: OrganizationInvitationPublicMetadata;
status: OrganizationInvitationStatus;
role: MembershipRole;
role: OrganizationCustomRoleKey;
created_at: number;
updated_at: number;
}
Expand Down Expand Up @@ -403,7 +403,7 @@ export interface UserOrganizationInvitationJSON extends ClerkResourceJSON {
public_organization_data: PublicOrganizationDataJSON;
public_metadata: OrganizationInvitationPublicMetadata;
status: OrganizationInvitationStatus;
role: MembershipRole;
role: OrganizationCustomRoleKey;
created_at: number;
updated_at: number;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MembershipRole } from './organizationMembership';
import type { OrganizationCustomRoleKey } from './organizationMembership';

export interface JWT {
encoded: { header: string; payload: string; signature: string };
Expand Down Expand Up @@ -84,7 +84,7 @@ export interface ClerkJWTClaims {
/**
* Active organization role
*/
org_role?: MembershipRole;
org_role?: OrganizationCustomRoleKey;

/**
* Any other JWT Claim Set member.
Expand All @@ -100,4 +100,4 @@ export interface ActJWTClaim {
[x: string]: unknown;
}

export type OrganizationsJWTClaim = Record<string, MembershipRole>;
export type OrganizationsJWTClaim = Record<string, OrganizationCustomRoleKey>;
4 changes: 2 additions & 2 deletions packages/types/src/jwtv2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MembershipRole, OrganizationCustomPermissionKey } from './organizationMembership';
import type { OrganizationCustomPermissionKey, OrganizationCustomRoleKey } from './organizationMembership';

export interface Jwt {
header: JwtHeader;
Expand Down Expand Up @@ -94,7 +94,7 @@ export interface JwtPayload extends CustomJwtSessionClaims {
/**
* Active organization role
*/
org_role?: MembershipRole;
org_role?: OrganizationCustomRoleKey;

/**
* Active organization role
Expand Down
12 changes: 6 additions & 6 deletions packages/types/src/organization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OrganizationDomainResource, OrganizationEnrollmentMode } from './organizationDomain';
import type { OrganizationInvitationResource, OrganizationInvitationStatus } from './organizationInvitation';
import type { MembershipRole, OrganizationMembershipResource } from './organizationMembership';
import type { OrganizationCustomRoleKey, OrganizationMembershipResource } from './organizationMembership';
import type { OrganizationMembershipRequestResource } from './organizationMembershipRequest';
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
import type { ClerkResource } from './resource';
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface OrganizationResource extends ClerkResource {
export type GetRolesParams = ClerkPaginationParams;

export type GetMembersParams = ClerkPaginationParams<{
role?: MembershipRole[];
role?: OrganizationCustomRoleKey[];
}>;

export type GetDomainsParams = ClerkPaginationParams<{
Expand All @@ -84,22 +84,22 @@ export type GetMembershipRequestParams = ClerkPaginationParams<{

export interface AddMemberParams {
userId: string;
role: MembershipRole;
role: OrganizationCustomRoleKey;
}

export interface InviteMemberParams {
emailAddress: string;
role: MembershipRole;
role: OrganizationCustomRoleKey;
}

export interface InviteMembersParams {
emailAddresses: string[];
role: MembershipRole;
role: OrganizationCustomRoleKey;
}

export interface UpdateMembershipParams {
userId: string;
role: MembershipRole;
role: OrganizationCustomRoleKey;
}

export interface UpdateOrganizationParams {
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/organizationInvitation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MembershipRole } from './organizationMembership';
import type { OrganizationCustomRoleKey } from './organizationMembership';
import type { ClerkResource } from './resource';

declare global {
Expand All @@ -21,7 +21,7 @@ export interface OrganizationInvitationResource extends ClerkResource {
emailAddress: string;
organizationId: string;
publicMetadata: OrganizationInvitationPublicMetadata;
role: MembershipRole;
role: OrganizationCustomRoleKey;
status: OrganizationInvitationStatus;
createdAt: Date;
updatedAt: Date;
Expand Down
19 changes: 5 additions & 14 deletions packages/types/src/organizationMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface OrganizationMembershipResource extends ClerkResource {
permissions: OrganizationPermissionKey[];
publicMetadata: OrganizationMembershipPublicMetadata;
publicUserData: PublicUserData;
role: MembershipRole;
role: OrganizationCustomRoleKey;
createdAt: Date;
updatedAt: Date;
destroy: () => Promise<OrganizationMembershipResource>;
Expand All @@ -56,24 +56,15 @@ export type OrganizationCustomPermissionKey = ClerkAuthorization extends Placeho
: Base['permission']
: Base['permission'];

/**
* OrganizationCustomRoleKey will be string unless the developer has provided their own types through `ClerkAuthorization`
*/
export type OrganizationCustomRoleKey = ClerkAuthorization extends Placeholder
? ClerkAuthorization['role'] extends string
? ClerkAuthorization['role']
: Base['role']
: Base['role'];

/**
* @deprecated This type is deprecated and will be removed in the next major release.
* Use `OrganizationCustomRoleKey` instead.
* MembershipRole includes `admin`, `basic_member`, `guest_member`. With the introduction of "Custom roles"
* these types will no longer match a developer's custom logic.
*/
export type MembershipRole = ClerkAuthorization extends Placeholder
? ClerkAuthorization['role'] extends string
? ClerkAuthorization['role'] | 'admin' | 'basic_member' | 'guest_member'
: Autocomplete<'admin' | 'basic_member' | 'guest_member'>
: Autocomplete<'admin' | 'basic_member' | 'guest_member'>;

export type OrganizationSystemPermissionKey =
| 'org:sys_domains:manage'
| 'org:sys_profile:manage'
Expand All @@ -93,5 +84,5 @@ export type OrganizationPermissionKey = ClerkAuthorization extends Placeholder
: Autocomplete<OrganizationSystemPermissionKey>;

export type UpdateOrganizationMembershipParams = {
role: MembershipRole;
role: OrganizationCustomRoleKey;
};
3 changes: 1 addition & 2 deletions packages/types/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ActJWTClaim } from './jwt';
import type {
MembershipRole,
OrganizationCustomPermissionKey,
OrganizationCustomRoleKey,
OrganizationPermissionKey,
Expand Down Expand Up @@ -28,7 +27,7 @@ export type CheckAuthorization = CheckAuthorizationFn<CheckAuthorizationParams>;

type CheckAuthorizationParams =
| {
role: MembershipRole;
role: OrganizationCustomRoleKey;
permission?: never;
}
| {
Expand Down
Loading

0 comments on commit fe2607b

Please sign in to comment.