-
Notifications
You must be signed in to change notification settings - Fork 418
chore(clerk-js, shared): Remove SWR from clerk-js and create variants for existing internal hooks #7270
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(clerk-js, shared): Remove SWR from clerk-js and create variants for existing internal hooks #7270
Changes from 2 commits
2f87ceb
5855d81
c10e94d
5c4823d
c258f7a
a9359ad
7f118ff
86373fc
94ef80b
42fda7b
bc36ccd
121977e
3b52ab4
10c1b5f
8141be7
acbc638
dc4843d
d9a307c
f820c96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { eventMethodCalled } from '../../telemetry/events'; | ||
| import { useClerkQuery } from '../clerk-rq/useQuery'; | ||
| import { | ||
| useAssertWrappedByClerkProvider, | ||
| useClerkInstanceContext, | ||
| useOrganizationContext, | ||
| useUserContext, | ||
| } from '../contexts'; | ||
| import { usePaymentAttemptQueryCacheKeys } from './usePaymentAttemptQuery.shared'; | ||
| import type { PaymentAttemptQueryResult, UsePaymentAttemptQueryParams } from './usePaymentAttemptQuery.types'; | ||
|
|
||
| const HOOK_NAME = 'usePaymentAttemptQuery'; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| function KeepPreviousDataFn<Data>(previousData: Data): Data { | ||
| return previousData; | ||
| } | ||
|
|
||
| /** | ||
| * This is the new implementation of usePaymentAttemptQuery using React Query. | ||
| * It is exported only if the package is built with the `CLERK_USE_RQ` environment variable set to `true`. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function __internal_usePaymentAttemptQuery(params: UsePaymentAttemptQueryParams): PaymentAttemptQueryResult { | ||
| useAssertWrappedByClerkProvider(HOOK_NAME); | ||
|
|
||
| const { paymentAttemptId, enabled = true, keepPreviousData = false, for: forType = 'user' } = params; | ||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserContext(); | ||
| const { organization } = useOrganizationContext(); | ||
|
|
||
| clerk.telemetry?.record(eventMethodCalled(HOOK_NAME)); | ||
|
|
||
| const organizationId = forType === 'organization' ? (organization?.id ?? null) : null; | ||
| const userId = user?.id ?? null; | ||
|
|
||
| const { queryKey } = usePaymentAttemptQueryCacheKeys({ | ||
| paymentAttemptId, | ||
| userId, | ||
| orgId: organizationId, | ||
| for: forType, | ||
| }); | ||
|
|
||
| const queryEnabled = Boolean(paymentAttemptId) && enabled && (forType !== 'organization' || Boolean(organizationId)); | ||
|
|
||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: ({ queryKey }) => { | ||
| const args = queryKey[3].args; | ||
| return clerk.billing.getPaymentAttempt(args); | ||
| }, | ||
| enabled: queryEnabled, | ||
| placeholderData: keepPreviousData ? KeepPreviousDataFn : undefined, | ||
| staleTime: 1_000 * 60, | ||
| }); | ||
|
|
||
| return { | ||
| data: query.data, | ||
| error: (query.error ?? null) as PaymentAttemptQueryResult['error'], | ||
| isLoading: query.isLoading, | ||
| isFetching: query.isFetching, | ||
| }; | ||
|
Comment on lines
+42
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainVerify error type compatibility. Line 44 uses a type assertion to coerce Please verify that errors thrown by 🏁 Script executed: #!/bin/bash
# Description: Search for the getPaymentAttempt implementation and error handling to verify error types
# Search for getPaymentAttempt method definition
ast-grep --pattern 'getPaymentAttempt($$$) {
$$$
}'
# Search for ClerkAPIResponseError usage in billing-related code
rg -n --type=ts -C3 'ClerkAPIResponseError' -g '*billing*'
# Search for error handling in clerk-rq useQuery implementation
rg -n --type=ts -C5 'export function useClerkQuery' -A 20Length of output: 2693 🏁 Script executed: #!/bin/bash
# Search for the full usePaymentAttemptQuery implementation
rg -n --type=ts -B5 -A30 'function usePaymentAttemptQuery' packages/shared/src/react/hooks/usePaymentAttemptQuery.rq.tsx
# Search for PaymentAttemptQueryResult type definition
rg -n --type=ts 'type PaymentAttemptQueryResult\|interface PaymentAttemptQueryResult'
# Search for clerk.billing.getPaymentAttempt usage and type hints
rg -n --type=ts 'clerk\.billing\.getPaymentAttempt\|getPaymentAttempt' -g '*billing*'Length of output: 1248 🏁 Script executed: #!/bin/bash
# View the full file to see the complete return statement
cat packages/shared/src/react/hooks/usePaymentAttemptQuery.rq.tsx
# Search for .types file with PaymentAttemptQueryResult
fd 'usePaymentAttemptQuery.types' packages/Length of output: 1831 🏁 Script executed: #!/bin/bash
# View the types file
cat packages/shared/src/react/hooks/usePaymentAttemptQuery.types.ts
# Search for DefaultError definition
rg -n --type=ts 'type DefaultError\|export.*DefaultError' packages/shared/src/react/Length of output: 1329 🏁 Script executed: #!/bin/bash
# Search for DefaultError from React Query
rg -n 'DefaultError' packages/shared/src/react/clerk-rq/ -A2 -B2
# Search for other uses of useClerkQuery with explicit TError
rg -n 'useClerkQuery' packages/shared/src/react/hooks/ -A5 | head -100
# Check if clerk.billing methods are documented to throw ClerkAPIResponseError
rg -n 'clerk\.billing\.' packages/shared/src/react/hooks/ -C2Length of output: 24853 🏁 Script executed: #!/bin/bash
# Check return statements in similar query hooks for type assertions
rg -n 'error:' packages/shared/src/react/hooks/ --type=ts -A2 -B2 | grep -A5 -B5 'as'
# Search for all type assertions on error in billing hooks
rg -n 'as.*\[.*error' packages/shared/src/react/hooks/ --type=ts
# View useSubscription return to compare patterns
cat packages/shared/src/react/hooks/useSubscription.rq.tsx | tail -20
# View usePlanDetailsQuery return to compare patterns
cat packages/shared/src/react/hooks/usePlanDetailsQuery.rq.tsx | tail -20Length of output: 6438 Add explicit TError parameter to useClerkQuery or validate error types at runtime. The type assertion on line 44 masks a real type mismatch. Fix by either:
Similar hooks like 🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { useMemo } from 'react'; | ||
|
|
||
| import type { ForPayerType } from '../../types'; | ||
| import { INTERNAL_STABLE_KEYS } from '../stable-keys'; | ||
| import { createCacheKeys } from './createCacheKeys'; | ||
|
|
||
| export function usePaymentAttemptQueryCacheKeys(params: { | ||
| paymentAttemptId: string; | ||
| userId: string | null; | ||
| orgId: string | null; | ||
| for?: ForPayerType; | ||
| }) { | ||
| const { paymentAttemptId, userId, orgId, for: forType } = params; | ||
| return useMemo(() => { | ||
| return createCacheKeys({ | ||
| stablePrefix: INTERNAL_STABLE_KEYS.PAYMENT_ATTEMPT_KEY, | ||
| authenticated: true, | ||
| tracked: { | ||
| paymentAttemptId, | ||
| forType, | ||
| userId, | ||
| orgId, | ||
| }, | ||
| untracked: { | ||
| args: { | ||
| id: paymentAttemptId ?? undefined, | ||
| orgId: orgId ?? undefined, | ||
| }, | ||
| }, | ||
| }); | ||
| }, [paymentAttemptId, forType, userId, orgId]); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.