@seamapi/react 4.8.2 → 4.9.1

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 (75) hide show
  1. package/README.md +17 -2
  2. package/dist/elements.js +22079 -18028
  3. package/dist/elements.js.map +1 -1
  4. package/lib/index.d.ts +1 -0
  5. package/lib/index.js +1 -0
  6. package/lib/index.js.map +1 -1
  7. package/lib/seam/SeamProvider.d.ts +7 -16
  8. package/lib/seam/SeamProvider.js +12 -101
  9. package/lib/seam/SeamProvider.js.map +1 -1
  10. package/lib/seam/SeamQueryProvider.d.ts +34 -0
  11. package/lib/seam/SeamQueryProvider.js +136 -0
  12. package/lib/seam/SeamQueryProvider.js.map +1 -0
  13. package/lib/seam/access-codes/use-access-code.d.ts +2 -2
  14. package/lib/seam/access-codes/use-access-codes.d.ts +2 -2
  15. package/lib/seam/access-codes/use-create-access-code.d.ts +2 -7
  16. package/lib/seam/access-codes/use-create-access-code.js +3 -9
  17. package/lib/seam/access-codes/use-create-access-code.js.map +1 -1
  18. package/lib/seam/access-codes/use-generate-access-code-code.d.ts +2 -2
  19. package/lib/seam/client-sessions/use-client-session.d.ts +2 -2
  20. package/lib/seam/components/SupportedDeviceTable/use-device-model.d.ts +2 -2
  21. package/lib/seam/components/SupportedDeviceTable/use-device-models.d.ts +2 -2
  22. package/lib/seam/components/SupportedDeviceTable/use-manufacturer.d.ts +2 -2
  23. package/lib/seam/components/SupportedDeviceTable/use-manufacturers.d.ts +2 -2
  24. package/lib/seam/connected-accounts/use-connected-account.d.ts +2 -2
  25. package/lib/seam/devices/use-device-providers.d.ts +2 -2
  26. package/lib/seam/devices/use-device.d.ts +2 -2
  27. package/lib/seam/devices/use-devices.d.ts +2 -2
  28. package/lib/seam/events/use-events.d.ts +2 -2
  29. package/lib/seam/index.d.ts +2 -0
  30. package/lib/seam/index.js +2 -0
  31. package/lib/seam/index.js.map +1 -1
  32. package/lib/seam/noise-sensors/use-noise-thresholds.d.ts +2 -2
  33. package/lib/seam/thermostats/use-delete-thermostat-climate-preset.d.ts +2 -2
  34. package/lib/seam/use-seam-client.d.ts +3 -1
  35. package/lib/seam/use-seam-client.js +42 -9
  36. package/lib/seam/use-seam-client.js.map +1 -1
  37. package/lib/seam/use-seam-mutation.d.ts +8 -0
  38. package/lib/seam/use-seam-mutation.js +17 -0
  39. package/lib/seam/use-seam-mutation.js.map +1 -0
  40. package/lib/seam/use-seam-query-result.d.ts +1 -1
  41. package/lib/seam/use-seam-query.d.ts +8 -0
  42. package/lib/seam/use-seam-query.js +23 -0
  43. package/lib/seam/use-seam-query.js.map +1 -0
  44. package/lib/telemetry/hooks.js +2 -2
  45. package/lib/telemetry/hooks.js.map +1 -1
  46. package/lib/ui/DateTimePicker/DateTimePicker.d.ts +1 -1
  47. package/lib/version.d.ts +1 -1
  48. package/lib/version.js +1 -1
  49. package/package.json +3 -2
  50. package/src/lib/index.ts +1 -0
  51. package/src/lib/seam/SeamProvider.tsx +31 -182
  52. package/src/lib/seam/SeamQueryProvider.tsx +270 -0
  53. package/src/lib/seam/access-codes/use-access-code.ts +2 -2
  54. package/src/lib/seam/access-codes/use-access-codes.ts +2 -2
  55. package/src/lib/seam/access-codes/use-create-access-code.ts +7 -32
  56. package/src/lib/seam/access-codes/use-generate-access-code-code.ts +2 -2
  57. package/src/lib/seam/client-sessions/use-client-session.ts +2 -2
  58. package/src/lib/seam/components/SupportedDeviceTable/use-device-model.ts +2 -2
  59. package/src/lib/seam/components/SupportedDeviceTable/use-device-models.ts +2 -2
  60. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturer.ts +2 -2
  61. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturers.ts +2 -2
  62. package/src/lib/seam/connected-accounts/use-connected-account.ts +2 -2
  63. package/src/lib/seam/devices/use-device-providers.ts +2 -2
  64. package/src/lib/seam/devices/use-device.ts +2 -2
  65. package/src/lib/seam/devices/use-devices.ts +2 -2
  66. package/src/lib/seam/events/use-events.ts +2 -2
  67. package/src/lib/seam/index.ts +2 -0
  68. package/src/lib/seam/noise-sensors/use-noise-thresholds.ts +2 -2
  69. package/src/lib/seam/thermostats/use-delete-thermostat-climate-preset.ts +2 -2
  70. package/src/lib/seam/use-seam-client.ts +64 -10
  71. package/src/lib/seam/use-seam-mutation.ts +50 -0
  72. package/src/lib/seam/use-seam-query-result.ts +6 -4
  73. package/src/lib/seam/use-seam-query.ts +49 -0
  74. package/src/lib/telemetry/hooks.ts +2 -2
  75. package/src/lib/version.ts +1 -1
