Skip to content

Commit 5efe098

Browse files
committed
feat: Add useSeamQueryWithoutWorkspace and UseSeamMutationWithoutWorkspace
1 parent 685c195 commit 5efe098

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/lib/seam/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export * from './devices/use-devices.js'
1313
export * from './SeamProvider.js'
1414
export * from './use-seam-client.js'
1515
export * from './use-seam-mutation.js'
16+
export * from './use-seam-mutation-without-workspace.js'
1617
export * from './use-seam-query.js'
1718
export * from './use-seam-query-result.js'
19+
export * from './use-seam-query-without-workspace.js'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type {
2+
SeamHttpApiError,
3+
SeamHttpEndpointsWithoutWorkspace,
4+
SeamHttpEndpointWithoutWorkspaceMutationPaths,
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+
export type UseSeamMutationWithoutWorkspaceVariables<
15+
T extends SeamHttpEndpointWithoutWorkspaceMutationPaths,
16+
> = Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[0]
17+
18+
export type UseSeamMutationWithoutWorkspaceResult<
19+
T extends SeamHttpEndpointWithoutWorkspaceMutationPaths,
20+
> = UseMutationResult<
21+
MutationData<T>,
22+
SeamHttpApiError,
23+
UseSeamMutationWithoutWorkspaceVariables<T>
24+
>
25+
26+
export function UseSeamMutationWithoutWorkspace<
27+
T extends SeamHttpEndpointWithoutWorkspaceMutationPaths,
28+
>(
29+
endpointPath: T,
30+
options: Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[1] &
31+
MutationOptions<
32+
MutationData<T>,
33+
SeamHttpApiError,
34+
UseSeamMutationWithoutWorkspaceVariables<T>
35+
> = {}
36+
): UseSeamMutationWithoutWorkspaceResult<T> {
37+
const { endpointClient: client } = useSeamClient()
38+
return useMutation({
39+
...options,
40+
mutationFn: async (variables) => {
41+
if (client === null) throw new NullSeamClientError()
42+
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
43+
// Type assertion is needed here for performance reasons. The types are correct at runtime.
44+
const endpoint = client[endpointPath] as (...args: any) => Promise<any>
45+
return await endpoint(variables, options)
46+
},
47+
})
48+
}
49+
50+
type MutationData<T extends SeamHttpEndpointWithoutWorkspaceMutationPaths> =
51+
Awaited<ReturnType<SeamHttpEndpointsWithoutWorkspace[T]>>
52+
53+
type MutationOptions<X, Y, Z> = Omit<UseMutationOptions<X, Y, Z>, 'mutationFn'>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type {
2+
SeamHttpApiError,
3+
SeamHttpEndpointsWithoutWorkspace,
4+
SeamHttpEndpointWithoutWorkspaceQueryPaths,
5+
} from '@seamapi/http/connect'
6+
import {
7+
useQuery,
8+
type UseQueryOptions,
9+
type UseQueryResult,
10+
} from '@tanstack/react-query'
11+
12+
import { useSeamClient } from 'lib/seam/use-seam-client.js'
13+
14+
export type UseSeamQueryWithoutWorkspaceParameters<
15+
T extends SeamHttpEndpointWithoutWorkspaceQueryPaths,
16+
> = Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[0]
17+
18+
export type UseSeamQueryWithoutWorkspaceResult<
19+
T extends SeamHttpEndpointWithoutWorkspaceQueryPaths,
20+
> = UseQueryResult<QueryData<T>, SeamHttpApiError>
21+
22+
export function useSeamQueryWithoutWorkspace<
23+
T extends SeamHttpEndpointWithoutWorkspaceQueryPaths,
24+
>(
25+
endpointPath: T,
26+
parameters?: UseSeamQueryWithoutWorkspaceParameters<T>,
27+
options: Parameters<SeamHttpEndpointsWithoutWorkspace[T]>[1] &
28+
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
29+
): UseSeamQueryWithoutWorkspaceResult<T> {
30+
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
31+
return useQuery({
32+
enabled: client != null,
33+
...options,
34+
queryKey: [
35+
...queryKeyPrefixes,
36+
...endpointPath.split('/').filter((v) => v !== ''),
37+
parameters,
38+
],
39+
queryFn: async () => {
40+
if (client == null) return null
41+
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
42+
// Type assertion is needed here for performance reasons. The types are correct at runtime.
43+
const endpoint = client[endpointPath] as (...args: any) => Promise<any>
44+
return await endpoint(parameters, options)
45+
},
46+
})
47+
}
48+
49+
type QueryData<T extends SeamHttpEndpointWithoutWorkspaceQueryPaths> = Awaited<
50+
ReturnType<SeamHttpEndpointsWithoutWorkspace[T]>
51+
>
52+
53+
type QueryOptions<X, Y> = Omit<UseQueryOptions<X, Y>, 'queryKey' | 'queryFn'>

0 commit comments

Comments
 (0)