@seamapi/react 0.15.1 → 0.16.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.
- package/lib/SeamProvider.d.ts +14 -6
- package/lib/SeamProvider.js +62 -16
- package/lib/SeamProvider.js.map +1 -1
- package/lib/seam/use-seam-client.js +19 -4
- package/lib/seam/use-seam-client.js.map +1 -1
- package/package.json +2 -1
- package/src/lib/SeamProvider.tsx +102 -30
- package/src/lib/seam/use-seam-client.ts +28 -4
package/lib/SeamProvider.d.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import { type PropsWithChildren } from 'react';
|
|
2
2
|
import type { Seam, SeamClientOptions } from 'seamapi';
|
|
3
3
|
declare global {
|
|
4
|
-
var seam:
|
|
4
|
+
var seam: SeamProviderProps | undefined;
|
|
5
5
|
}
|
|
6
6
|
export interface SeamContext {
|
|
7
7
|
client: Seam | null;
|
|
8
|
-
clientOptions?:
|
|
8
|
+
clientOptions?: AllowedSeamClientOptions | undefined;
|
|
9
9
|
publishableKey?: string | undefined;
|
|
10
10
|
userIdentifierKey?: string | undefined;
|
|
11
|
+
clientSessionToken?: string | undefined;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
type SeamProviderProps = SeamProviderPropsWithClient | (SeamProviderPropsWithPublishableKey & AllowedSeamClientOptions) | (SeamProviderPropsWithClientSessionToken & AllowedSeamClientOptions);
|
|
14
|
+
interface SeamProviderPropsWithClient {
|
|
15
|
+
client: Seam;
|
|
16
|
+
}
|
|
17
|
+
interface SeamProviderPropsWithPublishableKey {
|
|
18
|
+
publishableKey: string;
|
|
15
19
|
userIdentifierKey?: string;
|
|
16
|
-
endpoint?: string;
|
|
17
20
|
}
|
|
21
|
+
interface SeamProviderPropsWithClientSessionToken {
|
|
22
|
+
clientSessionToken: string;
|
|
23
|
+
}
|
|
24
|
+
type AllowedSeamClientOptions = Pick<SeamClientOptions, 'endpoint'>;
|
|
18
25
|
export declare function SeamProvider({ children, ...props }: PropsWithChildren<SeamProviderProps>): JSX.Element;
|
|
19
26
|
export declare const seamContext: import("react").Context<SeamContext>;
|
|
20
27
|
export declare function useSeamContext(): SeamContext;
|
|
28
|
+
export {};
|
package/lib/SeamProvider.js
CHANGED
|
@@ -17,8 +17,7 @@ export function SeamProvider({ children, ...props }) {
|
|
|
17
17
|
return (_jsx("div", { className: 'seam-components', children: _jsx(QueryClientProvider, { client: queryClientRef.current, children: _jsx(Provider, { value: { ...contextRef.current }, children: children }) }) }));
|
|
18
18
|
}
|
|
19
19
|
const createDefaultSeamContextValue = () => {
|
|
20
|
-
if (globalThis.seam
|
|
21
|
-
globalThis.seam?.publishableKey == null) {
|
|
20
|
+
if (globalThis.seam == null) {
|
|
22
21
|
return { client: null };
|
|
23
22
|
}
|
|
24
23
|
try {
|
|
@@ -30,23 +29,28 @@ const createDefaultSeamContextValue = () => {
|
|
|
30
29
|
return { client: null };
|
|
31
30
|
}
|
|
32
31
|
};
|
|
33
|
-
const createSeamContextValue = (
|
|
34
|
-
if (
|
|
35
|
-
return
|
|
32
|
+
const createSeamContextValue = (options) => {
|
|
33
|
+
if (isSeamProviderPropsWithClient(options)) {
|
|
34
|
+
return options;
|
|
36
35
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
36
|
+
if (isSeamProviderPropsWithClientSessionToken(options)) {
|
|
37
|
+
const { clientSessionToken, ...clientOptions } = options;
|
|
38
|
+
return {
|
|
39
|
+
clientSessionToken,
|
|
40
|
+
clientOptions,
|
|
41
|
+
client: null,
|
|
42
|
+
};
|
|
39
43
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return {
|
|
44
|
+
if (isSeamProviderPropsWithPublishableKey(options)) {
|
|
45
|
+
const { publishableKey, userIdentifierKey, ...clientOptions } = options;
|
|
46
|
+
return {
|
|
47
|
+
publishableKey,
|
|
48
|
+
userIdentifierKey,
|
|
49
|
+
clientOptions,
|
|
50
|
+
client: null,
|
|
51
|
+
};
|
|
43
52
|
}
|
|
44
|
-
|
|
45
|
-
client: null,
|
|
46
|
-
publishableKey,
|
|
47
|
-
userIdentifierKey,
|
|
48
|
-
clientOptions,
|
|
49
|
-
};
|
|
53
|
+
throw new Error('Invalid SeamProvider options');
|
|
50
54
|
};
|
|
51
55
|
const defaultSeamContextValue = createDefaultSeamContextValue();
|
|
52
56
|
export const seamContext = createContext(defaultSeamContextValue);
|
|
@@ -57,4 +61,46 @@ export function useSeamContext() {
|
|
|
57
61
|
}
|
|
58
62
|
return context;
|
|
59
63
|
}
|
|
64
|
+
const isSeamProviderPropsWithClient = (props) => {
|
|
65
|
+
if (!('client' in props))
|
|
66
|
+
return false;
|
|
67
|
+
const { client } = props;
|
|
68
|
+
if (client == null)
|
|
69
|
+
return false;
|
|
70
|
+
if (Object.values(props).some((v) => v == null)) {
|
|
71
|
+
throw new Error('Cannot provide a Seam client along with other options.');
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
75
|
+
const isSeamProviderPropsWithPublishableKey = (props) => {
|
|
76
|
+
if (!('publishableKey' in props))
|
|
77
|
+
return false;
|
|
78
|
+
const { publishableKey } = props;
|
|
79
|
+
if (publishableKey == null)
|
|
80
|
+
return false;
|
|
81
|
+
if ('client' in props && props.client != null) {
|
|
82
|
+
throw new Error('Cannot provide a Seam client along with other options.');
|
|
83
|
+
}
|
|
84
|
+
if ('clientSessionToken' in props && props.clientSessionToken != null) {
|
|
85
|
+
throw new Error('Cannot provide both a publishableKey and a clientSessionToken.');
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
};
|
|
89
|
+
const isSeamProviderPropsWithClientSessionToken = (props) => {
|
|
90
|
+
if (!('clientSessionToken' in props))
|
|
91
|
+
return false;
|
|
92
|
+
const { clientSessionToken } = props;
|
|
93
|
+
if (clientSessionToken == null)
|
|
94
|
+
return false;
|
|
95
|
+
if ('client' in props && props.client != null) {
|
|
96
|
+
throw new Error('Cannot provide a Seam client along with other options.');
|
|
97
|
+
}
|
|
98
|
+
if ('publishableKey' in props && props.publishableKey != null) {
|
|
99
|
+
throw new Error('Cannot provide both a clientSessionToken and a publishableKey .');
|
|
100
|
+
}
|
|
101
|
+
if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
|
|
102
|
+
throw new Error('Cannot use a userIdentifierKey with a clientSessionToken.');
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
};
|
|
60
106
|
//# sourceMappingURL=SeamProvider.js.map
|
package/lib/SeamProvider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SeamProvider.js","sourceRoot":"","sources":["../src/lib/SeamProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AACxE,OAAO,EACL,aAAa,EAEb,UAAU,EACV,MAAM,GACP,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"SeamProvider.js","sourceRoot":"","sources":["../src/lib/SeamProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AACxE,OAAO,EACL,aAAa,EAEb,UAAU,EACV,MAAM,GACP,MAAM,OAAO,CAAA;AAoCd,MAAM,UAAU,YAAY,CAAC,EAC3B,QAAQ,EACR,GAAG,KAAK,EAC6B;IACrC,MAAM,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAA;IAEhC,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC,CAAA;IAEhD,MAAM,UAAU,GAAG,MAAM,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAA;IACxD,IACE,UAAU,CAAC,OAAO,CAAC,MAAM,IAAI,IAAI;QACjC,UAAU,CAAC,OAAO,CAAC,cAAc,IAAI,IAAI,EACzC;QACA,UAAU,CAAC,OAAO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QAC7C,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAA;KAC1E;IAED,IAAI,oBAAoB,IAAI,KAAK,IAAI,KAAK,CAAC,kBAAkB,IAAI,IAAI,EAAE;QACrE,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAA;KACF;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAED,MAAM,yCAAyC,GAAG,CAChD,KAAwB,EAEC,EAAE;IAC3B,IAAI,CAAC,CAAC,oBAAoB,IAAI,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAElD,MAAM,EAAE,kBAAkB,EAAE,GAAG,KAAK,CAAA;IACpC,IAAI,kBAAkB,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IAE5C,IAAI,QAAQ,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,IAAI,IAAI,EAAE;QAC7C,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAA;KAC1E;IAED,IAAI,gBAAgB,IAAI,KAAK,IAAI,KAAK,CAAC,cAAc,IAAI,IAAI,EAAE;QAC7D,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAA;KACF;IAED,IAAI,mBAAmB,IAAI,KAAK,IAAI,KAAK,CAAC,iBAAiB,IAAI,IAAI,EAAE;QACnE,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAA;KAC7E;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA"}
|
|
@@ -3,15 +3,30 @@ import { Seam } from 'seamapi';
|
|
|
3
3
|
import { v4 as uuidv4 } from 'uuid';
|
|
4
4
|
import { useSeamContext } from '../../lib/SeamProvider.js';
|
|
5
5
|
export function useSeamClient() {
|
|
6
|
-
const { client, clientOptions, publishableKey, ...context } = useSeamContext();
|
|
7
|
-
const userIdentifierKey = useUserIdentifierKeyOrFingerprint(context.userIdentifierKey);
|
|
6
|
+
const { client, clientOptions, publishableKey, clientSessionToken, ...context } = useSeamContext();
|
|
7
|
+
const userIdentifierKey = useUserIdentifierKeyOrFingerprint(clientSessionToken != null ? '' : context.userIdentifierKey);
|
|
8
8
|
const { isLoading, isError, error, data } = useQuery({
|
|
9
|
-
queryKey: [
|
|
9
|
+
queryKey: [
|
|
10
|
+
'client',
|
|
11
|
+
{
|
|
12
|
+
client,
|
|
13
|
+
clientOptions,
|
|
14
|
+
publishableKey,
|
|
15
|
+
userIdentifierKey,
|
|
16
|
+
clientSessionToken,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
10
19
|
queryFn: async () => {
|
|
11
20
|
if (client != null)
|
|
12
21
|
return client;
|
|
22
|
+
if (clientSessionToken != null) {
|
|
23
|
+
return new Seam({
|
|
24
|
+
...clientOptions,
|
|
25
|
+
clientSessionToken,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
13
28
|
if (publishableKey == null) {
|
|
14
|
-
throw new Error('Missing publishableKey');
|
|
29
|
+
throw new Error('Missing either a client, publishableKey, or clientSessionToken');
|
|
15
30
|
}
|
|
16
31
|
const res = await Seam.getClientSessionToken({
|
|
17
32
|
...clientOptions,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-seam-client.js","sourceRoot":"","sources":["../../src/lib/seam/use-seam-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAA;AAEnC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,MAAM,UAAU,aAAa;IAM3B,MAAM,
|
|
1
|
+
{"version":3,"file":"use-seam-client.js","sourceRoot":"","sources":["../../src/lib/seam/use-seam-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE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AC,KAAK;aAC7C,CAAC,CAAA;QACJ,CAAC;KACF,CAAC,CAAA;IAEF,OAAO,EAAE,MAAM,EAAE,IAAI,IAAI,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;AAC5D,CAAC;AAED;;;;;GAKG;AACH,SAAS,iCAAiC,CACxC,iBAAqC;IAErC,IAAI,iBAAiB,IAAI,IAAI,EAAE;QAC7B,OAAO,iBAAiB,CAAA;KACzB;IAED,MAAM,gBAAgB,GACpB,UAAU,EAAE,YAAY,EAAE,OAAO,CAAC,uBAAuB,CAAC;QAC1D,eAAe,MAAM,EAAE,EAAE,CAAA;IAE3B,UAAU,EAAE,YAAY,EAAE,OAAO,CAAC,uBAAuB,EAAE,gBAAgB,CAAC,CAAA;IAE5E,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seamapi/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Seam React component library.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
],
|
|
46
46
|
"scripts": {
|
|
47
47
|
"start": "concurrently --raw --kill-others npm:examples npm:storybook",
|
|
48
|
+
"dev": "npm run start",
|
|
48
49
|
"build": "npm run build:js",
|
|
49
50
|
"prebuild": "del 'index.*' lib",
|
|
50
51
|
"postbuild": "node ./index.js",
|
package/src/lib/SeamProvider.tsx
CHANGED
|
@@ -11,23 +11,37 @@ import type { Seam, SeamClientOptions } from 'seamapi'
|
|
|
11
11
|
|
|
12
12
|
declare global {
|
|
13
13
|
// eslint-disable-next-line no-var
|
|
14
|
-
var seam:
|
|
14
|
+
var seam: SeamProviderProps | undefined
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface SeamContext {
|
|
18
18
|
client: Seam | null
|
|
19
|
-
clientOptions?:
|
|
19
|
+
clientOptions?: AllowedSeamClientOptions | undefined
|
|
20
20
|
publishableKey?: string | undefined
|
|
21
21
|
userIdentifierKey?: string | undefined
|
|
22
|
+
clientSessionToken?: string | undefined
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
type SeamProviderProps =
|
|
26
|
+
| SeamProviderPropsWithClient
|
|
27
|
+
| (SeamProviderPropsWithPublishableKey & AllowedSeamClientOptions)
|
|
28
|
+
| (SeamProviderPropsWithClientSessionToken & AllowedSeamClientOptions)
|
|
29
|
+
|
|
30
|
+
interface SeamProviderPropsWithClient {
|
|
31
|
+
client: Seam
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface SeamProviderPropsWithPublishableKey {
|
|
35
|
+
publishableKey: string
|
|
27
36
|
userIdentifierKey?: string
|
|
28
|
-
endpoint?: string
|
|
29
37
|
}
|
|
30
38
|
|
|
39
|
+
interface SeamProviderPropsWithClientSessionToken {
|
|
40
|
+
clientSessionToken: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type AllowedSeamClientOptions = Pick<SeamClientOptions, 'endpoint'>
|
|
44
|
+
|
|
31
45
|
export function SeamProvider({
|
|
32
46
|
children,
|
|
33
47
|
...props
|
|
@@ -61,10 +75,7 @@ export function SeamProvider({
|
|
|
61
75
|
}
|
|
62
76
|
|
|
63
77
|
const createDefaultSeamContextValue = (): SeamContext => {
|
|
64
|
-
if (
|
|
65
|
-
globalThis.seam?.client == null &&
|
|
66
|
-
globalThis.seam?.publishableKey == null
|
|
67
|
-
) {
|
|
78
|
+
if (globalThis.seam == null) {
|
|
68
79
|
return { client: null }
|
|
69
80
|
}
|
|
70
81
|
|
|
@@ -77,32 +88,31 @@ const createDefaultSeamContextValue = (): SeamContext => {
|
|
|
77
88
|
}
|
|
78
89
|
}
|
|
79
90
|
|
|
80
|
-
const createSeamContextValue = ({
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}: Omit<SeamProviderProps, 'children'>): SeamContext => {
|
|
84
|
-
if (client == null && Object.keys(options).length === 0) {
|
|
85
|
-
return { client: null }
|
|
91
|
+
const createSeamContextValue = (options: SeamProviderProps): SeamContext => {
|
|
92
|
+
if (isSeamProviderPropsWithClient(options)) {
|
|
93
|
+
return options
|
|
86
94
|
}
|
|
87
95
|
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
if (isSeamProviderPropsWithClientSessionToken(options)) {
|
|
97
|
+
const { clientSessionToken, ...clientOptions } = options
|
|
98
|
+
return {
|
|
99
|
+
clientSessionToken,
|
|
100
|
+
clientOptions,
|
|
101
|
+
client: null,
|
|
102
|
+
}
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
105
|
+
if (isSeamProviderPropsWithPublishableKey(options)) {
|
|
106
|
+
const { publishableKey, userIdentifierKey, ...clientOptions } = options
|
|
107
|
+
return {
|
|
108
|
+
publishableKey,
|
|
109
|
+
userIdentifierKey,
|
|
110
|
+
clientOptions,
|
|
111
|
+
client: null,
|
|
112
|
+
}
|
|
98
113
|
}
|
|
99
114
|
|
|
100
|
-
|
|
101
|
-
client: null,
|
|
102
|
-
publishableKey,
|
|
103
|
-
userIdentifierKey,
|
|
104
|
-
clientOptions,
|
|
105
|
-
}
|
|
115
|
+
throw new Error('Invalid SeamProvider options')
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
const defaultSeamContextValue = createDefaultSeamContextValue()
|
|
@@ -118,3 +128,65 @@ export function useSeamContext(): SeamContext {
|
|
|
118
128
|
|
|
119
129
|
return context
|
|
120
130
|
}
|
|
131
|
+
|
|
132
|
+
const isSeamProviderPropsWithClient = (
|
|
133
|
+
props: SeamProviderProps
|
|
134
|
+
): props is SeamProviderPropsWithClient => {
|
|
135
|
+
if (!('client' in props)) return false
|
|
136
|
+
|
|
137
|
+
const { client } = props
|
|
138
|
+
if (client == null) return false
|
|
139
|
+
|
|
140
|
+
if (Object.values(props).some((v) => v == null)) {
|
|
141
|
+
throw new Error('Cannot provide a Seam client along with other options.')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return true
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const isSeamProviderPropsWithPublishableKey = (
|
|
148
|
+
props: SeamProviderProps
|
|
149
|
+
): props is SeamProviderPropsWithPublishableKey & AllowedSeamClientOptions => {
|
|
150
|
+
if (!('publishableKey' in props)) return false
|
|
151
|
+
|
|
152
|
+
const { publishableKey } = props
|
|
153
|
+
if (publishableKey == null) return false
|
|
154
|
+
|
|
155
|
+
if ('client' in props && props.client != null) {
|
|
156
|
+
throw new Error('Cannot provide a Seam client along with other options.')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if ('clientSessionToken' in props && props.clientSessionToken != null) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
'Cannot provide both a publishableKey and a clientSessionToken.'
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return true
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const isSeamProviderPropsWithClientSessionToken = (
|
|
169
|
+
props: SeamProviderProps
|
|
170
|
+
): props is SeamProviderPropsWithClientSessionToken &
|
|
171
|
+
AllowedSeamClientOptions => {
|
|
172
|
+
if (!('clientSessionToken' in props)) return false
|
|
173
|
+
|
|
174
|
+
const { clientSessionToken } = props
|
|
175
|
+
if (clientSessionToken == null) return false
|
|
176
|
+
|
|
177
|
+
if ('client' in props && props.client != null) {
|
|
178
|
+
throw new Error('Cannot provide a Seam client along with other options.')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if ('publishableKey' in props && props.publishableKey != null) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
'Cannot provide both a clientSessionToken and a publishableKey .'
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
|
|
188
|
+
throw new Error('Cannot use a userIdentifierKey with a clientSessionToken.')
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return true
|
|
192
|
+
}
|
|
@@ -10,18 +10,42 @@ export function useSeamClient(): {
|
|
|
10
10
|
isError: boolean
|
|
11
11
|
error: unknown
|
|
12
12
|
} {
|
|
13
|
-
const {
|
|
13
|
+
const {
|
|
14
|
+
client,
|
|
15
|
+
clientOptions,
|
|
16
|
+
publishableKey,
|
|
17
|
+
clientSessionToken,
|
|
18
|
+
...context
|
|
19
|
+
} = useSeamContext()
|
|
14
20
|
const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
|
|
15
|
-
context.userIdentifierKey
|
|
21
|
+
clientSessionToken != null ? '' : context.userIdentifierKey
|
|
16
22
|
)
|
|
17
23
|
|
|
18
24
|
const { isLoading, isError, error, data } = useQuery<Seam>({
|
|
19
|
-
queryKey: [
|
|
25
|
+
queryKey: [
|
|
26
|
+
'client',
|
|
27
|
+
{
|
|
28
|
+
client,
|
|
29
|
+
clientOptions,
|
|
30
|
+
publishableKey,
|
|
31
|
+
userIdentifierKey,
|
|
32
|
+
clientSessionToken,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
20
35
|
queryFn: async () => {
|
|
21
36
|
if (client != null) return client
|
|
22
37
|
|
|
38
|
+
if (clientSessionToken != null) {
|
|
39
|
+
return new Seam({
|
|
40
|
+
...clientOptions,
|
|
41
|
+
clientSessionToken,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
23
45
|
if (publishableKey == null) {
|
|
24
|
-
throw new Error(
|
|
46
|
+
throw new Error(
|
|
47
|
+
'Missing either a client, publishableKey, or clientSessionToken'
|
|
48
|
+
)
|
|
25
49
|
}
|
|
26
50
|
|
|
27
51
|
const res = await Seam.getClientSessionToken({
|