Skip to content

Commit 12962bc

Browse files
authoredNov 27, 2023
chore(types,clerk-js,backend): Re-use common pagination types (#2210)
* chore(types,clerk-js): Rename ClerkPaginationParams to ClerkPaginationRequest * chore(types,clerk-js): Use ClerkPaginationParams type as base pagination type * chore(clerk-js): Drop unused import * chore(types): Extract pagination types to separate module * chore(backend,types): Re-use ClerkPaginationRequest as base pagination type * chore(repo): Add changeset
1 parent 63713c3 commit 12962bc

File tree

13 files changed

+95
-142
lines changed

13 files changed

+95
-142
lines changed
 

‎.changeset/quick-countries-scream.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/backend': minor
4+
'@clerk/types': minor
5+
---
6+
7+
Re-use common pagination types for consistency across types.
8+
9+
Types introduced in `@clerk/types`:
10+
- `ClerkPaginationRequest` : describes pagination related props in request payload
11+
- `ClerkPaginatedResponse` : describes pagination related props in response body
12+
- `ClerkPaginationParams` : describes pagination related props in api client method params

‎packages/backend/src/api/endpoints/OrganizationApi.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ClerkPaginationRequest } from '@clerk/types';
2+
13
import runtime from '../../runtime';
24
import { joinPaths } from '../../util/path';
35
import type {
@@ -16,12 +18,10 @@ type MetadataParams<TPublic = OrganizationPublicMetadata, TPrivate = Organizatio
1618
privateMetadata?: TPrivate;
1719
};
1820

19-
type GetOrganizationListParams = {
20-
limit?: number;
21-
offset?: number;
21+
type GetOrganizationListParams = ClerkPaginationRequest<{
2222
includeMembersCount?: boolean;
2323
query?: string;
24-
};
24+
}>;
2525

