Skip to content

Commit a91d01d

Browse files
committed
feat: Return queryKey from hooks
1 parent 70322d7 commit a91d01d

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

src/lib/seam/use-seam-infinite-query.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from '@seamapi/http/connect'
1111
import type { ActionAttempt } from '@seamapi/types/connect'
1212
import {
13+
type QueryKey,
1314
useInfiniteQuery,
1415
type UseInfiniteQueryOptions,
1516
type UseInfiniteQueryResult,
@@ -29,19 +30,20 @@ export function useSeamInfiniteQuery<
2930
T extends SeamHttpEndpointPaginatedQueryPaths,
3031
>(
3132
endpointPath: T,
32-
parameters?: UseSeamInfiniteQueryParameters<T>,
33+
parameters: UseSeamInfiniteQueryParameters<T> = {},
3334
options: Parameters<SeamHttpEndpoints[T]>[1] &
3435
QueryOptions<QueryData<T>, QueryError<T>> = {}
35-
): UseSeamInfiniteQueryResult<T> {
36+
): UseSeamInfiniteQueryResult<T> & { queryKey: QueryKey } {
3637
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
37-
return useInfiniteQuery({
38+
const queryKey = [
39+
...queryKeyPrefixes,
40+
...endpointPath.split('/').filter((v) => v !== ''),
41+
parameters,
42+
]
43+
const result = useInfiniteQuery({
3844
enabled: client != null,
3945
...options,
40-
queryKey: [
41-
...queryKeyPrefixes,
42-
...endpointPath.split('/').filter((v) => v !== ''),
43-
parameters,
44-
],
46+
queryKey,
4547
initialPageParam: null,
4648
getNextPageParam: (lastPage) => lastPage.nextPageCursor,
4749
queryFn: async ({ pageParam }) => {
@@ -72,6 +74,7 @@ export function useSeamInfiniteQuery<
7274
}
7375
},
7476
})
77+
return { ...result, queryKey }
7578
}
7679

7780
interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> {

src/lib/seam/use-seam-query-without-workspace.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
import {
88
useQuery,
99
type UseQueryOptions,
10+
type QueryKey,
1011
type UseQueryResult,
1112
} from '@tanstack/react-query'
1213

@@ -24,19 +25,20 @@ export function useSeamQueryWithoutWorkspace<
2425
T extends SeamHttpEndpointWithoutWorkspaceQueryPaths,
2526
>(
2627
endpointPath: T,
27-
parameters?: UseSeamQueryWithoutWorkspaceParameters<T>,
28+
parameters: UseSeamQueryWithoutWorkspaceParameters<T> = {},
2829
options: Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[1] &
2930
QueryOptions<QueryData<T>, QueryError> = {}
30-
): UseSeamQueryWithoutWorkspaceResult<T> {
31+
): UseSeamQueryWithoutWorkspaceResult<T> & { queryKey: QueryKey } {
3132
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
32-
return useQuery({
33+
const queryKey = [
34+
...queryKeyPrefixes,
35+
...endpointPath.split('/').filter((v) => v !== ''),
36+
parameters,
37+
]
38+
const result = useQuery({
3339
enabled: client != null,
3440
...options,
35-
queryKey: [
36-
...queryKeyPrefixes,
37-
...endpointPath.split('/').filter((v) => v !== ''),
38-
parameters,
39-
],
41+
queryKey,
4042
queryFn: async () => {
4143
if (client == null) return null
4244
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
@@ -45,6 +47,7 @@ export function useSeamQueryWithoutWorkspace<
4547
return await endpoint(parameters, options)
4648
},
4749
})
50+
return { ...result, queryKey }
4851
}
4952

5053
type QueryData<T extends SeamHttpEndpointWithoutWorkspaceQueryPaths> = Awaited<

src/lib/seam/use-seam-query.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
} from '@seamapi/http/connect'
99
import type { ActionAttempt } from '@seamapi/types/connect'
1010
import {
11+
type QueryKey,
1112
useQuery,
1213
type UseQueryOptions,
1314
type UseQueryResult,
@@ -23,19 +24,20 @@ export type UseSeamQueryResult<T extends SeamHttpEndpointQueryPaths> =
2324

2425
export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
2526
endpointPath: T,
26-
parameters?: UseSeamQueryParameters<T>,
27+
parameters: UseSeamQueryParameters<T> = {},
2728
options: Parameters<SeamHttpEndpoints[T]>[1] &
2829
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
29-
): UseSeamQueryResult<T> {
30+
): UseSeamQueryResult<T> & { queryKey: QueryKey } {
3031
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
31-
return useQuery({
32+
const queryKey = [
33+
...queryKeyPrefixes,
34+
...endpointPath.split('/').filter((v) => v !== ''),
35+
parameters,
36+
]
37+
const result = useQuery({
3238
enabled: client != null,
3339
...options,
34-
queryKey: [
35-
...queryKeyPrefixes,
36-
...endpointPath.split('/').filter((v) => v !== ''),
37-
parameters,
38-
],
40+
queryKey,
3941
queryFn: async () => {
4042
if (client == null) return null
4143
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
@@ -44,6 +46,7 @@ export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
4446
return await endpoint(parameters, options)
4547
},
4648
})
49+
return { ...result, queryKey }
4750
}
4851

4952
type QueryData<T extends SeamHttpEndpointQueryPaths> = Awaited<

0 commit comments

Comments
 (0)