@shop-on/theme-server 0.10.0 → 0.11.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 (168) hide show
  1. package/dist/account-state.js +1 -2
  2. package/dist/account.d.ts +1 -3
  3. package/dist/account.js +1 -3
  4. package/dist/activity.d.ts +1 -3
  5. package/dist/activity.js +1 -2
  6. package/dist/analytics-proxy.d.ts +8 -14
  7. package/dist/analytics-proxy.js +111 -28
  8. package/dist/analytics-script-proxy.d.ts +1 -5
  9. package/dist/analytics-script-proxy.js +4 -14
  10. package/dist/api-contract/src/addresses.js +32 -0
  11. package/dist/api-contract/src/areas.js +49 -0
  12. package/dist/api-contract/src/custom-data.d.ts +31 -0
  13. package/dist/api-contract/src/custom-data.js +303 -0
  14. package/dist/api-contract/src/ids.js +110 -0
  15. package/dist/api-contract/src/image.js +65 -0
  16. package/dist/api-contract/src/limits.js +44 -0
  17. package/dist/api-contract/src/media.js +13 -0
  18. package/dist/api-contract/src/pickup.js +11 -0
  19. package/dist/api-contract/src/recommendations.js +4 -0
  20. package/dist/api-contract/src/shops.js +27 -0
  21. package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
  22. package/dist/api-contract/src/storefront/analytics.js +138 -0
  23. package/dist/api-contract/src/storefront/areas.d.ts +2 -0
  24. package/dist/api-contract/src/storefront/cart.d.ts +132 -0
  25. package/dist/api-contract/src/storefront/cart.js +248 -0
  26. package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
  27. package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
  28. package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
  29. package/dist/api-contract/src/storefront/checkout.js +624 -0
  30. package/dist/api-contract/src/storefront/client.d.ts +3 -0
  31. package/dist/api-contract/src/storefront/client.js +6 -0
  32. package/dist/api-contract/src/storefront/content.d.ts +3 -0
  33. package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
  34. package/dist/api-contract/src/storefront/custom-data.js +27 -0
  35. package/dist/api-contract/src/storefront/customer.d.ts +2 -0
  36. package/dist/api-contract/src/storefront/customer.js +261 -0
  37. package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
  38. package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
  39. package/dist/api-contract/src/storefront/forms.d.ts +2 -0
  40. package/dist/api-contract/src/storefront/forms.js +82 -0
  41. package/dist/api-contract/src/storefront/index.d.ts +20956 -0
  42. package/dist/api-contract/src/storefront/orders.d.ts +2 -0
  43. package/dist/api-contract/src/storefront/policies.d.ts +2 -0
  44. package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
  45. package/dist/api-contract/src/storefront/procedure.js +29 -0
  46. package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
  47. package/dist/api-contract/src/storefront/search.d.ts +3 -0
  48. package/dist/api-contract/src/storefront/seo.d.ts +2 -0
  49. package/dist/api-contract/src/storefront/seo.js +132 -0
  50. package/dist/api-contract/src/storefront/shop.d.ts +2 -0
  51. package/dist/api-contract/src/storefront/shop.js +101 -0
  52. package/dist/api-contract/src/taxonomy.js +5 -0
  53. package/dist/api-contract/src/text.js +15 -0
  54. package/dist/api-contract/src/theme-documents.js +13 -0
  55. package/dist/api-contract/src/theme-translations.d.ts +14 -0
  56. package/dist/api-contract/src/theme-translations.js +30 -0
  57. package/dist/api-contract/src/video.js +19 -0
  58. package/dist/availability.d.ts +6 -3
  59. package/dist/availability.js +11 -5
  60. package/dist/blog.d.ts +53 -59
  61. package/dist/blog.js +197 -497
  62. package/dist/cache.d.ts +5 -6
  63. package/dist/cache.js +1 -2
  64. package/dist/canonical-host.d.ts +4 -6
  65. package/dist/canonical-host.js +5 -6
  66. package/dist/card.js +159 -0
  67. package/dist/cart-customer.d.ts +1 -3
  68. package/dist/cart-customer.js +64 -105
  69. package/dist/cart-mutations.d.ts +43 -77
  70. package/dist/cart-mutations.js +106 -232
  71. package/dist/cart-recommendations.d.ts +1 -3
  72. package/dist/cart-recommendations.js +1 -2
  73. package/dist/cart-session.d.ts +1 -3
  74. package/dist/cart-session.js +1 -2
  75. package/dist/cart.d.ts +63 -224
  76. package/dist/cart.js +70 -197
  77. package/dist/checkout.d.ts +13 -17
  78. package/dist/checkout.js +75 -101
  79. package/dist/coalesce.js +57 -0
  80. package/dist/collection.d.ts +89 -298
  81. package/dist/collection.js +210 -656
  82. package/dist/contact.d.ts +20 -10
  83. package/dist/contact.js +36 -30
  84. package/dist/content-version.d.ts +37 -21
  85. package/dist/content-version.js +64 -42
  86. package/dist/contract-kit/src/client.d.ts +12 -0
  87. package/dist/contract-kit/src/client.js +156 -0
  88. package/dist/contract-kit/src/errors.d.ts +1 -0
  89. package/dist/contract-kit/src/errors.js +81 -0
  90. package/dist/contract-kit/src/ids.d.ts +1 -0
  91. package/dist/contract-kit/src/ids.js +118 -0
  92. package/dist/contract-kit/src/index.d.ts +7 -0
  93. package/dist/contract-kit/src/money.d.ts +1 -0
  94. package/dist/contract-kit/src/money.js +10 -0
  95. package/dist/contract-kit/src/pagination.d.ts +1 -0
  96. package/dist/contract-kit/src/procedure.d.ts +69 -0
  97. package/dist/contract-kit/src/procedure.js +16 -0
  98. package/dist/contract-kit/src/realtime.d.ts +1 -0
  99. package/dist/cookie-secret.js +1 -2
  100. package/dist/csrf.js +1 -2
  101. package/dist/customer-handoff.d.ts +1 -3
  102. package/dist/customer-handoff.js +10 -25
  103. package/dist/customer-session.d.ts +1 -3
  104. package/dist/customer-session.js +1 -2
  105. package/dist/customer.d.ts +2 -3
  106. package/dist/customer.js +27 -44
  107. package/dist/files.d.ts +49 -10
  108. package/dist/files.js +106 -89
  109. package/dist/fonts.d.ts +3 -5
  110. package/dist/fonts.js +16 -35
  111. package/dist/html.d.ts +1 -3
  112. package/dist/html.js +1 -2
  113. package/dist/liquid.d.ts +4 -10
  114. package/dist/liquid.js +25 -32
  115. package/dist/listing.js +79 -0
  116. package/dist/marketing-cookies.d.ts +1 -3
  117. package/dist/marketing-cookies.js +1 -2
  118. package/dist/menu.d.ts +46 -155
  119. package/dist/menu.js +51 -61
  120. package/dist/metafields.d.ts +9 -5
  121. package/dist/metafields.js +10 -38
  122. package/dist/metaobject.d.ts +10 -5
  123. package/dist/metaobject.js +43 -66
  124. package/dist/money.d.ts +7 -0
  125. package/dist/money.js +18 -0
  126. package/dist/newsletter.d.ts +19 -10
  127. package/dist/newsletter.js +31 -25
  128. package/dist/page.d.ts +13 -17
  129. package/dist/page.js +49 -105
  130. package/dist/password-session.d.ts +1 -3
  131. package/dist/password-session.js +1 -2
  132. package/dist/password.d.ts +25 -18
  133. package/dist/password.js +41 -59
  134. package/dist/policy.d.ts +7 -7
  135. package/dist/policy.js +19 -55
  136. package/dist/product.d.ts +42 -67
  137. package/dist/product.js +144 -644
  138. package/dist/read-ahead.d.ts +18 -0
  139. package/dist/read-ahead.js +32 -0
  140. package/dist/recommendations.d.ts +6 -12
  141. package/dist/recommendations.js +36 -60
  142. package/dist/review.d.ts +134 -94
  143. package/dist/review.js +257 -469
  144. package/dist/search.d.ts +59 -64
  145. package/dist/search.js +170 -469
  146. package/dist/seo-context.d.ts +1 -3
  147. package/dist/seo-context.js +4 -5
  148. package/dist/seo-documents.d.ts +50 -0
  149. package/dist/seo-documents.js +149 -0
  150. package/dist/shop-seo.d.ts +3 -5
  151. package/dist/shop-seo.js +7 -26
  152. package/dist/shop.d.ts +38 -42
  153. package/dist/shop.js +37 -51
  154. package/dist/store-availability.d.ts +12 -14
  155. package/dist/store-availability.js +18 -56
  156. package/dist/storefront.js +199 -0
  157. package/dist/structured-data.d.ts +3 -28
  158. package/dist/structured-data.js +19 -49
  159. package/dist/trace.js +1 -2
  160. package/dist/translations.d.ts +26 -31
  161. package/dist/translations.js +55 -28
  162. package/package.json +16 -14
  163. package/dist/graphql-env.d.ts +0 -22778
  164. package/dist/graphql.d.ts +0 -64
  165. package/dist/graphql.js +0 -344
  166. package/dist/schema.storefront.graphql +0 -10483
  167. package/dist/seo-proxy.d.ts +0 -11
  168. package/dist/seo-proxy.js +0 -26
