Skip to content

Commit 66fdeb4

Browse files
authored
chore(shared): Add stable keys registry for billing and paginated hooks (#7248)
1 parent a9c13ca commit 66fdeb4

15 files changed

+92
-20
lines changed

.changeset/flat-grapes-visit.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/shared/src/react/hooks/__tests__/createBillingPaginatedHook.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
33

44
import type { ClerkResource } from '../../../types';
5+
import type { ResourceCacheStableKey } from '../../stable-keys';
56
import { createBillingPaginatedHook } from '../createBillingPaginatedHook';
67
import { createMockClerk, createMockOrganization, createMockQueryClient, createMockUser } from './mocks/clerk';
78
import { wrapper } from './wrapper';
@@ -33,13 +34,13 @@ const useFetcherMock = vi.fn(() => fetcherMock);
3334

3435
const useDummyAuth = createBillingPaginatedHook<DummyResource, DummyParams>({
3536
hookName: 'useDummyAuth',
36-
resourceType: 'dummy',
37+
resourceType: 'dummy' as ResourceCacheStableKey,
3738
useFetcher: useFetcherMock,
3839
});
3940

4041
const useDummyUnauth = createBillingPaginatedHook<DummyResource, DummyParams>({
4142
hookName: 'useDummyUnauth',
42-
resourceType: 'dummy',
43+
resourceType: 'dummy' as ResourceCacheStableKey,
4344
useFetcher: useFetcherMock,
4445
options: { unauthenticated: true },
4546
});

packages/shared/src/react/hooks/__tests__/usePagesOrInfinite.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
33

44
import { createDeferredPromise } from '../../../utils/createDeferredPromise';
5+
import type { ResourceCacheStableKey } from '../../stable-keys';
56
import { createCacheKeys } from '../createCacheKeys';
67
import { usePagesOrInfinite } from '../usePagesOrInfinite';
78
import { createMockClerk, createMockQueryClient } from './mocks/clerk';
@@ -43,7 +44,11 @@ const buildKeys = <Params extends Record<string, unknown>>(
4344
authenticated = true,
4445
) =>
4546
createCacheKeys({
46-
stablePrefix,
47+
// Casting to ResourceCacheStableKey to satisfy the type checker,
48+
// it is fine because we only want to limit the types to ensure our stable keys
49+
// do not diverge when consumed from other pacakges.
50+
// Since this is a test mocking most things we can safely ignore the type checker.
51+
stablePrefix: stablePrefix as ResourceCacheStableKey,
4752
authenticated,
4853
tracked,
4954
untracked: { args: params },

packages/shared/src/react/hooks/createBillingPaginatedHook.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useOrganizationContext,
77
useUserContext,
88
} from '../contexts';
9+
import type { ResourceCacheStableKey } from '../stable-keys';
910
import type { PagesOrInfiniteOptions, PaginatedHookConfig, PaginatedResources } from '../types';
1011
import { createCacheKeys } from './createCacheKeys';
1112
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
@@ -15,7 +16,7 @@ import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
1516
*/
1617
type BillingHookConfig<TResource extends ClerkResource, TParams extends PagesOrInfiniteOptions> = {
1718
hookName: string;
18-
resourceType: string;
19+
resourceType: ResourceCacheStableKey;
1920
useFetcher: (
2021
param: ForPayerType,
2122
) => ((params: TParams & { orgId?: string }) => Promise<ClerkPaginatedResponse<TResource>>) | undefined;

packages/shared/src/react/hooks/createCacheKeys.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import type { ResourceCacheStableKey } from '../stable-keys';
2+
13
/**
24
* @internal
35
*/
46
export function createCacheKeys<
57
Params,
6-
StableKey extends string,
78
T extends Record<string, unknown> = Record<string, unknown>,
89
U extends Record<string, unknown> | undefined = undefined,
910
>(params: {
10-
stablePrefix: StableKey;
11+
stablePrefix: ResourceCacheStableKey;
1112
authenticated: boolean;
1213
tracked: T;
1314
untracked: U extends { args: Params } ? U : never;

packages/shared/src/react/hooks/useAPIKeys.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { eventMethodCalled } from '../../telemetry/events/method-called';
44
import type { APIKeyResource, GetAPIKeysParams } from '../../types';
55
import { useAssertWrappedByClerkProvider, useClerkInstanceContext } from '../contexts';
6+
import { STABLE_KEYS } from '../stable-keys';
67
import type { PaginatedHookConfig, PaginatedResources } from '../types';
78
import { createCacheKeys } from './createCacheKeys';
89
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
@@ -104,7 +105,7 @@ export function useAPIKeys<T extends UseAPIKeysParams>(params?: T): UseAPIKeysRe
104105
pageSize: safeValues.pageSize,
105106
},
106107
keys: createCacheKeys({
107-
stablePrefix: 'apiKeys',
108+
stablePrefix: STABLE_KEYS.API_KEYS_KEY,
108109
authenticated: Boolean(clerk.user),
109110
tracked: {
110111
subject: safeValues.subject,

packages/shared/src/react/hooks/useOrganization.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
useOrganizationContext,
1818
useSessionContext,
1919
} from '../contexts';
20+
import { STABLE_KEYS } from '../stable-keys';
2021
import type { PaginatedHookConfig, PaginatedResources, PaginatedResourcesWithDefault } from '../types';
2122
import { createCacheKeys } from './createCacheKeys';
2223
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
@@ -368,7 +369,7 @@ export function useOrganization<T extends UseOrganizationParams>(params?: T): Us
368369
pageSize: domainSafeValues.pageSize,
369370
},
370371
keys: createCacheKeys({
371-
stablePrefix: 'domains',
372+
stablePrefix: STABLE_KEYS.DOMAINS_KEY,
372373
authenticated: Boolean(organization),
373374
tracked: {
374375
organizationId: organization?.id,
@@ -390,7 +391,7 @@ export function useOrganization<T extends UseOrganizationParams>(params?: T): Us
390391
pageSize: membershipRequestSafeValues.pageSize,
391392
},
392393
keys: createCacheKeys({
393-
stablePrefix: 'membershipRequests',
394+
stablePrefix: STABLE_KEYS.MEMBERSHIP_REQUESTS_KEY,
394395
authenticated: Boolean(organization),
395396
tracked: {
396397
organizationId: organization?.id,
@@ -412,7 +413,7 @@ export function useOrganization<T extends UseOrganizationParams>(params?: T): Us
412413
pageSize: membersSafeValues.pageSize,
413414
},
414415
keys: createCacheKeys({
415-
stablePrefix: 'members',
416+
stablePrefix: STABLE_KEYS.MEMBERSHIPS_KEY,
416417
authenticated: Boolean(organization),
417418
tracked: {
418419
organizationId: organization?.id,
@@ -434,7 +435,7 @@ export function useOrganization<T extends UseOrganizationParams>(params?: T): Us
434435
pageSize: invitationsSafeValues.pageSize,
435436
},
436437
keys: createCacheKeys({
437-
stablePrefix: 'invitations',
438+
stablePrefix: STABLE_KEYS.INVITATIONS_KEY,
438439
authenticated: Boolean(organization),
439440
tracked: {
440441
organizationId: organization?.id,

packages/shared/src/react/hooks/useOrganizationList.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
UserOrganizationInvitationResource,
1212
} from '../../types';
1313
import { useAssertWrappedByClerkProvider, useClerkInstanceContext, useUserContext } from '../contexts';
14+
import { STABLE_KEYS } from '../stable-keys';
1415
import type { PaginatedHookConfig, PaginatedResources, PaginatedResourcesWithDefault } from '../types';
1516
import { createCacheKeys } from './createCacheKeys';
1617
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
@@ -318,7 +319,7 @@ export function useOrganizationList<T extends UseOrganizationListParams>(params?
318319
pageSize: userMembershipsSafeValues.pageSize,
319320
},
320321
keys: createCacheKeys({
321-
stablePrefix: 'userMemberships',
322+
stablePrefix: STABLE_KEYS.USER_MEMBERSHIPS_KEY,
322323
authenticated: Boolean(user),
323324
tracked: {
324325
userId: user?.id,
@@ -341,7 +342,7 @@ export function useOrganizationList<T extends UseOrganizationListParams>(params?
341342
pageSize: userInvitationsSafeValues.pageSize,
342343
},
343344
keys: createCacheKeys({
344-
stablePrefix: 'userInvitations',
345+
stablePrefix: STABLE_KEYS.USER_INVITATIONS_KEY,
345346
authenticated: Boolean(user),
346347
tracked: {
347348
userId: user?.id,
@@ -363,7 +364,7 @@ export function useOrganizationList<T extends UseOrganizationListParams>(params?
363364
pageSize: userSuggestionsSafeValues.pageSize,
364365
},
365366
keys: createCacheKeys({
366-
stablePrefix: 'userSuggestions',
367+
stablePrefix: STABLE_KEYS.USER_SUGGESTIONS_KEY,
367368
authenticated: Boolean(user),
368369
tracked: {
369370
userId: user?.id,

packages/shared/src/react/hooks/usePaymentAttempts.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { BillingPaymentResource, GetPaymentAttemptsParams } from '../../types';
22
import { useClerkInstanceContext } from '../contexts';
3+
import { STABLE_KEYS } from '../stable-keys';
34
import { createBillingPaginatedHook } from './createBillingPaginatedHook';
45

56
/**
67
* @internal
78
*/
89
export const usePaymentAttempts = createBillingPaginatedHook<BillingPaymentResource, GetPaymentAttemptsParams>({
910
hookName: 'usePaymentAttempts',
10-
resourceType: 'billing-payment-attempts',
11+
resourceType: STABLE_KEYS.PAYMENT_ATTEMPTS_KEY,
1112
useFetcher: () => {
1213
const clerk = useClerkInstanceContext();
1314
if (clerk.loaded) {

packages/shared/src/react/hooks/usePaymentMethods.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { BillingPaymentMethodResource, GetPaymentMethodsParams } from '../../types';
22
import { useOrganizationContext, useUserContext } from '../contexts';
3+
import { STABLE_KEYS } from '../stable-keys';
34
import { createBillingPaginatedHook } from './createBillingPaginatedHook';
45

56
/**
67
* @internal
78
*/
89
export const usePaymentMethods = createBillingPaginatedHook<BillingPaymentMethodResource, GetPaymentMethodsParams>({
910
hookName: 'usePaymentMethods',
10-
resourceType: 'commerce-payment-methods',
11+
resourceType: STABLE_KEYS.PAYMENT_METHODS_KEY,
1112
useFetcher: resource => {
1213
const { organization } = useOrganizationContext();
1314
const user = useUserContext();

0 commit comments

Comments
 (0)