diff --git a/src/lib/seam/use-seam-infinite-query.ts b/src/lib/seam/use-seam-infinite-query.ts index d0f93efe8..4b649cfeb 100644 --- a/src/lib/seam/use-seam-infinite-query.ts +++ b/src/lib/seam/use-seam-infinite-query.ts @@ -10,6 +10,7 @@ import type { } from '@seamapi/http/connect' import type { ActionAttempt } from '@seamapi/types/connect' import { + type QueryKey, useInfiniteQuery, type UseInfiniteQueryOptions, type UseInfiniteQueryResult, @@ -29,19 +30,25 @@ export function useSeamInfiniteQuery< T extends SeamHttpEndpointPaginatedQueryPaths, >( endpointPath: T, - parameters?: UseSeamInfiniteQueryParameters, + parameters: UseSeamInfiniteQueryParameters = {}, options: Parameters[1] & QueryOptions, QueryError> = {} -): UseSeamInfiniteQueryResult { +): UseSeamInfiniteQueryResult & { queryKey: QueryKey } { const { endpointClient: client, queryKeyPrefixes } = useSeamClient() - return useInfiniteQuery({ + + if ('page_cursor' in (parameters ?? {})) { + throw new Error('Cannot use page_cursor with useSeamInfiniteQuery') + } + + const queryKey = [ + ...queryKeyPrefixes, + ...endpointPath.split('/').filter((v) => v !== ''), + parameters ?? {}, + ] + const result = useInfiniteQuery({ enabled: client != null, ...options, - queryKey: [ - ...queryKeyPrefixes, - ...endpointPath.split('/').filter((v) => v !== ''), - parameters, - ], + queryKey, initialPageParam: null, getNextPageParam: (lastPage) => lastPage.nextPageCursor, queryFn: async ({ pageParam }) => { @@ -72,6 +79,7 @@ export function useSeamInfiniteQuery< } }, }) + return { ...result, queryKey } } interface QueryData { diff --git a/src/lib/seam/use-seam-query-without-workspace.ts b/src/lib/seam/use-seam-query-without-workspace.ts index 3067f026e..79c9a1c9e 100644 --- a/src/lib/seam/use-seam-query-without-workspace.ts +++ b/src/lib/seam/use-seam-query-without-workspace.ts @@ -5,6 +5,7 @@ import type { SeamHttpInvalidInputError, } from '@seamapi/http/connect' import { + type QueryKey, useQuery, type UseQueryOptions, type UseQueryResult, @@ -24,19 +25,20 @@ export function useSeamQueryWithoutWorkspace< T extends SeamHttpEndpointWithoutWorkspaceQueryPaths, >( endpointPath: T, - parameters?: UseSeamQueryWithoutWorkspaceParameters, + parameters: UseSeamQueryWithoutWorkspaceParameters = {}, options: Parameters[1] & QueryOptions, QueryError> = {} -): UseSeamQueryWithoutWorkspaceResult { +): UseSeamQueryWithoutWorkspaceResult & { queryKey: QueryKey } { const { endpointClient: client, queryKeyPrefixes } = useSeamClient() - return useQuery({ + const queryKey = [ + ...queryKeyPrefixes, + ...endpointPath.split('/').filter((v) => v !== ''), + parameters ?? {}, + ] + const result = useQuery({ enabled: client != null, ...options, - queryKey: [ - ...queryKeyPrefixes, - ...endpointPath.split('/').filter((v) => v !== ''), - parameters, - ], + queryKey, queryFn: async () => { if (client == null) return null // 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< return await endpoint(parameters, options) }, }) + return { ...result, queryKey } } type QueryData = Awaited< diff --git a/src/lib/seam/use-seam-query.ts b/src/lib/seam/use-seam-query.ts index a83cc81cd..12f10d5ca 100644 --- a/src/lib/seam/use-seam-query.ts +++ b/src/lib/seam/use-seam-query.ts @@ -8,6 +8,7 @@ import type { } from '@seamapi/http/connect' import type { ActionAttempt } from '@seamapi/types/connect' import { + type QueryKey, useQuery, type UseQueryOptions, type UseQueryResult, @@ -23,19 +24,20 @@ export type UseSeamQueryResult = export function useSeamQuery( endpointPath: T, - parameters?: UseSeamQueryParameters, + parameters: UseSeamQueryParameters = {}, options: Parameters[1] & QueryOptions, SeamHttpApiError> = {} -): UseSeamQueryResult { +): UseSeamQueryResult & { queryKey: QueryKey } { const { endpointClient: client, queryKeyPrefixes } = useSeamClient() - return useQuery({ + const queryKey = [ + ...queryKeyPrefixes, + ...endpointPath.split('/').filter((v) => v !== ''), + parameters ?? {}, + ] + const result = useQuery({ enabled: client != null, ...options, - queryKey: [ - ...queryKeyPrefixes, - ...endpointPath.split('/').filter((v) => v !== ''), - parameters, - ], + queryKey, queryFn: async () => { if (client == null) return null // 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( return await endpoint(parameters, options) }, }) + return { ...result, queryKey } } type QueryData = Awaited<