@seamapi/react 4.12.0 → 4.13.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.
Files changed (140) hide show
  1. package/README.md +2 -17
  2. package/dist/elements.js +8340 -8097
  3. package/dist/elements.js.map +1 -1
  4. package/index.d.ts +1 -0
  5. package/index.js +1 -0
  6. package/index.js.map +1 -1
  7. package/lib/index.d.ts +0 -1
  8. package/lib/index.js +0 -1
  9. package/lib/index.js.map +1 -1
  10. package/lib/seam/SeamProvider.d.ts +1 -1
  11. package/lib/seam/SeamProvider.js +1 -1
  12. package/lib/seam/SeamProvider.js.map +1 -1
  13. package/lib/seam/access-codes/use-access-code.js +1 -1
  14. package/lib/seam/access-codes/use-access-code.js.map +1 -1
  15. package/lib/seam/access-codes/use-access-codes.js +1 -1
  16. package/lib/seam/access-codes/use-access-codes.js.map +1 -1
  17. package/lib/seam/access-codes/use-create-access-code.d.ts +1 -1
  18. package/lib/seam/access-codes/use-create-access-code.js +1 -1
  19. package/lib/seam/access-codes/use-create-access-code.js.map +1 -1
  20. package/lib/seam/access-codes/use-delete-access-code.js +1 -1
  21. package/lib/seam/access-codes/use-delete-access-code.js.map +1 -1
  22. package/lib/seam/access-codes/use-generate-access-code-code.js +1 -1
  23. package/lib/seam/access-codes/use-generate-access-code-code.js.map +1 -1
  24. package/lib/seam/access-codes/use-update-access-code.js +1 -1
  25. package/lib/seam/access-codes/use-update-access-code.js.map +1 -1
  26. package/lib/seam/client-sessions/use-client-session.js +1 -1
  27. package/lib/seam/client-sessions/use-client-session.js.map +1 -1
  28. package/lib/seam/components/SupportedDeviceTable/use-device-model.js +1 -1
  29. package/lib/seam/components/SupportedDeviceTable/use-device-model.js.map +1 -1
  30. package/lib/seam/components/SupportedDeviceTable/use-device-models.js +1 -1
  31. package/lib/seam/components/SupportedDeviceTable/use-device-models.js.map +1 -1
  32. package/lib/seam/components/SupportedDeviceTable/use-manufacturer.js +1 -1
  33. package/lib/seam/components/SupportedDeviceTable/use-manufacturer.js.map +1 -1
  34. package/lib/seam/components/SupportedDeviceTable/use-manufacturers.js +1 -1
  35. package/lib/seam/components/SupportedDeviceTable/use-manufacturers.js.map +1 -1
  36. package/lib/seam/connect-webviews/use-create-connect-webview.js +1 -1
  37. package/lib/seam/connect-webviews/use-create-connect-webview.js.map +1 -1
  38. package/lib/seam/connected-accounts/use-connected-account.js +1 -1
  39. package/lib/seam/connected-accounts/use-connected-account.js.map +1 -1
  40. package/lib/seam/devices/use-device-providers.js +1 -1
  41. package/lib/seam/devices/use-device-providers.js.map +1 -1
  42. package/lib/seam/devices/use-device.js +1 -1
  43. package/lib/seam/devices/use-device.js.map +1 -1
  44. package/lib/seam/devices/use-devices.js +1 -1
  45. package/lib/seam/devices/use-devices.js.map +1 -1
  46. package/lib/seam/devices/use-update-device-name.js +1 -1
  47. package/lib/seam/devices/use-update-device-name.js.map +1 -1
  48. package/lib/seam/events/use-events.js +1 -1
  49. package/lib/seam/events/use-events.js.map +1 -1
  50. package/lib/seam/index.d.ts +0 -6
  51. package/lib/seam/index.js +0 -6
  52. package/lib/seam/index.js.map +1 -1
  53. package/lib/seam/locks/use-toggle-lock.js +1 -1
  54. package/lib/seam/locks/use-toggle-lock.js.map +1 -1
  55. package/lib/seam/noise-sensors/use-noise-thresholds.js +1 -1
  56. package/lib/seam/noise-sensors/use-noise-thresholds.js.map +1 -1
  57. package/lib/seam/thermostats/use-cool-thermostat.js +1 -1
  58. package/lib/seam/thermostats/use-cool-thermostat.js.map +1 -1
  59. package/lib/seam/thermostats/use-create-thermostat-climate-preset.js +1 -1
  60. package/lib/seam/thermostats/use-create-thermostat-climate-preset.js.map +1 -1
  61. package/lib/seam/thermostats/use-delete-thermostat-climate-preset.js +1 -1
  62. package/lib/seam/thermostats/use-delete-thermostat-climate-preset.js.map +1 -1
  63. package/lib/seam/thermostats/use-heat-cool-thermostat.js +1 -1
  64. package/lib/seam/thermostats/use-heat-cool-thermostat.js.map +1 -1
  65. package/lib/seam/thermostats/use-heat-thermostat.js +1 -1
  66. package/lib/seam/thermostats/use-heat-thermostat.js.map +1 -1
  67. package/lib/seam/thermostats/use-set-thermostat-fan-mode.js +1 -1
  68. package/lib/seam/thermostats/use-set-thermostat-fan-mode.js.map +1 -1
  69. package/lib/seam/thermostats/use-set-thermostat-off.js +1 -1
  70. package/lib/seam/thermostats/use-set-thermostat-off.js.map +1 -1
  71. package/lib/seam/thermostats/use-update-thermostat-climate-preset.js +1 -1
  72. package/lib/seam/thermostats/use-update-thermostat-climate-preset.js.map +1 -1
  73. package/lib/telemetry/hooks.js +1 -1
  74. package/lib/telemetry/hooks.js.map +1 -1
  75. package/lib/ui/DateTimePicker/DateTimePicker.d.ts +1 -1
  76. package/lib/version.d.ts +1 -1
  77. package/lib/version.js +1 -1
  78. package/package.json +9 -9
  79. package/src/index.ts +1 -0
  80. package/src/lib/index.ts +0 -1
  81. package/src/lib/seam/SeamProvider.tsx +4 -4
  82. package/src/lib/seam/access-codes/use-access-code.ts +1 -1
  83. package/src/lib/seam/access-codes/use-access-codes.ts +1 -1
  84. package/src/lib/seam/access-codes/use-create-access-code.ts +2 -3
  85. package/src/lib/seam/access-codes/use-delete-access-code.ts +1 -2
  86. package/src/lib/seam/access-codes/use-generate-access-code-code.ts +1 -2
  87. package/src/lib/seam/access-codes/use-update-access-code.ts +1 -2
  88. package/src/lib/seam/client-sessions/use-client-session.ts +1 -1
  89. package/src/lib/seam/components/SupportedDeviceTable/use-device-model.ts +1 -1
  90. package/src/lib/seam/components/SupportedDeviceTable/use-device-models.ts +1 -1
  91. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturer.ts +1 -1
  92. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturers.ts +1 -1
  93. package/src/lib/seam/connect-webviews/use-create-connect-webview.ts +1 -1
  94. package/src/lib/seam/connected-accounts/use-connected-account.ts +1 -1
  95. package/src/lib/seam/devices/use-device-providers.ts +1 -1
  96. package/src/lib/seam/devices/use-device.ts +1 -1
  97. package/src/lib/seam/devices/use-devices.ts +1 -1
  98. package/src/lib/seam/devices/use-update-device-name.ts +1 -2
  99. package/src/lib/seam/events/use-events.ts +1 -1
  100. package/src/lib/seam/index.ts +0 -6
  101. package/src/lib/seam/locks/use-toggle-lock.ts +1 -2
  102. package/src/lib/seam/noise-sensors/use-noise-thresholds.ts +1 -1
  103. package/src/lib/seam/thermostats/use-cool-thermostat.ts +1 -1
  104. package/src/lib/seam/thermostats/use-create-thermostat-climate-preset.ts +1 -1
  105. package/src/lib/seam/thermostats/use-delete-thermostat-climate-preset.ts +1 -1
  106. package/src/lib/seam/thermostats/use-heat-cool-thermostat.ts +1 -1
  107. package/src/lib/seam/thermostats/use-heat-thermostat.ts +1 -1
  108. package/src/lib/seam/thermostats/use-set-thermostat-fan-mode.ts +1 -2
  109. package/src/lib/seam/thermostats/use-set-thermostat-off.ts +1 -2
  110. package/src/lib/seam/thermostats/use-update-thermostat-climate-preset.ts +1 -1
  111. package/src/lib/telemetry/hooks.ts +1 -1
  112. package/src/lib/version.ts +1 -1
  113. package/lib/seam/SeamQueryProvider.d.ts +0 -40
  114. package/lib/seam/SeamQueryProvider.js +0 -180
  115. package/lib/seam/SeamQueryProvider.js.map +0 -1
  116. package/lib/seam/use-seam-client.d.ts +0 -14
  117. package/lib/seam/use-seam-client.js +0 -134
  118. package/lib/seam/use-seam-client.js.map +0 -1
  119. package/lib/seam/use-seam-infinite-query.d.ts +0 -15
  120. package/lib/seam/use-seam-infinite-query.js +0 -47
  121. package/lib/seam/use-seam-infinite-query.js.map +0 -1
  122. package/lib/seam/use-seam-mutation-without-workspace.d.ts +0 -9
  123. package/lib/seam/use-seam-mutation-without-workspace.js +0 -17
  124. package/lib/seam/use-seam-mutation-without-workspace.js.map +0 -1
  125. package/lib/seam/use-seam-mutation.d.ts +0 -10
  126. package/lib/seam/use-seam-mutation.js +0 -17
  127. package/lib/seam/use-seam-mutation.js.map +0 -1
  128. package/lib/seam/use-seam-query-without-workspace.d.ts +0 -11
  129. package/lib/seam/use-seam-query-without-workspace.js +0 -25
  130. package/lib/seam/use-seam-query-without-workspace.js.map +0 -1
  131. package/lib/seam/use-seam-query.d.ts +0 -12
  132. package/lib/seam/use-seam-query.js +0 -25
  133. package/lib/seam/use-seam-query.js.map +0 -1
  134. package/src/lib/seam/SeamQueryProvider.tsx +0 -356
  135. package/src/lib/seam/use-seam-client.ts +0 -225
  136. package/src/lib/seam/use-seam-infinite-query.ts +0 -103
  137. package/src/lib/seam/use-seam-mutation-without-workspace.ts +0 -56
  138. package/src/lib/seam/use-seam-mutation.ts +0 -64
  139. package/src/lib/seam/use-seam-query-without-workspace.ts +0 -59
  140. package/src/lib/seam/use-seam-query.ts +0 -66
