@seamapi/react 5.3.2 → 5.4.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 (65) hide show
  1. package/README.md +66 -3
  2. package/dist/ecosystem.js +14 -14
  3. package/dist/ecosystem.js.map +1 -1
  4. package/dist/elements.js +21042 -19731
  5. package/dist/elements.js.map +1 -1
  6. package/ecosystem-react.d.ts +3 -0
  7. package/ecosystem-react.js +7 -0
  8. package/ecosystem-react.js.map +1 -0
  9. package/lib/seam/components/SupportedDevices/SupportedDevices.js +2 -4
  10. package/lib/seam/components/SupportedDevices/SupportedDevices.js.map +1 -1
  11. package/lib/seam/components/SupportedDevices/presentation.d.ts +1 -0
  12. package/lib/seam/components/SupportedDevices/presentation.js +3 -0
  13. package/lib/seam/components/SupportedDevices/presentation.js.map +1 -1
  14. package/lib/seam/ecosystem/EcosystemProvider.d.ts +4 -2
  15. package/lib/seam/ecosystem/EcosystemProvider.js +9 -6
  16. package/lib/seam/ecosystem/EcosystemProvider.js.map +1 -1
  17. package/lib/seam/ecosystem/client.d.ts +5 -1
  18. package/lib/seam/ecosystem/client.js +4 -2
  19. package/lib/seam/ecosystem/client.js.map +1 -1
  20. package/lib/seam/ecosystem/embed-resolution.d.ts +7 -0
  21. package/lib/seam/ecosystem/embed-resolution.js +18 -0
  22. package/lib/seam/ecosystem/embed-resolution.js.map +1 -0
  23. package/lib/seam/ecosystem/options.d.ts +6 -0
  24. package/lib/seam/ecosystem/options.js +13 -0
  25. package/lib/seam/ecosystem/options.js.map +1 -0
  26. package/lib/seam/ecosystem/prefetch-supported-devices.d.ts +14 -0
  27. package/lib/seam/ecosystem/prefetch-supported-devices.js +79 -0
  28. package/lib/seam/ecosystem/prefetch-supported-devices.js.map +1 -0
  29. package/lib/seam/ecosystem/queries.d.ts +17 -0
  30. package/lib/seam/ecosystem/queries.js +144 -0
  31. package/lib/seam/ecosystem/queries.js.map +1 -0
  32. package/lib/seam/ecosystem/use-ecosystem-device-model-summaries.d.ts +5 -9
  33. package/lib/seam/ecosystem/use-ecosystem-device-model-summaries.js +7 -59
  34. package/lib/seam/ecosystem/use-ecosystem-device-model-summaries.js.map +1 -1
  35. package/lib/seam/ecosystem/use-ecosystem-manufacturers.d.ts +3 -3
  36. package/lib/seam/ecosystem/use-ecosystem-manufacturers.js +7 -41
  37. package/lib/seam/ecosystem/use-ecosystem-manufacturers.js.map +1 -1
  38. package/lib/seam/ecosystem/use-ecosystem-provider-requirements.d.ts +2 -2
  39. package/lib/seam/ecosystem/use-ecosystem-provider-requirements.js +6 -40
  40. package/lib/seam/ecosystem/use-ecosystem-provider-requirements.js.map +1 -1
  41. package/lib/seam/ecosystem/use-embed-key-resolution.d.ts +4 -14
  42. package/lib/seam/ecosystem/use-embed-key-resolution.js +8 -43
  43. package/lib/seam/ecosystem/use-embed-key-resolution.js.map +1 -1
  44. package/lib/version.d.ts +1 -1
  45. package/lib/version.js +1 -1
  46. package/package.json +23 -10
  47. package/server.d.ts +2 -0
  48. package/server.js +3 -0
  49. package/server.js.map +1 -0
  50. package/src/ecosystem-react.ts +17 -0
  51. package/src/lib/ecosystem-element.tsx +4 -1
  52. package/src/lib/seam/components/SupportedDevices/SupportedDevices.tsx +2 -11
  53. package/src/lib/seam/components/SupportedDevices/presentation.ts +11 -0
  54. package/src/lib/seam/ecosystem/EcosystemProvider.tsx +17 -7
  55. package/src/lib/seam/ecosystem/client.ts +14 -2
  56. package/src/lib/seam/ecosystem/embed-resolution.ts +20 -0
  57. package/src/lib/seam/ecosystem/options.ts +21 -0
  58. package/src/lib/seam/ecosystem/prefetch-supported-devices.ts +150 -0
  59. package/src/lib/seam/ecosystem/queries.ts +204 -0
  60. package/src/lib/seam/ecosystem/use-ecosystem-device-model-summaries.ts +10 -79
  61. package/src/lib/seam/ecosystem/use-ecosystem-manufacturers.ts +9 -60
  62. package/src/lib/seam/ecosystem/use-ecosystem-provider-requirements.ts +10 -50
  63. package/src/lib/seam/ecosystem/use-embed-key-resolution.ts +11 -57
  64. package/src/lib/version.ts +1 -1
  65. package/src/server.ts +5 -0
