Skip to content

Commit 09fa409

Browse files
committed
feat: Add useSeamQuery
1 parent 302082d commit 09fa409

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/lib/seam/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export * from './devices/use-device-providers.js'
1212
export * from './devices/use-devices.js'
1313
export * from './SeamProvider.js'
1414
export * from './use-seam-client.js'
15+
export * from './use-seam-query.js'
1516
export * from './use-seam-query-result.js'

src/lib/seam/use-seam-query.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { SeamHttpApiError, SeamHttpEndpoints } from '@seamapi/http/connect'
2+
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
3+
4+
import { useSeamClient } from 'lib/seam/use-seam-client.js'
5+
6+
type EndpointsOnly = Pick<
7+
SeamHttpEndpoints,
8+
'/devices/list' | '/devices/get' | '/action_attempts/get'
9+
>
10+
11+
export function useSeamQuery<T extends keyof EndpointsOnly>(
12+
endpointPath: T,
13+
params?: Parameters<EndpointsOnly[T]>[0],
14+
options?: Parameters<EndpointsOnly[T]>[1]
15+
): UseQueryResult<Awaited<ReturnType<EndpointsOnly[T]>>, SeamHttpApiError> {
16+
const { endpointClient: client } = useSeamClient()
17+
return useQuery({
18+
enabled: client != null,
19+
queryKey: [endpointPath, params],
20+
queryFn: async () => {
21+
if (client == null) return null
22+
const endpoint = client[endpointPath] as EndpointsOnly[T]
23+
// @ts-expect-error: The types are correct at runtime, but TypeScript can't infer the specific endpoint types
24+
return await endpoint(params, options)
25+
},
26+
})
27+
}

0 commit comments

Comments
 (0)