Skip to content

Commit 3deb7c0

Browse files
committed
feat: Add useSeamMutation
1 parent a981f99 commit 3deb7c0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/lib/seam/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export * from './devices/use-devices.js'
1313
export * from './SeamProvider.js'
1414
export * from './use-seam-client.js'
1515
export * from './use-seam-query.js'
16+
export * from './use-seam-mutation.js'
1617
export * from './use-seam-query-result.js'

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type {
2+
SeamHttpApiError,
3+
SeamHttpEndpointMutationPaths,
4+
SeamHttpEndpoints,
5+
} from '@seamapi/http/connect'
6+
import {
7+
useMutation,
8+
type UseMutationOptions,
9+
type UseMutationResult,
10+
} from '@tanstack/react-query'
11+
12+
import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'
13+
14+
type MutationOptions = Omit<UseMutationOptions, 'mutationFn'>
15+
16+
export function useSeamMutation<T extends SeamHttpEndpointMutationPaths>(
17+
endpointPath: T,
18+
options?: Parameters<SeamHttpEndpoints[T]>[1],
19+
_mutationOptions: MutationOptions = {}
20+
): UseMutationResult<
21+
Awaited<ReturnType<SeamHttpEndpoints[T]>>,
22+
SeamHttpApiError,
23+
Parameters<SeamHttpEndpoints[T]>[1]
24+
> {
25+
const { endpointClient: client } = useSeamClient()
26+
return useMutation({
27+
mutationFn: async (parameters) => {
28+
if (client === null) throw new NullSeamClientError()
29+
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
30+
// Type assertion is needed here for performance reasons. The types are correct at runtime.
31+
const endpoint = client[endpointPath] as any
32+
return await endpoint(parameters, options)
33+
},
34+
})
35+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import type {
33
SeamHttpEndpointQueryPaths,
44
SeamHttpEndpoints,
55
} from '@seamapi/http/connect'
6-
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
6+
import {
7+
useQuery,
8+
type UseQueryOptions,
9+
type UseQueryResult,
10+
} from '@tanstack/react-query'
711

812
import { useSeamClient } from 'lib/seam/use-seam-client.js'
913

14+
type QueryOptions = Omit<UseQueryOptions, 'queryKey' | 'queryFn'>
15+
1016
export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
1117
endpointPath: T,
1218
parameters?: Parameters<SeamHttpEndpoints[T]>[0],
13-
options?: Parameters<SeamHttpEndpoints[T]>[1]
19+
options?: Parameters<SeamHttpEndpoints[T]>[1],
20+
_queryOptions: QueryOptions = {}
1421
): UseQueryResult<Awaited<ReturnType<SeamHttpEndpoints[T]>>, SeamHttpApiError> {
1522
const { endpointClient: client } = useSeamClient()
1623
return useQuery({

0 commit comments

Comments
 (0)