Skip to content

Commit 4a78202

Browse files
authored
chore(shared): Proper return type of useSubscription (#7147)
1 parent 2738c7e commit 4a78202

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

.changeset/fancy-poems-pay.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__/useSubscription.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ describe('useSubscription', () => {
6161
expect(result.current.isLoading).toBe(false);
6262
expect(result.current.isFetching).toBe(false);
6363
expect(result.current.data).toBeUndefined();
64+
expect(result.current.error).toBeUndefined();
65+
expect(result.current.revalidate).toBeInstanceOf(Function);
6466
});
6567

6668
it('fetches user subscription when billing enabled (no org)', async () => {
@@ -81,6 +83,7 @@ describe('useSubscription', () => {
8183
expect(getSubscriptionSpy).toHaveBeenCalledTimes(1);
8284
expect(getSubscriptionSpy).toHaveBeenCalledWith({ orgId: 'org_1' });
8385
expect(result.current.data).toEqual({ id: 'sub_org_org_1' });
86+
expect(result.current.error).toBeUndefined();
8487
});
8588

8689
it('hides stale data on sign-out', async () => {

packages/shared/src/react/hooks/useSubscription.rq.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback, useMemo } from 'react';
22

33
import { eventMethodCalled } from '../../telemetry/events';
4-
import type { BillingSubscriptionResource, EnvironmentResource } from '../../types';
4+
import type { EnvironmentResource } from '../../types';
55
import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client';
66
import { useClerkQuery } from '../clerk-rq/useQuery';
77
import {
@@ -15,11 +15,12 @@ import type { SubscriptionResult, UseSubscriptionParams } from './useSubscriptio
1515
const hookName = 'useSubscription';
1616

1717
/**
18-
* @internal
1918
* This is the new implementation of useSubscription using React Query.
2019
* It is exported only if the package is build with the `CLERK_USE_RQ` environment variable set to `true`.
20+
*
21+
* @internal
2122
*/
22-
export function useSubscription(params?: UseSubscriptionParams): SubscriptionResult<BillingSubscriptionResource> {
23+
export function useSubscription(params?: UseSubscriptionParams): SubscriptionResult {
2324
useAssertWrappedByClerkProvider(hookName);
2425

2526
const clerk = useClerkInstanceContext();
@@ -62,7 +63,9 @@ export function useSubscription(params?: UseSubscriptionParams): SubscriptionRes
6263

6364
return {
6465
data: query.data,
65-
error: query.error,
66+
// Our existing types for SWR return undefined when there is no error, but React Query returns null.
67+
// So we need to convert the error to undefined, for backwards compatibility.
68+
error: query.error ?? undefined,
6669
isLoading: query.isLoading,
6770
isFetching: query.isFetching,
6871
revalidate,

packages/shared/src/react/hooks/useSubscription.swr.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback } from 'react';
22

33
import { eventMethodCalled } from '../../telemetry/events';
4-
import type { BillingSubscriptionResource, EnvironmentResource } from '../../types';
4+
import type { EnvironmentResource } from '../../types';
55
import { useSWR } from '../clerk-swr';
66
import {
77
useAssertWrappedByClerkProvider,
@@ -14,11 +14,12 @@ import type { SubscriptionResult, UseSubscriptionParams } from './useSubscriptio
1414
const hookName = 'useSubscription';
1515

1616
/**
17-
* @internal
1817
* This is the existing implementation of useSubscription using SWR.
1918
* It is kept here for backwards compatibility until our next major version.
19+
*
20+
* @internal
2021
*/
21-
export function useSubscription(params?: UseSubscriptionParams): SubscriptionResult<BillingSubscriptionResource> {
22+
export function useSubscription(params?: UseSubscriptionParams): SubscriptionResult {
2223
useAssertWrappedByClerkProvider(hookName);
2324

2425
const clerk = useClerkInstanceContext();

packages/shared/src/react/hooks/useSubscription.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ForPayerType } from '../../types';
1+
import type { BillingSubscriptionResource, ForPayerType } from '../../types';
22

33
export type UseSubscriptionParams = {
44
for?: ForPayerType;
@@ -9,9 +9,9 @@ export type UseSubscriptionParams = {
99
keepPreviousData?: boolean;
1010
};
1111

12-
export type SubscriptionResult<TData> = {
13-
data: TData | undefined | null;
14-
error: unknown;
12+
export type SubscriptionResult = {
13+
data: BillingSubscriptionResource | undefined | null;
14+
error: Error | undefined;
1515
isLoading: boolean;
1616
isFetching: boolean;
1717
/**

0 commit comments

Comments
 (0)