Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/lib/seam/use-seam-infinite-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,19 +30,25 @@ export function useSeamInfiniteQuery<
T extends SeamHttpEndpointPaginatedQueryPaths,
>(
endpointPath: T,
parameters?: UseSeamInfiniteQueryParameters<T>,
parameters: UseSeamInfiniteQueryParameters<T> = {},
options: Parameters<SeamHttpEndpoints[T]>[1] &
QueryOptions<QueryData<T>, QueryError<T>> = {}
): UseSeamInfiniteQueryResult<T> {
): UseSeamInfiniteQueryResult<T> & { 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 }) => {
Expand Down Expand Up @@ -72,6 +79,7 @@ export function useSeamInfiniteQuery<
}
},
})
return { ...result, queryKey }
}

interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> {
Expand Down
19 changes: 11 additions & 8 deletions src/lib/seam/use-seam-query-without-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
SeamHttpInvalidInputError,
} from '@seamapi/http/connect'
import {
type QueryKey,
useQuery,
type UseQueryOptions,
type UseQueryResult,
Expand All @@ -24,19 +25,20 @@ export function useSeamQueryWithoutWorkspace<
T extends SeamHttpEndpointWithoutWorkspaceQueryPaths,
>(
endpointPath: T,
parameters?: UseSeamQueryWithoutWorkspaceParameters<T>,
parameters: UseSeamQueryWithoutWorkspaceParameters<T> = {},
options: Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[1] &
QueryOptions<QueryData<T>, QueryError> = {}
): UseSeamQueryWithoutWorkspaceResult<T> {
): UseSeamQueryWithoutWorkspaceResult<T> & { 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.
Expand All @@ -45,6 +47,7 @@ export function useSeamQueryWithoutWorkspace<
return await endpoint(parameters, options)
},
})
return { ...result, queryKey }
}

type QueryData<T extends SeamHttpEndpointWithoutWorkspaceQueryPaths> = Awaited<
Expand Down
19 changes: 11 additions & 8 deletions src/lib/seam/use-seam-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,19 +24,20 @@ export type UseSeamQueryResult<T extends SeamHttpEndpointQueryPaths> =

export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
endpointPath: T,
parameters?: UseSeamQueryParameters<T>,
parameters: UseSeamQueryParameters<T> = {},
options: Parameters<SeamHttpEndpoints[T]>[1] &
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
): UseSeamQueryResult<T> {
): UseSeamQueryResult<T> & { 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.
Expand All @@ -44,6 +46,7 @@ export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
return await endpoint(parameters, options)
},
})
return { ...result, queryKey }
}

type QueryData<T extends SeamHttpEndpointQueryPaths> = Awaited<
Expand Down
Loading