Skip to content

Commit 28193ca

Browse files
committed
feat: Add clientWithoutWorkspace to seam context
1 parent 9067665 commit 28193ca

File tree

2 files changed

+161
-26
lines changed

2 files changed

+161
-26
lines changed

src/lib/seam/SeamQueryProvider.tsx

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ export interface SeamQueryContext {
2525
publishableKey?: string | undefined
2626
userIdentifierKey?: string | undefined
2727
clientSessionToken?: string | undefined
28+
consoleSessionToken?: string | undefined
29+
workspaceId?: string | undefined
2830
queryKeyPrefix?: string | undefined
2931
}
3032

3133
export type SeamQueryProviderProps =
3234
| SeamQueryProviderPropsWithClient
3335
| SeamQueryProviderPropsWithPublishableKey
3436
| SeamQueryProviderPropsWithClientSessionToken
37+
| SeamQueryProviderPropsWithConsoleSessionToken
3538

3639
export interface SeamQueryProviderPropsWithClient
3740
extends SeamQueryProviderBaseProps {
@@ -52,6 +55,13 @@ export interface SeamQueryProviderPropsWithClientSessionToken
5255
clientSessionToken: string
5356
}
5457

58+
export interface SeamQueryProviderPropsWithConsoleSessionToken
59+
extends SeamQueryProviderBaseProps,
60+
SeamQueryProviderClientOptions {
61+
consoleSessionToken: string
62+
workspaceId?: string | undefined
63+
}
64+
5565
interface SeamQueryProviderBaseProps extends PropsWithChildren {
5666
queryClient?: QueryClient | undefined
5767
onSessionUpdate?: (client: SeamHttp) => void
@@ -74,7 +84,8 @@ export function SeamQueryProvider({
7484
if (
7585
context.client == null &&
7686
context.publishableKey == null &&
77-
context.clientSessionToken == null
87+
context.clientSessionToken == null &&
88+
context.consoleSessionToken == null
7889
) {
7990
return defaultSeamQueryContextValue
8091
}
@@ -84,10 +95,11 @@ export function SeamQueryProvider({
8495
if (
8596
value.client == null &&
8697
value.publishableKey == null &&
87-
value.clientSessionToken == null
98+
value.clientSessionToken == null &&
99+
value.consoleSessionToken == null
88100
) {
89101
throw new Error(
90-
`Must provide either a Seam client, clientSessionToken, or a publishableKey.`
102+
`Must provide either a Seam client, clientSessionToken, publishableKey or consoleSessionToken.`
91103
)
92104
}
93105

@@ -177,6 +189,17 @@ const createSeamQueryContextValue = (
177189
}
178190
}
179191

192+
if (isSeamQueryProviderPropsWithConsoleSessionToken(options)) {
193+
const { consoleSessionToken, workspaceId, ...clientOptions } = options
194+
return {
195+
consoleSessionToken,
196+
workspaceId,
197+
clientOptions,
198+
client: null,
199+
endpointClient: null,
200+
}
201+
}
202+
180203
return { client: null, endpointClient: null }
181204
}
182205

@@ -229,6 +252,18 @@ const isSeamQueryProviderPropsWithPublishableKey = (
229252
)
230253
}
231254

255+
if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
256+
throw new InvalidSeamQueryProviderProps(
257+
'The consoleSessionToken prop cannot be used with the publishableKey prop.'
258+
)
259+
}
260+
261+
if ('workspaceId' in props && props.workspaceId != null) {
262+
throw new InvalidSeamQueryProviderProps(
263+
'The workspaceId prop cannot be used with the publishableKey prop.'
264+
)
265+
}
266+
232267
return true
233268
}
234269

@@ -259,6 +294,54 @@ const isSeamQueryProviderPropsWithClientSessionToken = (
259294
)
260295
}
261296

297+
if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
298+
throw new InvalidSeamQueryProviderProps(
299+
'The consoleSessionToken prop cannot be used with the clientSessionToken prop.'
300+
)
301+
}
302+
303+
if ('workspaceId' in props && props.workspaceId != null) {
304+
throw new InvalidSeamQueryProviderProps(
305+
'The workspaceId prop cannot be used with the clientSessionToken prop.'
306+
)
307+
}
308+
309+
return true
310+
}
311+
312+
const isSeamQueryProviderPropsWithConsoleSessionToken = (
313+
props: SeamQueryProviderProps
314+
): props is SeamQueryProviderPropsWithConsoleSessionToken &
315+
SeamQueryProviderClientOptions => {
316+
if (!('consoleSessionToken' in props)) return false
317+
318+
const { consoleSessionToken } = props
319+
if (consoleSessionToken == null) return false
320+
321+
if ('client' in props && props.client != null) {
322+
throw new InvalidSeamQueryProviderProps(
323+
'The client prop cannot be used with the publishableKey prop.'
324+
)
325+
}
326+
327+
if ('clientSessionToken' in props && props.clientSessionToken != null) {
328+
throw new InvalidSeamQueryProviderProps(
329+
'The clientSessionToken prop cannot be used with the publishableKey prop.'
330+
)
331+
}
332+
333+
if ('publishableKey' in props && props.publishableKey != null) {
334+
throw new InvalidSeamQueryProviderProps(
335+
'The publishableKey prop cannot be used with the consoleSessionToken prop.'
336+
)
337+
}
338+
339+
if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
340+
throw new InvalidSeamQueryProviderProps(
341+
'The userIdentifierKey prop cannot be used with the consoleSessionToken prop.'
342+
)
343+
}
344+
262345
return true
263346
}
264347

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

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { SeamHttp, SeamHttpEndpoints } from '@seamapi/http/connect'
1+
import {
2+
SeamHttp,
3+
SeamHttpEndpoints,
4+
SeamHttpMultiWorkspace,
5+
} from '@seamapi/http/connect'
26
import { useQuery } from '@tanstack/react-query'
37
import { useEffect } from 'react'
48
import { v4 as uuidv4 } from 'uuid'
@@ -8,6 +12,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
812
export function useSeamClient(): {
913
client: SeamHttp | null
1014
endpointClient: SeamHttpEndpoints | null
15+
clientWithoutWorkspace: SeamHttpMultiWorkspace | null
1116
queryKeyPrefixes: string[]
1217
isPending: boolean
1318
isError: boolean
@@ -18,16 +23,20 @@ export function useSeamClient(): {
1823
clientOptions,
1924
publishableKey,
2025
clientSessionToken,
26+
consoleSessionToken,
27+
workspaceId,
2128
queryKeyPrefix,
2229
...context
2330
} = useSeamQueryContext()
2431
const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
2532
clientSessionToken != null ? '' : context.userIdentifierKey
2633
)
2734

