|
| 1 | +import type { |
| 2 | + SeamHttpApiError, |
| 3 | + SeamHttpEndpointPaginatedQueryPaths, |
| 4 | + SeamHttpEndpoints, |
| 5 | + SeamPageCursor, |
| 6 | +} from '@seamapi/http/connect' |
| 7 | +import { |
| 8 | + useInfiniteQuery, |
| 9 | + type UseInfiniteQueryOptions, |
| 10 | + type UseInfiniteQueryResult, |
| 11 | +} from '@tanstack/react-query' |
| 12 | + |
| 13 | +import { useSeamClient } from 'lib/seam/use-seam-client.js' |
| 14 | + |
| 15 | +export type UseSeamInfiniteQueryParameters< |
| 16 | + T extends SeamHttpEndpointPaginatedQueryPaths, |
| 17 | +> = Parameters<SeamHttpEndpoints[T]>[0] |
| 18 | + |
| 19 | +export type UseSeamInfiniteQueryResult< |
| 20 | + T extends SeamHttpEndpointPaginatedQueryPaths, |
| 21 | +> = UseInfiniteQueryResult<QueryData<T>, SeamHttpApiError> |
| 22 | + |
| 23 | +export function useSeamInfiniteQuery< |
| 24 | + T extends SeamHttpEndpointPaginatedQueryPaths, |
| 25 | +>( |
| 26 | + endpointPath: T, |
| 27 | + parameters?: UseSeamInfiniteQueryParameters<T>, |
| 28 | + options: Parameters<SeamHttpEndpoints[T]>[1] & |
| 29 | + QueryOptions<QueryData<T>, SeamHttpApiError> = {} |
| 30 | +): UseSeamInfiniteQueryResult<T> { |
| 31 | + const { endpointClient: client, queryKeyPrefixes } = useSeamClient() |
| 32 | + return useInfiniteQuery({ |
| 33 | + enabled: client != null, |
| 34 | + ...options, |
| 35 | + queryKey: [ |
| 36 | + ...queryKeyPrefixes, |
| 37 | + ...endpointPath.split('/').filter((v) => v !== ''), |
| 38 | + parameters, |
| 39 | + ], |
| 40 | + initialPageParam: null, |
| 41 | + getNextPageParam: (lastPage) => lastPage.nextPageCursor, |
| 42 | + queryFn: async ({ pageParam }) => { |
| 43 | + if (client == null) return { data: [] as Awaited<ReturnType<SeamHttpEndpoints[T]>>, nextPageCursor: null } |
| 44 | + // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory. |
| 45 | + // Type assertion is needed here for performance reasons. The types are correct at runtime. |
| 46 | + const endpoint = client[endpointPath] as (...args: any) => any |
| 47 | + const request = endpoint(parameters, options) |
| 48 | + const pages = client.createPaginator(request) |
| 49 | + if (pageParam == null) { |
| 50 | + const [data, { nextPageCursor }] = await pages.firstPage() |
| 51 | + return { data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>, nextPageCursor } |
| 52 | + } |
| 53 | + // Type assertion is needed for pageParam since the Seam API expects a branded PageCursor type. |
| 54 | + const [data, { nextPageCursor }] = await pages.nextPage(pageParam as SeamPageCursor) |
| 55 | + return { data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>, nextPageCursor } |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> { |
| 61 | + data: Awaited<ReturnType<SeamHttpEndpoints[T]>> |
| 62 | + nextPageCursor: SeamPageCursor | null |
| 63 | +} |
| 64 | + |
| 65 | +type QueryOptions<X, Y> = Omit< |
| 66 | + UseInfiniteQueryOptions<X, Y>, |
| 67 | + 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam' |
| 68 | +> |
0 commit comments