@shop-on/theme-server 0.9.0 → 0.11.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 (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/product.js CHANGED
@@ -1,674 +1,174 @@
1
1
  import { memoTTL } from "./cache.js";
2
- import { graphql, request } from "./graphql.js";
2
+ import { cardFields, cardShape, compareAt, productRef, readCard, shapeKey, toCard, toOption } from "./card.js";
3
3
  import { getContentVersion } from "./content-version.js";
4
- import { fetchFileById, toFileInfo } from "./files.js";
5
- import { toMetafieldMap } from "./metafields.js";
6
- //#region src/product.ts
7
- const BadgeTagsFragment = graphql(`
8
- fragment BadgeTags on ShopProduct @_unmask {
9
- tags(filter: { start: "tag__" })
10
- }
11
- `);
12
- const ProductCardMediaNodeFragment = graphql(`
13
- fragment ProductCardMediaNode on FileInterface @_unmask {
14
- __typename
15
- id
16
- alt
17
- thumbhash
18
- ... on ShopMediaImage {
19
- url(transform: { maxWidth: 480 })
20
- url2x: url(transform: { maxWidth: 480, scale: 2 })
21
- thumbUrl: url(transform: { maxWidth: 80 })
22
- width
23
- height
24
- focalPoint {
25
- x
26
- y
27
- }
28
- }
29
- ... on ShopVideo {
30
- url
31
- hlsUrl
32
- preview {
33
- url(transform: { maxWidth: 480 })
34
- }
35
- width
36
- height
37
- duration
38
- }
39
- }
40
- `);
41
- const ProductGalleryMediaNodeFragment = graphql(`
42
- fragment ProductGalleryMediaNode on FileInterface @_unmask {
43
- __typename
44
- id
45
- alt
46
- thumbhash
47
- ... on ShopMediaImage {
48
- url(transform: { maxWidth: 1200 })
49
- url2x: url(transform: { maxWidth: 1200, scale: 2 })
50
- thumbUrl: url(transform: { maxWidth: 160 })
51
- width
52
- height
53
- focalPoint {
54
- x
55
- y
56
- }
57
- }
58
- ... on ShopVideo {
59
- url
60
- hlsUrl
61
- preview {
62
- url(transform: { maxWidth: 1200 })
63
- }
64
- width
65
- height
66
- duration
67
- }
68
- }
69
- `);
70
- const ProductVariantNodeFragment = graphql(`
71
- fragment ProductVariantNode on ShopProductVariant @_unmask {
72
- id
73
- title
74
- position
75
- availableForSale
76
- sku
77
- quantityAvailable
78
- requiresShipping
79
- selectedOptions {
80
- name
81
- value
82
- }
83
- priceV2 {
84
- amount
85
- currencyCode
86
- }
87
- compareAtV2 {
88
- amount
89
- currencyCode
90
- }
91
- featuredMedia {
92
- id
93
- }
94
- }
95
- `);
96
- const ProductOptionNodeFragment = graphql(`
97
- fragment ProductOptionNode on ShopProductOption @_unmask {
98
- id
99
- name
100
- position
101
- linkedMetafield {
102
- namespace
103
- key
104
- }
105
- values {
106
- id
107
- name
108
- position
109
- hasVariants
110
- linkedMetaobject {
111
- id
112
- fields {
113
- key
114
- value
115
- reference {
116
- __typename
117
- ... on ShopMediaImage {
118
- url(transform: { maxWidth: 128, maxHeight: 128 })
119
- }
120
- }
121
- }
122
- }
123
- }
124
- }
125
- `);
126
- const FetchProductCardQuery = graphql(`
127
- query FetchProductCard(
128
- $handle: String
129
- $id: ID
130
- $includeOptions: Boolean!
131
- $withMetafields: Boolean = false
132
- $metafieldFirst: Int = 0
133
- $metafieldKeys: [MetafieldKeyInput!] = []
134
- ) {
135
- product(handle: $handle, id: $id) {
136
- id
137
- handle
138
- title
139
- vendor
140
- aggregateRating {
141
- value
142
- count
143
- }
144
- ...BadgeTags
145
- defaultVariant {
146
- id
147
- title
148
- availableForSale
149
- sku
150
- priceV2 {
151
- amount
152
- currencyCode
153
- }
154
- compareAtV2 {
155
- amount
156
- currencyCode
157
- }
158
- }
159
- variantsCount
160
- # Variant options (with the linked color_pattern swatch metadata) are
161
- # the heaviest part of a card — callers only ask for them when the
162
- # theme's card swatches actually render.
163
- options @include(if: $includeOptions) {
164
- ...ProductOptionNode
165
- }
166
- # The storefront-visible metafields a card names and no more (a
167
- # theme's product-variations swatches name one; a closest product
168
- # metafield source bound in the picker's scope names each it reads).
169
- metafields(first: $metafieldFirst, keys: $metafieldKeys) @include(if: $withMetafields) {
170
- nodes {
171
- namespace
172
- key
173
- type
174
- value
175
- }
176
- }
177
- media(first: 5) {
178
- nodes {
179
- ...ProductCardMediaNode
180
- }
181
- }
182
- }
183
- }
184
- `, [
185
- ProductCardMediaNodeFragment,
186
- ProductOptionNodeFragment,
187
- BadgeTagsFragment
188
- ]);
189
- const FetchProductFullQuery = graphql(`
190
- query FetchProductFull($handle: String, $id: ID) {
191
- product(handle: $handle, id: $id) {
192
- id
193
- handle
194
- title
195
- vendor
196
- themeTemplate
197
- ...BadgeTags
198
- giftCard
199
- finalSale
200
- descriptionHtml
201
- # Server-side plain-text projection of descriptionHtml — Rails
202
- # strips tags AND decodes HTML entities, which a client-side regex
203
- # cannot do safely. Truncated to the meta-description storage cap.
204
- descriptionPlain: description(truncateAt: 320)
205
- seo {
206
- title
207
- description
208
- }
209
- defaultVariant {
210
- ...ProductVariantNode
211
- gtin
212
- }
213
- options {
214
- ...ProductOptionNode
215
- }
216
- variants(first: 100) {
217
- pageInfo {
218
- hasNextPage
219
- }
220
- nodes {
221
- ...ProductVariantNode
222
- }
223
- }
224
- media(first: 20) {
225
- nodes {
226
- ...ProductGalleryMediaNode
227
- }
228
- }
229
- aggregateRating {
230
- value
231
- count
232
- # Per-star bucket counts (always all 5), for the PDP reviews section's
233
- # distribution bars. Card fetches skip it — a card shows the average.
234
- distribution {
235
- rating
236
- count
237
- }
238
- }
239
- # Storefront-visible product metafields, for theme dynamic-source
240
- # bindings. Only definitions the merchant published via storefrontVisible
241
- # are returned (the API gates on the storefront scope), so the set is
242
- # small; 50 is a generous cap on a single shop's custom fields.
243
- metafields(first: 50) {
244
- nodes {
245
- namespace
246
- key
247
- type
248
- value
249
- }
250
- }
251
- }
252
- }
253
- `, [
254
- ProductGalleryMediaNodeFragment,
255
- ProductVariantNodeFragment,
256
- ProductOptionNodeFragment,
257
- BadgeTagsFragment
258
- ]);
259
- function toVariant(v) {
260
- return {
4
+ import { imageFileInfo, videoFileInfo } from "./files.js";
5
+ import { customDataMap } from "./metafields.js";
6
+ import { toMoney } from "./money.js";
7
+ import { requestSlot } from "./request-scope.js";
8
+ import { orFallback, orNotFound, storefront } from "./storefront.js";
9
+ // src/product.ts
10
+ function toPage(p) {
11
+ const options = p.options.map(toOption);
12
+ const selections = new Map(p.options.flatMap((o) => o.values.map((v) => [v.id, {
13
+ name: o.name,
14
+ value: v.name
15
+ }])));
16
+ const variants = p.variants.map((v, index) => ({
261
17
  id: v.id,
262
18
  title: v.title,
263
- position: v.position,
264
- availableForSale: v.availableForSale,
265
- sku: v.sku ?? null,
266
- quantityAvailable: v.quantityAvailable ?? null,
19
+ position: index + 1,
20
+ availableForSale: v.available,
21
+ sku: v.sku,
22
+ quantityAvailable: v.quantity,
267
23
  requiresShipping: v.requiresShipping,
268
- selectedOptions: v.selectedOptions.map((s) => ({
269
- name: s.name,
270
- value: s.value
271
- })),
272
- priceV2: toMoney(v.priceV2),
273
- compareAtV2: v.compareAtV2 ? toMoney(v.compareAtV2) : null,
274
- featuredMedia: v.featuredMedia ? { id: v.featuredMedia.id } : null
275
- };
276
- }
277
- function toOption(o) {
24
+ selectedOptions: v.optionValueIds.flatMap((id) => selections.get(id) ?? []),
25
+ priceV2: toMoney(v.price),
26
+ compareAtV2: compareAt(v.compareAtPrice),
27
+ featuredMedia: v.mediaFileIds[0] ? { id: v.mediaFileIds[0] } : null
28
+ }));
29
+ const defaultVariant = variants.find((v) => v.id === p.defaultVariant.id) ?? variants[0];
278
30
  return {
279
- id: o.id,
280
- name: o.name,
281
- position: o.position,
282
- linkedMetafield: o.linkedMetafield ? {
283
- namespace: o.linkedMetafield.namespace,
284
- key: o.linkedMetafield.key
285
- } : null,
286
- values: o.values.map((v) => ({
287
- id: v.id,
288
- name: v.name,
289
- position: v.position,
290
- hasVariants: v.hasVariants,
291
- linkedMetaobject: v.linkedMetaobject ? {
292
- id: v.linkedMetaobject.id,
293
- fields: v.linkedMetaobject.fields.map((f) => ({
294
- key: f.key,
295
- value: f.value,
296
- imageUrl: f.reference?.__typename === "ShopMediaImage" ? f.reference.url : null
297
- }))
298
- } : null
299
- }))
31
+ ...cardFields(p, PAGE_BASE_WIDTH),
32
+ themeTemplate: p.themeTemplate ?? "product",
33
+ giftCard: p.giftCard,
34
+ descriptionHtml: p.descriptionHtml,
35
+ descriptionPlain: p.descriptionText,
36
+ seo: {
37
+ title: p.seoTitle,
38
+ description: p.seoDescription
39
+ },
40
+ options,
41
+ variants,
42
+ defaultVariant,
43
+ variantMediaMap: Object.fromEntries(p.variants.filter((v) => v.mediaFileIds.length > 0).map((v) => [v.id, v.mediaFileIds])),
44
+ rating: p.rating && {
45
+ value: p.rating.value,
46
+ count: p.rating.count,
47
+ distribution: p.rating.distribution.map((count, index) => ({
48
+ rating: index + 1,
49
+ count
50
+ }))
51
+ },
52
+ metafields: customDataMap(p.metafields),
53
+ finalSale: p.finalSale,
54
+ gtin: p.variants.find((v) => v.id === p.defaultVariant.id)?.gtin ?? null
300
55
  };
301
56
  }
302
- function parseRef(ref) {
303
- return ref.startsWith("gid://") ? { id: ref } : { handle: ref };
304
- }
305
- function toMedia(nodes, baseWidth) {
306
- return nodes.map((n) => toFileInfo(n, { baseWidth })).filter((m) => m !== null);
57
+ /** The base width a page's gallery draws media at (`imageFileInfo`). */
58
+ const PAGE_BASE_WIDTH = 1200;
59
+ const PAGES = Symbol("product pages");
60
+ /** The page answers this request read, by the ref each was asked for, so a
61
+ * card of the page's own product (a section naming it, a recently-viewed row)
62
+ * is drawn from the page's answer instead of a second read. */
63
+ function pagesThisRequest() {
64
+ return requestSlot(PAGES, () => /* @__PURE__ */ new Map());
307
65
  }
308
- function toMoney(m) {
309
- return {
310
- amount: Number(m.amount),
311
- currencyCode: m.currencyCode
312
- };
313
- }
314
- /** `"namespace.key"` names as the card queries' metafield variables. Nothing
315
- * named ⇒ the selection stays `@include`d out and the variables at their
316
- * defaults, so a caller naming no keys sends exactly the query it always did.
317
- * A name without a dot (or with an empty side) names no key and is dropped. */
318
- function metafieldKeysVariables(keys) {
319
- const parsed = [];
320
- for (const ref of keys ?? []) {
321
- const dot = ref.indexOf(".");
322
- if (dot <= 0 || dot === ref.length - 1) continue;
323
- parsed.push({
324
- namespace: ref.slice(0, dot),
325
- key: ref.slice(dot + 1)
326
- });
327
- }
328
- if (parsed.length === 0) return {};
329
- return {
330
- withMetafields: true,
331
- metafieldFirst: parsed.length,
332
- metafieldKeys: parsed
333
- };
334
- }
335
- /** The cache-key segment for a key list: sorted, so two callers naming the
336
- * same keys in a different order share one entry. */
337
- function metafieldKeysKey(keys) {
338
- return keys?.length ? [...keys].sort().join(",") : "";
339
- }
340
- const fetchProductCardCached = memoTTL(async (ref, includeOptions, locale, metafieldKeys) => {
341
- try {
342
- const p = (await request(FetchProductCardQuery, {
343
- ...parseRef(ref),
344
- includeOptions,
345
- ...metafieldKeysVariables(metafieldKeys)
346
- }, locale ? { acceptLanguage: locale } : void 0)).product;
347
- if (!p) return null;
348
- return {
349
- id: p.id,
350
- handle: p.handle,
351
- title: p.title,
352
- vendor: p.vendor ?? null,
353
- aggregateRating: p.aggregateRating,
354
- tags: p.tags,
355
- available: p.defaultVariant.availableForSale,
356
- defaultVariantId: p.defaultVariant.id,
357
- defaultVariantTitle: p.defaultVariant.title,
358
- sku: p.defaultVariant.sku ?? null,
359
- variantsCount: p.variantsCount,
360
- price: toMoney(p.defaultVariant.priceV2),
361
- compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
362
- media: toMedia(p.media.nodes, 480),
363
- ...p.options ? { options: p.options.map(toOption) } : {},
364
- ...p.metafields ? { metafields: toMetafieldMap(p.metafields.nodes) } : {}
365
- };
366
- } catch (err) {
367
- console.warn("[fetchProduct] failed:", err);
368
- return null;
369
- }
370
- }, {
371
- key: (ref, includeOptions, locale, metafieldKeys) => `card|${ref}|${includeOptions}|${locale ?? ""}|${metafieldKeysKey(metafieldKeys)}`,
372
- shouldCache: (v) => v !== null,
373
- version: getContentVersion
374
- });
375
- /** Lean fetch for product cards (collection grids, social tiles, shop-the-look).
376
- * Skips description and caps media at 5. Used by the `product_list` resolver.
377
- * `includeOptions` (default true) gates the variant-options payload — pass
378
- * false when the consuming cards render no swatches. */
379
- function fetchProduct(ref, opts) {
380
- return fetchProductCardCached(ref, opts?.includeOptions ?? true, opts?.locale, opts?.metafieldKeys);
66
+ async function loadPage(ref, locale) {
67
+ const p = await orFallback(orNotFound(storefront(locale).products.get(productRef(ref))), null, "fetchProductFull");
68
+ return p && toPage(p);
381
69
  }
382
- const FetchProductShowcaseQuery = graphql(`
383
- query FetchProductShowcase($handle: String, $id: ID) {
384
- product(handle: $handle, id: $id) {
385
- id
386
- handle
387
- title
388
- vendor
389
- productType
390
- aggregateRating {
391
- value
392
- count
393
- }
394
- # Server-side plain-text projection of the description (tags stripped +
395
- # entities decoded), truncated — the card clamps it to 1–3 lines anyway.
396
- descriptionPlain: description(truncateAt: 200)
397
- defaultVariant {
398
- priceV2 {
399
- amount
400
- currencyCode
401
- }
402
- compareAtV2 {
403
- amount
404
- currencyCode
405
- }
406
- }
407
- media(first: 3) {
408
- nodes {
409
- ...ProductCardMediaNode
410
- }
411
- }
412
- }
413
- }
414
- `, [ProductCardMediaNodeFragment]);
415
- /** Lean card fetch with a plain-text description — same footprint as
416
- * {@link fetchProduct}, plus `descriptionPlain`; no options/variants. For cards
417
- * that render a short copy blurb (e.g. editorial "showcase" cards). */
418
- const fetchProductShowcase = memoTTL(async (ref, locale) => {
419
- try {
420
- const p = (await request(FetchProductShowcaseQuery, parseRef(ref), locale ? { acceptLanguage: locale } : void 0)).product;
421
- if (!p) return null;
422
- return {
423
- id: p.id,
424
- handle: p.handle,
425
- title: p.title,
426
- vendor: p.vendor ?? null,
427
- product_type: p.productType ?? null,
428
- aggregateRating: p.aggregateRating,
429
- descriptionPlain: p.descriptionPlain,
430
- price: toMoney(p.defaultVariant.priceV2),
431
- compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
432
- media: toMedia(p.media.nodes, 480)
433
- };
434
- } catch (err) {
435
- console.warn("[fetchProductShowcase] failed:", err);
436
- return null;
437
- }
438
- }, {
439
- key: (ref, locale) => `showcase|${ref}|${locale ?? ""}`,
440
- shouldCache: (v) => v !== null,
441
- version: getContentVersion
442
- });
443
- /** Rich fetch for the PDP. Includes descriptionHtml, up to 20 media, options
444
- * and variants for the variant picker. */
445
- async function loadProductFull(handle, locale) {
446
- try {
447
- const p = (await request(FetchProductFullQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).product;
448
- if (!p) return null;
449
- if (process.env.NODE_ENV !== "production" && p.variants.pageInfo.hasNextPage) console.warn(`[fetchProductFull] product ${p.handle} has more than 100 variants — picker will only see the first 100. Increase the cap in FetchProductFullQuery.`);
450
- const defaultVariant = toVariant(p.defaultVariant);
451
- const variants = p.variants.nodes.map(toVariant);
452
- const options = p.options.map(toOption);
453
- return {
454
- id: p.id,
455
- handle: p.handle,
456
- title: p.title,
457
- vendor: p.vendor ?? null,
458
- themeTemplate: p.themeTemplate,
459
- giftCard: p.giftCard,
460
- tags: p.tags,
461
- available: p.defaultVariant.availableForSale,
462
- descriptionHtml: p.descriptionHtml,
463
- descriptionPlain: p.descriptionPlain,
464
- price: toMoney(p.defaultVariant.priceV2),
465
- compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
466
- media: toMedia(p.media.nodes, 1200),
467
- options,
468
- variants,
469
- defaultVariant,
470
- aggregateRating: {
471
- value: p.aggregateRating.value,
472
- count: p.aggregateRating.count,
473
- distribution: p.aggregateRating.distribution.map((b) => ({
474
- rating: b.rating,
475
- count: b.count
476
- }))
477
- },
478
- seo: {
479
- title: p.seo.title,
480
- description: p.seo.description
481
- },
482
- metafields: toMetafieldMap(p.metafields.nodes),
483
- finalSale: p.finalSale,
484
- gtin: p.defaultVariant.gtin ?? null
485
- };
486
- } catch (err) {
487
- console.warn("[fetchProductFull] failed:", err);
488
- return null;
489
- }
490
- }
491
- const fullProductOpts = {
492
- key: (handle, locale) => `full|${handle}|${locale ?? ""}`,
70
+ const pageOpts = {
71
+ key: (ref, locale) => `full|${ref}|${locale ?? ""}`,
493
72
  shouldCache: (v) => v !== null
494
73
  };
495
- const cachedProductFull = memoTTL(loadProductFull, {
496
- ...fullProductOpts,
74
+ const cachedPage = memoTTL(loadPage, {
75
+ ...pageOpts,
497
76
  version: getContentVersion
498
77
  });
499
- const previewProductFull = memoTTL(loadProductFull, {
500
- ...fullProductOpts,
78
+ const previewPage = memoTTL(loadPage, {
79
+ ...pageOpts,
501
80
  ttlMs: 5e3
502
81
  });
503
- function fetchProductFull(handle, opts) {
504
- return (opts?.preview ? previewProductFull : cachedProductFull)(handle, opts?.locale);
505
- }
506
- const FetchVariantMediaMapQuery = graphql(`
507
- query FetchVariantMediaMap($handle: String) {
508
- product(handle: $handle) {
509
- id
510
- variants(first: 100) {
511
- nodes {
512
- id
513
- media(first: 20) {
514
- nodes {
515
- id
516
- }
517
- }
518
- }
519
- }
520
- }
521
- }
522
- `);
523
- async function loadVariantMediaMap(handle) {
524
- try {
525
- const res = await request(FetchVariantMediaMapQuery, { handle });
526
- const out = {};
527
- for (const v of res.product?.variants.nodes ?? []) {
528
- const ids = v.media.nodes.map((m) => m.id);
529
- if (ids.length > 0) out[v.id] = ids;
530
- }
531
- return out;
532
- } catch (err) {
533
- console.warn("[fetchVariantMediaMap] failed:", err);
534
- return {};
82
+ /** The product page whole, by handle or `prod_…` id (a bound product
83
+ * reference's value): description, every option, variant and media, every
84
+ * storefront-visible metafield. Null when a buyer may not see it, and on a
85
+ * failed read (not cached). */
86
+ function fetchProductFull(ref, opts) {
87
+ const read = (opts?.preview ? previewPage : cachedPage)(ref, opts?.locale);
88
+ const pages = pagesThisRequest();
89
+ if (pages) {
90
+ const locale = opts?.locale ?? "";
91
+ pages.set(`${ref}|${locale}`, read);
92
+ read.then((p) => {
93
+ if (!p) return;
94
+ if (p.id) pages.set(`${p.id}|${locale}`, read);
95
+ pages.set(`${p.handle}|${locale}`, read);
96
+ });
535
97
  }
98
+ return read;
536
99
  }
537
- const fetchVariantMediaMap = memoTTL(loadVariantMediaMap, {
538
- key: (handle) => `variant-media|${handle}`,
539
- version: getContentVersion
540
- });
541
- const FetchProductListDetailQuery = graphql(`
542
- query FetchProductListDetail($handle: String, $id: ID, $includeOptions: Boolean!) {
543
- product(handle: $handle, id: $id) {
544
- id
545
- handle
546
- title
547
- vendor
548
- ...BadgeTags
549
- # Server-side plain-text projection of the description (tags stripped +
550
- # entities decoded), truncated — the detail panel clamps it anyway and
551
- # links out to the PDP for the full copy.
552
- descriptionPlain: description(truncateAt: 300)
553
- defaultVariant {
554
- id
555
- title
556
- availableForSale
557
- sku
558
- priceV2 {
559
- amount
560
- currencyCode
561
- }
562
- compareAtV2 {
563
- amount
564
- currencyCode
565
- }
566
- }
567
- variantsCount
568
- options @include(if: $includeOptions) {
569
- ...ProductOptionNode
570
- }
571
- media(first: 5) {
572
- nodes {
573
- ...ProductCardMediaNode
574
- }
575
- }
576
- }
577
- }
578
- `, [
579
- ProductCardMediaNodeFragment,
580
- ProductOptionNodeFragment,
581
- BadgeTagsFragment
582
- ]);
583
- const fetchProductListDetailCached = memoTTL(async (ref, includeOptions, locale) => {
584
- try {
585
- const p = (await request(FetchProductListDetailQuery, {
586
- ...parseRef(ref),
587
- includeOptions
588
- }, locale ? { acceptLanguage: locale } : void 0)).product;
589
- if (!p) return null;
590
- return {
591
- id: p.id,
592
- handle: p.handle,
593
- title: p.title,
594
- vendor: p.vendor ?? null,
595
- tags: p.tags,
596
- descriptionPlain: p.descriptionPlain,
597
- available: p.defaultVariant.availableForSale,
598
- defaultVariantId: p.defaultVariant.id,
599
- defaultVariantTitle: p.defaultVariant.title,
600
- sku: p.defaultVariant.sku ?? null,
601
- variantsCount: p.variantsCount,
602
- price: toMoney(p.defaultVariant.priceV2),
603
- compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
604
- media: toMedia(p.media.nodes, 480),
605
- ...p.options ? { options: p.options.map(toOption) } : {}
606
- };
607
- } catch (err) {
608
- console.warn("[fetchProductListDetail] failed:", err);
609
- return null;
610
- }
100
+ const fetchCardCached = memoTTL(async (ref, shape, locale) => {
101
+ const card = await orFallback(readCard(ref, shape, locale), null, "fetchProduct");
102
+ return card ? toCard(card) : null;
611
103
  }, {
612
- key: (ref, includeOptions, locale) => `list-detail|${ref}|${includeOptions}|${locale ?? ""}`,
104
+ key: (ref, shape, locale) => `card|${ref}|${shapeKey(shape)}|${locale ?? ""}`,
613
105
  shouldCache: (v) => v !== null,
614
106
  version: getContentVersion
615
107
  });
616
- /** Card fetch that also carries a truncated plain-text description — for a
617
- * `product_list` whose first product is featured in a detail panel while the
618
- * rest render as cards. `includeOptions` (default true) gates the swatch payload
619
- * exactly as in {@link fetchProduct}. */
108
+ async function fetchCard(ref, shape, locale) {
109
+ const page = pagesThisRequest()?.get(`${ref}|${locale ?? ""}`);
110
+ if (page) return page;
111
+ return fetchCardCached(ref, cardShape(shape), locale);
112
+ }
113
+ /** A product card (grids, social tiles, shop-the-look, the `product` and
114
+ * `product_list` settings). `includeOptions` (default true) asks for the
115
+ * options a swatch row draws; `metafieldKeys` for the metafields a bound
116
+ * source on the card reads. */
117
+ function fetchProduct(ref, opts) {
118
+ return fetchCard(ref, {
119
+ options: opts?.includeOptions ?? true,
120
+ metafieldKeys: [...opts?.metafieldKeys ?? []]
121
+ }, opts?.locale);
122
+ }
123
+ /** A card with its plain-text description and no options, for cards that
124
+ * render a short blurb (editorial "showcase" cards). Three media: the card
125
+ * shows one, the rest cover a video-first list. */
126
+ function fetchProductShowcase(ref, locale) {
127
+ return fetchCard(ref, {
128
+ media: 3,
129
+ description: true
130
+ }, locale);
131
+ }
132
+ /** A card with its plain-text description, for a `product_list` whose first
133
+ * product is featured in a detail panel (its description and a small grid of
134
+ * its media) while the rest render as cards. `includeOptions` (default true)
135
+ * as in {@link fetchProduct}. */
620
136
  function fetchProductListDetail(ref, opts) {
621
- return fetchProductListDetailCached(ref, opts?.includeOptions ?? true, opts?.locale);
137
+ return fetchCard(ref, {
138
+ options: opts?.includeOptions ?? true,
139
+ description: true
140
+ }, opts?.locale);
622
141
  }
623
- const FetchProductShowcaseMediaQuery = graphql(`
624
- query FetchProductShowcaseMedia(
625
- $handle: String!
626
- $namespace: String!
627
- $imageKey: String!
628
- $videoKey: String!
629
- ) {
630
- product(handle: $handle) {
631
- image: metafield(namespace: $namespace, key: $imageKey) {
632
- value
633
- type
634
- }
635
- video: metafield(namespace: $namespace, key: $videoKey) {
636
- value
637
- type
638
- }
639
- }
640
- }
641
- `);
142
+ /** The image a showcase draws at. */
143
+ const SHOWCASE_IMAGE_WIDTH = 800;
144
+ /** "Showcase" media for a product, from two `file_reference` metafields
145
+ * (`<namespace>.<imageKey>`, `<namespace>.<videoKey>`), each read off the
146
+ * file the value names. Each is null when its metafield is unset, not
147
+ * storefront-visible, or names a file of the other kind, so a caller falls
148
+ * back to the product's own media. The whole is null only on a failed read
149
+ * (not cached). */
642
150
  const fetchProductShowcaseMedia = memoTTL(async ({ handle, namespace, imageKey, videoKey }) => {
643
- try {
644
- const p = (await request(FetchProductShowcaseMediaQuery, {
645
- handle,
646
- namespace,
647
- imageKey,
648
- videoKey
649
- })).product;
650
- if (!p) return {
651
- image: null,
652
- video: null
653
- };
654
- const imageRef = p.image?.type === "FILE_REFERENCE" ? p.image.value : null;
655
- const videoRef = p.video?.type === "FILE_REFERENCE" ? p.video.value : null;
656
- const [image, video] = await Promise.all([imageRef ? fetchFileById({
657
- id: imageRef,
658
- transform: { maxWidth: 800 }
659
- }) : null, videoRef ? fetchFileById({ id: videoRef }) : null]);
660
- return {
661
- image: image?.kind === "image" ? image : null,
662
- video: video?.kind === "video" ? video : null
663
- };
664
- } catch (err) {
665
- console.warn("[fetchProductShowcaseMedia] failed:", err);
666
- return null;
667
- }
151
+ const image = `${namespace}.${imageKey}`;
152
+ const video = `${namespace}.${videoKey}`;
153
+ const card = await orFallback(readCard(handle, cardShape({
154
+ media: 0,
155
+ metafieldKeys: [image, video]
156
+ })), void 0, "fetchProductShowcaseMedia");
157
+ if (card === void 0) return null;
158
+ const metafields = card?.metafields;
159
+ const file = (name) => {
160
+ const value = metafields?.values.find((v) => `${v.namespace}.${v.key}` === name);
161
+ return value?.type === "file_reference" ? metafields?.references.files.find((f) => f.id === value.value) : void 0;
162
+ };
163
+ const imageFile = file(image)?.image;
164
+ const videoFile = file(video)?.video;
165
+ return {
166
+ image: imageFile ? imageFileInfo(imageFile, SHOWCASE_IMAGE_WIDTH) : null,
167
+ video: videoFile ? videoFileInfo(videoFile, SHOWCASE_IMAGE_WIDTH) : null
168
+ };
668
169
  }, {
669
170
  key: ({ handle, namespace, imageKey, videoKey }) => `showcase-media|${namespace}.${imageKey}|${namespace}.${videoKey}|${handle}`,
670
171
  shouldCache: (v) => v !== null,
671
172
  version: getContentVersion
672
173
  });
673
- //#endregion
674
- export { BadgeTagsFragment, fetchProduct, fetchProductFull, fetchProductListDetail, fetchProductShowcase, fetchProductShowcaseMedia, fetchVariantMediaMap, metafieldKeysKey, metafieldKeysVariables };
174
+ export { fetchProduct, fetchProductFull, fetchProductListDetail, fetchProductShowcase, fetchProductShowcaseMedia };