@@ -12,6 +12,10 @@ export interface EcosystemRequestOptions {
12
12
  embedKey?: string | undefined
13
13
  params?: EcosystemQueryParams
14
14
  signal?: AbortSignal
15
+ /** Page origin for server-side embed-key validation. Browsers send their own Origin. */
16
+ origin?: string
17
+ /** Optional server fetch implementation, e.g., Next.js fetch with revalidation. */
18
+ fetch?: typeof globalThis.fetch
15
19
  }
16
20
 
17
21
  /**
@@ -70,16 +74,24 @@ export const getEcosystemUrl = (
70
74
 
71
75
  export const fetchEcosystem = async <T>(
72
76
  path: string,
73
- { endpoint, embedKey, params, signal }: EcosystemRequestOptions
77
+ {
78
+ endpoint,
79
+ embedKey,
80
+ params,
81
+ signal,
82
+ origin,
83
+ fetch = globalThis.fetch,
84
+ }: EcosystemRequestOptions
74
85
  ): Promise<T> => {
75
86
  const url = getEcosystemUrl(endpoint, path, params)
76
87
 
77
88
  const headers: Record<string, string> = { Accept: 'application/json' }
89
+ if (origin != null) headers['Origin'] = origin
78
90
  if (embedKey != null && embedKey.length > 0) {
79
91
  headers['X-Embed-Key'] = embedKey
80
92
  }
81
93
 
82
- const res = await globalThis.fetch(url, {
94
+ const res = await fetch(url, {
83
95
  headers,
84
96
  ...(signal == null ? {} : { signal }),
85
97
  })
@@ -0,0 +1,20 @@
1
+ import type { EmbedResolution } from './types.js'
2
+
3
+ /**
4
+ * The manufacturer an embed key is scoped to, if any.
5
+ * Prefers the `manufacturer_context` Ecosystem returns for manufacturer keys (DEVDB-439),
6
+ * and falls back to the key's own `manufacturer_slug`.
7
+ */
8
+ export const getEmbedKeyManufacturer = (
9
+ resolution: EmbedResolution | null | undefined
10
+ ): string | null => {
11
+ if (resolution == null) return null
12
+ const contextSlug = resolution.manufacturer_context?.manufacturer_slug
13
+ if (contextSlug != null && contextSlug.length > 0) return contextSlug
14
+ const { owner_kind: ownerKind, manufacturer_slug: slug } =
15
+ resolution.embed_key
16
+ if (ownerKind === 'manufacturer' && slug != null && slug.length > 0) {
17
+ return slug
18
+ }
19
+ return null
20
+ }
@@ -0,0 +1,21 @@
1
+ import { defaultEcosystemEndpoint } from './client.js'
2
+ import type { EcosystemOptions } from './EcosystemProvider.js'
3
+
4
+ export const normalizeEcosystemOptions = ({
5
+ embedKey,
6
+ ecosystemEndpoint,
7
+ }: EcosystemOptions): { embedKey: string | undefined; endpoint: string } => {
8
+ const endpoint =
9
+ normalizeOptionalString(ecosystemEndpoint) ?? defaultEcosystemEndpoint
10
+ return {
11
+ embedKey: normalizeOptionalString(embedKey),
12
+ endpoint: endpoint.endsWith('/') ? endpoint : `${endpoint}/`,
13
+ }
14
+ }
15
+
16
+ export const normalizeOptionalString = (
17
+ value: string | undefined
18
+ ): string | undefined => {
19
+ const trimmed = value?.trim()
20
+ return trimmed == null || trimmed.length === 0 ? undefined : trimmed
21
+ }
@@ -0,0 +1,150 @@
1
+ import {
2
+ dehydrate,
3
+ type DehydratedState,
4
+ QueryClient,
5
+ } from '@tanstack/react-query'
6
+
7
+ import {
8
+ applyEmbedExclusions,
9
+ capabilityLabels,
10
+ resolveProviderRequirements,
11
+ scopeDeviceModelsToProvider,
12
+ } from 'lib/seam/components/SupportedDevices/device-models.js'
13
+ import {
14
+ getAccessorySlugs,
15
+ presentDevice,
16
+ } from 'lib/seam/components/SupportedDevices/presentation.js'
17
+ import type { SupportedDevicesProps } from 'lib/seam/components/SupportedDevices/SupportedDevices.js'
18
+
19
+ import type { EcosystemRequestOptions } from './client.js'
20
+ import type { EcosystemOptions } from './EcosystemProvider.js'
21
+ import { getEmbedKeyManufacturer } from './embed-resolution.js'
22
+ import {
23
+ normalizeEcosystemOptions,
24
+ normalizeOptionalString,
25
+ } from './options.js'
26
+ import {
27
+ deviceModelSummariesQuery,
28
+ embedResolutionQuery,
29
+ manufacturersQuery,
30
+ providerRequirementsQuery,
31
+ } from './queries.js'
32
+
33
+ export interface PrefetchSupportedDevicesOptions
34
+ extends EcosystemOptions,
35
+ Pick<
36
+ SupportedDevicesProps,
37
+ 'manufacturer' | 'provider' | 'deviceType' | 'capability'
38
+ >,
39
+ Pick<EcosystemRequestOptions, 'fetch' | 'origin'> {}
40
+
41
+ /**
42
+ * Fetch the complete initial SupportedDevices view before rendering HTML.
43
+ * Pass the result to EcosystemProvider.dehydratedState and use the same key,
44
+ * endpoint and SupportedDevices filters on both server and client.
45
+ * Supply the page origin when using an embed key so its allowlist is checked.
46
+ * Each call owns its query cache; failures throw instead of serializing a loader.
47
+ */
48
+ export async function prefetchSupportedDevices(
49
+ options: PrefetchSupportedDevicesOptions = {}
50
+ ): Promise<DehydratedState> {
51
+ const context = {
52
+ ...normalizeEcosystemOptions(options),
53
+ fetch: options.fetch,
54
+ origin: options.origin,
55
+ }
56
+ if (
57
+ context.embedKey != null &&
58
+ (context.origin == null || context.origin.trim().length === 0)
59
+ ) {
60
+ throw new Error(
61
+ 'prefetchSupportedDevices requires the page origin when using an embed key.'
62
+ )
63
+ }
64
+ if (context.origin != null) {
65
+ const origin = new URL(context.origin)
66
+ if (
67
+ !['http:', 'https:'].includes(origin.protocol) ||
68
+ origin.origin !== context.origin
69
+ ) {
70
+ throw new Error(
71
+ 'Supply origin as an HTTP(S) origin, e.g., https://example.com.'
72
+ )
73
+ }
74
+ }
75
+
76
+ const queryClient = new QueryClient({
77
+ defaultOptions: { queries: { retry: false } },
78
+ })
79
+ try {
80
+ const capability = options.capability?.trim() ?? ''
81
+ const provider = normalizeOptionalString(options.provider) ?? null
82
+ const resolution =
83
+ context.embedKey == null
84
+ ? undefined
85
+ : await queryClient.fetchQuery(
86
+ embedResolutionQuery(context, capability)
87
+ )
88
+ const query = deviceModelSummariesQuery(context, {
89
+ manufacturer:
90
+ normalizeOptionalString(options.manufacturer) ??
91
+ getEmbedKeyManufacturer(resolution) ??
92
+ '',
93
+ provider: provider ?? '',
94
+ deviceType: options.deviceType,
95
+ capability: capability in capabilityLabels ? capability : '',
96
+ })
97
+ const models = applyEmbedExclusions(
98
+ await queryClient.fetchQuery(query),
99
+ resolution?.exclusions
100
+ )
101
+ // Exclusions must apply to serialized data too, not only to visible rows.
102
+ queryClient.setQueryData(query.queryKey, models)
103
+ const providerSlugs = models.flatMap((model) =>
104
+ model.providers.map((provider) => provider.slug)
105
+ )
106
+ const scopedModels = scopeDeviceModelsToProvider(models, provider)
107
+ const manufacturerSlugs = scopedModels.flatMap((model) =>
108
+ model.manufacturer == null ? [] : [model.manufacturer.slug]
109
+ )
110
+ const [providers] = await Promise.all([
111
+ providerSlugs.length === 0
112
+ ? []
113
+ : queryClient.fetchQuery(
114
+ providerRequirementsQuery(context, providerSlugs)
115
+ ),
116
+ manufacturerSlugs.length === 0
117
+ ? []
118
+ : queryClient.fetchQuery(
119
+ manufacturersQuery(context, manufacturerSlugs)
120
+ ),
121
+ ])
122
+ const resolvedModels = resolveProviderRequirements(models, providers)
123
+ const modelsById = new Map(
124
+ resolvedModels.map((model) => [model.device_model_id, model])
125
+ )
126
+ const devices = scopeDeviceModelsToProvider(resolvedModels, provider).map(
127
+ (model) =>
128
+ presentDevice(
129
+ model,
130
+ providers,
131
+ modelsById.get(model.device_model_id),
132
+ provider ?? undefined
133
+ )
134
+ )
135
+ const accessorySlugs = getAccessorySlugs(devices)
136
+ if (accessorySlugs.length > 0) {
137
+ const accessoriesQuery = deviceModelSummariesQuery(context, {
138
+ slugs: accessorySlugs,
139
+ })
140
+ const accessories = await queryClient.fetchQuery(accessoriesQuery)
141
+ queryClient.setQueryData(
142
+ accessoriesQuery.queryKey,
143
+ applyEmbedExclusions(accessories, resolution?.exclusions)
144
+ )
145
+ }
146
+ return dehydrate(queryClient)
147
+ } finally {
148
+ queryClient.clear()
149
+ }
150
+ }
@@ -0,0 +1,204 @@
1
+ import type { FetchQueryOptions } from '@tanstack/react-query'
2
+
3
+ import {
4
+ EcosystemApiError,
5
+ type EcosystemRequestOptions,
6
+ fetchEcosystem,
7
+ } from './client.js'
8
+ import type {
9
+ DeviceModelSummariesListResponse,
10
+ DeviceModelSummary,
11
+ EmbedResolution,
12
+ Manufacturer,
13
+ ManufacturersListResponse,
14
+ ProviderRequirements,
15
+ ProvidersListResponse,
16
+ } from './types.js'
17
+
18
+ type QueryOptions<T> = FetchQueryOptions<T, EcosystemApiError>
19
+
20
+ type QueryContext = Omit<EcosystemRequestOptions, 'params' | 'signal'>
21
+
22
+ export interface DeviceModelSummaryFilters {
23
+ manufacturer?: string
24
+ provider?: string
25
+ deviceType?: string
26
+ capability?: string
27
+ slugs?: string[]
28
+ }
29
+
30
+ // Reuse server data on hydration instead of immediately fetching it again.
31
+ const staleTime = 60_000
32
+
33
+ export const deviceModelSummariesQuery = (
34
+ context: QueryContext,
35
+ {
36
+ manufacturer = '',
37
+ deviceType = '',
38
+ provider = '',
39
+ capability = '',
40
+ slugs = [],
41
+ }: DeviceModelSummaryFilters = {}
42
+ ): QueryOptions<DeviceModelSummary[]> => {
43
+ const params = {
44
+ manufacturer: manufacturer.trim(),
45
+ device_type: deviceType.trim(),
46
+ provider: provider.trim(),
47
+ capability: capability.trim(),
48
+ slugs: [
49
+ ...new Set(slugs.map((slug) => slug.trim()).filter(Boolean)),
50
+ ].sort(),
51
+ }
52
+ return {
53
+ queryKey: [
54
+ 'ecosystem',
55
+ context.endpoint,
56
+ context.embedKey ?? null,
57
+ 'device_models',
58
+ 'summaries',
59
+ 'list',
60
+ params,
61
+ ],
62
+ staleTime,
63
+ queryFn: async ({ signal }) => {
64
+ const models: DeviceModelSummary[] = []
65
+ const cursors = new Set<string>()
66
+ let cursor: string | undefined
67
+ for (let page = 0; page < 20; page++) {
68
+ const res = await fetchEcosystem<DeviceModelSummariesListResponse>(
69
+ 'device_models/summaries/list',
70
+ {
71
+ ...context,
72
+ signal,
73
+ params: { ...params, limit: 500, page_cursor: cursor },
74
+ }
75
+ )
76
+ models.push(...res.device_models)
77
+ if (!res.pagination.has_next_page) return models
78
+ const next = res.pagination.next_page_cursor
79
+ if (next == null || cursors.has(next)) break
80
+ cursors.add(next)
81
+ cursor = next
82
+ }
83
+ throw incompleteCatalog('The device catalog returned incomplete results.')
84
+ },
85
+ }
86
+ }
87
+
88
+ export const embedResolutionQuery = (
89
+ context: QueryContext,
90
+ capability = ''
91
+ ): QueryOptions<EmbedResolution> => {
92
+ const params = { capability: capability.trim() }
93
+ return {
94
+ queryKey: [
95
+ 'ecosystem',
96
+ context.endpoint,
97
+ context.embedKey ?? null,
98
+ 'embed',
99
+ 'resolve',
100
+ params,
101
+ ],
102
+ staleTime,
103
+ queryFn: async ({ signal }) =>
104
+ await fetchEcosystem<EmbedResolution>('embed/resolve', {
105
+ ...context,
106
+ params,
107
+ signal,
108
+ }),
109
+ }
110
+ }
111
+
112
+ export const providerRequirementsQuery = (
113
+ context: QueryContext,
114
+ slugs: string[]
115
+ ): QueryOptions<ProviderRequirements[]> => {
116
+ const sortedSlugs = [...new Set(slugs)].sort()
117
+ return {
118
+ queryKey: [
119
+ 'ecosystem',
120
+ context.endpoint,
121
+ context.embedKey ?? null,
122
+ 'providers',
123
+ 'list',
124
+ { slugs: sortedSlugs },
125
+ ],
126
+ staleTime,
127
+ queryFn: async ({ signal }) => {
128
+ const providers: ProviderRequirements[] = []
129
+ const cursors = new Set<string>()
130
+ let cursor: string | undefined
131
+ for (let page = 0; page < 10; page++) {
132
+ const res = await fetchEcosystem<ProvidersListResponse>(
133
+ 'providers/list',
134
+ {
135
+ ...context,
136
+ signal,
137
+ params: { slugs: sortedSlugs, limit: 500, page_cursor: cursor },
138
+ }
139
+ )
140
+ providers.push(...res.providers)
141
+ if (!res.pagination.has_next_page) return providers
142
+ const next = res.pagination.next_page_cursor
143
+ if (next == null || cursors.has(next)) break
144
+ cursors.add(next)
145
+ cursor = next
146
+ }
147
+ throw incompleteCatalog(
148
+ 'The provider catalog returned incomplete requirements.'
149
+ )
150
+ },
151
+ }
152
+ }
153
+
154
+ export const manufacturersQuery = (
155
+ context: QueryContext,
156
+ slugs?: string[]
157
+ ): QueryOptions<Manufacturer[]> => {
158
+ const sortedSlugs =
159
+ slugs == null
160
+ ? null
161
+ : [...new Set(slugs)].sort((a, b) => a.localeCompare(b))
162
+ return {
163
+ queryKey: [
164
+ 'ecosystem',
165
+ context.endpoint,
166
+ context.embedKey ?? null,
167
+ 'manufacturers',
168
+ 'list',
169
+ { slugs: sortedSlugs },
170
+ ],
171
+ staleTime,
172
+ queryFn: async ({ signal }) => {
173
+ const manufacturers: Manufacturer[] = []
174
+ const cursors = new Set<string>()
175
+ let cursor: string | undefined
176
+ for (let page = 0; page < 10; page++) {
177
+ const res = await fetchEcosystem<ManufacturersListResponse>(
178
+ 'manufacturers/list',
179
+ {
180
+ ...context,
181
+ signal,
182
+ params: {
183
+ slugs: sortedSlugs ?? undefined,
184
+ limit: 500,
185
+ page_cursor: cursor,
186
+ },
187
+ }
188
+ )
189
+ manufacturers.push(...res.manufacturers)
190
+ if (!res.pagination.has_next_page) return manufacturers
191
+ const next = res.pagination.next_page_cursor
192
+ if (next == null || cursors.has(next)) break
193
+ cursors.add(next)
194
+ cursor = next
195
+ }
196
+ throw incompleteCatalog(
197
+ 'The manufacturer catalog returned incomplete results.'
198
+ )
199
+ },
200
+ }
201
+ }
202
+
203
+ const incompleteCatalog = (message: string): EcosystemApiError =>
204
+ new EcosystemApiError({ status: 502, type: 'pagination_incomplete', message })
@@ -1,16 +1,13 @@
1
1
  import { useQuery } from '@tanstack/react-query'
