Skip to content

Commit 0a02e72

Browse files
committed
Return queryKeyPrefixes in context
1 parent 8c125e6 commit 0a02e72

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
88
export function useSeamClient(): {
99
client: SeamHttp | null
1010
endpointClient: SeamHttpEndpoints | null
11-
queryKeyPrefix: string[]
11+
queryKeyPrefixes: string[]
1212
isPending: boolean
1313
isError: boolean
1414
error: unknown
@@ -29,7 +29,7 @@ export function useSeamClient(): {
2929
[SeamHttp, SeamHttpEndpoints]
3030
>({
3131
queryKey: [
32-
'seam',
32+
...getQueryKeyPrefixes({ queryKeyPrefix }),
3333
'client',
3434
{
3535
client,
@@ -76,15 +76,12 @@ export function useSeamClient(): {
7676
return {
7777
client: data?.[0] ?? null,
7878
endpointClient: data?.[1] ?? null,
79-
queryKeyPrefix: [
80-
'seam',
81-
queryKeyPrefix ??
82-
getQueryKeyPrefix({
83-
userIdentifierKey,
84-
publishableKey,
85-
clientSessionToken,
86-
}),
87-
],
79+
queryKeyPrefixes: getQueryKeyPrefixes({
80+
queryKeyPrefix,
81+
userIdentifierKey,
82+
publishableKey,
83+
clientSessionToken,
84+
}),
8885
isPending,
8986
isError,
9087
error,
@@ -130,22 +127,28 @@ This is not recommended because the client session is now bound to this machine
130127
return fingerprint
131128
}
132129

133-
const getQueryKeyPrefix = ({
130+
const getQueryKeyPrefixes = ({
131+
queryKeyPrefix,
134132
userIdentifierKey,
135133
publishableKey,
136134
clientSessionToken,
137135
}: {
138-
userIdentifierKey: string
139-
publishableKey: string | undefined
140-
clientSessionToken: string | undefined
141-
}): string => {
136+
queryKeyPrefix: string | undefined
137+
userIdentifierKey?: string
138+
publishableKey?: string | undefined
139+
clientSessionToken?: string | undefined
140+
}): string[] => {
141+
const seamPrefix = 'seam'
142+
143+
if (queryKeyPrefix != null) return [seamPrefix, queryKeyPrefix]
144+
142145
if (clientSessionToken != null) {
143-
return clientSessionToken
146+
return [seamPrefix, clientSessionToken]
144147
}
145148

146-
if (publishableKey != null) {
147-
return [publishableKey, userIdentifierKey].join(':')
149+
if (publishableKey != null && userIdentifierKey != null) {
150+
return [seamPrefix, publishableKey, userIdentifierKey]
148151
}
149152

150-
throw new Error('Could not determine a queryKeyPrefix')
153+
return [seamPrefix]
151154
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
2323
options: Parameters<SeamHttpEndpoints[T]>[1] &
2424
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
2525
): UseSeamQueryResult<T> {
26-
const { endpointClient: client, queryKeyPrefix } = useSeamClient()
26+
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
2727
return useQuery({
2828
enabled: client != null,
2929
...options,
3030
queryKey: [
31-
...queryKeyPrefix,
31+
...queryKeyPrefixes,
3232
...endpointPath.split('/').filter((v) => v !== ''),
3333
parameters,
3434
],

0 commit comments

Comments
 (0)