@seamapi/react 4.9.1 → 4.10.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.
@@ -1,4 +1,9 @@
1
- import { SeamHttp, SeamHttpEndpoints } from '@seamapi/http/connect'
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
- [SeamHttp, SeamHttpEndpoints]
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 [client, SeamHttpEndpoints.fromClient(client.client)]
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 clientSessionTokenClient = SeamHttp.fromClientSessionToken(
64
+ const seam = SeamHttp.fromClientSessionToken(
48
65
  clientSessionToken,
49
66
  clientOptions
50
67
  )
51
68
 
52
- return [
53
- clientSessionTokenClient,
54
- SeamHttpEndpoints.fromClient(clientSessionTokenClient.client),
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 == null) {
59
- throw new Error(
60
- 'Missing either a client, publishableKey, or clientSessionToken'
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
- const publishableKeyClient = await SeamHttp.fromPublishableKey(
65
- publishableKey,
66
- userIdentifierKey,
67
- clientOptions
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?.[0] ?? null,
78
- endpointClient: data?.[1] ?? null,
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,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'>
@@ -1,3 +1,3 @@
1
- const seamapiReactVersion = '4.9.1'
1
+ const seamapiReactVersion = '4.10.0'
2
2
 
3
3
  export default seamapiReactVersion