2
2
 
3
- import { EcosystemApiError, fetchEcosystem } from 'lib/seam/ecosystem/client.js'
4
- import { useEcosystemContext } from 'lib/seam/ecosystem/EcosystemProvider.js'
5
- import type {
6
- DeviceModelSummariesListResponse,
7
- DeviceModelSummary,
8
- } from 'lib/seam/ecosystem/types.js'
9
- import type { UseEcosystemDeviceModelsParams } from 'lib/seam/ecosystem/use-ecosystem-device-models.js'
3
+ import { useEcosystemContext } from './EcosystemProvider.js'
4
+ import { deviceModelSummariesQuery } from './queries.js'
5
+ import type { DeviceModelSummary } from './types.js'
6
+ import type { UseEcosystemDeviceModelsParams } from './use-ecosystem-device-models.js'
10
7
  import {
11
8
  retryEcosystemQuery,
12
9
  type UseEcosystemQueryResult,
13
- } from 'lib/seam/ecosystem/use-ecosystem-query-result.js'
10
+ } from './use-ecosystem-query-result.js'
14
11
 
15
12
  export type UseEcosystemDeviceModelSummariesParams =
16
13
  UseEcosystemDeviceModelsParams & {
@@ -20,85 +17,19 @@ export type UseEcosystemDeviceModelSummariesParams =
20
17
 
21
18
  export type UseEcosystemDeviceModelSummariesData = DeviceModelSummary[]
22
19
 
23
- const pageSize = 500
24
- const maxPages = 20
25
-
26
- /**
27
- * Published device models in the slim summary shape from Ecosystem
28
- * `GET /v1/device_models/summaries/list` (no commerce data),
29
- * following pagination to return every matching model.
30
- */
20
+ /** Published slim summaries, following pagination to return every matching model. */
31
21
  export function useEcosystemDeviceModelSummaries({
32
- manufacturer = '',
33
- deviceType = '',
34
- provider = '',
35
- capability = '',
36
- slugs = [],
37
22
  enabled = true,
23
+ ...filters
38
24
  }: UseEcosystemDeviceModelSummariesParams = {}): UseEcosystemQueryResult<
39
25
  'deviceModels',
40
26
  UseEcosystemDeviceModelSummariesData
41
27
  > {
42
- const { embedKey, endpoint } = useEcosystemContext()
43
-
44
- const params = {
45
- manufacturer: manufacturer.trim(),
46
- device_type: deviceType.trim(),
47
- provider: provider.trim(),
48
- capability: capability.trim(),
49
- slugs: [
50
- ...new Set(slugs.map((slug) => slug.trim()).filter(Boolean)),
51
- ].sort(),
52
- }
53
-
54
- const { data, ...rest } = useQuery<
55
- UseEcosystemDeviceModelSummariesData,
56
- EcosystemApiError
57
- >({
28
+ const context = useEcosystemContext()
29
+ const { data, ...rest } = useQuery({
30
+ ...deviceModelSummariesQuery(context, filters),
58
31
  enabled,
59
- queryKey: [
60
- 'ecosystem',
61
- endpoint,
62
- embedKey ?? null,
63
- 'device_models',
64
- 'summaries',
65
- 'list',
66
- params,
67
- ],
68
- queryFn: async ({ signal }) => {
69
- const deviceModels: DeviceModelSummary[] = []
70
- let pageCursor: string | undefined
71
- for (let page = 0; page < maxPages; page++) {
72
- const res = await fetchEcosystem<DeviceModelSummariesListResponse>(
73
- 'device_models/summaries/list',
74
- {
75
- endpoint,
76
- embedKey,
77
- params: { ...params, limit: pageSize, page_cursor: pageCursor },
78
- signal,
79
- }
80
- )
81
- deviceModels.push(...res.device_models)
82
- const { has_next_page: hasNextPage, next_page_cursor: nextPageCursor } =
83
- res.pagination
84
- if (!hasNextPage) return deviceModels
85
- if (nextPageCursor == null || nextPageCursor === pageCursor) {
86
- throw new EcosystemApiError({
87
- status: 502,
88
- type: 'pagination_incomplete',
89
- message: 'The device catalog returned an invalid page cursor.',
90
- })
91
- }
92
- pageCursor = nextPageCursor
93
- }
94
- throw new EcosystemApiError({
95
- status: 502,
96
- type: 'pagination_incomplete',
97
- message: 'The device catalog exceeded the supported number of pages.',
98
- })
99
- },
100
32
  retry: retryEcosystemQuery,
101
33
  })
102
-
103
34
  return { ...rest, deviceModels: data }
104
35
  }
@@ -1,18 +1,12 @@
1
1
  import { useQuery } from '@tanstack/react-query'
2
2
 
3
- import {
4
- type EcosystemApiError,
5
- fetchEcosystem,
6
- } from 'lib/seam/ecosystem/client.js'
7
- import { useEcosystemContext } from 'lib/seam/ecosystem/EcosystemProvider.js'
8
- import type {
9
- Manufacturer,
10
- ManufacturersListResponse,
11
- } from 'lib/seam/ecosystem/types.js'
3
+ import { useEcosystemContext } from './EcosystemProvider.js'
4
+ import { manufacturersQuery } from './queries.js'
5
+ import type { Manufacturer } from './types.js'
12
6
  import {
13
7
  retryEcosystemQuery,
14
8
  type UseEcosystemQueryResult,
15
- } from 'lib/seam/ecosystem/use-ecosystem-query-result.js'
9
+ } from './use-ecosystem-query-result.js'
16
10
 
17
11
  export interface UseEcosystemManufacturersParams {
18
12
  /** Only manufacturers with these slugs. */
@@ -23,10 +17,7 @@ export interface UseEcosystemManufacturersParams {
23
17
 
24
18
  export type UseEcosystemManufacturersData = Manufacturer[]
25
19
 
26
- const pageSize = 500
27
- const maxPages = 10
28
-
29
- /** Published manufacturers from Ecosystem `GET /v1/manufacturers/list`. */
20
+ /** Published manufacturers, following pagination to return every matching manufacturer. */
30
21
  export function useEcosystemManufacturers({
31
22
  slugs,
32
23
  enabled = true,
@@ -34,53 +25,11 @@ export function useEcosystemManufacturers({
34
25
  'manufacturers',
35
26
  UseEcosystemManufacturersData
36
27
  > {
37
- const { embedKey, endpoint } = useEcosystemContext()
38
-
39
- const sortedSlugs =
40
- slugs == null
41
- ? null
42
- : [...new Set(slugs)].sort((a, b) => a.localeCompare(b))
43
-
44
- const { data, ...rest } = useQuery<
45
- UseEcosystemManufacturersData,
46
- EcosystemApiError
47
- >({
48
- enabled: enabled && (sortedSlugs == null || sortedSlugs.length > 0),
49
- queryKey: [
50
- 'ecosystem',
51
- endpoint,
52
- embedKey ?? null,
53
- 'manufacturers',
54
- 'list',
55
- { slugs: sortedSlugs },
56
- ],
57
- queryFn: async ({ signal }) => {
58
- const manufacturers: Manufacturer[] = []
59
- let pageCursor: string | undefined
60
- for (let page = 0; page < maxPages; page++) {
61
- const res = await fetchEcosystem<ManufacturersListResponse>(
62
- 'manufacturers/list',
63
- {
64
- endpoint,
65
- embedKey,
66
- params: {
67
- slugs: sortedSlugs ?? undefined,
68
- limit: pageSize,
69
- page_cursor: pageCursor,
70
- },
71
- signal,
72
- }
73
- )
74
- manufacturers.push(...res.manufacturers)
75
- const { has_next_page: hasNextPage, next_page_cursor: nextPageCursor } =
76
- res.pagination
77
- if (!hasNextPage || nextPageCursor == null) break
78
- pageCursor = nextPageCursor
79
- }
80
- return manufacturers
81
- },
28
+ const context = useEcosystemContext()
29
+ const { data, ...rest } = useQuery({
30
+ ...manufacturersQuery(context, slugs),
31
+ enabled: enabled && (slugs == null || slugs.length > 0),
82
32
  retry: retryEcosystemQuery,
83
33
  })
84
-
85
34
  return { ...rest, manufacturers: data }
86
35
  }