|
1 | 1 | import type { |
2 | 2 | SeamHttpApiError, |
3 | | - SeamHttpEndpointPaths, |
| 3 | + SeamHttpEndpointQueryPaths, |
4 | 4 | SeamHttpEndpoints, |
5 | 5 | } from '@seamapi/http/connect' |
6 | 6 | import { useQuery, type UseQueryResult } from '@tanstack/react-query' |
7 | 7 |
|
8 | 8 | import { useSeamClient } from 'lib/seam/use-seam-client.js' |
9 | 9 |
|
10 | | -type Endpoints = Pick<SeamHttpEndpoints, SeamHttpEndpointPaths> |
11 | | - |
12 | | -export function useSeamQuery<T extends keyof Endpoints>( |
| 10 | +export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>( |
13 | 11 | endpointPath: T, |
14 | | - params?: Parameters<Endpoints[T]>[0], |
15 | | - options?: Parameters<Endpoints[T]>[1] |
16 | | -): UseQueryResult<Awaited<ReturnType<Endpoints[T]>>, SeamHttpApiError> { |
| 12 | + params?: Parameters<SeamHttpEndpoints[T]>[0], |
| 13 | + options?: Parameters<SeamHttpEndpoints[T]>[1] |
| 14 | +): UseQueryResult<Awaited<ReturnType<SeamHttpEndpoints[T]>>, SeamHttpApiError> { |
17 | 15 | const { endpointClient: client } = useSeamClient() |
18 | 16 | return useQuery({ |
19 | 17 | enabled: client != null, |
20 | 18 | queryKey: [endpointPath, params], |
21 | 19 | queryFn: async () => { |
22 | 20 | if (client == null) return null |
23 | | - const endpoint = client[endpointPath] as Endpoints[T] |
24 | | - // @ts-expect-error: The types are correct at runtime, but TypeScript can't infer the specific endpoint types |
| 21 | + // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory. |
| 22 | + // Type assertion is needed here for performance reasons. The types are correct at runtime. |
| 23 | + const endpoint = client[endpointPath] as any |
25 | 24 | return await endpoint(params, options) |
26 | 25 | }, |
27 | 26 | }) |
|
0 commit comments