Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(types,clerk-js,backend): Re-use common pagination types #2210

Merged
merged 6 commits into from
Nov 27, 2023
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
12 changes: 12 additions & 0 deletions .changeset/quick-countries-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@clerk/clerk-js': minor
'@clerk/backend': minor
'@clerk/types': minor
---

Re-use common pagination types for consistency across types.

Types introduced in `@clerk/types`:
- `ClerkPaginationRequest` : describes pagination related props in request payload
- `ClerkPaginatedResponse` : describes pagination related props in response body
- `ClerkPaginationParams` : describes pagination related props in api client method params
20 changes: 8 additions & 12 deletions packages/backend/src/api/endpoints/OrganizationApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ClerkPaginationRequest } from '@clerk/types';

import runtime from '../../runtime';
import { joinPaths } from '../../util/path';
import type {
Expand All @@ -16,12 +18,10 @@ type MetadataParams<TPublic = OrganizationPublicMetadata, TPrivate = Organizatio
privateMetadata?: TPrivate;
};

type GetOrganizationListParams = {
limit?: number;
offset?: number;
type GetOrganizationListParams = ClerkPaginationRequest<{
includeMembersCount?: boolean;
query?: string;
};
}>;

type CreateParams = {
name: string;
Expand All @@ -46,11 +46,9 @@ type UpdateLogoParams = {

type UpdateMetadataParams = MetadataParams;

type GetOrganizationMembershipListParams = {
type GetOrganizationMembershipListParams = ClerkPaginationRequest<{
organizationId: string;
limit?: number;
offset?: number;
};
}>;

type CreateOrganizationMembershipParams = {
organizationId: string;
Expand Down Expand Up @@ -79,12 +77,10 @@ type CreateOrganizationInvitationParams = {
publicMetadata?: OrganizationInvitationPublicMetadata;
};

type GetOrganizationInvitationListParams = {
type GetOrganizationInvitationListParams = ClerkPaginationRequest<{
organizationId: string;
status?: OrganizationInvitationStatus[];
limit?: number;
offset?: number;
};
}>;

type GetOrganizationInvitationParams = {
organizationId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ClerkPaginationRequest } from '@clerk/types';

import { joinPaths } from '../../util/path';
import type { DeletedObject, Permission } from '../resources';
import { AbstractAPI } from './AbstractApi';

const basePath = '/organizations_permissions';

type GetOrganizationPermissionListParams = {
limit?: number;
offset?: number;
type GetOrganizationPermissionListParams = ClerkPaginationRequest<{
query?: string;
orderBy?: string;
};
}>;

type CreateParams = {
name: string;
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/api/endpoints/OrganizationRoleApi.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ClerkPaginationRequest } from '@clerk/types';

import { joinPaths } from '../../util/path';
import type { DeletedObject, Role } from '../resources';
import { AbstractAPI } from './AbstractApi';

const basePath = '/organizations_roles';

type GetRoleListParams = {
limit?: number;
offset?: number;
type GetRoleListParams = ClerkPaginationRequest<{
query?: string;
order_by?: string;
};
}>;

type CreateParams = {
/**
Expand Down
18 changes: 8 additions & 10 deletions packages/backend/src/api/endpoints/UserApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OAuthProvider } from '@clerk/types';
import type { ClerkPaginationRequest, OAuthProvider } from '@clerk/types';

import runtime from '../../runtime';
import { joinPaths } from '../../util/path';
Expand All @@ -17,11 +17,11 @@ type UserCountParams = {
externalId?: string[];
};

type UserListParams = UserCountParams & {
limit?: number;
offset?: number;
orderBy?: 'created_at' | 'updated_at' | '+created_at' | '+updated_at' | '-created_at' | '-updated_at';
};
type UserListParams = ClerkPaginationRequest<
UserCountParams & {
orderBy?: 'created_at' | 'updated_at' | '+created_at' | '+updated_at' | '-created_at' | '-updated_at';
}
>;

type UserMetadataParams = {
publicMetadata?: UserPublicMetadata;
Expand Down Expand Up @@ -77,11 +77,9 @@ interface UpdateUserParams extends UserMetadataParams {
createdAt?: Date;
}

type GetOrganizationMembershipListParams = {
type GetOrganizationMembershipListParams = ClerkPaginationRequest<{
userId: string;
limit?: number;
offset?: number;
};
}>;

type VerifyPasswordParams = {
userId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CreateOrganizationModalProps, CreateOrganizationProps } from '@clerk/types';
import type { CreateOrganizationModalProps } from '@clerk/types';

import { withOrganizationsEnabledGuard } from '../../common';
import { ComponentContext, withCoreUserGuard } from '../../contexts';
Expand Down
12 changes: 5 additions & 7 deletions packages/clerk-js/src/utils/pagesToOffset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { ClerkPaginationParams } from '@clerk/types';
import type { ClerkPaginationParams, ClerkPaginationRequest } from '@clerk/types';

type Pages = {
initialPage?: number;
pageSize?: number;
};
function getNonUndefinedValues<T>(obj: Record<string, T>): Record<string, T> {
return Object.keys(obj).reduce((result, key) => {
if (obj[key] !== undefined) {
Expand All @@ -12,8 +8,10 @@ function getNonUndefinedValues<T>(obj: Record<string, T>): Record<string, T> {
return result;
}, {} as Record<string, T>);
}
export function convertPageToOffset<T extends Pages | undefined>(pageParams: T): ClerkPaginationParams {
const { pageSize, initialPage, ...restParams } = pageParams || {};
export function convertPageToOffset<T extends ClerkPaginationParams | undefined>(
pageParams: T,
): ClerkPaginationRequest {
const { pageSize, initialPage, ...restParams } = pageParams || ({} as ClerkPaginationParams);
const _pageSize = pageSize ?? 10;
const _initialPage = initialPage ?? 1;

Expand Down
16 changes: 0 additions & 16 deletions packages/types/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,3 @@ export interface ClerkRuntimeError {
code: string;
message: string;
}

/**
* Pagination params
*/
export interface ClerkPaginationParams {
limit?: number;
offset?: number;
}

/**
* Pagination params
*/
export interface ClerkPaginatedResponse<T> {
data: T[];
total_count: number;
}
1 change: 1 addition & 0 deletions packages/types/src/index.retheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ export * from './verification';
export * from './web3';
export * from './web3Wallet';
export * from './customPages';
export * from './pagination';
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ export * from './verification';
export * from './web3';
export * from './web3Wallet';
export * from './customPages';
export * from './pagination';
65 changes: 10 additions & 55 deletions packages/types/src/organization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ClerkPaginatedResponse } from './api';
import type { OrganizationDomainResource, OrganizationEnrollmentMode } from './organizationDomain';
import type { OrganizationInvitationResource, OrganizationInvitationStatus } from './organizationInvitation';
import type { MembershipRole, OrganizationMembershipResource } from './organizationMembership';
import type { OrganizationMembershipRequestResource } from './organizationMembershipRequest';
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
import type { ClerkResource } from './resource';
import type { RoleResource } from './role';

Expand Down Expand Up @@ -64,68 +64,23 @@ export interface OrganizationResource extends ClerkResource {
/**
* @experimental
*/
export type GetRolesParams = {
/**
* This is 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;
};

export type GetMembersParams = {
/**
* This is 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;
export type GetRolesParams = ClerkPaginationParams;

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

export type GetDomainsParams = {
/**
* This is 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;
}>;

export type GetDomainsParams = ClerkPaginationParams<{
enrollmentMode?: OrganizationEnrollmentMode;
};

export type GetInvitationsParams = {
/**
* This is 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;
}>;

export type GetInvitationsParams = ClerkPaginationParams<{
status?: OrganizationInvitationStatus[];
};

export type GetMembershipRequestParams = {
/**
* This is 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;
}>;

export type GetMembershipRequestParams = ClerkPaginationParams<{
status?: OrganizationInvitationStatus;
};
}>;

export interface AddMemberParams {
userId: string;
Expand Down
35 changes: 35 additions & 0 deletions packages/types/src/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Pagination params in request
*/
export type ClerkPaginationRequest<T = any> = {
/**
* Maximum number of items returned per request.
*/
limit?: number;
/**
* This is the starting point for your fetched results.
*/
offset?: number;
} & T;

/**
* Pagination params in response
*/
export interface ClerkPaginatedResponse<T> {
data: T[];
total_count: number;
}

/**
* Pagination params passed in FAPI client methods
*/
export type ClerkPaginationParams<T = any> = {
/**
* This is the starting point for your fetched results.
*/
initialPage?: number;
/**
* Maximum number of items returned per request.
*/
pageSize?: number;
} & T;
39 changes: 6 additions & 33 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ClerkPaginatedResponse } from './api';
import type { BackupCodeResource } from './backupCode';
import type { DeletedObjectResource } from './deletedObject';
import type { EmailAddressResource } from './emailAddress';
Expand All @@ -9,6 +8,7 @@ import type { OAuthScope } from './oauth';
import type { OrganizationInvitationStatus } from './organizationInvitation';
import type { OrganizationMembershipResource } from './organizationMembership';
import type { OrganizationSuggestionResource, OrganizationSuggestionStatus } from './organizationSuggestion';
import type { ClerkPaginatedResponse, ClerkPaginationParams } from './pagination';
import type { PhoneNumberResource } from './phoneNumber';
import type { ClerkResource } from './resource';
import type { SamlAccountResource } from './samlAccount';
Expand Down Expand Up @@ -148,42 +148,15 @@ export type UpdateUserPasswordParams = {

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

export type GetUserOrganizationInvitationsParams = {
/**
* 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;

export type GetUserOrganizationInvitationsParams = ClerkPaginationParams<{
status?: OrganizationInvitationStatus;
};

export type GetUserOrganizationSuggestionsParams = {
/**
* 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;
}>;

export type GetUserOrganizationSuggestionsParams = ClerkPaginationParams<{
status?: OrganizationSuggestionStatus | OrganizationSuggestionStatus[];
};
}>;

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;
};
export type GetUserOrganizationMembershipParams = ClerkPaginationParams;

export type GetOrganizationMemberships = (
params?: GetUserOrganizationMembershipParams,
Expand Down
Loading