2626
type CreateParams = {
2727
name: string;
@@ -46,11 +46,9 @@ type UpdateLogoParams = {
4646

4747
type UpdateMetadataParams = MetadataParams;
4848

49-
type GetOrganizationMembershipListParams = {
49+
type GetOrganizationMembershipListParams = ClerkPaginationRequest<{
5050
organizationId: string;
51-
limit?: number;
52-
offset?: number;
53-
};
51+
}>;
5452

5553
type CreateOrganizationMembershipParams = {
5654
organizationId: string;
@@ -79,12 +77,10 @@ type CreateOrganizationInvitationParams = {
7977
publicMetadata?: OrganizationInvitationPublicMetadata;
8078
};
8179

82-
type GetOrganizationInvitationListParams = {
80+
type GetOrganizationInvitationListParams = ClerkPaginationRequest<{
8381
organizationId: string;
8482
status?: OrganizationInvitationStatus[];
85-
limit?: number;
86-
offset?: number;
87-
};
83+
}>;
8884

8985
type GetOrganizationInvitationParams = {
9086
organizationId: string;

‎packages/backend/src/api/endpoints/OrganizationPermissionApi.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
import type { ClerkPaginationRequest } from '@clerk/types';
2+
13
import { joinPaths } from '../../util/path';
24
import type { DeletedObject, Permission } from '../resources';
35
import { AbstractAPI } from './AbstractApi';
46

57
const basePath = '/organizations_permissions';
68

7-
type GetOrganizationPermissionListParams = {
8-
limit?: number;
9-
offset?: number;
9+
type GetOrganizationPermissionListParams = ClerkPaginationRequest<{
1010
query?: string;
1111
orderBy?: string;
12-
};
12+
}>;
1313

1414
type CreateParams = {
1515
name: string;

‎packages/backend/src/api/endpoints/OrganizationRoleApi.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
import type { ClerkPaginationRequest } from '@clerk/types';
2+
13
import { joinPaths } from '../../util/path';
24
import type { DeletedObject, Role } from '../resources';
35
import { AbstractAPI } from './AbstractApi';
46

57
const basePath = '/organizations_roles';
68

7-
type GetRoleListParams = {
8-
limit?: number;
9-
offset?: number;
9+
type GetRoleListParams = ClerkPaginationRequest<{
1010
query?: string;
1111
order_by?: string;
12-
};
12+
}>;
1313

1414
type CreateParams = {
1515
/**

‎packages/backend/src/api/endpoints/UserApi.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OAuthProvider } from '@clerk/types';
1+
import type { ClerkPaginationRequest, OAuthProvider } from '@clerk/types';
22

33
import runtime from '../../runtime';
44
import { joinPaths } from '../../util/path';
@@ -17,11 +17,11 @@ type UserCountParams = {
1717
externalId?: string[];
1818
};
1919

20-
type UserListParams = UserCountParams & {
21-
limit?: number;
22-
offset?: number;
23-
orderBy?: 'created_at' | 'updated_at' | '+created_at' | '+updated_at' | '-created_at' | '-updated_at';
24-
};
20+
type UserListParams = ClerkPaginationRequest<
21+
UserCountParams & {
22+
orderBy?: 'created_at' | 'updated_at' | '+created_at' | '+updated_at' | '-created_at' | '-updated_at';
23+
}
24+
>;
2525

2626
type UserMetadataParams = {
2727
publicMetadata?: UserPublicMetadata;
@@ -77,11 +77,9 @@ interface UpdateUserParams extends UserMetadataParams {
7777
createdAt?: Date;
7878
}
7979

80-
type GetOrganizationMembershipListParams = {
80+
type GetOrganizationMembershipListParams = ClerkPaginationRequest<{
8181
userId: string;
82-
limit?: number;
83-
offset?: number;
84-
};
82+
}>;
8583

8684
type VerifyPasswordParams = {
8785
userId: string;

‎packages/clerk-js/src/ui.retheme/components/CreateOrganization/CreateOrganization.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CreateOrganizationModalProps, CreateOrganizationProps } from '@clerk/types';
1+
import type { CreateOrganizationModalProps } from '@clerk/types';
22

33
import { withOrganizationsEnabledGuard } from '../../common';
44
import { ComponentContext, withCoreUserGuard } from '../../contexts';

‎packages/clerk-js/src/utils/pagesToOffset.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import type { ClerkPaginationParams } from '@clerk/types';
1+
import type { ClerkPaginationParams, ClerkPaginationRequest } from '@clerk/types';
22

3-
type Pages = {
4-
initialPage?: number;
5-
pageSize?: number;
6-
};
73
function getNonUndefinedValues<T>(obj: Record<string, T>): Record<string, T> {
84
return Object.keys(obj).reduce((result, key) => {
95
if (obj[key] !== undefined) {
@@ -12,8 +8,10 @@ function getNonUndefinedValues<T>(obj: Record<string, T>): Record<string, T> {
128
return result;
139
}, {} as Record<string, T>);
1410
}
15-
export function convertPageToOffset<T extends Pages | undefined>(pageParams: T): ClerkPaginationParams {
16-
const { pageSize, initialPage, ...restParams } = pageParams || {};
11+
export function convertPageToOffset<T extends ClerkPaginationParams | undefined>(
12+
pageParams: T,
13+
): ClerkPaginationRequest {
14+
const { pageSize, initialPage, ...restParams } = pageParams || ({} as ClerkPaginationParams);
1715
const _pageSize = pageSize ?? 10;
1816
const _initialPage = initialPage ?? 1;
1917

‎packages/types/src/api.ts

-16
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,3 @@ export interface ClerkRuntimeError {
2323
code: string;
2424
message: string;
2525
}
26-
27-
/**
28-
* Pagination params
29-
*/
30-
export interface ClerkPaginationParams {
31-
limit?: number;
32-
offset?: number;
33-
}
34-
35-
/**
36-
* Pagination params
37-
*/
38-
export interface ClerkPaginatedResponse<T> {
39-
data: T[];
40-
total_count: number;
41-
}

‎packages/types/src/index.retheme.ts

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ export * from './verification';
5353
export * from './web3';
5454
export * from './web3Wallet';
5555
export * from './customPages';
56+
export * from './pagination';

‎packages/types/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ export * from './verification';
5353
export * from './web3';
5454
export * from './web3Wallet';
5555
export * from './customPages';
56+
export * from './pagination';

‎packages/types/src/organization.ts

+10-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { ClerkPaginatedResponse } from './api';
21
import type { OrganizationDomainResource, OrganizationEnrollmentMode } from './organizationDomain';
32
import type { OrganizationInvitationResource, OrganizationInvitationStatus } from './organizationInvitation';
43
import type { MembershipRole, OrganizationMembershipResource } from './organizationMembership';
54
import type { OrganizationMembershipRequestResource } from './organizationMembershipRequest';
5+
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
66
import type { ClerkResource } from './resource';
77
import type { RoleResource } from './role';
88

@@ -64,68 +64,23 @@ export interface OrganizationResource extends ClerkResource {
6464
/**
6565
* @experimental
6666
*/
67-
export type GetRolesParams = {
68-
/**
69-
* This is the starting point for your fetched results. The initial value persists between re-renders
70-
*/
71-
initialPage?: number;
72-
/**
73-
* Maximum number of items returned per request. The initial value persists between re-renders
74-
*/
75-
pageSize?: number;
76-
};
77-
78-
export type GetMembersParams = {
79-
/**
80-
* This is the starting point for your fetched results. The initial value persists between re-renders
81-
*/
82-
initialPage?: number;
83-
/**
84-
* Maximum number of items returned per request. The initial value persists between re-renders
85-
*/
86-
pageSize?: number;
67+
export type GetRolesParams = ClerkPaginationParams;
8768

69+
export type GetMembersParams = ClerkPaginationParams<{
8870
role?: MembershipRole[];
89-
};
90-
91-
export type GetDomainsParams = {
92-
/**
93-
* This is the starting point for your fetched results. The initial value persists between re-renders
94-
*/
95-
initialPage?: number;
96-
/**
97-
* Maximum number of items returned per request. The initial value persists between re-renders
98-
*/
99-
pageSize?: number;
71+
}>;
10072

73+
export type GetDomainsParams = ClerkPaginationParams<{
10174
enrollmentMode?: OrganizationEnrollmentMode;
102-
};
103-
104-
export type GetInvitationsParams = {
105-
/**
106-
* This is the starting point for your fetched results. The initial value persists between re-renders
107-
*/
108-
initialPage?: number;
109-
/**
110-
* Maximum number of items returned per request. The initial value persists between re-renders
111-
*/
112-
pageSize?: number;
75+
}>;
11376

77+
export type GetInvitationsParams = ClerkPaginationParams<{
11478
status?: OrganizationInvitationStatus[];
115-
};
116-
117-
export type GetMembershipRequestParams = {
118-
/**
119-
* This is the starting point for your fetched results. The initial value persists between re-renders
120-
*/
121-
initialPage?: number;
122-
/**
123-
* Maximum number of items returned per request. The initial value persists between re-renders
124-
*/
125-
pageSize?: number;
79+
}>;
12680

81+
export type GetMembershipRequestParams = ClerkPaginationParams<{
12782
status?: OrganizationInvitationStatus;
128-
};
83+
}>;
12984

13085
export interface AddMemberParams {
13186
userId: string;

‎packages/types/src/pagination.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Pagination params in request
3+
*/
4+
export type ClerkPaginationRequest<T = any> = {
5+
/**
6+
* Maximum number of items returned per request.
7+
*/
8+
limit?: number;
9+
/**
10+
* This is the starting point for your fetched results.
11+
*/
12+
offset?: number;
13+
} & T;
14+
15+
/**
16+
* Pagination params in response
17+
*/
18+
export interface ClerkPaginatedResponse<T> {
19+
data: T[];
20+
total_count: number;
21+
}
22+
23+
/**
24+
* Pagination params passed in FAPI client methods
25+
*/
26+
export type ClerkPaginationParams<T = any> = {
27+
/**
28+
* This is the starting point for your fetched results.
29+
*/
30+
initialPage?: number;
31+
/**
32+
* Maximum number of items returned per request.
33+
*/
34+
pageSize?: number;
35+
} & T;

‎packages/types/src/user.ts

+6-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ClerkPaginatedResponse } from './api';
21
import type { BackupCodeResource } from './backupCode';
32
import type { DeletedObjectResource } from './deletedObject';
43
import type { EmailAddressResource } from './emailAddress';
@@ -9,6 +8,7 @@ import type { OAuthScope } from './oauth';
98
import type { OrganizationInvitationStatus } from './organizationInvitation';
109
import type { OrganizationMembershipResource } from './organizationMembership';
1110
import type { OrganizationSuggestionResource, OrganizationSuggestionStatus } from './organizationSuggestion';
11+
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
1212
import type { PhoneNumberResource } from './phoneNumber';
1313
import type { ClerkResource } from './resource';
1414
import type { SamlAccountResource } from './samlAccount';
@@ -148,42 +148,15 @@ export type UpdateUserPasswordParams = {
148148

149149
export type RemoveUserPasswordParams = Pick<UpdateUserPasswordParams, 'currentPassword'>;
150150

151-
export type GetUserOrganizationInvitationsParams = {
152-
/**
153-
* This the starting point for your fetched results. The initial value persists between re-renders
154-
*/
155-
initialPage?: number;
156-
/**
157-
* Maximum number of items returned per request. The initial value persists between re-renders
158-
*/
159-
pageSize?: number;
160-
151+
export type GetUserOrganizationInvitationsParams = ClerkPaginationParams<{
161152
status?: OrganizationInvitationStatus;
162-
};
163-
164-
export type GetUserOrganizationSuggestionsParams = {
165-
/**
166-
* This the starting point for your fetched results. The initial value persists between re-renders
167-
*/
168-
initialPage?: number;
169-
/**
170-
* Maximum number of items returned per request. The initial value persists between re-renders
171-
*/
172-
pageSize?: number;
153+
}>;
173154

155+
export type GetUserOrganizationSuggestionsParams = ClerkPaginationParams<{
174156
status?: OrganizationSuggestionStatus | OrganizationSuggestionStatus[];
175-
};
157+
}>;
176158

177-
export type GetUserOrganizationMembershipParams = {
178-
/**
179-
* This the starting point for your fetched results. The initial value persists between re-renders
180-
*/
181-
initialPage?: number;
182-
/**
183-
* Maximum number of items returned per request. The initial value persists between re-renders
184-
*/
185-
pageSize?: number;
186-
};
159+
export type GetUserOrganizationMembershipParams = ClerkPaginationParams;
187160

188161
export type GetOrganizationMemberships = (
189162
params?: GetUserOrganizationMembershipParams,

0 commit comments

Comments
 (0)
Please sign in to comment.