Skip to content

Commit 8c125e6

Browse files
committed
fix: Ensure query key is prefix by session scope
1 parent ca24ae8 commit 8c125e6

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/lib/seam/SeamQueryProvider.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface SeamQueryContext {
2121
publishableKey?: string | undefined
2222
userIdentifierKey?: string | undefined
2323
clientSessionToken?: string | undefined
24+
queryKeyPrefix?: string | undefined
2425
}
2526

2627
export type SeamQueryProviderProps =
@@ -31,6 +32,7 @@ export type SeamQueryProviderProps =
3132
export interface SeamQueryProviderPropsWithClient
3233
extends SeamQueryProviderBaseProps {
3334
client: SeamHttp
35+
queryKeyPrefix: string
3436
}
3537

3638
export interface SeamQueryProviderPropsWithPublishableKey
@@ -126,6 +128,11 @@ const createSeamQueryContextValue = (
126128
options: SeamQueryProviderProps
127129
): SeamQueryContext => {
128130
if (isSeamQueryProviderPropsWithClient(options)) {
131+
if (options.queryKeyPrefix == null) {
132+
throw new InvalidSeamQueryProviderProps(
133+
'The client prop must be used with a queryKeyPrefix prop.'
134+
)
135+
}
129136
return {
130137
...options,
131138
endpointClient: null,

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
88
export function useSeamClient(): {
99
client: SeamHttp | null
1010
endpointClient: SeamHttpEndpoints | null
11+
queryKeyPrefix: string[]
1112
isPending: boolean
1213
isError: boolean
1314
error: unknown
@@ -17,6 +18,7 @@ export function useSeamClient(): {
1718
clientOptions,
1819
publishableKey,
1920
clientSessionToken,
21+
queryKeyPrefix,
2022
...context
2123
} = useSeamQueryContext()
2224
const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
@@ -27,6 +29,7 @@ export function useSeamClient(): {
2729
[SeamHttp, SeamHttpEndpoints]
2830
>({
2931
queryKey: [
32+
'seam',
3033
'client',
3134
{
3235
client,
@@ -73,6 +76,15 @@ export function useSeamClient(): {
7376
return {
7477
client: data?.[0] ?? null,
7578
endpointClient: data?.[1] ?? null,
79+
queryKeyPrefix: [
80+
'seam',
81+
queryKeyPrefix ??
82+
getQueryKeyPrefix({
83+
userIdentifierKey,
84+
publishableKey,
85+
clientSessionToken,
86+
}),
87+
],
7688
isPending,
7789
isError,
7890
error,
@@ -117,3 +129,23 @@ This is not recommended because the client session is now bound to this machine
117129

118130
return fingerprint
119131
}
132+
133+
const getQueryKeyPrefix = ({
134+
userIdentifierKey,
135+
publishableKey,
136+
clientSessionToken,
137+
}: {
138+
userIdentifierKey: string
139+
publishableKey: string | undefined
140+
clientSessionToken: string | undefined
141+
}): string => {
142+
if (clientSessionToken != null) {
143+
return clientSessionToken
144+
}
145+
146+
if (publishableKey != null) {
147+
return [publishableKey, userIdentifierKey].join(':')
148+
}
149+
150+
throw new Error('Could not determine a queryKeyPrefix')
151+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
2323
options: Parameters<SeamHttpEndpoints[T]>[1] &
2424
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
2525
): UseSeamQueryResult<T> {
26-
const { endpointClient: client } = useSeamClient()
26+
const { endpointClient: client, queryKeyPrefix } = useSeamClient()
2727
return useQuery({
2828
enabled: client != null,
2929
...options,
30-
queryKey: [endpointPath, parameters],
30+
queryKey: [
31+
...queryKeyPrefix,
32+
...endpointPath.split('/').filter((v) => v !== ''),
33+
parameters,
34+
],
3135
queryFn: async () => {
3236
if (client == null) return null
3337
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.

0 commit comments

Comments
 (0)