Skip to content

Commit 302082d

Browse files
committed
Add endpointClient to SeamQueryContext
1 parent ef74e12 commit 302082d

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/lib/seam/SeamQueryProvider.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
SeamHttp,
3+
SeamHttpEndpoints,
34
SeamHttpOptionsWithClientSessionToken,
45
} from '@seamapi/http/connect'
56
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
@@ -15,6 +16,7 @@ import { useSeamClient } from './use-seam-client.js'
1516

1617
export interface SeamQueryContext {
1718
client: SeamHttp | null
19+
endpointClient: SeamHttpEndpoints | null
1820
clientOptions?: SeamQueryProviderClientOptions | undefined
1921
publishableKey?: string | undefined
2022
userIdentifierKey?: string | undefined
@@ -112,21 +114,24 @@ function Session({
112114
const createDefaultSeamQueryContextValue = (): SeamQueryContext => {
113115
try {
114116
if (globalThis.seam == null) {
115-
return { client: null }
117+
return { client: null, endpointClient: null }
116118
}
117119
return createSeamQueryContextValue(globalThis.seam)
118120
} catch (err) {
119121
// eslint-disable-next-line no-console
120122
console.warn(err)
121-
return { client: null }
123+
return { client: null, endpointClient: null }
122124
}
123125
}
124126

125127
const createSeamQueryContextValue = (
126128
options: SeamQueryProviderProps
127129
): SeamQueryContext => {
128130
if (isSeamQueryProviderPropsWithClient(options)) {
129-
return options
131+
return {
132+
...options,
133+
endpointClient: null,
134+
}
130135
}
131136

132137
if (isSeamQueryProviderPropsWithClientSessionToken(options)) {
@@ -135,6 +140,7 @@ const createSeamQueryContextValue = (
135140
clientSessionToken,
136141
clientOptions,
137142
client: null,
143+
endpointClient: null,
138144
}
139145
}
140146

@@ -145,10 +151,11 @@ const createSeamQueryContextValue = (
145151
userIdentifierKey,
146152
clientOptions,
147153
client: null,
154+
endpointClient: null,
148155
}
149156
}
150157

151-
return { client: null }
158+
return { client: null, endpointClient: null }
152159
}
153160

154161
const defaultSeamQueryContextValue = createDefaultSeamQueryContextValue()

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SeamHttp } from '@seamapi/http/connect'
1+
import { SeamHttp, SeamHttpEndpoints } from '@seamapi/http/connect'
22
import { useQuery } from '@tanstack/react-query'
33
import { useEffect } from 'react'
44
import { v4 as uuidv4 } from 'uuid'
@@ -7,6 +7,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
77

88
export function useSeamClient(): {
99
client: SeamHttp | null
10+
endpointClient: SeamHttpEndpoints | null
1011
isPending: boolean
1112
isError: boolean
1213
error: unknown
@@ -22,7 +23,9 @@ export function useSeamClient(): {
2223
clientSessionToken != null ? '' : context.userIdentifierKey
2324
)
2425

25-
const { isPending, isError, error, data } = useQuery<SeamHttp>({
26+
const { isPending, isError, error, data } = useQuery<
27+
[SeamHttp, SeamHttpEndpoints]
28+
>({
2629
queryKey: [
2730
'client',
2831
{
@@ -34,13 +37,19 @@ export function useSeamClient(): {
3437
},
3538
],
3639
queryFn: async () => {
37-
if (client != null) return client
40+
if (client != null)
41+
return [client, SeamHttpEndpoints.fromClient(client.client)]
3842

3943
if (clientSessionToken != null) {
40-
return SeamHttp.fromClientSessionToken(
44+
const clientSessionTokenClient = SeamHttp.fromClientSessionToken(
4145
clientSessionToken,
4246
clientOptions
4347
)
48+
49+
return [
50+
clientSessionTokenClient,
51+
SeamHttpEndpoints.fromClient(clientSessionTokenClient.client),
52+
]
4453
}
4554

4655
if (publishableKey == null) {
@@ -49,15 +58,25 @@ export function useSeamClient(): {
4958
)
5059
}
5160

52-
return await SeamHttp.fromPublishableKey(
61+
const publishableKeyClient = await SeamHttp.fromPublishableKey(
5362
publishableKey,
5463
userIdentifierKey,
5564
clientOptions
5665
)
66+
return [
67+
publishableKeyClient,
68+
SeamHttpEndpoints.fromClient(publishableKeyClient.client),
69+
]
5770
},
5871
})
5972

60-
return { client: data ?? null, isPending, isError, error }
73+
return {
74+
client: data?.[0] ?? null,
75+
endpointClient: data?.[1] ?? null,
76+
isPending,
77+
isError,
78+
error,
79+
}
6180
}
6281

6382
export class NullSeamClientError extends Error {

0 commit comments

Comments
 (0)