package/dist/search.js CHANGED
@@ -1,523 +1,224 @@
1
- import { graphql, request } from "./graphql.js";
2
- import { toFileInfo } from "./files.js";
3
- import { metafieldKeysVariables } from "./product.js";
4
- import { CollectionImageFragment, CollectionProductNodeFragment, toCollectionProduct, toPriceBounds, variantsVariable } from "./collection.js";
5
- //#region src/search.ts
6
- const SEARCH_PAGE_SIZE = 24;
7
- const FetchProductSearchPageQuery = graphql(`
8
- query FetchProductSearchPage(
9
- $query: String!
10
- $first: Int!
11
- $mediaFirst: Int!
12
- $includeOptions: Boolean!
13
- $includeVariants: Boolean = false
14
- $withMetafields: Boolean = false
15
- $metafieldFirst: Int = 0
16
- $metafieldKeys: [MetafieldKeyInput!] = []
17
- $after: String
18
- $filter: CollectionProductFilterInput
19
- $sort: ProductSearchSortEnum
20
- ) {
21
- productSearch(query: $query, first: $first, after: $after, filter: $filter, sort: $sort) {
22
- totalCount
23
- pageInfo {
24
- endCursor
25
- hasNextPage
26
- }
27
- nodes {
28
- ...CollectionProductNode
29
- }
30
- }
31
- productSearchFacets(query: $query, filter: $filter) {
32
- namespace
33
- key
34
- label
35
- values {
36
- value
37
- label
38
- count
39
- selected
40
- swatch {
41
- hex
42
- image {
43
- __typename
44
- ... on ShopMediaImage {
45
- url(transform: { maxWidth: 128, maxHeight: 128 })
46
- }
47
- }
48
- }
49
- }
50
- # Present only on the price facet — min/max bounds for the slider.
51
- range {
52
- min
53
- max
54
- }
55
- }
56
- # Unfiltered min/max default-variant price across the query matches, for
57
- # seeding the price-filter slider's bounds (no $filter, so the slider
58
- # endpoints stay stable as the buyer toggles filters).
59
- productSearchPriceRange(query: $query) {
60
- min
61
- max
62
- }
63
- }
64
- `, [CollectionProductNodeFragment]);
65
- function toMoney(m) {
66
- return {
67
- amount: Number(m.amount),
68
- currencyCode: m.currencyCode
69
- };
1
+ import { cardShape, toCard } from "./card.js";
2
+ import { imageFileInfo } from "./files.js";
3
+ import { listingCard, listingInput, pageInput, toCollectionListItem, toFacets, toPriceBounds } from "./listing.js";
4
+ import { toMoney } from "./money.js";
5
+ import { orFallback, storefront } from "./storefront.js";
6
+ // src/search.ts
7
+ /** A query as the api reads it; empty when there is nothing to search for. */
8
+ function searchQuery(query) {
9
+ return query.trim().slice(0, 200).trim();
70
10
  }
71
- /** Server-driven /search page. Returns `null` for blank query (caller
72
- * route renders the empty-prompt UI) or on GraphQL error. Uncached because
73
- * filter/sort/cursor permutations would blow the TTL cache. */
11
+ /** `RELEVANCE` is the api's `featured` on a search: the merchant's pins for
12
+ * the query first, then the best match. */
13
+ const SEARCH_SORT = {
14
+ RELEVANCE: "featured",
15
+ CREATED_DESC: "newest",
16
+ TITLE_ASC: "title_asc",
17
+ TITLE_DESC: "title_desc",
18
+ PRICE_ASC: "price_asc",
19
+ PRICE_DESC: "price_desc",
20
+ BEST_SELLING: "best_selling"
21
+ };
22
+ const SEARCH_PAGE_SIZE = 24;
23
+ /** Server-driven /search page: one page of the matches with their facets.
24
+ * `null` for a blank query (the route renders the empty prompt) and on a
25
+ * failed read. Uncached because query/filter/sort/page permutations would
26
+ * blow the TTL cache. */
74
27
  const fetchProductSearchPage = async (data) => {
75
- const query = data.query.trim();
28
+ const query = searchQuery(data.query);
76
29
  if (!query) return null;
77
- const { filter, sort, after } = data;
78
- const first = data.first ?? SEARCH_PAGE_SIZE;
79
- const mediaFirst = data.mediaFirst ?? 5;
80
- const includeOptions = data.includeOptions ?? true;
30
+ const input = listingInput(data.filter, data.page ?? 1, data.first ?? SEARCH_PAGE_SIZE);
81
31
  try {
82
- const res = await request(FetchProductSearchPageQuery, {
32
+ const listing = await storefront(data.locale).search.products({
83
33
  query,
84
- first,
85
- mediaFirst,
86
- includeOptions,
87
- ...variantsVariable(data.includeVariants),
88
- ...metafieldKeysVariables(data.metafieldKeys),
89
- after: after ?? null,
90
- filter: filter ?? null,
91
- sort: sort ?? null
92
- }, data.locale ? { acceptLanguage: data.locale } : void 0);
93
- const conn = res.productSearch;
94
- const products = conn.nodes.map(toCollectionProduct);
95
- const facets = res.productSearchFacets.map((f) => ({
96
- namespace: f.namespace,
97
- key: f.key,
98
- label: f.label,
99
- values: f.values.map((v) => ({
100
- value: v.value,
101
- label: v.label,
102
- count: v.count,
103
- selected: v.selected,
104
- swatch: v.swatch ? {
105
- hex: v.swatch.hex ?? null,
106
- imageUrl: v.swatch.image?.__typename === "ShopMediaImage" ? v.swatch.image.url : null
107
- } : null
108
- })),
109
- range: f.range ? toPriceBounds(f.range) : null
110
- }));
34
+ ...input,
35
+ sort: SEARCH_SORT[data.sort ?? "RELEVANCE"],
36
+ card: listingCard({
37
+ mediaFirst: data.mediaFirst ?? 5,
38
+ includeOptions: data.includeOptions ?? true,
39
+ metafieldKeys: data.metafieldKeys
40
+ })
41
+ });
111
42
  return {
112
43
  query,
113
- total_count: conn.totalCount,
114
- page_info: {
115
- end_cursor: conn.pageInfo.endCursor ?? null,
116
- has_next_page: conn.pageInfo.hasNextPage
117
- },
118
- products,
119
- facets,
120
- price_bounds: toPriceBounds(res.productSearchPriceRange)
44
+ total_count: listing.total,
45
+ products: listing.items.map(toCard),
46
+ facets: toFacets(listing.facets, input.selections),
47
+ price_bounds: toPriceBounds(listing.priceRange)
121
48
  };
122
49
  } catch (err) {
123
50
  console.warn("[fetchProductSearchPage] failed:", err);
124
51
  return null;
125
52
  }
126
53
  };
127
- const EXCERPT_LENGTH = 160;
128
- const SearchCountsQuery = graphql(`
129
- query SearchCounts($query: String!) {
130
- productSearch(query: $query, first: 0) {
131
- totalCount
132
- }
133
- collectionSearch(query: $query, first: 0) {
134
- totalCount
135
- }
136
- contentSearch(query: $query, first: 0) {
137
- totalCount
138
- }
139
- }
140
- `);
141
- const CollectionSearchQuery = graphql(`
142
- query CollectionSearch($query: String!, $first: Int!, $after: String) {
143
- collectionSearch(query: $query, first: $first, after: $after) {
144
- totalCount
145
- pageInfo {
146
- hasNextPage
147
- }
148
- nodes {
149
- handle
150
- title
151
- description
152
- descriptionHtml
153
- productsCount
154
- featuredImage {
155
- ...CollectionImage
156
- }
157
- }
158
- }
159
- }
160
- `, [CollectionImageFragment]);
161
- const ContentSearchQuery = graphql(`
162
- query ContentSearch(
163
- $query: String!
164
- $first: Int!
165
- $after: String
166
- $excerptLength: Int!
167
- $withImage: Boolean!
168
- $imageWidth: Int!
169
- ) {
170
- contentSearch(query: $query, first: $first, after: $after) {
171
- totalCount
172
- pageInfo {
173
- hasNextPage
174
- }
175
- nodes {
176
- __typename
177
- ... on ShopPage {
178
- handle
179
- title
180
- publishedAt
181
- excerpt: body(truncateAt: $excerptLength)
182
- }
183
- ... on ShopArticle {
184
- handle
185
- title
186
- publishedAt
187
- summary(truncateAt: $excerptLength)
188
- bodyExcerpt: body(truncateAt: $excerptLength)
189
- blog {
190
- handle
191
- title
192
- }
193
- image @include(if: $withImage) {
194
- __typename
195
- id
196
- alt
197
- ... on ShopMediaImage {
198
- url(transform: { maxWidth: $imageWidth })
199
- url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
200
- width
201
- height
202
- focalPoint {
203
- x
204
- y
205
- }
206
- }
207
- }
208
- }
209
- }
210
- }
211
- }
212
- `);
213
54
  /** A result row's thumbnail width when none is asked for, and its clamp. */
214
55
  const RESOURCE_IMAGE_WIDTH = 120;
215
- function clampImageWidth(width) {
216
- return Math.min(Math.max(Math.round(width ?? RESOURCE_IMAGE_WIDTH), 1), SUGGESTIONS_MEDIA_MAX_WIDTH);
56
+ const IMAGE_MAX_WIDTH = 1200;
57
+ function clampWidth(width, fallback) {
58
+ return Math.min(Math.max(Math.round(width ?? fallback), 1), IMAGE_MAX_WIDTH);
217
59
  }
218
60
  const emptyResourcePage = () => ({
219
61
  total_count: 0,
220
62
  has_next_page: false,
221
63
  items: []
222
64
  });
223
- /** Count-only read of all three search fields (`first: 0` loads no record).
224
- * Null when the request fails — a theme then draws no tab row rather than a
225
- * row of zeroes it can't stand behind. */
65
+ /** A page of `items` at `offset` out of `total`. */
66
+ function resourcePage(items, total, offset) {
67
+ return {
68
+ total_count: total,
69
+ has_next_page: offset + items.length < total,
70
+ items
71
+ };
72
+ }
73
+ /** How many products, collections, and pages and articles a query matches.
74
+ * Null when the read fails — a theme then draws no tab row rather than a row
75
+ * of zeroes it can't stand behind. */
226
76
  async function fetchSearchCounts(data) {
227
- const query = data.query.trim();
77
+ const query = searchQuery(data.query);
228
78
  if (!query) return {
229
79
  products: 0,
230
80
  collections: 0,
231
81
  content: 0
232
82
  };
233
- try {
234
- const res = await request(SearchCountsQuery, { query }, data.locale ? { acceptLanguage: data.locale } : void 0);
235
- return {
236
- products: res.productSearch.totalCount,
237
- collections: res.collectionSearch?.totalCount ?? 0,
238
- content: res.contentSearch?.totalCount ?? 0
239
- };
240
- } catch (err) {
241
- console.warn("[fetchSearchCounts] failed:", err);
242
- return null;
243
- }
83
+ return orFallback(storefront(data.locale).search.counts({ query }), null, "fetchSearchCounts");
244
84
  }
245
85
  /** One page of matching collections, in the shape the collections list page
246
86
  * already renders (`CollectionListItem`), best match first. */
247
87
  async function fetchCollectionSearchPage(data) {
248
- const query = data.query.trim();
88
+ const query = searchQuery(data.query);
249
89
  if (!query) return emptyResourcePage();
90
+ const page = pageInput(data.page ?? 1, data.first);
250
91
  try {
251
- const conn = (await request(CollectionSearchQuery, {
92
+ const answer = await storefront(data.locale).search.collections({
252
93
  query,
253
- first: data.first,
254
- after: data.after ?? null
255
- }, data.locale ? { acceptLanguage: data.locale } : void 0)).collectionSearch;
256
- if (!conn) return emptyResourcePage();
257
- return {
258
- total_count: conn.totalCount,
259
- has_next_page: conn.pageInfo.hasNextPage,
260
- items: conn.nodes.map((c) => ({
261
- handle: c.handle,
262
- title: c.title,
263
- description: c.description,
264
- description_html: c.descriptionHtml,
265
- image: c.featuredImage ? toFileInfo(c.featuredImage, { baseWidth: 640 }) : null,
266
- products_count: c.productsCount
267
- }))
268
- };
94
+ ...page
95
+ });
96
+ return resourcePage(answer.items.map(toCollectionListItem), answer.total, page.offset);
269
97
  } catch (err) {
270
98
  console.warn("[fetchCollectionSearchPage] failed:", err);
271
99
  return emptyResourcePage();
272
100
  }
273
101
  }
274
102
  /** One page of matching pages and articles as a single ranked list. An
275
- * article's excerpt is its summary, falling back to the body's opening. Pass
276
- * `imageWidth` for a thumbnail on each article; without it none is fetched. */
103
+ * article's excerpt is its summary, else its body's opening. Pass
104
+ * `imageWidth` for a thumbnail on each article; without it none is drawn. */
277
105
  async function fetchContentSearchPage(data) {
278
- const query = data.query.trim();
106
+ const query = searchQuery(data.query);
279
107
  if (!query) return emptyResourcePage();
280
108
  const withImage = data.imageWidth !== void 0;
281
- const imageWidth = clampImageWidth(data.imageWidth);
109
+ const imageWidth = clampWidth(data.imageWidth, RESOURCE_IMAGE_WIDTH);
110
+ const page = pageInput(data.page ?? 1, data.first);
282
111
  try {
283
- const conn = (await request(ContentSearchQuery, {
112
+ const answer = await storefront(data.locale).search.content({
284
113
  query,
285
- first: data.first,
286
- after: data.after ?? null,
287
- excerptLength: EXCERPT_LENGTH,
288
- withImage,
289
- imageWidth
290
- }, data.locale ? { acceptLanguage: data.locale } : void 0)).contentSearch;
291
- if (!conn) return emptyResourcePage();
292
- return {
293
- total_count: conn.totalCount,
294
- has_next_page: conn.pageInfo.hasNextPage,
295
- items: conn.nodes.map((node) => node.__typename === "ShopArticle" ? {
296
- kind: "article",
297
- handle: node.handle,
298
- blogHandle: node.blog.handle,
299
- blogTitle: node.blog.title,
300
- title: node.title,
301
- excerpt: node.summary || node.bodyExcerpt,
302
- publishedAt: node.publishedAt ?? null,
303
- ...withImage && { image: node.image ? toFileInfo(node.image, { baseWidth: imageWidth }) : null }
304
- } : {
305
- kind: "page",
306
- handle: node.handle,
307
- title: node.title,
308
- excerpt: node.excerpt,
309
- publishedAt: node.publishedAt ?? null
310
- })
311
- };
114
+ ...page
115
+ });
116
+ return resourcePage(answer.items.map((hit) => hit.kind === "article" ? {
117
+ kind: "article",
118
+ handle: hit.handle,
119
+ blogHandle: hit.blog.handle,
120
+ blogTitle: hit.blog.title,
121
+ title: hit.title,
122
+ excerpt: hit.excerpt,
123
+ publishedAt: hit.publishedAt,
124
+ ...withImage && { image: hit.image ? imageFileInfo(hit.image, imageWidth) : null }
125
+ } : {
126
+ kind: "page",
127
+ handle: hit.handle,
128
+ title: hit.title,
129
+ excerpt: hit.excerpt,
130
+ publishedAt: hit.publishedAt
131
+ }), answer.total, page.offset);
312
132
  } catch (err) {
313
133
  console.warn("[fetchContentSearchPage] failed:", err);
314
134
  return emptyResourcePage();
315
135
  }
316
136
  }
317
- const RESOURCE_SUGGESTIONS = 3;
318
- const ResourceSuggestionsQuery = graphql(`
319
- query ResourceSuggestions($query: String!, $first: Int!) {
320
- collectionSearch(query: $query, first: $first) {
321
- nodes {
322
- handle
323
- title
324
- }
325
- }
326
- contentSearch(query: $query, first: $first) {
327
- nodes {
328
- __typename
329
- ... on ShopPage {
330
- handle
331
- title
332
- }
333
- ... on ShopArticle {
334
- handle
335
- title
336
- blog {
337
- handle
338
- }
339
- }
340
- }
341
- }
342
- }
343
- `);
344
- /** Top matching collections, pages and articles for a typeahead — its own
345
- * lean operation, issued in the same tick as `fetchProductSuggestions` so the
346
- * pair rides one POST. Hit per keystroke, so a failure (or a tripped rate
347
- * limit, which nulls a field) is simply two empty groups: the product rows
348
- * must not go down with it. */
349
- async function fetchResourceSuggestions(data) {
350
- const query = data.query.trim();
351
- if (query.length < 2) return {
352
- collections: [],
353
- content: []
354
- };
355
- try {
356
- const res = await request(ResourceSuggestionsQuery, {
357
- query,
358
- first: RESOURCE_SUGGESTIONS
359
- }, data.locale ? { acceptLanguage: data.locale } : void 0);
360
- return {
361
- collections: (res.collectionSearch?.nodes ?? []).map((c) => ({
362
- handle: c.handle,
363
- title: c.title
364
- })),
365
- content: (res.contentSearch?.nodes ?? []).map((node) => node.__typename === "ShopArticle" ? {
366
- kind: "article",
367
- handle: node.handle,
368
- blogHandle: node.blog.handle,
369
- title: node.title
370
- } : {
371
- kind: "page",
372
- handle: node.handle,
373
- title: node.title
374
- })
375
- };
376
- } catch (err) {
377
- console.warn("[fetchResourceSuggestions] failed:", err);
378
- return {
379
- collections: [],
380
- content: []
381
- };
382
- }
383
- }
384
137
  const SUGGESTIONS_DEFAULT = 6;
385
- const SUGGESTIONS_MAX = 10;
386
- const SUGGESTIONS_MIN_QUERY_LENGTH = 1;
138
+ const RESOURCE_SUGGESTIONS = 3;
387
139
  const SUGGESTIONS_MEDIA_WIDTH = 80;
388
- const SUGGESTIONS_MEDIA_MAX_WIDTH = 1200;
389
140
  const TERM_SUGGESTIONS = 5;
390
- const ProductSuggestionsQuery = graphql(`
391
- query ProductSuggestions(
392
- $query: String!
393
- $first: Int!
394
- $mediaWidth: Int!
395
- $withHover: Boolean!
396
- $withQuickAdd: Boolean!
397
- ) {
398
- productSuggestions(query: $query, first: $first) {
399
- handle
400
- title
401
- vendor
402
- # The hover pair: the first two media, from which the first two IMAGES
403
- # are taken. A row-shaped typeahead never swaps pictures, so the whole
404
- # selection stays off unless the caller asked for it.
405
- media(first: 2) @include(if: $withHover) {
406
- nodes {
407
- __typename
408
- id
409
- alt
410
- thumbhash
411
- ... on ShopMediaImage {
412
- url(transform: { maxWidth: $mediaWidth })
413
- url2x: url(transform: { maxWidth: $mediaWidth, scale: 2 })
414
- width
415
- height
416
- focalPoint {
417
- x
418
- y
419
- }
420
- }
421
- ... on ShopVideo {
422
- url
423
- hlsUrl
424
- preview {
425
- url(transform: { maxWidth: $mediaWidth })
426
- }
427
- width
428
- height
429
- duration
430
- }
431
- }
432
- }
433
- featuredMedia {
434
- __typename
435
- id
436
- alt
437
- ... on ShopMediaImage {
438
- url(transform: { maxWidth: $mediaWidth, maxHeight: $mediaWidth })
439
- width
440
- height
441
- focalPoint {
442
- x
443
- y
444
- }
445
- }
446
- }
447
- # What a row's add button decides on — off unless the caller adds.
448
- variantsCount @include(if: $withQuickAdd)
449
- defaultVariant {
450
- id @include(if: $withQuickAdd)
451
- availableForSale @include(if: $withQuickAdd)
452
- priceV2 {
453
- amount
454
- currencyCode
455
- }
456
- compareAtV2 {
457
- amount
458
- currencyCode
459
- }
460
- }
461
- }
462
- }
463
- `);
464
- const SearchTermSuggestionsQuery = graphql(`
465
- query SearchTermSuggestions($query: String!, $first: Int!) {
466
- searchTermSuggestions(query: $query, first: $first)
467
- }
468
- `);
469
- async function fetchProductSuggestions(data) {
470
- const q = data.query.trim();
471
- const options = data.locale ? { acceptLanguage: data.locale } : void 0;
472
- let termsPromise;
473
- try {
474
- termsPromise = request(SearchTermSuggestionsQuery, {
475
- query: q,
476
- first: TERM_SUGGESTIONS
477
- }, options).catch(() => ({ searchTermSuggestions: null }));
478
- if (q.length < SUGGESTIONS_MIN_QUERY_LENGTH) return {
479
- suggestions: [],
480
- terms: (await termsPromise).searchTermSuggestions
481
- };
482
- const first = Math.min(Math.max(data.first ?? SUGGESTIONS_DEFAULT, 1), SUGGESTIONS_MAX);
483
- const mediaWidth = Math.min(Math.max(Math.round(data.mediaWidth ?? SUGGESTIONS_MEDIA_WIDTH), 1), SUGGESTIONS_MEDIA_MAX_WIDTH);
484
- const withHover = data.hover === true;
485
- const withQuickAdd = data.quickAdd === true;
486
- const productsPromise = request(ProductSuggestionsQuery, {
487
- query: q,
488
- first,
489
- mediaWidth,
490
- withHover,
491
- withQuickAdd
492
- }, options);
493
- const [res, termsRes] = await Promise.all([productsPromise, termsPromise]);
494
- return {
495
- suggestions: res.productSuggestions.map((p) => {
496
- const images = (p.media?.nodes ?? []).map((n) => toFileInfo(n, { baseWidth: mediaWidth })).filter((m) => m?.kind === "image");
497
- return {
498
- handle: p.handle,
499
- title: p.title,
500
- vendor: p.vendor ?? null,
501
- featured_media: p.featuredMedia ? toFileInfo(p.featuredMedia) : null,
502
- secondary_media: images[1] ?? null,
503
- price: toMoney(p.defaultVariant.priceV2),
504
- compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
505
- ...withQuickAdd ? {
506
- defaultVariantId: p.defaultVariant.id,
507
- available: p.defaultVariant.availableForSale,
508
- variantsCount: p.variantsCount
509
- } : {}
510
- };
511
- }),
512
- terms: termsRes.searchTermSuggestions
513
- };
514
- } catch (err) {
515
- console.warn("[fetchProductSuggestions] failed:", err);
516
- return {
517
- suggestions: [],
518
- terms: termsPromise ? (await termsPromise).searchTermSuggestions : null
519
- };
520
- }
141
+ const noRows = {
142
+ suggestions: [],
143
+ collections: [],
144
+ content: []
145
+ };
146
+ /**
147
+ * Storefront typeahead, hit per keystroke (debounced by the caller): one
148
+ * `search.suggest` for the rows of every kind and, beside it, one
149
+ * `search.terms` for the completions. Terms are read unless the theme says
150
+ * `terms: false` (one that draws them only for a blank query passes it on a
151
+ * matched one) — a blank query is the empty field's "most searched" list —
152
+ * and are the optional half: a terms failure (or `terms: false`) is
153
+ * `terms: null`, a rows failure no rows, neither takes the other down. The
154
+ * server is the trust boundary for every number.
155
+ *
156
+ * A plain function, not a `createServerFn`: a server fn declared in a package
157
+ * and reached only through a theme handler's dynamic import never makes
158
+ * Start's server-fn manifest, so calling it fails at runtime with "Server
159
+ * function info not found". A theme wraps this in its own server fn.
160
+ */
161
+ async function fetchSearchSuggestions(data) {
162
+ const query = searchQuery(data.query);
163
+ const client = storefront(data.locale);
164
+ const terms = data.terms === false ? Promise.resolve(null) : orFallback(client.search.terms({
165
+ query,
166
+ limit: TERM_SUGGESTIONS
167
+ }).then((answer) => answer.terms), null, "fetchSearchSuggestions terms");
168
+ if (!query) return {
169
+ ...noRows,
170
+ terms: await terms
171
+ };
172
+ const mediaWidth = clampWidth(data.mediaWidth, SUGGESTIONS_MEDIA_WIDTH);
173
+ const hover = data.hover === true;
174
+ const quickAdd = data.quickAdd === true;
175
+ const rows = orFallback(client.search.suggest({
176
+ query,
177
+ products: Math.min(Math.max(data.first ?? SUGGESTIONS_DEFAULT, 1), 10),
178
+ resources: data.resources ? RESOURCE_SUGGESTIONS : 0,
179
+ card: cardShape({ media: hover ? 2 : 1 })
180
+ }), null, "fetchSearchSuggestions");
181
+ const [answer, termList] = await Promise.all([rows, terms]);
182
+ if (!answer) return {
183
+ ...noRows,
184
+ terms: termList
185
+ };
186
+ return {
187
+ suggestions: answer.products.map((p) => {
188
+ const lead = p.media[0];
189
+ const shown = lead?.image ?? lead?.video?.poster ?? null;
190
+ const second = p.media[1]?.image ?? null;
191
+ return {
192
+ handle: p.handle,
193
+ title: p.title,
194
+ vendor: p.vendor,
195
+ featured_media: shown ? imageFileInfo(shown, mediaWidth) : null,
196
+ ...hover && { secondary_media: lead?.image && second ? imageFileInfo(second, mediaWidth) : null },
197
+ price: toMoney(p.defaultVariant.price),
198
+ compare_at: p.defaultVariant.compareAtPrice === null ? null : toMoney(p.defaultVariant.compareAtPrice),
199
+ ...quickAdd && {
200
+ defaultVariantId: p.defaultVariant.id,
201
+ available: p.defaultVariant.available,
202
+ variantsCount: p.variantsCount
203
+ }
204
+ };
205
+ }),
206
+ collections: answer.collections.map((c) => ({
207
+ handle: c.handle,
208
+ title: c.title
209
+ })),
210
+ content: answer.content.map((hit) => hit.kind === "article" ? {
211
+ kind: "article",
212
+ handle: hit.handle,
213
+ blogHandle: hit.blog.handle,
214
+ blogTitle: hit.blog.title,
215
+ title: hit.title
216
+ } : {
217
+ kind: "page",
218
+ handle: hit.handle,
219
+ title: hit.title
220
+ }),
221
+ terms: termList
222
+ };
521
223
  }
522
- //#endregion
523
- export { fetchCollectionSearchPage, fetchContentSearchPage, fetchProductSearchPage, fetchProductSuggestions, fetchResourceSuggestions, fetchSearchCounts };
224
+ export { fetchCollectionSearchPage, fetchContentSearchPage, fetchProductSearchPage, fetchSearchCounts, fetchSearchSuggestions };