@seamapi/react 4.8.1 → 4.9.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 (84) hide show
  1. package/README.md +17 -2
  2. package/dist/elements.js +22003 -18123
  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 +32 -0
  11. package/lib/seam/SeamQueryProvider.js +127 -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/filters.js +3 -1
  30. package/lib/seam/filters.js.map +1 -1
  31. package/lib/seam/index.d.ts +2 -0
  32. package/lib/seam/index.js +2 -0
  33. package/lib/seam/index.js.map +1 -1
  34. package/lib/seam/noise-sensors/use-noise-thresholds.d.ts +2 -2
  35. package/lib/seam/thermostats/use-create-thermostat-climate-preset.js +1 -0
  36. package/lib/seam/thermostats/use-create-thermostat-climate-preset.js.map +1 -1
  37. package/lib/seam/thermostats/use-delete-thermostat-climate-preset.d.ts +2 -2
  38. package/lib/seam/thermostats/use-update-thermostat-climate-preset.js +1 -0
  39. package/lib/seam/thermostats/use-update-thermostat-climate-preset.js.map +1 -1
  40. package/lib/seam/use-seam-client.d.ts +2 -1
  41. package/lib/seam/use-seam-client.js +23 -9
  42. package/lib/seam/use-seam-client.js.map +1 -1
  43. package/lib/seam/use-seam-mutation.d.ts +8 -0
  44. package/lib/seam/use-seam-mutation.js +17 -0
  45. package/lib/seam/use-seam-mutation.js.map +1 -0
  46. package/lib/seam/use-seam-query-result.d.ts +1 -1
  47. package/lib/seam/use-seam-query.d.ts +8 -0
  48. package/lib/seam/use-seam-query.js +19 -0
  49. package/lib/seam/use-seam-query.js.map +1 -0
  50. package/lib/telemetry/hooks.js +2 -2
  51. package/lib/telemetry/hooks.js.map +1 -1
  52. package/lib/ui/DateTimePicker/DateTimePicker.d.ts +1 -1
  53. package/lib/version.d.ts +1 -1
  54. package/lib/version.js +1 -1
  55. package/package.json +5 -4
  56. package/src/lib/index.ts +1 -0
  57. package/src/lib/seam/SeamProvider.tsx +28 -183
  58. package/src/lib/seam/SeamQueryProvider.tsx +248 -0
  59. package/src/lib/seam/access-codes/use-access-code.ts +2 -2
  60. package/src/lib/seam/access-codes/use-access-codes.ts +2 -2
  61. package/src/lib/seam/access-codes/use-create-access-code.ts +7 -32
  62. package/src/lib/seam/access-codes/use-generate-access-code-code.ts +2 -2
  63. package/src/lib/seam/client-sessions/use-client-session.ts +2 -2
  64. package/src/lib/seam/components/SupportedDeviceTable/use-device-model.ts +2 -2
  65. package/src/lib/seam/components/SupportedDeviceTable/use-device-models.ts +2 -2
  66. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturer.ts +2 -2
  67. package/src/lib/seam/components/SupportedDeviceTable/use-manufacturers.ts +2 -2
  68. package/src/lib/seam/connected-accounts/use-connected-account.ts +2 -2
  69. package/src/lib/seam/devices/use-device-providers.ts +2 -2
  70. package/src/lib/seam/devices/use-device.ts +2 -2
  71. package/src/lib/seam/devices/use-devices.ts +2 -2
  72. package/src/lib/seam/events/use-events.ts +2 -2
  73. package/src/lib/seam/filters.ts +3 -1
  74. package/src/lib/seam/index.ts +2 -0
  75. package/src/lib/seam/noise-sensors/use-noise-thresholds.ts +2 -2
  76. package/src/lib/seam/thermostats/use-create-thermostat-climate-preset.ts +1 -0
  77. package/src/lib/seam/thermostats/use-delete-thermostat-climate-preset.ts +2 -2
  78. package/src/lib/seam/thermostats/use-update-thermostat-climate-preset.ts +1 -0
  79. package/src/lib/seam/use-seam-client.ts +29 -10
  80. package/src/lib/seam/use-seam-mutation.ts +50 -0
  81. package/src/lib/seam/use-seam-query-result.ts +6 -4
  82. package/src/lib/seam/use-seam-query.ts +45 -0
  83. package/src/lib/telemetry/hooks.ts +2 -2
  84. package/src/lib/version.ts +1 -1
@@ -1,12 +1,13 @@
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
10
11
  isPending: boolean
