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