@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/cart.d.ts CHANGED
@@ -1,246 +1,85 @@
1
+ import { StorefrontCart } from "./api-contract/src/storefront/cart.js";
2
+ import "./api-contract/src/storefront/index.js";
1
3
  import { ImageFileInfo } from "./files.js";
2
- import { FragmentOf } from "gql.tada";
3
4
  import { Money } from "@shop-on/theme-runtime/money";
4
- //#region src/cart.d.ts
5
- export type CartLineAttribute = {
6
- name: string;
7
- value: string;
8
- visible: boolean;
9
- };
10
5
  export type CartLine = {
11
6
  id: string;
12
7
  quantity: number;
13
- cost: Money;
14
- /** Buyer-supplied line-item properties (gift message, engraving, …). The
15
- * storefront-scoped field returns visible-only attributes; hidden (`_`-style)
16
- * ones never reach the buyer. Optional on the type so existing `CartLine`
17
- * object literals (test fixtures) still typecheck — `readCartFields` always
18
- * populates it (the API field is non-null), so read it as `?? []`. */
19
- customAttributes?: CartLineAttribute[];
8
+ /** The buyer's line properties (gift message, engraving, …): the visible ones only. */
9
+ customAttributes: {
10
+ name: string;
11
+ value: string;
12
+ }[];
13
+ /** The variant's price now. */
14
+ unitPrice: Money;
15
+ /** Its compare-at price, only while it is above the price (on sale). */
16
+ compareAtPrice: Money | null;
17
+ /** The unit price × quantity. */
18
+ subtotal: Money;
19
+ /** What the cart's discounts take off this line. */
20
+ discount: Money;
21
+ /** Whether the whole cart's demand on this variant can be sold now. */
22
+ available: boolean;
23
+ /** Units the shop can sell; null when nothing bounds them. */
24
+ quantityAvailable: number | null;
20
25
  variant: {
21
26
  id: string;
27
+ /** Its option values joined by " / "; empty without options. */
22
28
  title: string;
23
29
  sku: string | null;
24
- /** Resolved option name/value pairs (`Size` → `XS`). A cart row prints
25
- * these rather than the variant title so a multi-option line reads
26
- * "Size: XS / Color: Black" instead of one opaque "XS / Black" string.
27
- * Empty for a product with no options. Optional on the type for the same
28
- * reason as `customAttributes` — existing fixtures predate the field. */
29
- selectedOptions?: {
30
+ /** `Size` → `XS`: a row prints these rather than the opaque title. */
31
+ selectedOptions: {
30
32
  name: string;
31
33
  value: string;
32
34
  }[];
33
- price: Money;
34
- /** Compare-at (original) price, null when the variant is not on sale. A
35
- * cart row prints it struck through beside the paid price, which is the
36
- * only place a buyer sees the saving after leaving the product page. */
37
- compareAtPrice?: Money | null;
38
- availableForSale: boolean;
39
- quantityAvailable: number | null;
40
- /** Whether the line ships, which the free-shipping threshold counts.
41
- * Optional on the type for the same reason as `selectedOptions`. */
42
- requiresShipping?: boolean;
35
+ /** Whether the line ships, which a free-shipping threshold counts. */
36
+ requiresShipping: boolean;
37
+ image: ImageFileInfo | null;
38
+ };
39
+ product: {
40
+ id: string;
41
+ handle: string;
42
+ /** `/products/<handle>`. */
43
+ url: string;
44
+ title: string;
45
+ vendor: string | null;
46
+ /** The product's first image: a row's fallback when the variant has none. */
43
47
  image: ImageFileInfo | null;
44
- product: {
45
- id: string;
46
- handle: string;
47
- title: string;
48
- /** Merchant vendor, null when unset. Cart UIs that offer a "show vendor"
49
- * setting read it off the line rather than refetching the product. */
50
- vendor: string | null;
51
- /** Product's featured media. Cart UIs use this as a fallback when the
52
- * variant carries no media of its own (common for single-image listings
53
- * where merchants only attach a photo to the product, not each variant). */
54
- image: ImageFileInfo | null;
55
- };
56
48
  };
57
- };
58
- export type CartDiscountAllocation = {
59
- /** Non-null for line-item allocations — matches `CartLine.id`. Null for
60
- * order-level allocations (whole-cart deductions). Shipping allocations
61
- * can't appear at the cart layer; the cart never resolves a shipping rate. */
62
- targetLineItemId: string | null;
63
- amount: Money;
64
49
  };
65
50
  export type CartDiscountApplication = {
66
- id: string | null;
51
+ source: "code" | "automatic";
52
+ class: "product" | "order" | "shipping";
67
53
  title: string;
68
54
  code: string | null;
69
- source: "AUTOMATIC" | "CODE" | "MANUAL";
70
- targetType: "LINE_ITEM" | "ORDER" | "SHIPPING";
71
- allocatedAmount: Money;
72
- allocations: CartDiscountAllocation[];
55
+ amount: Money;
56
+ /** By line; `lineId` null for a deduction off the whole order. */
57
+ allocations: {
58
+ lineId: string | null;
59
+ amount: Money;
60
+ }[];
73
61
  };
74
- export type CartData = {
75
- id: string;
76
- totalQuantity: number;
77
- subtotalAmount: Money;
78
- /** Automatic-discount preview total (product + order class). Cart never
79
- * carries a shipping-class discount preview — no rate is selected yet,
80
- * so the API's `ShopCart.discountAmount` excludes shipping by design.
81
- * Zero when no discount applies. */
82
- discountAmount: Money;
83
- discountApplications: CartDiscountApplication[];
62
+ export type CartData = StorefrontCartFields & {
84
63
  lines: CartLine[];
85
- /** The buyer's cart-level note (gift message, delivery instructions), null
86
- * when unset. `checkoutCreate` copies it onto the checkout. Optional on the
87
- * type for the same reason as `CartLine.customAttributes` — existing
88
- * fixtures predate the field; `readCartFields` always populates it. */
89
- note?: string | null;
90
- };
91
- export declare const CartFields: import("gql.tada").TadaDocumentNode<{
92
- attachedToCurrentCustomer: boolean;
93
- discountAmount: {
94
- amount: string;
95
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
96
- };
97
- discountApplications: {
98
- allocatedAmount: {
99
- amount: string;
100
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
101
- };
102
- allocations: {
103
- amount: {
104
- amount: string;
105
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
106
- };
107
- targetLineItemId: string | null;
108
- }[];
109
- code: string | null;
110
- id: string | null;
111
- source: "AUTOMATIC" | "CODE" | "MANUAL";
112
- targetType: "LINE_ITEM" | "ORDER" | "SHIPPING";
113
- title: string;
114
- }[];
115
- expiresAt: string;
116
- hasCustomer: boolean;
117
- id: string;
118
- lines: {
119
- nodes: {
120
- cost: {
121
- amount: string;
122
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
123
- };
124
- customAttributes: {
125
- name: string;
126
- value: string;
127
- visible: boolean;
128
- }[];
129
- id: string;
130
- quantity: number;
131
- variant: {
132
- availableForSale: boolean;
133
- compareAtV2: {
134
- amount: string;
135
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
136
- } | null;
137
- featuredMedia: {
138
- __typename: "ShopFont";
139
- alt: string | null;
140
- id: string;
141
- } | {
142
- __typename: "ShopMediaImage";
143
- alt: string | null;
144
- focalPoint: {
145
- x: number;
146
- y: number;
147
- } | null;
148
- height: number | null;
149
- id: string;
150
- url: string | null;
151
- url2x: string | null;
152
- width: number | null;
153
- } | {
154
- __typename: "ShopVideo";
155
- alt: string | null;
156
- id: string;
157
- } | null;
158
- id: string;
159
- priceV2: {
160
- amount: string;
161
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
162
- };
163
- product: {
164
- featuredMedia: {
165
- __typename: "ShopFont";
166
- alt: string | null;
167
- id: string;
168
- } | {
169
- __typename: "ShopMediaImage";
170
- alt: string | null;
171
- focalPoint: {
172
- x: number;
173
- y: number;
174
- } | null;
175
- height: number | null;
176
- id: string;
177
- url: string | null;
178
- url2x: string | null;
179
- width: number | null;
180
- } | {
181
- __typename: "ShopVideo";
182
- alt: string | null;
183
- id: string;
184
- } | null;
185
- handle: string;
186
- id: string;
187
- title: string;
188
- vendor: string | null;
189
- };
190
- quantityAvailable: number | null;
191
- requiresShipping: boolean;
192
- selectedOptions: {
193
- name: string;
194
- value: string;
195
- }[];
196
- sku: string | null;
197
- title: string;
198
- };
199
- }[];
200
- };
201
- note: string | null;
202
- subtotalAmount: {
203
- amount: string;
204
- currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
205
- };
206
- totalQuantity: number;
207
- }, {}, {
208
- fragment: "CartFields";
209
- on: "ShopCart";
210
- masked: true;
211
- }>;
212
- /** Exported because a theme's `resolveCart` needs to forward a customer-session
213
- * token via the request helper's third arg (the package-level
214
- * `fetchCartByToken` is anonymous-only). Anonymous callers should prefer
215
- * `fetchCartByToken` — it owns the error swallowing + decode for that path. */
216
- export declare const CartByTokenQuery: import("gql.tada").TadaDocumentNode<{
217
- cart: {
218
- [$tada.fragmentRefs]: {
219
- CartFields: "ShopCart";
220
- };
221
- } | null;
222
- }, {
223
- cartToken: string;
224
- }, void>;
225
- export type CartReadResult = {
226
- cart: CartData;
227
- attachedToCurrentCustomer: boolean;
228
- hasCustomer: boolean;
229
- expiresAt: string;
64
+ discountApplications: CartDiscountApplication[];
65
+ subtotal: Money;
66
+ discount: Money;
67
+ vat: Money;
68
+ total: Money;
230
69
  };
231
- export declare function readCartFields(raw: FragmentOf<typeof CartFields>): CartReadResult;
232
- export declare function decodeCart(raw: FragmentOf<typeof CartFields>): CartData;
233
- /** Tri-state anonymous cart read by token. Distinguishes an authoritatively-
234
- * gone token (`not_found` — expired / merged / unknown; caller should clear its
235
- * `cart_session` cookie and show empty state) from a transient transport
236
- * failure (`error` — caller should keep the cookie and retry later, since
237
- * clearing on a network blip would orphan a still-valid cart). GraphQL failures
238
- * are swallowed (logged), keeping cart resolution progressive enhancement, not
239
- * load-blocking.
240
- *
241
- * Logged-in flow (cart-claim / cart-attach branches) is NOT covered here —
242
- * callers that need ownership-aware resolution layer on top, the way care's
243
- * `resolveCart` does. */
70
+ type StorefrontCartFields = Pick<StorefrontCart, "id" | "note" | "totalQuantity" | "discountCode" | "taxesIncluded" | "warnings">;
71
+ /** The api's cart as a theme reads it. */
72
+ export declare function toCart(cart: StorefrontCart): CartData;
73
+ /** Whether a call failed because its token opens no cart (unknown, expired,
74
+ * placed or merged): the theme's signal to drop its cookie. A NOT_FOUND that
75
+ * names a variant or a line leaves the cart as it was. */
76
+ export declare function isCartGone(error: unknown): boolean;
77
+ /**
78
+ * The cart a token opens, three ways: `not_found` when the api says it opens
79
+ * none (the caller clears its cookie), `error` on any other failure (the
80
+ * caller keeps its cookie: clearing on a blip would orphan a valid cart).
81
+ * A misconfigured storefront still throws.
82
+ */
244
83
  export type CartTokenReadResult = {
245
84
  status: "ok";
246
85
  cart: CartData;
@@ -249,5 +88,5 @@ export type CartTokenReadResult = {
249
88
  } | {
250
89
  status: "error";
251
90
  };
252
- export declare function readCartByToken(cartToken: string, locale?: string): Promise<CartTokenReadResult>;
253
- //#endregion
91
+ export declare function readCartByToken(token: string, locale?: string): Promise<CartTokenReadResult>;
92
+ export {};
package/dist/cart.js CHANGED
@@ -1,209 +1,82 @@
1
- import { graphql, request } from "./graphql.js";
2
- import { toFileInfo } from "./files.js";
3
- import { readFragment } from "gql.tada";
4
- //#region src/cart.ts
5
- const CartFields = graphql(`
6
- fragment CartFields on ShopCart {
7
- id
8
- totalQuantity
9
- note
10
- expiresAt
11
- hasCustomer
12
- attachedToCurrentCustomer
13
- subtotalAmount {
14
- amount
15
- currencyCode
16
- }
17
- discountAmount {
18
- amount
19
- currencyCode
20
- }
21
- discountApplications {
22
- id
23
- title
24
- code
25
- source
26
- targetType
27
- allocatedAmount {
28
- amount
29
- currencyCode
30
- }
31
- allocations {
32
- targetLineItemId
33
- amount {
34
- amount
35
- currencyCode
36
- }
37
- }
38
- }
39
- lines(first: 25) {
40
- nodes {
41
- id
42
- quantity
43
- cost {
44
- amount
45
- currencyCode
46
- }
47
- customAttributes {
48
- name
49
- value
50
- visible
51
- }
52
- variant {
53
- id
54
- title
55
- sku
56
- selectedOptions {
57
- name
58
- value
59
- }
60
- priceV2 {
61
- amount
62
- currencyCode
63
- }
64
- compareAtV2 {
65
- amount
66
- currencyCode
67
- }
68
- availableForSale
69
- quantityAvailable
70
- requiresShipping
71
- featuredMedia {
72
- __typename
73
- id
74
- alt
75
- ... on ShopMediaImage {
76
- url(transform: { maxWidth: 128 })
77
- url2x: url(transform: { maxWidth: 128, scale: 2 })
78
- width
79
- height
80
- focalPoint {
81
- x
82
- y
83
- }
84
- }
85
- }
86
- product {
87
- id
88
- handle
89
- title
90
- vendor
91
- featuredMedia {
92
- __typename
93
- id
94
- alt
95
- ... on ShopMediaImage {
96
- url(transform: { maxWidth: 128 })
97
- url2x: url(transform: { maxWidth: 128, scale: 2 })
98
- width
99
- height
100
- focalPoint {
101
- x
102
- y
103
- }
104
- }
105
- }
106
- }
107
- }
108
- }
109
- }
110
- }
111
- `);
112
- /** Exported because a theme's `resolveCart` needs to forward a customer-session
113
- * token via the request helper's third arg (the package-level
114
- * `fetchCartByToken` is anonymous-only). Anonymous callers should prefer
115
- * `fetchCartByToken` — it owns the error swallowing + decode for that path. */
116
- const CartByTokenQuery = graphql(`
117
- query CartByToken($cartToken: String!) {
118
- cart(cartToken: $cartToken) {
119
- ...CartFields
120
- }
121
- }
122
- `, [CartFields]);
123
- function toMoney(m) {
124
- return {
125
- amount: Number(m.amount),
126
- currencyCode: m.currencyCode
127
- };
1
+ import { imageFileInfo } from "./files.js";
2
+ import { toMoney } from "./money.js";
3
+ import { StorefrontConfigError, errorCode, storefront } from "./storefront.js";
4
+ // src/cart.ts
5
+ /** The width a cart row draws its thumbnail at. */
6
+ const CART_IMAGE_WIDTH = 128;
7
+ function toImage(image) {
8
+ return image ? imageFileInfo(image, CART_IMAGE_WIDTH) : null;
128
9
  }
129
- function readCartFields(raw) {
130
- const data = readFragment(CartFields, raw);
10
+ /** The api's cart as a theme reads it. */
11
+ function toCart(cart) {
131
12
  return {
132
- cart: {
133
- id: data.id,
134
- totalQuantity: data.totalQuantity,
135
- note: data.note ?? null,
136
- subtotalAmount: toMoney(data.subtotalAmount),
137
- discountAmount: toMoney(data.discountAmount),
138
- discountApplications: data.discountApplications.map((app) => ({
139
- id: app.id ?? null,
140
- title: app.title,
141
- code: app.code ?? null,
142
- source: app.source,
143
- targetType: app.targetType,
144
- allocatedAmount: toMoney(app.allocatedAmount),
145
- allocations: app.allocations.map((a) => ({
146
- targetLineItemId: a.targetLineItemId ?? null,
147
- amount: toMoney(a.amount)
148
- }))
149
- })),
150
- lines: data.lines.nodes.map((l) => {
151
- const variantFile = toFileInfo(l.variant.featuredMedia, { baseWidth: 128 });
152
- const productFile = toFileInfo(l.variant.product.featuredMedia, { baseWidth: 128 });
153
- return {
154
- id: l.id,
155
- quantity: l.quantity,
156
- cost: toMoney(l.cost),
157
- customAttributes: (l.customAttributes ?? []).map((a) => ({
158
- name: a.name,
159
- value: a.value,
160
- visible: a.visible
161
- })),
162
- variant: {
163
- id: l.variant.id,
164
- title: l.variant.title,
165
- sku: l.variant.sku,
166
- selectedOptions: (l.variant.selectedOptions ?? []).map((o) => ({
167
- name: o.name,
168
- value: o.value
169
- })),
170
- price: toMoney(l.variant.priceV2),
171
- compareAtPrice: l.variant.compareAtV2 ? toMoney(l.variant.compareAtV2) : null,
172
- availableForSale: l.variant.availableForSale,
173
- quantityAvailable: l.variant.quantityAvailable,
174
- requiresShipping: l.variant.requiresShipping,
175
- image: variantFile?.kind === "image" ? variantFile : null,
176
- product: {
177
- id: l.variant.product.id,
178
- handle: l.variant.product.handle,
179
- title: l.variant.product.title,
180
- vendor: l.variant.product.vendor ?? null,
181
- image: productFile?.kind === "image" ? productFile : null
182
- }
183
- }
184
- };
185
- })
186
- },
187
- attachedToCurrentCustomer: data.attachedToCurrentCustomer,
188
- hasCustomer: data.hasCustomer,
189
- expiresAt: data.expiresAt
13
+ id: cart.id,
14
+ note: cart.note,
15
+ totalQuantity: cart.totalQuantity,
16
+ discountCode: cart.discountCode,
17
+ taxesIncluded: cart.taxesIncluded,
18
+ warnings: cart.warnings,
19
+ subtotal: toMoney(cart.subtotal),
20
+ discount: toMoney(cart.discount),
21
+ vat: toMoney(cart.vat),
22
+ total: toMoney(cart.total),
23
+ discountApplications: cart.discountApplications.map((application) => ({
24
+ source: application.source,
25
+ class: application.class,
26
+ title: application.title,
27
+ code: application.code,
28
+ amount: toMoney(application.amount),
29
+ allocations: application.allocations.map((a) => ({
30
+ lineId: a.lineId,
31
+ amount: toMoney(a.amount)
32
+ }))
33
+ })),
34
+ lines: cart.lines.map((line) => ({
35
+ id: line.id,
36
+ quantity: line.quantity,
37
+ customAttributes: line.customAttributes,
38
+ unitPrice: toMoney(line.unitPrice),
39
+ compareAtPrice: line.compareAtPrice === null ? null : toMoney(line.compareAtPrice),
40
+ subtotal: toMoney(line.subtotal),
41
+ discount: toMoney(line.discount),
42
+ available: line.available,
43
+ quantityAvailable: line.quantityAvailable,
44
+ variant: {
45
+ id: line.variant.id,
46
+ title: line.variant.title,
47
+ sku: line.variant.sku,
48
+ selectedOptions: line.variant.selectedOptions,
49
+ requiresShipping: line.variant.requiresShipping,
50
+ image: toImage(line.variant.image)
51
+ },
52
+ product: {
53
+ id: line.product.id,
54
+ handle: line.product.handle,
55
+ url: line.product.url,
56
+ title: line.product.title,
57
+ vendor: line.product.vendor,
58
+ image: toImage(line.product.image)
59
+ }
60
+ }))
190
61
  };
191
62
  }
192
- function decodeCart(raw) {
193
- return readCartFields(raw).cart;
63
+ /** Whether a call failed because its token opens no cart (unknown, expired,
64
+ * placed or merged): the theme's signal to drop its cookie. A NOT_FOUND that
65
+ * names a variant or a line leaves the cart as it was. */
66
+ function isCartGone(error) {
67
+ return errorCode(error) === "NOT_FOUND" && error.data?.resource === "cart";
194
68
  }
195
- async function readCartByToken(cartToken, locale) {
69
+ async function readCartByToken(token, locale) {
196
70
  try {
197
- const result = await request(CartByTokenQuery, { cartToken }, locale ? { acceptLanguage: locale } : void 0);
198
- if (!result.cart) return { status: "not_found" };
199
71
  return {
200
72
  status: "ok",
201
- cart: decodeCart(result.cart)
73
+ cart: toCart(await storefront(locale).cart.get({ token }))
202
74
  };
203
- } catch (err) {
204
- console.warn("[readCartByToken] failed:", err);
75
+ } catch (error) {
76
+ if (error instanceof StorefrontConfigError) throw error;
77
+ if (isCartGone(error)) return { status: "not_found" };
78
+ console.warn(`[readCartByToken] failed: ${errorCode(error) ?? String(error)}`);
205
79
  return { status: "error" };
206
80
  }
207
81
  }
208
- //#endregion
209
- export { CartByTokenQuery, CartFields, decodeCart, readCartByToken, readCartFields };
82
+ export { isCartGone, readCartByToken, toCart };
@@ -1,25 +1,21 @@
1
- //#region src/checkout.d.ts
2
1
  export type CheckoutHandoffResult = {
3
2
  ok: true;
4
3
  redirectTo: string;
5
4
  } | {
6
5
  ok: false;
7
- reason: "no_cart" | "bad_state";
6
+ reason: "no_cart";
8
7
  };
8
+ /** Whether this buyer has a cart to check out, and where to go with it: the theme's `/checkout`. */
9
+ export declare function checkoutEntry(): Promise<CheckoutHandoffResult>;
9
10
  /**
10
- * The theme's `/checkout`, both passes. `state` is the URL's `checkout_state`,
11
- * a REQUIRED key even when `undefined`: a caller that never passed it would
12
- * bounce theme ↔ `/begin` forever, and this way that is a compile error.
13
- *
14
- * - No state: the URL of checkout's `/begin`, which binds this browser and
15
- * returns the buyer here with one. Mints nothing.
16
- * - A state: mint the cart handoff with it (`mintCheckoutHandoff`).
17
- *
18
- * The customer-session token is injected: forwarded as `X-Customer-Session` on
19
- * the mint so Rails folds the buyer into the handoff payload in one trip.
11
+ * The theme's `GET /{-$locale}/checkout`. A preview (`?preview`) is a 404;
12
+ * a buyer with no cart, an unknown or expired one, or an empty one goes to
13
+ * the storefront's home; a cart already being paid goes to the checkout
14
+ * app, whose cookie still opens its checkout. Anything else the api refuses
15
+ * is thrown (the theme's 500).
20
16
  */
21
- export declare function startCheckoutHandoff(opts: {
22
- state: string | undefined;
23
- customerToken?: string;
24
- }): Promise<CheckoutHandoffResult>;
25
- //#endregion
17
+ export declare function handleCheckoutGet(request: Request, { locale }: {
18
+ locale?: string;
19
+ }): Promise<Response>;
20
+ /** The page that POSTs the checkout token, and a signed-in buyer's handoff token, to the checkout app's `/begin`. Pure. */
21
+ export declare function renderCheckoutHandoffForm(beginUrl: string, token: string, customerHandoff: string | null): string;