@seamapi/react 4.9.1 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/elements.js +8731 -9043
- package/dist/elements.js.map +1 -1
- package/lib/seam/SeamQueryProvider.d.ts +7 -1
- package/lib/seam/SeamQueryProvider.js +47 -3
- package/lib/seam/SeamQueryProvider.js.map +1 -1
- package/lib/seam/index.d.ts +3 -0
- package/lib/seam/index.js +3 -0
- package/lib/seam/index.js.map +1 -1
- package/lib/seam/use-seam-client.d.ts +3 -1
- package/lib/seam/use-seam-client.js +56 -18
- package/lib/seam/use-seam-client.js.map +1 -1
- package/lib/seam/use-seam-infinite-query.d.ts +11 -0
- package/lib/seam/use-seam-infinite-query.js +42 -0
- package/lib/seam/use-seam-infinite-query.js.map +1 -0
- package/lib/seam/use-seam-mutation-without-workspace.d.ts +8 -0
- package/lib/seam/use-seam-mutation-without-workspace.js +17 -0
- package/lib/seam/use-seam-mutation-without-workspace.js.map +1 -0
- package/lib/seam/use-seam-query-without-workspace.d.ts +8 -0
- package/lib/seam/use-seam-query-without-workspace.js +23 -0
- package/lib/seam/use-seam-query-without-workspace.js.map +1 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/seam/SeamQueryProvider.tsx +86 -3
- package/src/lib/seam/index.ts +3 -0
- package/src/lib/seam/use-seam-client.ts +94 -23
- package/src/lib/seam/use-seam-infinite-query.ts +81 -0
- package/src/lib/seam/use-seam-mutation-without-workspace.ts +53 -0
- package/src/lib/seam/use-seam-query-without-workspace.ts +53 -0
- package/src/lib/version.ts +1 -1
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
SeamHttp,
|
|
3
|
+
SeamHttpEndpoints,
|
|
4
|
+
SeamHttpEndpointsWithoutWorkspace,
|
|
5
|
+
SeamHttpWithoutWorkspace,
|
|
6
|
+
} from '@seamapi/http/connect'
|
|
2
7
|
import { useQuery } from '@tanstack/react-query'
|
|
3
8
|
import { useEffect } from 'react'
|
|
4
9
|
import { v4 as uuidv4 } from 'uuid'
|
|
@@ -8,6 +13,8 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
|
|
|
8
13
|
export function useSeamClient(): {
|
|
9
14
|
client: SeamHttp | null
|
|
10
15
|
endpointClient: SeamHttpEndpoints | null
|
|
16
|
+
clientWithoutWorkspace: SeamHttpWithoutWorkspace | null
|
|
17
|
+
endpointClientWithoutWorkspace: SeamHttpEndpointsWithoutWorkspace | null
|
|
11
18
|
queryKeyPrefixes: string[]
|
|
12
19
|
isPending: boolean
|
|
13
20
|
isError: boolean
|
|
@@ -18,6 +25,8 @@ export function useSeamClient(): {
|
|
|
18
25
|
clientOptions,
|
|
19
26
|
publishableKey,
|
|
20
27
|
clientSessionToken,
|
|
28
|
+
consoleSessionToken,
|
|
29
|
+
workspaceId,
|
|
21
30
|
queryKeyPrefix,
|
|
22
31
|
...context
|
|
23
32
|
} = useSeamQueryContext()
|
|
@@ -25,9 +34,12 @@ export function useSeamClient(): {
|
|
|
25
34
|
clientSessionToken != null ? '' : context.userIdentifierKey
|
|
26
35
|
)
|
|
27
36
|
|
|
28
|
-
const { isPending, isError, error, data } = useQuery<
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
const { isPending, isError, error, data } = useQuery<{
|
|
38
|
+
client: SeamHttp | null
|
|
39
|
+
endpointClient: SeamHttpEndpoints | null
|
|
40
|
+
clientWithoutWorkspace: SeamHttpWithoutWorkspace | null
|
|
41
|
+
endpointClientWithoutWorkspace: SeamHttpEndpointsWithoutWorkspace | null
|
|
42
|
+
}>({
|
|
31
43
|
queryKey: [
|
|
32
44
|
...getQueryKeyPrefixes({ queryKeyPrefix }),
|
|
33
45
|
'client',
|
|
@@ -41,46 +53,93 @@ export function useSeamClient(): {
|
|
|
41
53
|
],
|
|
42
54
|
queryFn: async () => {
|
|
43
55
|
if (client != null)
|
|
44
|
-
return
|
|
56
|
+
return {
|
|
57
|
+
client,
|
|
58
|
+
endpointClient: SeamHttpEndpoints.fromClient(client.client),
|
|
59
|
+
clientWithoutWorkspace: null,
|
|
60
|
+
endpointClientWithoutWorkspace: null,
|
|
61
|
+
}
|
|
45
62
|
|
|
46
63
|
if (clientSessionToken != null) {
|
|
47
|
-
const
|
|
64
|
+
const seam = SeamHttp.fromClientSessionToken(
|
|
48
65
|
clientSessionToken,
|
|
49
66
|
clientOptions
|
|
50
67
|
)
|
|
51
68
|
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
SeamHttpEndpoints.fromClient(
|
|
55
|
-
|
|
69
|
+
return {
|
|
70
|
+
client: seam,
|
|
71
|
+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
|
|
72
|
+
clientWithoutWorkspace: null,
|
|
73
|
+
endpointClientWithoutWorkspace: null,
|
|
74
|
+
}
|
|
56
75
|
}
|
|
57
76
|
|
|
58
|
-
if (publishableKey
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
if (publishableKey != null) {
|
|
78
|
+
const seam = await SeamHttp.fromPublishableKey(
|
|
79
|
+
publishableKey,
|
|
80
|
+
userIdentifierKey,
|
|
81
|
+
clientOptions
|
|
61
82
|
)
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
client: seam,
|
|
86
|
+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
|
|
87
|
+
clientWithoutWorkspace: null,
|
|
88
|
+
endpointClientWithoutWorkspace: null,
|
|
89
|
+
}
|
|
62
90
|
}
|
|
63
91
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
92
|
+
if (consoleSessionToken != null) {
|
|
93
|
+
const clientWithoutWorkspace =
|
|
94
|
+
SeamHttpWithoutWorkspace.fromConsoleSessionToken(consoleSessionToken)
|
|
95
|
+
|
|
96
|
+
const endpointClientWithoutWorkspace =
|
|
97
|
+
SeamHttpEndpointsWithoutWorkspace.fromClient(
|
|
98
|
+
clientWithoutWorkspace.client
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
if (workspaceId == null) {
|
|
102
|
+
return {
|
|
103
|
+
client: null,
|
|
104
|
+
endpointClient: null,
|
|
105
|
+
clientWithoutWorkspace,
|
|
106
|
+
endpointClientWithoutWorkspace,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const seam = SeamHttp.fromConsoleSessionToken(
|
|
111
|
+
consoleSessionToken,
|
|
112
|
+
workspaceId,
|
|
113
|
+
clientOptions
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
client: seam,
|
|
118
|
+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
|
|
119
|
+
clientWithoutWorkspace,
|
|
120
|
+
endpointClientWithoutWorkspace,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
throw new Error(
|
|
125
|
+
'Missing either a client, publishableKey, clientSessionToken, or consoleSessionToken.'
|
|
68
126
|
)
|
|
69
|
-
return [
|
|
70
|
-
publishableKeyClient,
|
|
71
|
-
SeamHttpEndpoints.fromClient(publishableKeyClient.client),
|
|
72
|
-
]
|
|
73
127
|
},
|
|
74
128
|
})
|
|
75
129
|
|
|
76
130
|
return {
|
|
77
|
-
client: data?.
|
|
78
|
-
endpointClient: data?.
|
|
131
|
+
client: data?.client ?? null,
|
|
132
|
+
endpointClient: data?.endpointClient ?? null,
|
|
133
|
+
clientWithoutWorkspace: data?.clientWithoutWorkspace ?? null,
|
|
134
|
+
endpointClientWithoutWorkspace:
|
|
135
|
+
data?.endpointClientWithoutWorkspace ?? null,
|
|
79
136
|
queryKeyPrefixes: getQueryKeyPrefixes({
|
|
80
137
|
queryKeyPrefix,
|
|
81
138
|
userIdentifierKey,
|
|
82
139
|
publishableKey,
|
|
83
140
|
clientSessionToken,
|
|
141
|
+
consoleSessionToken,
|
|
142
|
+
workspaceId,
|
|
84
143
|
}),
|
|
85
144
|
isPending,
|
|
86
145
|
isError,
|
|
@@ -132,11 +191,15 @@ const getQueryKeyPrefixes = ({
|
|
|
132
191
|
userIdentifierKey,
|
|
133
192
|
publishableKey,
|
|
134
193
|
clientSessionToken,
|
|
194
|
+
consoleSessionToken,
|
|
195
|
+
workspaceId,
|
|
135
196
|
}: {
|
|
136
197
|
queryKeyPrefix: string | undefined
|
|
137
198
|
userIdentifierKey?: string
|
|
138
199
|
publishableKey?: string | undefined
|
|
139
200
|
clientSessionToken?: string | undefined
|
|
201
|
+
consoleSessionToken?: string | undefined
|
|
202
|
+
workspaceId?: string | undefined
|
|
140
203
|
}): string[] => {
|
|
141
204
|
const seamPrefix = 'seam'
|
|
142
205
|
|
|
@@ -150,5 +213,13 @@ const getQueryKeyPrefixes = ({
|
|
|
150
213
|
return [seamPrefix, publishableKey, userIdentifierKey]
|
|
151
214
|
}
|
|
152
215
|
|
|
216
|
+
if (consoleSessionToken != null) {
|
|
217
|
+
if (workspaceId != null) {
|
|
218
|
+
return [seamPrefix, consoleSessionToken, workspaceId]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return [seamPrefix, consoleSessionToken, 'without_workspace']
|
|
222
|
+
}
|
|
223
|
+
|
|
153
224
|
return [seamPrefix]
|
|
154
225
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SeamHttpApiError,
|
|
3
|
+
SeamHttpEndpointPaginatedQueryPaths,
|
|
4
|
+
SeamHttpEndpoints,
|
|
5
|
+
SeamHttpRequest,
|
|
6
|
+
SeamPageCursor,
|
|
7
|
+
} from '@seamapi/http/connect'
|
|
8
|
+
import {
|
|
9
|
+
useInfiniteQuery,
|
|
10
|
+
type UseInfiniteQueryOptions,
|
|
11
|
+
type UseInfiniteQueryResult,
|
|
12
|
+
} from '@tanstack/react-query'
|
|
13
|
+
|
|
14
|
+
import { useSeamClient } from 'lib/seam/use-seam-client.js'
|
|
15
|
+
|
|
16
|
+
export type UseSeamInfiniteQueryParameters<
|
|
17
|
+
T extends SeamHttpEndpointPaginatedQueryPaths,
|
|
18
|
+
> = Parameters<SeamHttpEndpoints[T]>[0]
|
|
19
|
+
|
|
20
|
+
export type UseSeamInfiniteQueryResult<
|
|
21
|
+
T extends SeamHttpEndpointPaginatedQueryPaths,
|
|
22
|
+
> = UseInfiniteQueryResult<QueryData<T>, SeamHttpApiError>
|
|
23
|
+
|
|
24
|
+
export function useSeamInfiniteQuery<
|
|
25
|
+
T extends SeamHttpEndpointPaginatedQueryPaths,
|
|
26
|
+
>(
|
|
27
|
+
endpointPath: T,
|
|
28
|
+
parameters?: UseSeamInfiniteQueryParameters<T>,
|
|
29
|
+
options: Parameters<SeamHttpEndpoints[T]>[1] &
|
|
30
|
+
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
|
|
31
|
+
): UseSeamInfiniteQueryResult<T> {
|
|
32
|
+
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
|
|
33
|
+
return useInfiniteQuery({
|
|
34
|
+
enabled: client != null,
|
|
35
|
+
...options,
|
|
36
|
+
queryKey: [
|
|
37
|
+
...queryKeyPrefixes,
|
|
38
|
+
...endpointPath.split('/').filter((v) => v !== ''),
|
|
39
|
+
parameters,
|
|
40
|
+
],
|
|
41
|
+
initialPageParam: null,
|
|
42
|
+
getNextPageParam: (lastPage) => lastPage.nextPageCursor,
|
|
43
|
+
queryFn: async ({ pageParam }) => {
|
|
44
|
+
if (client == null)
|
|
45
|
+
return {
|
|
46
|
+
data: [] as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
|
|
47
|
+
nextPageCursor: null,
|
|
48
|
+
}
|
|
49
|
+
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
|
|
50
|
+
// Type assertion is needed here for performance reasons. The types are correct at runtime.
|
|
51
|
+
const endpoint = client[endpointPath] as (...args: any) => any
|
|
52
|
+
const request = endpoint(parameters, options)
|
|
53
|
+
const pages = client.createPaginator(request as SeamHttpRequest<any, any>)
|
|
54
|
+
if (pageParam == null) {
|
|
55
|
+
const [data, { nextPageCursor }] = await pages.firstPage()
|
|
56
|
+
return {
|
|
57
|
+
data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
|
|
58
|
+
nextPageCursor,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Type assertion is needed for pageParam since the Seam API expects a branded PageCursor type.
|
|
62
|
+
const [data, { nextPageCursor }] = await pages.nextPage(
|
|
63
|
+
pageParam as SeamPageCursor
|
|
64
|
+
)
|
|
65
|
+
return {
|
|
66
|
+
data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
|
|
67
|
+
nextPageCursor,
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> {
|
|
74
|
+
data: Awaited<ReturnType<SeamHttpEndpoints[T]>>
|
|
75
|
+
nextPageCursor: SeamPageCursor | null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type QueryOptions<X, Y> = Omit<
|
|
79
|
+
UseInfiniteQueryOptions<X, Y>,
|
|
80
|
+
'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam'
|
|
81
|
+
>
|
|
@@ -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'>
|
|
@@ -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'>
|
package/src/lib/version.ts
CHANGED