-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuse-seam-query.ts
More file actions
66 lines (59 loc) · 2.13 KB
/
Copy pathuse-seam-query.ts
File metadata and controls
66 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type {
SeamActionAttemptFailedError,
SeamActionAttemptTimeoutError,
SeamHttpApiError,
SeamHttpEndpointQueryPaths,
SeamHttpEndpoints,
SeamHttpInvalidInputError,
} from '@seamapi/http/connect'
import type { ActionAttempt } from '@seamapi/types/connect'
import {
type QueryKey,
useQuery,
type UseQueryOptions,
type UseQueryResult,
} from '@tanstack/react-query'
import { useSeamClient } from 'lib/seam/use-seam-client.js'
export type UseSeamQueryParameters<T extends SeamHttpEndpointQueryPaths> =
Parameters<SeamHttpEndpoints[T]>[0]
export type UseSeamQueryResult<T extends SeamHttpEndpointQueryPaths> =
UseQueryResult<QueryData<T>, QueryError<T>>
export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
endpointPath: T,
parameters: UseSeamQueryParameters<T> = {},
options: Parameters<SeamHttpEndpoints[T]>[1] &
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
): UseSeamQueryResult<T> & { queryKey: QueryKey } {
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
const queryKey = [
...queryKeyPrefixes,
...endpointPath.split('/').filter((v) => v !== ''),
parameters ?? {},
]
const result = useQuery({
enabled: client != null,
...options,
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.
// Type assertion is needed here for performance reasons. The types are correct at runtime.
const endpoint = client[endpointPath] as (...args: any) => Promise<any>
return await endpoint(parameters, options)
},
})
return { ...result, queryKey }
}
type QueryData<T extends SeamHttpEndpointQueryPaths> = Awaited<
ReturnType<SeamHttpEndpoints[T]>
>
type QueryError<T extends SeamHttpEndpointQueryPaths> =
| Error
| SeamHttpApiError
| SeamHttpInvalidInputError
| (QueryData<T> extends ActionAttempt
?
| SeamActionAttemptFailedError<QueryData<T>>
| SeamActionAttemptTimeoutError<QueryData<T>>
: never)
type QueryOptions<X, Y> = Omit<UseQueryOptions<X, Y>, 'queryKey' | 'queryFn'>