11
12
  isError: boolean
12
13
  error: unknown
@@ -17,12 +18,14 @@ export function useSeamClient(): {
17
18
  publishableKey,
18
19
  clientSessionToken,
19
20
  ...context
20
- } = useSeamContext()
21
+ } = useSeamQueryContext()
21
22
  const userIdentifierKey = useUserIdentifierKeyOrFingerprint(
22
23
  clientSessionToken != null ? '' : context.userIdentifierKey
23
24
  )
24
25
 
25
- const { isPending, isError, error, data } = useQuery<SeamHttp>({
26
+ const { isPending, isError, error, data } = useQuery<
27
+ [SeamHttp, SeamHttpEndpoints]
28
+ >({
26
29
  queryKey: [
27
30
  'client',
28
31
  {
@@ -34,13 +37,19 @@ export function useSeamClient(): {
34
37
  },
35
38
  ],
36
39
  queryFn: async () => {
37
- if (client != null) return client
40
+ if (client != null)
41
+ return [client, SeamHttpEndpoints.fromClient(client.client)]
38
42
 
39
43
  if (clientSessionToken != null) {
40
- return SeamHttp.fromClientSessionToken(
44
+ const clientSessionTokenClient = SeamHttp.fromClientSessionToken(
41
45
  clientSessionToken,
42
46
  clientOptions
43
47
  )
48
+
49
+ return [
50
+ clientSessionTokenClient,
51
+ SeamHttpEndpoints.fromClient(clientSessionTokenClient.client),
52
+ ]
44
53
  }
45
54
 
46
55
  if (publishableKey == null) {
@@ -49,15 +58,25 @@ export function useSeamClient(): {
49
58
  )
50
59
  }
51
60
 
52
- return await SeamHttp.fromPublishableKey(
61
+ const publishableKeyClient = await SeamHttp.fromPublishableKey(
53
62
  publishableKey,
54
63
  userIdentifierKey,
55
64
  clientOptions
56
65
  )
66
+ return [
67
+ publishableKeyClient,
68
+ SeamHttpEndpoints.fromClient(publishableKeyClient.client),
69
+ ]
57
70
  },
58
71
  })
59
72
 
60
- return { client: data ?? null, isPending, isError, error }
73
+ return {
74
+ client: data?.[0] ?? null,
75
+ endpointClient: data?.[1] ?? null,
76
+ isPending,
77
+ isError,
78
+ error,
79
+ }
61
80
  }
62
81
 
63
82
  export class NullSeamClientError extends Error {
@@ -65,7 +84,7 @@ export class NullSeamClientError extends Error {
65
84
  super(
66
85
  [
67
86
  'Attempted to use a null Seam client.',
68
- 'Either a hook using useSeamClient was called outside of a SeamProvider,',
87
+ 'Either a hook using useSeamClient was called outside of a SeamProvider or SeamQueryProvider,',
69
88
  'or there was an error when creating the Seam client in useSeamClient,',
70
89
  'or useSeamClient is still loading the client.',
71
90
  ].join(' ')
@@ -81,7 +100,7 @@ function useUserIdentifierKeyOrFingerprint(
81
100
  useEffect(() => {
82
101
  if (userIdentifierKey != null) return
83
102
  // eslint-disable-next-line no-console
84
- console.warn(`Using an automatically generated fingerprint for the SeamProvider userIdentifierKey!
103
+ console.warn(`Using an automatically generated fingerprint for the Seam userIdentifierKey!
85
104
  The user interface will show warnings when using a fingerprint.
86
105
  This is not recommended because the client session is now bound to this machine and is effectively ephemeral.`)
87
106
  }, [userIdentifierKey])
@@ -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,45 @@
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 } = useSeamClient()
27
+ return useQuery({
28
+ enabled: client != null,
29
+ ...options,
30
+ queryKey: [endpointPath, parameters],
31
+ queryFn: async () => {
32
+ if (client == null) return null
33
+ // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
34
+ // Type assertion is needed here for performance reasons. The types are correct at runtime.
35
+ const endpoint = client[endpointPath] as (...args: any) => Promise<any>
36
+ return await endpoint(parameters, options)
37
+ },
38
+ })
39
+ }
40
+
41
+ type QueryData<T extends SeamHttpEndpointQueryPaths> = Awaited<
42
+ ReturnType<SeamHttpEndpoints[T]>
43
+ >
44
+
45
+ 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.1'
1
+ const seamapiReactVersion = '4.9.0'
2
2
 
3
3
  export default seamapiReactVersion