@@ -1,356 +0,0 @@
1
- import type {
2
- SeamHttp,
3
- SeamHttpEndpoints,
4
- SeamHttpOptionsWithClientSessionToken,
5
- } from '@seamapi/http/connect'
6
- import {
7
- QueryClient,
8
- QueryClientContext,
9
- QueryClientProvider,
10
- } from '@tanstack/react-query'
11
- import {
12
- createContext,
13
- type PropsWithChildren,
14
- useContext,
15
- useEffect,
16
- useMemo,
17
- } from 'react'
18
-
19
- import { useSeamClient } from './use-seam-client.js'
20
-
21
- export interface SeamQueryContext {
22
- client: SeamHttp | null
23
- endpointClient: SeamHttpEndpoints | null
24
- clientOptions?: SeamQueryProviderClientOptions | undefined
25
- publishableKey?: string | undefined
26
- userIdentifierKey?: string | undefined
27
- clientSessionToken?: string | undefined
28
- consoleSessionToken?: string | undefined
29
- workspaceId?: string | undefined
30
- queryKeyPrefix?: string | undefined
31
- }
32
-
33
- export type SeamQueryProviderProps =
34
- | SeamQueryProviderPropsWithClient
35
- | SeamQueryProviderPropsWithPublishableKey
36
- | SeamQueryProviderPropsWithClientSessionToken
37
- | SeamQueryProviderPropsWithConsoleSessionToken
38
-
39
- export interface SeamQueryProviderPropsWithClient
40
- extends SeamQueryProviderBaseProps {
41
- client: SeamHttp
42
- queryKeyPrefix: string
43
- }
44
-
45
- export interface SeamQueryProviderPropsWithPublishableKey
46
- extends SeamQueryProviderBaseProps,
47
- SeamQueryProviderClientOptions {
48
- publishableKey: string
49
- userIdentifierKey?: string
50
- }
51
-
52
- export interface SeamQueryProviderPropsWithClientSessionToken
53
- extends SeamQueryProviderBaseProps,
54
- SeamQueryProviderClientOptions {
55
- clientSessionToken: string
56
- }
57
-
58
- export interface SeamQueryProviderPropsWithConsoleSessionToken
59
- extends SeamQueryProviderBaseProps,
60
- SeamQueryProviderClientOptions {
61
- consoleSessionToken: string
62
- workspaceId?: string | undefined
63
- }
64
-
65
- interface SeamQueryProviderBaseProps extends PropsWithChildren {
66
- queryClient?: QueryClient | undefined
67
- onSessionUpdate?: (client: SeamHttp) => void
68
- }
69
-
70
- type SeamClientOptions = SeamHttpOptionsWithClientSessionToken
71
-
72
- export type SeamQueryProviderClientOptions = Pick<
73
- SeamClientOptions,
74
- 'endpoint' | 'isUndocumentedApiEnabled'
75
- >
76
-
77
- const defaultQueryClient = new QueryClient()
78
-
79
- export function SeamQueryProvider({
80
- children,
81
- onSessionUpdate = () => {},
82
- queryClient,
83
- ...props
84
- }: SeamQueryProviderProps): JSX.Element {
85
- const value = useMemo(() => {
86
- const context = createSeamQueryContextValue(props)
87
- if (
88
- context.client == null &&
89
- context.publishableKey == null &&
90
- context.clientSessionToken == null &&
91
- context.consoleSessionToken == null
92
- ) {
93
- return defaultSeamQueryContextValue
94
- }
95
- return context
96
- }, [props])
97
-
98
- if (
99
- value.client == null &&
100
- value.publishableKey == null &&
101
- value.clientSessionToken == null &&
102
- value.consoleSessionToken == null
103
- ) {
104
- throw new Error(
105
- `Must provide either a Seam client, clientSessionToken, publishableKey or consoleSessionToken.`
106
- )
107
- }
108
-
109
- const { Provider } = seamContext
110
- const queryClientFromContext = useContext(QueryClientContext)
111
-
112
- if (
113
- queryClientFromContext != null &&
114
- queryClient != null &&
115
- queryClientFromContext !== queryClient
116
- ) {
117
- throw new Error(
118
- 'The QueryClient passed into SeamQueryProvider is different from the one in the existing QueryClientContext. Omit the queryClient prop from SeamProvider or SeamQueryProvider to use the existing QueryClient provided by the QueryClientProvider.'
119
- )
120
- }
121
-
122
- return (
123
- <QueryClientProvider
124
- client={queryClientFromContext ?? queryClient ?? defaultQueryClient}
125
- >
126
- <Provider value={value}>
127
- <Session onSessionUpdate={onSessionUpdate}>{children}</Session>
128
- </Provider>
129
- </QueryClientProvider>
130
- )
131
- }
132
-
133
- function Session({
134
- onSessionUpdate,
135
- children,
136
- }: Required<Pick<SeamQueryProviderProps, 'onSessionUpdate'>> &
137
- PropsWithChildren): JSX.Element | null {
138
- const { client } = useSeamClient()
139
- useEffect(() => {
140
- if (client != null) onSessionUpdate(client)
141
- }, [onSessionUpdate, client])
142
-
143
- return <>{children}</>
144
- }
145
-
146
- const createDefaultSeamQueryContextValue = (): SeamQueryContext => {
147
- try {
148
- if (globalThis.seam == null) {
149
- return { client: null, endpointClient: null }
150
- }
151
- return createSeamQueryContextValue(globalThis.seam)
152
- } catch (err) {
153
- // eslint-disable-next-line no-console
154
- console.warn(err)
155
- return { client: null, endpointClient: null }
156
- }
157
- }
158
-
159
- const createSeamQueryContextValue = (
160
- options: SeamQueryProviderProps
161
- ): SeamQueryContext => {
162
- if (isSeamQueryProviderPropsWithClient(options)) {
163
- if (options.queryKeyPrefix == null) {
164
- throw new InvalidSeamQueryProviderProps(
165
- 'The client prop must be used with a queryKeyPrefix prop.'
166
- )
167
- }
168
- return {
169
- ...options,
170
- endpointClient: null,
171
- }
172
- }
173
-
174
- if (isSeamQueryProviderPropsWithClientSessionToken(options)) {
175
- const { clientSessionToken, ...clientOptions } = options
176
- return {
177
- clientSessionToken,
178
- clientOptions,
179
- client: null,
180
- endpointClient: null,
181
- }
182
- }
183
-
184
- if (isSeamQueryProviderPropsWithPublishableKey(options)) {
185
- const { publishableKey, userIdentifierKey, ...clientOptions } = options
186
- return {
187
- publishableKey,
188
- userIdentifierKey,
189
- clientOptions,
190
- client: null,
191
- endpointClient: null,
192
- }
193
- }
194
-
195
- if (isSeamQueryProviderPropsWithConsoleSessionToken(options)) {
196
- const { consoleSessionToken, workspaceId, ...clientOptions } = options
197
- return {
198
- consoleSessionToken,
199
- workspaceId,
200
- clientOptions,
201
- client: null,
202
- endpointClient: null,
203
- }
204
- }
205
-
206
- return { client: null, endpointClient: null }
207
- }
208
-
209
- const defaultSeamQueryContextValue = createDefaultSeamQueryContextValue()
210
-
211
- export const seamContext = createContext<SeamQueryContext>(
212
- defaultSeamQueryContextValue
213
- )
214
-
215
- export function useSeamQueryContext(): SeamQueryContext {
216
- return useContext(seamContext)
217
- }
218
-
219
- const isSeamQueryProviderPropsWithClient = (
220
- props: SeamQueryProviderProps
221
- ): props is SeamQueryProviderPropsWithClient => {
222
- if (!('client' in props)) return false
223
-
224
- const { client, ...otherProps } = props
225
- if (client == null) return false
226
-
227
- const otherNonNullProps = Object.values(otherProps).filter((v) => v != null)
228
- if (otherNonNullProps.length > 0) {
229
- throw new InvalidSeamQueryProviderProps(
230
- `The client prop cannot be used with ${otherNonNullProps.join(' or ')}.`
231
- )
232
- }
233
-
234
- return true
235
- }
236
-
237
- const isSeamQueryProviderPropsWithPublishableKey = (
238
- props: SeamQueryProviderProps
239
- ): props is SeamQueryProviderPropsWithPublishableKey &
240
- SeamQueryProviderClientOptions => {
241
- if (!('publishableKey' in props)) return false
242
-
243
- const { publishableKey } = props
244
- if (publishableKey == null) return false
245
-
246
- if ('client' in props && props.client != null) {
247
- throw new InvalidSeamQueryProviderProps(
248
- 'The client prop cannot be used with the publishableKey prop.'
249
- )
250
- }
251
-
252
- if ('clientSessionToken' in props && props.clientSessionToken != null) {
253
- throw new InvalidSeamQueryProviderProps(
254
- 'The clientSessionToken prop cannot be used with the publishableKey prop.'
255
- )
256
- }
257
-
258
- if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
259
- throw new InvalidSeamQueryProviderProps(
260
- 'The consoleSessionToken prop cannot be used with the publishableKey prop.'
261
- )
262
- }
263
-
264
- if ('workspaceId' in props && props.workspaceId != null) {
265
- throw new InvalidSeamQueryProviderProps(
266
- 'The workspaceId prop cannot be used with the publishableKey prop.'
267
- )
268
- }
269
-
270
- return true
271
- }
272
-
273
- const isSeamQueryProviderPropsWithClientSessionToken = (
274
- props: SeamQueryProviderProps
275
- ): props is SeamQueryProviderPropsWithClientSessionToken &
276
- SeamQueryProviderClientOptions => {
277
- if (!('clientSessionToken' in props)) return false
278
-
279
- const { clientSessionToken } = props
280
- if (clientSessionToken == null) return false
281
-
282
- if ('client' in props && props.client != null) {
283
- throw new InvalidSeamQueryProviderProps(
284
- 'The client prop cannot be used with the clientSessionToken prop.'
285
- )
286
- }
287
-
288
- if ('publishableKey' in props && props.publishableKey != null) {
289
- throw new InvalidSeamQueryProviderProps(
290
- 'The publishableKey prop cannot be used with the clientSessionToken prop.'
291
- )
292
- }
293
-
294
- if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
295
- throw new InvalidSeamQueryProviderProps(
296
- 'The userIdentifierKey prop cannot be used with the clientSessionToken prop.'
297
- )
298
- }
299
-
300
- if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
301
- throw new InvalidSeamQueryProviderProps(
302
- 'The consoleSessionToken prop cannot be used with the clientSessionToken prop.'
303
- )
304
- }
305
-
306
- if ('workspaceId' in props && props.workspaceId != null) {
307
- throw new InvalidSeamQueryProviderProps(
308
- 'The workspaceId prop cannot be used with the clientSessionToken prop.'
309
- )
310
- }
311
-
312
- return true
313
- }
314
-
315
- const isSeamQueryProviderPropsWithConsoleSessionToken = (
316
- props: SeamQueryProviderProps
317
- ): props is SeamQueryProviderPropsWithConsoleSessionToken &
318
- SeamQueryProviderClientOptions => {
319
- if (!('consoleSessionToken' in props)) return false
320
-
321
- const { consoleSessionToken } = props
322
- if (consoleSessionToken == null) return false
323
-
324
- if ('client' in props && props.client != null) {
325
- throw new InvalidSeamQueryProviderProps(
326
- 'The client prop cannot be used with the publishableKey prop.'
327
- )
328
- }
329
-
330
- if ('clientSessionToken' in props && props.clientSessionToken != null) {
331
- throw new InvalidSeamQueryProviderProps(
332
- 'The clientSessionToken prop cannot be used with the publishableKey prop.'
333
- )
334
- }
335
-
336
- if ('publishableKey' in props && props.publishableKey != null) {
337
- throw new InvalidSeamQueryProviderProps(
338
- 'The publishableKey prop cannot be used with the consoleSessionToken prop.'
339
- )
340
- }
341
-
342
- if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
343
- throw new InvalidSeamQueryProviderProps(
344
- 'The userIdentifierKey prop cannot be used with the consoleSessionToken prop.'
345
- )
346
- }
347
-
348
- return true
349
- }
350
-
351
- class InvalidSeamQueryProviderProps extends Error {
352
- constructor(message: string) {
353
- super(`SeamQueryProvider received invalid props: ${message}`)
354
- this.name = this.constructor.name
355
- }
356
- }
@@ -1,225 +0,0 @@
1
- import {
2
- SeamHttp,
3
- SeamHttpEndpoints,
4
- SeamHttpEndpointsWithoutWorkspace,
5
- SeamHttpWithoutWorkspace,
6
- } from '@seamapi/http/connect'
7
- import { useQuery } from '@tanstack/react-query'
8
- import { useEffect } from 'react'
9
- import { v4 as uuidv4 } from 'uuid'
10
-
11
- import { useSeamQueryContext } from './SeamQueryProvider.js'
12
-
13
- export function useSeamClient(): {
14
- client: SeamHttp | null
15
- endpointClient: SeamHttpEndpoints | null
16
- clientWithoutWorkspace: SeamHttpWithoutWorkspace | null
17
- endpointClientWithoutWorkspace: SeamHttpEndpointsWithoutWorkspace | null
18
- queryKeyPrefixes: string[]
19
- isPending: boolean
20
- isError: boolean
21
- error: unknown
22
- } {
23
- const {
24
- client,
25
- clientOptions,
26
- publishableKey,
27
- clientSessionToken,
28
- consoleSessionToken,
29
- workspaceId,
30
- queryKeyPrefix,
31
- ...context
32
- } = useSeamQueryContext()
33
- const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
34
- clientSessionToken != null ? '' : context.userIdentifierKey
35
- )
36
-
37
- const { isPending, isError, error, data } = useQuery<{
38
- client: SeamHttp | null
39
- endpointClient: SeamHttpEndpoints | null
40
- clientWithoutWorkspace: SeamHttpWithoutWorkspace | null
41
- endpointClientWithoutWorkspace: SeamHttpEndpointsWithoutWorkspace | null
42
- }>({
43
- queryKey: [
44
- ...getQueryKeyPrefixes({ queryKeyPrefix }),
45
- 'client',
46
- {
47
- client,
48
- clientOptions,
49
- publishableKey,
50
- userIdentifierKey,
51
- clientSessionToken,
52
- },
53
- ],
54
- queryFn: async () => {
55
- if (client != null)
56
- return {
57
- client,
58
- endpointClient: SeamHttpEndpoints.fromClient(client.client),
59
- clientWithoutWorkspace: null,
60
- endpointClientWithoutWorkspace: null,
61
- }
62
-
63
- if (clientSessionToken != null) {
64
- const seam = SeamHttp.fromClientSessionToken(
65
- clientSessionToken,
66
- clientOptions
67
- )
68
-
69
- return {
70
- client: seam,
71
- endpointClient: SeamHttpEndpoints.fromClient(seam.client),
72
- clientWithoutWorkspace: null,
73
- endpointClientWithoutWorkspace: null,
74
- }
75
- }
76
-
77
- if (publishableKey != null) {
78
- const seam = await SeamHttp.fromPublishableKey(
79
- publishableKey,
80
- userIdentifierKey,
81
- clientOptions
82
- )
83
-
84
- return {
85
- client: seam,
86
- endpointClient: SeamHttpEndpoints.fromClient(seam.client),
87
- clientWithoutWorkspace: null,
88
- endpointClientWithoutWorkspace: null,
89
- }
90
- }
91
-
92
- if (consoleSessionToken != null) {
93
- const clientWithoutWorkspace =
94
- SeamHttpWithoutWorkspace.fromConsoleSessionToken(consoleSessionToken)
95
-
96
- const endpointClientWithoutWorkspace =
97
- SeamHttpEndpointsWithoutWorkspace.fromClient(
98
- clientWithoutWorkspace.client
99
- )
100
-
101
- if (workspaceId == null) {
102
- return {
103
- client: null,
104
- endpointClient: null,
105
- clientWithoutWorkspace,
106
- endpointClientWithoutWorkspace,
107
- }
108
- }
109
-
110
- const seam = SeamHttp.fromConsoleSessionToken(
111
- consoleSessionToken,
112
- workspaceId,
113
- clientOptions
114
- )
115
-
116
- return {
117
- client: seam,
118
- endpointClient: SeamHttpEndpoints.fromClient(seam.client),
119
- clientWithoutWorkspace,
120
- endpointClientWithoutWorkspace,
121
- }
122
- }
123
-
124
- throw new Error(
125
- 'Missing either a client, publishableKey, clientSessionToken, or consoleSessionToken.'
126
- )
127
- },
128
- })
129
-
130
- return {
131
- client: data?.client ?? null,
132
- endpointClient: data?.endpointClient ?? null,
133
- clientWithoutWorkspace: data?.clientWithoutWorkspace ?? null,
134
- endpointClientWithoutWorkspace:
135
- data?.endpointClientWithoutWorkspace ?? null,
136
- queryKeyPrefixes: getQueryKeyPrefixes({
137
- queryKeyPrefix,
138
- userIdentifierKey,
139
- publishableKey,
140
- clientSessionToken,
141
- consoleSessionToken,
142
- workspaceId,
143
- }),
144
- isPending,
145
- isError,
146
- error,
147
- }
148
- }
149
-
150
- export class NullSeamClientError extends Error {
151
- constructor() {
152
- super(
153
- [
154
- 'Attempted to use a null Seam client.',
155
- 'Either a hook using useSeamClient was called outside of a SeamProvider or SeamQueryProvider,',
156
- 'or there was an error when creating the Seam client in useSeamClient,',
157
- 'or useSeamClient is still loading the client.',
158
- ].join(' ')
159
- )
160
- this.name = this.constructor.name
161
- Error.captureStackTrace(this, this.constructor)
162
- }
163
- }
164
-
165
- function useUserIdentifierKeyOrFingerprint(
166
- userIdentifierKey: string | undefined
167
- ): string {
168
- useEffect(() => {
169
- if (userIdentifierKey != null) return
170
- // eslint-disable-next-line no-console
171
- console.warn(`Using an automatically generated fingerprint for the Seam userIdentifierKey!
172
- The user interface will show warnings when using a fingerprint.
173
- This is not recommended because the client session is now bound to this machine and is effectively ephemeral.`)
174
- }, [userIdentifierKey])
175
-
176
- if (userIdentifierKey != null) {
177
- return userIdentifierKey
178
- }
179
-
180
- const fingerprint =
181
- globalThis.localStorage?.getItem('seam_user_fingerprint') ??
182
- `fingerprint_${uuidv4()}`
183
-
184
- globalThis.localStorage?.setItem('seam_user_fingerprint', fingerprint)
185
-
186
- return fingerprint
187
- }
188
-
189
- const getQueryKeyPrefixes = ({
190
- queryKeyPrefix,
191
- userIdentifierKey,
192
- publishableKey,
193
- clientSessionToken,
194
- consoleSessionToken,
195
- workspaceId,
196
- }: {
197
- queryKeyPrefix: string | undefined
198
- userIdentifierKey?: string
199
- publishableKey?: string | undefined
200
- clientSessionToken?: string | undefined
201
- consoleSessionToken?: string | undefined
202
- workspaceId?: string | undefined
203
- }): string[] => {
204
- const seamPrefix = 'seam'
205
-
206
- if (queryKeyPrefix != null) return [seamPrefix, queryKeyPrefix]
207
-
208
- if (clientSessionToken != null) {
209
- return [seamPrefix, clientSessionToken]
210
- }
211
-
212
- if (publishableKey != null && userIdentifierKey != null) {
213
- return [seamPrefix, publishableKey, userIdentifierKey]
214
- }
215
-
216
- if (consoleSessionToken != null) {
217
- if (workspaceId != null) {
218
- return [seamPrefix, consoleSessionToken, workspaceId]
219
- }
220
-
221
- return [seamPrefix, consoleSessionToken, 'without_workspace']
222
- }
223
-
224
- return [seamPrefix]
225
- }
@@ -1,103 +0,0 @@
1
- import type {
2
- SeamActionAttemptFailedError,
3
- SeamActionAttemptTimeoutError,
4
- SeamHttpApiError,
5
- SeamHttpEndpointPaginatedQueryPaths,
6
- SeamHttpEndpoints,
7
- SeamHttpInvalidInputError,
8
- SeamHttpRequest,
9
- SeamPageCursor,
10
- } from '@seamapi/http/connect'
11
- import type { ActionAttempt } from '@seamapi/types/connect'
12
- import {
13
- type QueryKey,
14
- useInfiniteQuery,
15
- type UseInfiniteQueryOptions,
16
- type UseInfiniteQueryResult,
17
- } from '@tanstack/react-query'
18
-
19
- import { useSeamClient } from 'lib/seam/use-seam-client.js'
20
-
21
- export type UseSeamInfiniteQueryParameters<
22
- T extends SeamHttpEndpointPaginatedQueryPaths,
23
- > = Parameters<SeamHttpEndpoints[T]>[0]
24
-
25
- export type UseSeamInfiniteQueryResult<
26
- T extends SeamHttpEndpointPaginatedQueryPaths,
27
- > = UseInfiniteQueryResult<QueryData<T>, QueryError<T>>
28
-
29
- export function useSeamInfiniteQuery<
30
- T extends SeamHttpEndpointPaginatedQueryPaths,
31
- >(
32
- endpointPath: T,
33
- parameters: UseSeamInfiniteQueryParameters<T> = {},
34
- options: Parameters<SeamHttpEndpoints[T]>[1] &
35
- QueryOptions<QueryData<T>, QueryError<T>> = {}
36
- ): UseSeamInfiniteQueryResult<T> & { queryKey: QueryKey } {
37
- const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
38
-
39
- if ('page_cursor' in (parameters ?? {})) {
40
- throw new Error('Cannot use page_cursor with useSeamInfiniteQuery')
41
- }
42
-
43
- const queryKey = [
44
- ...queryKeyPrefixes,
45
- ...endpointPath.split('/').filter((v) => v !== ''),
46
- parameters ?? {},
47
- ]
48
- const result = useInfiniteQuery({
49
- enabled: client != null,
50
- ...options,
51
- queryKey,
52
- initialPageParam: null,
53
- getNextPageParam: (lastPage) => lastPage.nextPageCursor,
54
- queryFn: async ({ pageParam }) => {
55
- if (client == null)
56
- return {
57
- data: [] as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
58
- nextPageCursor: null,
59
- }
60
- // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
61
- // Type assertion is needed here for performance reasons. The types are correct at runtime.
62
- const endpoint = client[endpointPath] as (...args: any) => any
63
- const request = endpoint(parameters, options)
64
- const pages = client.createPaginator(request as SeamHttpRequest<any, any>)
65
- if (pageParam == null) {
66
- const [data, { nextPageCursor }] = await pages.firstPage()
67
- return {
68
- data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
69
- nextPageCursor,
70
- }
71
- }
72
- // Type assertion is needed for pageParam since the Seam API expects a branded PageCursor type.
73
- const [data, { nextPageCursor }] = await pages.nextPage(
74
- pageParam as SeamPageCursor
75
- )
76
- return {
77
- data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
78
- nextPageCursor,
79
- }
80
- },
81
- })
82
- return { ...result, queryKey }
83
- }
84
-
85
- interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> {
86
- data: Awaited<ReturnType<SeamHttpEndpoints[T]>>
87
- nextPageCursor: SeamPageCursor | null
88
- }
89
-
90
- type QueryError<T extends SeamHttpEndpointPaginatedQueryPaths> =
91
- | Error
92
- | SeamHttpApiError
93
- | SeamHttpInvalidInputError
94
- | (QueryData<T>['data'] extends ActionAttempt
95
- ?
96
- | SeamActionAttemptFailedError<QueryData<T>['data']>
97
- | SeamActionAttemptTimeoutError<QueryData<T>['data']>
98
- : never)
99
-
100
- type QueryOptions<X, Y> = Omit<
101
- UseInfiniteQueryOptions<X, Y>,
102
- 'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam'
103
- >