Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/modern-plums-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@clerk/clerk-js': minor
---

Remove fallback data and allow promise to throw for paginated endpoint methods.
Affected methods:
- Organization.getDomains
- Organization.getInvitations
- Organization.getMembershipRequests
- Organization.getMemberships
- User.getOrganizationInvitations
- User.getOrganizationSuggestions
- User.getOrganizationMemberships
93 changes: 35 additions & 58 deletions packages/clerk-js/src/core/resources/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,14 @@ export class Organization extends BaseResource implements OrganizationResource {
{
forceUpdateClient: true,
},
)
.then(res => {
const { data: invites, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationDomainJSON>;

return {
total_count,
data: invites.map(domain => new OrganizationDomain(domain)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
).then(res => {
const { data: invites, total_count } = res?.response as unknown as ClerkPaginatedResponse<OrganizationDomainJSON>;

return {
total_count,
data: invites.map(domain => new OrganizationDomain(domain)),
};
});
};

getDomain = async ({ domainId }: { domainId: string }): Promise<OrganizationDomainResource> => {
Expand All @@ -147,20 +141,15 @@ export class Organization extends BaseResource implements OrganizationResource {
path: `/organizations/${this.id}/membership_requests`,
method: 'GET',
search: convertPageToOffset(getRequestParam),
})
.then(res => {
const { data: requests, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipRequestJSON>;

return {
total_count,
data: requests.map(request => new OrganizationMembershipRequest(request)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
}).then(res => {
const { data: requests, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipRequestJSON>;

return {
total_count,
data: requests.map(request => new OrganizationMembershipRequest(request)),
};
});
};

createDomain = async (name: string): Promise<OrganizationDomainResource> => {
Expand All @@ -174,22 +163,15 @@ export class Organization extends BaseResource implements OrganizationResource {
// `paginated` is used in some legacy endpoints to support clerk paginated responses
// The parameter will be dropped in FAPI v2
search: convertPageToOffset({ ...getMembershipsParams, paginated: true }),
})
.then(res => {
const { data: suggestions, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipJSON>;

return {
total_count,
data: suggestions.map(suggestion => new OrganizationMembership(suggestion)),
};
})
.catch(() => {
return {
total_count: 0,
data: [],
};
});
}).then(res => {
const { data: suggestions, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipJSON>;

return {
total_count,
data: suggestions.map(suggestion => new OrganizationMembership(suggestion)),
};
});
};

getInvitations = async (
Expand All @@ -204,20 +186,15 @@ export class Organization extends BaseResource implements OrganizationResource {
{
forceUpdateClient: true,
},
)
.then(res => {
const { data: requests, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationInvitationJSON>;

return {
total_count,
data: requests.map(request => new OrganizationInvitation(request)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
).then(res => {
const { data: requests, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationInvitationJSON>;

return {
total_count,
data: requests.map(request => new OrganizationInvitation(request)),
};
});
};

addMember = async ({ userId, role }: AddMemberParams) => {
Expand Down
34 changes: 10 additions & 24 deletions packages/clerk-js/src/core/resources/OrganizationMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,16 @@ export class OrganizationMembership extends BaseResource implements Organization
// `paginated` is used in some legacy endpoints to support clerk paginated responses
// The parameter will be dropped in FAPI v2
search: convertPageToOffset({ ...retrieveMembershipsParams, paginated: true }),
})
.then(res => {
if (!res?.response) {
return {
total_count: 0,
data: [],
};
}

// TODO: Fix typing
const { data: suggestions, total_count } =
res.response as unknown as ClerkPaginatedResponse<OrganizationMembershipJSON>;

return {
total_count,
data: suggestions.map(suggestion => new OrganizationMembership(suggestion)),
};
})
.catch(() => {
return {
total_count: 0,
data: [],
};
});
}).then(res => {
// TODO: Fix typing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ is this different from the above as unknown as ClerkPaginatedResponse... ? I think we should remove this TODO or add also to the other refs of this PR.
Let's also add a ticket to make this refactoring part of the v6 project

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we already have one. It is a general issue with BaseResource._fetch and type inference.

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

return {
total_count,
data: suggestions.map(suggestion => new OrganizationMembership(suggestion)),
};
});
};

destroy = async (): Promise<OrganizationMembership> => {
Expand Down
21 changes: 8 additions & 13 deletions packages/clerk-js/src/core/resources/OrganizationSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,15 @@ export class OrganizationSuggestion extends BaseResource implements Organization
path: '/me/organization_suggestions',
method: 'GET',
search: convertPageToOffset(params),
})
.then(res => {
const { data: suggestions, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationSuggestionJSON>;
}).then(res => {
const { data: suggestions, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationSuggestionJSON>;

return {
total_count,
data: suggestions.map(suggestion => new OrganizationSuggestion(suggestion)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
return {
total_count,
data: suggestions.map(suggestion => new OrganizationSuggestion(suggestion)),
};
});
}

accept = async (): Promise<OrganizationSuggestionResource> => {
Expand Down
21 changes: 8 additions & 13 deletions packages/clerk-js/src/core/resources/UserOrganizationInvitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,15 @@ export class UserOrganizationInvitation extends BaseResource implements UserOrga
path: '/me/organization_invitations',
method: 'GET',
search: convertPageToOffset(params),
})
.then(res => {
const { data: invites, total_count } =
res?.response as unknown as ClerkPaginatedResponse<UserOrganizationInvitationJSON>;
}).then(res => {
const { data: invites, total_count } =
res?.response as unknown as ClerkPaginatedResponse<UserOrganizationInvitationJSON>;

return {
total_count,
data: invites.map(invitation => new UserOrganizationInvitation(invitation)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
return {
total_count,
data: invites.map(invitation => new UserOrganizationInvitation(invitation)),
};
});
}

constructor(data: UserOrganizationInvitationJSON) {
Expand Down