28-
const { isPending, isError, error, data } = useQuery<
29-
[SeamHttp, SeamHttpEndpoints]
30-
>({
35+
const { isPending, isError, error, data } = useQuery<{
36+
client: SeamHttp | null
37+
endpointClient: SeamHttpEndpoints | null
38+
clientWithoutWorkspace: SeamHttpMultiWorkspace | null
39+
}>({
3140
queryKey: [
3241
...getQueryKeyPrefixes({ queryKeyPrefix }),
3342
'client',
@@ -41,46 +50,81 @@ export function useSeamClient(): {
4150
],
4251
queryFn: async () => {
4352
if (client != null)
44-
return [client, SeamHttpEndpoints.fromClient(client.client)]
53+
return {
54+
client,
55+
endpointClient: SeamHttpEndpoints.fromClient(client.client),
56+
clientWithoutWorkspace: null,
57+
}
4558

4659
if (clientSessionToken != null) {
47-
const clientSessionTokenClient = SeamHttp.fromClientSessionToken(
60+
const seam = SeamHttp.fromClientSessionToken(
4861
clientSessionToken,
4962
clientOptions
5063
)
5164

52-
return [
53-
clientSessionTokenClient,
54-
SeamHttpEndpoints.fromClient(clientSessionTokenClient.client),
55-
]
65+
return {
66+
client: seam,
67+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
68+
clientWithoutWorkspace: null,
69+
}
5670
}
5771

58-
if (publishableKey == null) {
59-
throw new Error(
60-
'Missing either a client, publishableKey, or clientSessionToken'
72+
if (publishableKey != null) {
73+
const seam = await SeamHttp.fromPublishableKey(
74+
publishableKey,
75+
userIdentifierKey,
76+
clientOptions
6177
)
78+
79+
return {
80+
client: seam,
81+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
82+
clientWithoutWorkspace: null,
83+
}
6284
}
6385

64-
const publishableKeyClient = await SeamHttp.fromPublishableKey(
65-
publishableKey,
66-
userIdentifierKey,
67-
clientOptions
86+
if (consoleSessionToken != null) {
87+
const clientWithoutWorkspace =
88+
SeamHttpMultiWorkspace.fromConsoleSessionToken(consoleSessionToken)
89+
90+
if (workspaceId == null) {
91+
return {
92+
client: null,
93+
endpointClient: null,
94+
clientWithoutWorkspace,
95+
}
96+
}
97+
98+
const seam = SeamHttp.fromConsoleSessionToken(
99+
consoleSessionToken,
100+
workspaceId,
101+
clientOptions
102+
)
103+
104+
return {
105+
client: seam,
106+
endpointClient: SeamHttpEndpoints.fromClient(seam.client),
107+
clientWithoutWorkspace,
108+
}
109+
}
110+
111+
throw new Error(
112+
'Missing either a client, publishableKey, clientSessionToken, or consoleSessionToken.'
68113
)
69-
return [
70-
publishableKeyClient,
71-
SeamHttpEndpoints.fromClient(publishableKeyClient.client),
72-
]
73114
},
74115
})
75116

76117
return {
77-
client: data?.[0] ?? null,
78-
endpointClient: data?.[1] ?? null,
118+
client: data?.client ?? null,
119+
endpointClient: data?.endpointClient ?? null,
120+
clientWithoutWorkspace: data?.clientWithoutWorkspace ?? null,
79121
queryKeyPrefixes: getQueryKeyPrefixes({
80122
queryKeyPrefix,
81123
userIdentifierKey,
82124
publishableKey,
83125
clientSessionToken,
126+
consoleSessionToken,
127+
workspaceId,
84128
}),
85129
isPending,
86130
isError,
@@ -132,11 +176,15 @@ const getQueryKeyPrefixes = ({
132176
userIdentifierKey,
133177
publishableKey,
134178
clientSessionToken,
179+
consoleSessionToken,
180+
workspaceId,
135181
}: {
136182
queryKeyPrefix: string | undefined
137183
userIdentifierKey?: string
138184
publishableKey?: string | undefined
139185
clientSessionToken?: string | undefined
186+
consoleSessionToken?: string | undefined
187+
workspaceId?: string | undefined
140188
}): string[] => {
141189
const seamPrefix = 'seam'
142190

@@ -150,5 +198,9 @@ const getQueryKeyPrefixes = ({
150198
return [seamPrefix, publishableKey, userIdentifierKey]
151199
}
152200

201+
if (consoleSessionToken != null && workspaceId != null) {
202+
return [seamPrefix, consoleSessionToken, workspaceId]
203+
}
204+
153205
return [seamPrefix]
154206
}

0 commit comments

Comments
 (0)