-
Notifications
You must be signed in to change notification settings - Fork 406
chore(clerk-js, shared): Use experimental paginated hooks for querying commerce data #6159
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
Merged
panteliselef
merged 16 commits into
main
from
elef/com-959-dx-guide-introduce-query-hooks-for-resources
Jun 25, 2025
+351
−89
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
07fcccf
wip add hooks for attempts, methods, statements
panteliselef f35fb2b
add subscription items
panteliselef b17c2e1
Create a factory
panteliselef 1cf74f0
cleanup
panteliselef 8a3d87c
cleanup of swr fetchOnMount logic
panteliselef ac26c47
add changesets
panteliselef c971964
Merge branch 'main' into elef/com-959-dx-guide-introduce-query-hooks-…
panteliselef 6991ac0
remove type change
panteliselef 59731cc
add internal jsdoc comments
panteliselef e693842
improve dx and add comments
panteliselef d13a64c
Merge branch 'main' into elef/com-959-dx-guide-introduce-query-hooks-…
panteliselef 99fe9d3
remove ts-ignore comment
panteliselef 4c8e43e
update jsdoc for `__experimental_mode`
panteliselef 13b2a1c
remove wrong types
panteliselef ed63fb2
revert type change
panteliselef 27a6c17
Merge branch 'main' into elef/com-959-dx-guide-introduce-query-hooks-…
panteliselef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| --- | ||
|
|
||
| Use hooks exported from `@clerk/shared` to query commerce data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@clerk/shared': minor | ||
| '@clerk/types': minor | ||
| --- | ||
|
|
||
| Introduce experimental paginated hooks for commerce data. | ||
| - `useStatements` | ||
| - `usePaymentAttempts` | ||
| - `usePaymentMethods` | ||
| Prefixed with `__experimental_` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { ClerkPaginatedResponse, ClerkResource } from '@clerk/types'; | ||
|
|
||
| import { eventMethodCalled } from '../../telemetry/events/method-called'; | ||
| import { | ||
| useAssertWrappedByClerkProvider, | ||
| useClerkInstanceContext, | ||
| useOrganizationContext, | ||
| useUserContext, | ||
| } from '../contexts'; | ||
| import type { PagesOrInfiniteOptions, PaginatedHookConfig, PaginatedResources } from '../types'; | ||
| import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| type CommerceHookConfig<TResource extends ClerkResource, TParams extends PagesOrInfiniteOptions> = { | ||
| hookName: string; | ||
| resourceType: string; | ||
| useFetcher: ( | ||
| param: 'organization' | 'user', | ||
| ) => ((params: TParams) => Promise<ClerkPaginatedResponse<TResource>>) | undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * A hook factory that creates paginated data fetching hooks for commerce-related resources. | ||
| * It provides a standardized way to create hooks that can fetch either user or organization resources | ||
| * with built-in pagination support. | ||
| * | ||
| * The generated hooks handle: | ||
| * - Clerk authentication context | ||
| * - Resource-specific data fetching | ||
| * - Pagination (both traditional and infinite scroll) | ||
| * - Telemetry tracking | ||
| * - Type safety for the specific resource. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function createCommerceHook<TResource extends ClerkResource, TParams extends PagesOrInfiniteOptions>({ | ||
| hookName, | ||
| resourceType, | ||
| useFetcher, | ||
| }: CommerceHookConfig<TResource, TParams>) { | ||
| type HookParams = PaginatedHookConfig<PagesOrInfiniteOptions> & { | ||
| for: 'organization' | 'user'; | ||
| }; | ||
|
|
||
| return function useCommerceHook<T extends HookParams>( | ||
| params: T, | ||
| ): PaginatedResources<TResource, T extends { infinite: true } ? true : false> { | ||
| const { for: _for, ...paginationParams } = params; | ||
|
|
||
| useAssertWrappedByClerkProvider(hookName); | ||
|
|
||
| const fetchFn = useFetcher(_for); | ||
|
|
||
| const safeValues = useWithSafeValues(paginationParams, { | ||
| initialPage: 1, | ||
| pageSize: 10, | ||
| keepPreviousData: false, | ||
| infinite: false, | ||
| __experimental_mode: undefined, | ||
| } as unknown as T); | ||
|
|
||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserContext(); | ||
| const { organization } = useOrganizationContext(); | ||
|
|
||
| clerk.telemetry?.record(eventMethodCalled(hookName)); | ||
|
|
||
| const hookParams = | ||
| typeof paginationParams === 'undefined' | ||
| ? undefined | ||
| : ({ | ||
| initialPage: safeValues.initialPage, | ||
| pageSize: safeValues.pageSize, | ||
| ...(_for === 'organization' ? { orgId: organization?.id } : {}), | ||
| } as TParams); | ||
|
|
||
| const isClerkLoaded = !!(clerk.loaded && user); | ||
|
|
||
| const isEnabled = !!hookParams && isClerkLoaded; | ||
|
|
||
| const result = usePagesOrInfinite<TParams, ClerkPaginatedResponse<TResource>>( | ||
| (hookParams || {}) as TParams, | ||
| fetchFn, | ||
| { | ||
| keepPreviousData: safeValues.keepPreviousData, | ||
| infinite: safeValues.infinite, | ||
| enabled: isEnabled, | ||
| __experimental_mode: safeValues.__experimental_mode, | ||
| }, | ||
| { | ||
| type: resourceType, | ||
| userId: user?.id, | ||
| ...(_for === 'organization' ? { orgId: organization?.id } : {}), | ||
| }, | ||
| ); | ||
|
|
||
| return result; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { CommercePaymentResource, GetPaymentAttemptsParams } from '@clerk/types'; | ||
|
|
||
| import { useClerkInstanceContext } from '../contexts'; | ||
| import { createCommerceHook } from './createCommerceHook'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const usePaymentAttempts = createCommerceHook<CommercePaymentResource, GetPaymentAttemptsParams>({ | ||
| hookName: 'usePaymentAttempts', | ||
| resourceType: 'commerce-payment-attempts', | ||
| useFetcher: () => { | ||
| const clerk = useClerkInstanceContext(); | ||
| return clerk.billing.getPaymentAttempts; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { CommercePaymentSourceResource, GetPaymentSourcesParams } from '@clerk/types'; | ||
|
|
||
| import { useOrganizationContext, useUserContext } from '../contexts'; | ||
| import { createCommerceHook } from './createCommerceHook'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const usePaymentMethods = createCommerceHook<CommercePaymentSourceResource, GetPaymentSourcesParams>({ | ||
| hookName: 'usePaymentMethods', | ||
| resourceType: 'commerce-payment-methods', | ||
| useFetcher: resource => { | ||
| const { organization } = useOrganizationContext(); | ||
| const user = useUserContext(); | ||
|
|
||
| if (resource === 'organization') { | ||
| return organization?.getPaymentSources; | ||
| } | ||
| return user?.getPaymentSources; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { CommerceStatementResource, GetStatementsParams } from '@clerk/types'; | ||
|
|
||
| import { useClerkInstanceContext } from '../contexts'; | ||
| import { createCommerceHook } from './createCommerceHook'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const useStatements = createCommerceHook<CommerceStatementResource, GetStatementsParams>({ | ||
| hookName: 'useStatements', | ||
| resourceType: 'commerce-statements', | ||
| useFetcher: () => { | ||
| const clerk = useClerkInstanceContext(); | ||
| return clerk.billing.getStatements; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { CommerceSubscriptionResource, GetSubscriptionsParams } from '@clerk/types'; | ||
|
|
||
| import { useClerkInstanceContext } from '../contexts'; | ||
| import { createCommerceHook } from './createCommerceHook'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const useSubscriptionItems = createCommerceHook<CommerceSubscriptionResource, GetSubscriptionsParams>({ | ||
| hookName: 'useSubscriptionItems', | ||
| resourceType: 'commerce-subscription-items', | ||
| useFetcher: () => { | ||
| const clerk = useClerkInstanceContext(); | ||
| return clerk.billing.getSubscriptions; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 This comment feels like it should be reflected in the
__experimental_modejsdoc description.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consumers of the exported hooks, do not have control over the fetcher, thus should not be mentioned