File tree Expand file tree Collapse file tree 3 files changed +45
-2
lines changed
Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -13,4 +13,5 @@ export * from './devices/use-devices.js'
1313export * from './SeamProvider.js'
1414export * from './use-seam-client.js'
1515export * from './use-seam-query.js'
16+ export * from './use-seam-mutation.js'
1617export * from './use-seam-query-result.js'
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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
812import { useSeamClient } from 'lib/seam/use-seam-client.js'
913
14+ type QueryOptions = Omit < UseQueryOptions , 'queryKey' | 'queryFn' >
15+
1016export 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 ( {
You can’t perform that action at this time.
0 commit comments