Skip to content

Commit

Permalink
feat(clerk-js): Support Paginated response in OrganizationMembership.…
Browse files Browse the repository at this point in the history
…retrieve
  • Loading branch information
panteliselef committed Aug 16, 2023
1 parent 0783e62 commit c78d028
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,9 @@ export default class Clerk implements ClerkInterface {
return Organization.create({ name, slug });
};

/**
* @deprecated use User.getOrganizationMemberships
*/
public getOrganizationMemberships = async (): Promise<OrganizationMembership[]> => {
return await OrganizationMembership.retrieve();
};
Expand Down
49 changes: 43 additions & 6 deletions packages/clerk-js/src/core/resources/OrganizationMembership.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type {
ClerkPaginatedResponse,
ClerkResourceReloadParams,
GetUserOrganizationMembershipParams,
MembershipRole,
OrganizationMembershipJSON,
OrganizationMembershipResource,
PublicUserData,
} from '@clerk/types';

import { unixEpochToDate } from '../../utils/date';
import { convertPageToOffset } from '../../utils/pagesToOffset';
import { BaseResource, Organization } from './internal';

export class OrganizationMembership extends BaseResource implements OrganizationMembershipResource {
Expand All @@ -23,18 +26,40 @@ export class OrganizationMembership extends BaseResource implements Organization
this.fromJSON(data);
}

static async retrieve(retrieveMembershipsParams?: RetrieveMembershipsParams): Promise<OrganizationMembership[]> {
static retrieve: GetOrganizationMembershipsClass = async retrieveMembershipsParams => {
const isDeprecatedParams =
typeof retrieveMembershipsParams === 'undefined' || !retrieveMembershipsParams?.paginated;
return await BaseResource._fetch({
path: '/me/organization_memberships',
method: 'GET',
search: retrieveMembershipsParams as any,
search: isDeprecatedParams
? retrieveMembershipsParams
: (convertPageToOffset(retrieveMembershipsParams as unknown as any) as any),
})
.then(res => {
const organizationMembershipsJSON = res?.response as unknown as OrganizationMembershipJSON[];
return organizationMembershipsJSON.map(orgMem => new OrganizationMembership(orgMem));
if (isDeprecatedParams) {
const organizationMembershipsJSON = res?.response as unknown as OrganizationMembershipJSON[];
return organizationMembershipsJSON.map(orgMem => new OrganizationMembership(orgMem)) as any;
}

const { data: suggestions, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipJSON>;

return {
total_count,
data: suggestions.map(suggestion => new OrganizationMembership(suggestion)),
} as any;
})
.catch(() => []);
}
.catch(() => {
if (isDeprecatedParams) {
return [];
}
return {
total_count: 0,
data: [],
};
});
};

destroy = async (): Promise<OrganizationMembership> => {
// TODO: Revise the return type of _baseDelete
Expand Down Expand Up @@ -100,7 +125,19 @@ export type UpdateOrganizationMembershipParams = {
role: MembershipRole;
};

/**
* @deprecated
*/
export type RetrieveMembershipsParams = {
limit?: number;
offset?: number;
};

type MembershipParams = (RetrieveMembershipsParams | GetUserOrganizationMembershipParams) & {
paginated?: boolean;
};
export type GetOrganizationMembershipsClass = <T extends MembershipParams>(
params?: T,
) => T['paginated'] extends true
? Promise<ClerkPaginatedResponse<OrganizationMembership>>
: Promise<OrganizationMembership[]>;
9 changes: 3 additions & 6 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
EmailAddressResource,
ExternalAccountJSON,
ExternalAccountResource,
GetOrganizationMemberships,
GetUserOrganizationInvitationsParams,
GetUserOrganizationSuggestionsParams,
ImageResource,
Expand All @@ -32,7 +33,6 @@ import { unixEpochToDate } from '../../utils/date';
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
import { getFullName } from '../../utils/user';
import { BackupCode } from './BackupCode';
import type { RetrieveMembershipsParams } from './internal';
import {
BaseResource,
DeletedObject,
Expand Down Expand Up @@ -266,11 +266,8 @@ export class User extends BaseResource implements UserResource {
return OrganizationSuggestion.retrieve(params);
};

getOrganizationMemberships = async (
retrieveMembership: RetrieveMembershipsParams,
): Promise<OrganizationMembership[]> => {
return await OrganizationMembership.retrieve(retrieveMembership);
};
getOrganizationMemberships: GetOrganizationMemberships = retrieveMembership =>
OrganizationMembership.retrieve(retrieveMembership);

get verifiedExternalAccounts() {
return this.externalAccounts.filter(externalAccount => externalAccount.verification?.status == 'verified');
Expand Down
27 changes: 27 additions & 0 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export interface UserResource extends ClerkResource {
getSessions: () => Promise<SessionWithActivitiesResource[]>;
setProfileImage: (params: SetProfileImageParams) => Promise<ImageResource>;
createExternalAccount: (params: CreateExternalAccountParams) => Promise<ExternalAccountResource>;
getOrganizationMemberships: GetOrganizationMemberships;
getOrganizationInvitations: (
params?: GetUserOrganizationInvitationsParams,
) => Promise<ClerkPaginatedResponse<UserOrganizationInvitationResource>>;
Expand Down Expand Up @@ -190,3 +191,29 @@ export type GetUserOrganizationSuggestionsParams = {

status?: OrganizationSuggestionStatus | OrganizationSuggestionStatus[];
};

type GetUserOrganizationMembershipOldParams = {
limit?: number;
offset?: number;
};

export type GetUserOrganizationMembershipParams = {
/**
* This the starting point for your fetched results. The initial value persists between re-renders
*/
initialPage?: number;
/**
* Maximum number of items returned per request. The initial value persists between re-renders
*/
pageSize?: number;
};

type MembershipParams = (GetUserOrganizationMembershipOldParams | GetUserOrganizationMembershipParams) & {
paginated?: boolean;
};

export type GetOrganizationMemberships = <T extends MembershipParams>(
params?: T,
) => T['paginated'] extends true
? Promise<ClerkPaginatedResponse<OrganizationMembershipResource>>
: Promise<OrganizationMembershipResource[]>;

0 comments on commit c78d028

Please sign in to comment.