@@ -1,12 +1,14 @@
1
- import { SeamHttp } from '@seamapi/http/connect'
1
+ import { SeamHttp, SeamHttpEndpoints } from '@seamapi/http/connect'
2
2
  import { useQuery } from '@tanstack/react-query'
3
3
  import { useEffect } from 'react'
4
4
  import { v4 as uuidv4 } from 'uuid'
5
5
 
6
- import { useSeamContext } from 'lib/seam/SeamProvider.js'
6
+ import { useSeamQueryContext } from './SeamQueryProvider.js'
7
7
 
8
8
  export function useSeamClient(): {
9
9
  client: SeamHttp | null
10
+ endpointClient: SeamHttpEndpoints | null
11
+ queryKeyPrefixes: string[]
10
12
  isPending: boolean
11
13
  isError: boolean
12
14
  error: unknown
@@ -16,14 +18,18 @@ export function useSeamClient(): {
16
18
  clientOptions,
17
19
  publishableKey,
18
20
  clientSessionToken,
21
+ queryKeyPrefix,
19
22
  ...context
20
- } = useSeamContext()
23
+ } = useSeamQueryContext()
21
24
  const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
22
25
  clientSessionToken != null ? '' : context.userIdentifierKey
23
26
  )
24
27
 
25
- const { isPending, isError, error, data } = useQuery<SeamHttp>({
28
+ const { isPending, isError, error, data } = useQuery<
29
+ [SeamHttp, SeamHttpEndpoints]
30
+ >({
26
31
  queryKey: [
32
+ ...getQueryKeyPrefixes({ queryKeyPrefix }),
27
33
  'client',
28
34
  {
29
35
  client,
@@ -34,13 +40,19 @@ export function useSeamClient(): {
34
40
  },
35
41
  ],
36
42
  queryFn: async () => {
37
- if (client != null) return client
43
+ if (client != null)
44
+ return [client, SeamHttpEndpoints.fromClient(client.client)]
38
45
 
39
46
  if (clientSessionToken != null) {
40
- return SeamHttp.fromClientSessionToken(
47
+ const clientSessionTokenClient = SeamHttp.fromClientSessionToken(
41
48
  clientSessionToken,
42
49
  clientOptions
43
50
  )
51
+
52
+ return [
53
+ clientSessionTokenClient,
54
+ SeamHttpEndpoints.fromClient(clientSessionTokenClient.client),
55
+ ]
44
56
  }
45
57
 
46
58
  if (publishableKey == null) {
@@ -49,15 +61,31 @@ export function useSeamClient(): {
49
61
  )
50
62
  }
51
63
 
52
- return await SeamHttp.fromPublishableKey(
64
+ const publishableKeyClient = await SeamHttp.fromPublishableKey(
53
65
  publishableKey,
54
66
  userIdentifierKey,
55
67
  clientOptions
56
68
  )
69
+ return [
70
+ publishableKeyClient,
71
+ SeamHttpEndpoints.fromClient(publishableKeyClient.client),
72
+ ]
57
73
  },
58
74
  })
59
75
 
60
- return { client: data ?? null, isPending, isError, error }
76
+ return {
77
+ client: data?.[0] ?? null,
78
+ endpointClient: data?.[1] ?? null,
79
+ queryKeyPrefixes: getQueryKeyPrefixes({
80
+ queryKeyPrefix,
81
+ userIdentifierKey,
82
+ publishableKey,
83
+ clientSessionToken,
84
+ }),
85
+ isPending,
86
+ isError,
87
+ error,
88
+ }
61
89
  }
62
90
 
63
91
  export class NullSeamClientError extends Error {
@@ -65,7 +93,7 @@ export class NullSeamClientError extends Error {
65
93
  super(
66
94
  [
67
95
  'Attempted to use a null Seam client.',
68
- 'Either a hook using useSeamClient was called outside of a SeamProvider,',
96
+ 'Either a hook using useSeamClient was called outside of a SeamProvider or SeamQueryProvider,',
69
97
  'or there was an error when creating the Seam client in useSeamClient,',
70
98
  'or useSeamClient is still loading the client.',
71
99
  ].join(' ')
@@ -81,7 +109,7 @@ function useUserIdentifierKeyOrFingerprint(
81
109
  useEffect(() => {
82
110
  if (userIdentifierKey != null) return
83
111
  // eslint-disable-next-line no-console
84
- console.warn(`Using an automatically generated fingerprint for the SeamProvider userIdentifierKey!
112
+ console.warn(`Using an automatically generated fingerprint for the Seam userIdentifierKey!
85
113
  The user interface will show warnings when using a fingerprint.
86
114
  This is not recommended because the client session is now bound to this machine and is effectively ephemeral.`)
87
115
  }, [userIdentifierKey])
@@ -98,3 +126,29 @@ This is not recommended because the client session is now bound to this machine
98
126
 
99
127
  return fingerprint
100
128
  }
129
+
130
+ const getQueryKeyPrefixes = ({
131
+ queryKeyPrefix,
132
+ userIdentifierKey,
133
+ publishableKey,
134
+ clientSessionToken,
135
+ }: {
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
+
145
+ if (clientSessionToken != null) {
146
+ return [seamPrefix, clientSessionToken]
147
+ }
148
+
149
+ if (publishableKey != null && userIdentifierKey != null) {
150
+ return [seamPrefix, publishableKey, userIdentifierKey]
151
+ }
152
+
153
+ return [seamPrefix]
154
+ }
@@ -0,0 +1,50 @@
1
+ import type {
2
+ SeamHttpApiError,
3
+ SeamHttpEndpointMutationPaths,
4
+ SeamHttpEndpoints,
5
+ } from '@seamapi/http/connect'
6
+ import {
7
+ useMutation,
8
+ type UseMutationOptions,
9
+ type UseMutationResult,
10
+ } from '@tanstack/react-query'
11
+
12
+ import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'
13
+
14
+ export type UseSeamMutationVariables<T extends SeamHttpEndpointMutationPaths> =
15
+ Parameters<SeamHttpEndpoints[T]>[0]
16
+
17
+ export type UseSeamMutationResult<T extends SeamHttpEndpointMutationPaths> =
18
+ UseMutationResult<
19
+ MutationData<T>,
20
+ SeamHttpApiError,
21
+ UseSeamMutationVariables<T>
22
+ >
23
+
24
+ export function useSeamMutation<T extends SeamHttpEndpointMutationPaths>(
25
+ endpointPath: T,
26
+ options: Parameters<SeamHttpEndpoints[T]>[1] &
27
+ MutationOptions<
28
+ MutationData<T>,
29
+ SeamHttpApiError,
30
+ UseSeamMutationVariables<T>
31
+ > = {}
32
+ ): UseSeamMutationResult<T> {
33
+ const { endpointClient: client } = useSeamClient()
34
+ return useMutation({
35
+ ...options,
36
+ mutationFn: async (variables) => {
37
+ if (client === null) throw new NullSeamClientError()
38
+ // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
39
+ // Type assertion is needed here for performance reasons. The types are correct at runtime.
40
+ const endpoint = client[endpointPath] as (...args: any) => Promise<any>
41
+ return await endpoint(variables, options)
42
+ },
43
+ })
44
+ }
45
+
46
+ type MutationData<T extends SeamHttpEndpointMutationPaths> = Awaited<
47
+ ReturnType<SeamHttpEndpoints[T]>
48
+ >
49
+
50
+ type MutationOptions<X, Y, Z> = Omit<UseMutationOptions<X, Y, Z>, 'mutationFn'>
@@ -1,7 +1,9 @@
1
1
  import type { SeamHttpApiError } from '@seamapi/http/connect'
2
2
  import type { UseQueryResult } from '@tanstack/react-query'
3
3
 
4
- export type UseSeamQueryResult<Field extends string, ResponsePayload> = Omit<
5
- UseQueryResult<ResponsePayload, SeamHttpApiError>,
6
- 'data'
7
- > & { [key in Field]?: ResponsePayload }
4
+ export type UseSeamQueryResultLegacy<
5
+ Field extends string,
6
+ ResponsePayload,
7
+ > = Omit<UseQueryResult<ResponsePayload, SeamHttpApiError>, 'data'> & {
8
+ [key in Field]?: ResponsePayload
9
+ }
@@ -0,0 +1,49 @@
1
+ import type {
2
+ SeamHttpApiError,
3
+ SeamHttpEndpointQueryPaths,
4
+ SeamHttpEndpoints,
5
+ } from '@seamapi/http/connect'
6
+ import {
7
+ useQuery,
8
+ type UseQueryOptions,
9
+ type UseQueryResult,
10
+ } from '@tanstack/react-query'
11
+
12
+ import { useSeamClient } from 'lib/seam/use-seam-client.js'
13
+
14
+ export type UseSeamQueryParameters<T extends SeamHttpEndpointQueryPaths> =
15
+ Parameters<SeamHttpEndpoints[T]>[0]
16
+
17
+ export type UseSeamQueryResult<T extends SeamHttpEndpointQueryPaths> =
18
+ UseQueryResult<QueryData<T>, SeamHttpApiError>
19
+
20
+ export function useSeamQuery<T extends SeamHttpEndpointQueryPaths>(
21
+ endpointPath: T,
22
+ parameters?: UseSeamQueryParameters<T>,
23
+ options: Parameters<SeamHttpEndpoints[T]>[1] &
24
+ QueryOptions<QueryData<T>, SeamHttpApiError> = {}
25
+ ): UseSeamQueryResult<T> {
26
+ const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
27
+ return useQuery({
28
+ enabled: client != null,
29
+ ...options,
30
+ queryKey: [
31
+ ...queryKeyPrefixes,
32
+ ...endpointPath.split('/').filter((v) => v !== ''),
33
+ parameters,
34
+ ],
35
+ queryFn: async () => {
36
+ if (client == null) return null
37
+ // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
38
+ // Type assertion is needed here for performance reasons. The types are correct at runtime.
39
+ const endpoint = client[endpointPath] as (...args: any) => Promise<any>
40
+ return await endpoint(parameters, options)
41
+ },
42
+ })
43
+ }
44
+
45
+ type QueryData<T extends SeamHttpEndpointQueryPaths> = Awaited<
46
+ ReturnType<SeamHttpEndpoints[T]>
47
+ >
48
+
49
+ type QueryOptions<X, Y> = Omit<UseQueryOptions<X, Y>, 'queryKey' | 'queryFn'>
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useLayoutEffect } from 'react'
2
2
 
3
3
  import { useClientSession } from 'lib/seam/client-sessions/use-client-session.js'
4
- import { useSeamContext } from 'lib/seam/SeamProvider.js'
4
+ import { useSeamQueryContext } from 'lib/seam/SeamQueryProvider.js'
5
5
 
6
6
  import type { TelemetryClient } from './client.js'
7
7
  import { useTelemetryContext } from './TelemetryProvider.js'
@@ -23,7 +23,7 @@ export function useComponentTelemetry(name: string): void {
23
23
 
24
24
  export function useUserTelemetry(): void {
25
25
  const telemetry = useTelemetryClient()
26
- const { publishableKey } = useSeamContext()
26
+ const { publishableKey } = useSeamQueryContext()
27
27
  const { clientSession } = useClientSession()
28
28
 
29
29
  // Ensure identify runs earlier than other effects to avoid anonymous telemetry data.
@@ -1,3 +1,3 @@
1
- const seamapiReactVersion = '4.8.2'
1
+ const seamapiReactVersion = '4.9.1'
2
2
 
3
3
  export default seamapiReactVersion