@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
@@ -0,0 +1,624 @@
1
+ import { mntNonNegative } from "../../../contract-kit/src/money.js";
2
+ import { areaRef } from "../areas.js";
3
+ import { liteId } from "../ids.js";
4
+ import { imageSchema } from "../image.js";
5
+ import { CART_NOTE_MAX, DISCOUNT_CODE_PATTERN } from "../limits.js";
6
+ import { PICKUP_LEAD_TIMES } from "../pickup.js";
7
+ import { e164Phone, giftCardCode, typedEmail } from "../text.js";
8
+ import { marketingAttribution } from "./analytics.js";
9
+ import { CART_DISCOUNT_ERRORS, cartToken, storefrontCart, storefrontCartLine } from "./cart.js";
10
+ import { storefrontProcedure } from "./procedure.js";
11
+ import { z } from "zod";
12
+ // ../api-contract/src/storefront/checkout.ts
13
+ const read = {
14
+ permission: "storefront.read",
15
+ rateLimit: "storefront"
16
+ };
17
+ const write = {
18
+ permission: "storefront.read",
19
+ rateLimit: "storefront-cart"
20
+ };
21
+ /** The token `checkout.start` answered; the checkout's only key. */
22
+ const checkoutToken = z.string().min(1).max(64);
23
+ const CHECKOUT_WARNINGS = [
24
+ "MERCHANDISE_OUT_OF_STOCK",
25
+ "MERCHANDISE_NOT_ENOUGH_STOCK",
26
+ "MERCHANDISE_PRICE_CHANGED"
27
+ ];
28
+ /**
29
+ * The eBarimt receipt the buyer asks for: a person's, or an organization's
30
+ * under its 7-digit register. The register may be null while the buyer has
31
+ * chosen an organization and not typed it yet (kept across a reload, legacy's
32
+ * receipt_type without receipt_org_register); placement refuses that
33
+ * (DETAILS_REQUIRED `receipt`, legacy RECEIPT_INFO_REQUIRED).
34
+ */
35
+ const checkoutReceipt = z.discriminatedUnion("kind", [z.object({ kind: z.literal("personal") }), z.object({
36
+ kind: z.literal("organization"),
37
+ organizationRegister: z.string().regex(/^[0-9]{7}$/).nullable()
38
+ })]);
39
+ const text = z.string().trim().max(255);
40
+ /** The ship-to address as the form holds it: every field may still be empty (placement checks it is complete). */
41
+ const checkoutAddressInput = z.object({
42
+ firstName: text.nullable().optional(),
43
+ lastName: text.nullable().optional(),
44
+ company: text.nullable().optional(),
45
+ phone: e164Phone.nullable().optional(),
46
+ address1: text.nullable().optional(),
47
+ address2: text.nullable().optional(),
48
+ city: text.nullable().optional(),
49
+ /** The most specific area chosen (khoroo, district, province or country). */
50
+ areaId: liteId("area").nullable().optional(),
51
+ postalCode: z.string().regex(/^[0-9A-Za-z -]{3,12}$/).nullable().optional(),
52
+ /** Directions to the door. */
53
+ instructions: z.string().trim().max(500).nullable().optional(),
54
+ coordinates: z.object({
55
+ latitude: z.number().min(-90).max(90),
56
+ longitude: z.number().min(-180).max(180)
57
+ }).nullable().optional(),
58
+ /** Placement adds the address to the signed-in buyer's address book (legacy "save this address"); only theirs may ask. */
59
+ saveToCustomer: z.boolean().optional()
60
+ });
61
+ const checkoutAddress = z.object({
62
+ firstName: z.string().nullable(),
63
+ lastName: z.string().nullable(),
64
+ company: z.string().nullable(),
65
+ phone: z.string().nullable(),
66
+ address1: z.string().nullable(),
67
+ address2: z.string().nullable(),
68
+ city: z.string().nullable(),
69
+ /** The area chosen and its path, the country first and the area itself last. */
70
+ area: z.object({
71
+ id: liteId("area"),
72
+ path: z.array(areaRef)
73
+ }).nullable(),
74
+ postalCode: z.string().nullable(),
75
+ instructions: z.string().nullable(),
76
+ coordinates: z.object({
77
+ latitude: z.number(),
78
+ longitude: z.number()
79
+ }).nullable()
80
+ });
81
+ /** One way the order can reach the buyer, as offered for the checkout as it stands. */
82
+ const deliveryOption = z.object({
83
+ /** What `delivery.select` names: a rate's handle, `__lite_combined`, `pickup_<location>`, `__lite_none`. */
84
+ handle: z.string(),
85
+ /** `none`: nothing in the checkout ships. */
86
+ kind: z.enum([
87
+ "ship",
88
+ "pickup",
89
+ "none"
90
+ ]),
91
+ /** In the buyer's language. */
92
+ title: z.string(),
93
+ amount: mntNonNegative,
94
+ deliveryDetails: z.string().nullable(),
95
+ transitMinDays: z.int().nonnegative().nullable(),
96
+ transitMaxDays: z.int().nonnegative().nullable(),
97
+ pickup: z.object({
98
+ locationId: liteId("location"),
99
+ /** The location's name. */
100
+ name: z.string(),
101
+ leadTime: z.enum(PICKUP_LEAD_TIMES),
102
+ instructions: z.string(),
103
+ /** Where the buyer comes to (legacy `addressLines`: these, then the city and the postal code). */
104
+ address1: z.string().nullable(),
105
+ address2: z.string().nullable(),
106
+ /** Its area's province (or the capital), in Mongolian; null without an area. */
107
+ city: z.string().nullable(),
108
+ postalCode: z.string().nullable(),
109
+ /** The location's map pin; null without one. */
110
+ coordinates: z.object({
111
+ latitude: z.number(),
112
+ longitude: z.number()
113
+ }).nullable()
114
+ }).nullable(),
115
+ /**
116
+ * The courier whose price `amount` is (a rate it calculates); null for a
117
+ * flat rate. Such a delivery is paid online: the courier carries paid
118
+ * orders only, so no cash on delivery.
119
+ */
120
+ calculatedBy: z.enum(["ubcab"]).nullable()
121
+ });
122
+ /**
123
+ * What stands between the buyer and a delivery a courier prices: `PIN`
124
+ * (drop a pin on the address), `PHONE` (a phone the courier can dial),
125
+ * `UNAVAILABLE` (the courier cannot price it right now).
126
+ */
127
+ const COURIER_PROMPTS = [
128
+ "PIN",
129
+ "PHONE",
130
+ "UNAVAILABLE"
131
+ ];
132
+ /**
133
+ * A bundle on the checkout (legacy `lineGroups`): what the buyer bought as
134
+ * one line, over its component lines (the lines whose `lineGroup` it is).
135
+ * Its price is the bundle variant's now; each component is charged its
136
+ * share of it, and the components' subtotals add up to its `subtotal`.
137
+ */
138
+ const checkoutLineGroup = z.object({
139
+ /** The cart's id for the bundle's line. */
140
+ id: liteId("basket_line"),
141
+ /** The bundle's product title. */
142
+ title: z.string(),
143
+ /** Its option values' names, joined by " / "; empty without options. */
144
+ variantTitle: z.string(),
145
+ selectedOptions: z.array(z.object({
146
+ name: z.string(),
147
+ value: z.string()
148
+ })),
149
+ /** Bundles bought. */
150
+ quantity: z.int().positive(),
151
+ /** The visible custom attributes, held once for the bundle. */
152
+ customAttributes: z.array(z.object({
153
+ name: z.string(),
154
+ value: z.string()
155
+ })),
156
+ /** One bundle's price now. */
157
+ unitPrice: mntNonNegative,
158
+ /** Its compare-at price, only above the price. */
159
+ compareAtUnitPrice: mntNonNegative.nullable(),
160
+ /** The bundle's price the buyer was shown when checkout started or they last accepted a change; MERCHANDISE_PRICE_CHANGED on its first component while it is not `unitPrice`. */
161
+ quotedUnitPrice: mntNonNegative,
162
+ /** `unitPrice` × `quantity`: its components' subtotals together. */
163
+ subtotal: mntNonNegative,
164
+ /** What the discounts take off its components together. */
165
+ discount: mntNonNegative,
166
+ image: storefrontCartLine.shape.variant.shape.image,
167
+ variant: z.object({ id: liteId("variant") }),
168
+ product: z.object({
169
+ id: liteId("prod"),
170
+ handle: z.string(),
171
+ url: z.string()
172
+ })
173
+ });
174
+ const storefrontCheckoutLine = storefrontCartLine.extend({
175
+ /** The unit price the buyer was shown when checkout started or they last accepted a change (a component's: its share). */
176
+ quotedUnitPrice: mntNonNegative,
177
+ /** On a bundle's component: the bundle, as `lineGroups` holds it. Its `unitPrice` is its share of the bundle's price, its `quantity` real units (bundles × `bundleItemQuantity`) and its attributes the bundle's. */
178
+ lineGroup: checkoutLineGroup.optional(),
179
+ /** On a bundle's component: its units in one bundle. */
180
+ bundleItemQuantity: z.int().positive().optional()
181
+ });
182
+ /** A gift card the checkout applies: shown by the code's last four characters, never the code. */
183
+ const checkoutGiftCard = z.object({
184
+ id: liteId("gift_card"),
185
+ codeSuffix: z.string(),
186
+ /** What it pays now: its balance up to what the cards applied before it leave; 0 when nothing is left, or it can no longer be spent. */
187
+ amount: mntNonNegative,
188
+ /** What it paid as the checkout's last write showed it. Placement refuses (GIFT_CARD_CHANGED) while it is not `amount`: any write, `drift.accept` among them, takes `amount`. */
189
+ quotedAmount: mntNonNegative
190
+ });
191
+ /** The signed-in buyer's store credit on their own checkout. */
192
+ const checkoutStoreCredit = z.object({
193
+ /** What the buyer can spend now: their credit not expired, less what their other payments hold. */
194
+ available: mntNonNegative,
195
+ /** What it pays now; 0 when not applied. */
196
+ amount: mntNonNegative,
197
+ /** What it paid as the checkout's last write showed it. Placement refuses (STORE_CREDIT_CHANGED) while it is not `amount`. */
198
+ quotedAmount: mntNonNegative,
199
+ /** Whether the buyer may apply it here: the shop lets buyers spend it and the checkout sells no gift card. */
200
+ usable: z.boolean(),
201
+ /** Whether the shop lets buyers spend it: with it on and `usable` off, the checkout sells a gift card (store value does not buy store value). */
202
+ enabled: z.boolean()
203
+ });
204
+ /** One of the signed-in buyer's saved addresses, as the form copies it into the checkout's. */
205
+ const checkoutSavedAddress = checkoutAddress.extend({
206
+ id: liteId("customer_address"),
207
+ isDefault: z.boolean(),
208
+ /** A saved address always names its area. */
209
+ area: z.object({
210
+ id: liteId("area"),
211
+ path: z.array(areaRef)
212
+ })
213
+ });
214
+ const storefrontCheckout = z.object({
215
+ id: liteId("basket"),
216
+ /** `paying` once a payment is under way: then every write is refused. */
217
+ status: z.enum(["checkout", "paying"]),
218
+ /** The cart's, never extended. */
219
+ expiresAt: z.iso.datetime(),
220
+ note: z.string().nullable(),
221
+ totalQuantity: z.int().nonnegative(),
222
+ /** Fixed while in checkout. A line whose variant was deleted is left out (and warned out of stock); a bundle's components are lines (`lineGroup`), and a bundle missing its variant or an item's is left out whole (warned on its first component). */
223
+ lines: z.array(storefrontCheckoutLine),
224
+ /** The bundles on the checkout; their component lines are in `lines`. */
225
+ lineGroups: z.array(checkoutLineGroup),
226
+ /** Whether any line ships: false, and `delivery` offers `__lite_none` alone. */
227
+ requiresShipping: z.boolean(),
228
+ /** Whether a line sells a download (legacy `emailRequired`): placement then needs an email to send it to (DETAILS_REQUIRED `email`). */
229
+ requiresEmail: z.boolean(),
230
+ /** How to reach the buyer, and the receipt asked for; null until set. */
231
+ contact: z.object({
232
+ email: z.string().nullable(),
233
+ phone: z.string().nullable(),
234
+ receipt: checkoutReceipt
235
+ }).nullable(),
236
+ address: checkoutAddress.extend({
237
+ /** Whether placement adds it to the buyer's address book. */
238
+ saveToCustomer: z.boolean() }).nullable(),
239
+ /**
240
+ * The signed-in buyer whose checkout it is (their session came in
241
+ * `x-buyer-session`), with their saved addresses, the default first; null
242
+ * for anyone else, a guest's checkout, and a customer since erased.
243
+ */
244
+ customer: z.object({
245
+ email: z.string().nullable(),
246
+ phone: z.string().nullable(),
247
+ firstName: z.string().nullable(),
248
+ lastName: z.string().nullable(),
249
+ addresses: z.array(checkoutSavedAddress)
250
+ }).nullable(),
251
+ delivery: z.object({
252
+ /** What the checkout can be delivered by now: no ship option without an address with an area. */
253
+ options: z.array(deliveryOption),
254
+ /** The option chosen, one of `options`; null when none is offered (or only pickup, not picked). */
255
+ selected: z.string().nullable(),
256
+ /** Null when nothing stands in the way, or no shipping address is set yet. */
257
+ courierPrompt: z.enum(COURIER_PROMPTS).nullable()
258
+ }),
259
+ /** As a cart's; a free-delivery code applies once a delivery it covers is selected. */
260
+ discountCode: z.object({
261
+ code: z.string(),
262
+ error: z.enum(CART_DISCOUNT_ERRORS).nullable()
263
+ }).nullable(),
264
+ /** Every discount the checkout takes, in the order taken; a free-delivery one's allocation has no line. */
265
+ discountApplications: storefrontCart.shape.discountApplications,
266
+ /** The lines' subtotals together. */
267
+ subtotal: mntNonNegative,
268
+ /** What the discounts take off the lines. */
269
+ discount: mntNonNegative,
270
+ /** The selected delivery's price. */
271
+ shipping: mntNonNegative,
272
+ /** What a free-delivery discount takes off it. */
273
+ shippingDiscount: mntNonNegative,
274
+ /** The VAT on the lines and, when the shop taxes it, the delivery, after their discounts. */
275
+ vat: mntNonNegative,
276
+ taxesIncluded: z.boolean(),
277
+ /** Subtotal less discount, plus shipping less its discount, plus the VAT unless included. */
278
+ total: mntNonNegative,
279
+ /** The gift cards applied, in the order they cover the total. */
280
+ giftCards: z.array(checkoutGiftCard),
281
+ /** The session's buyer's store credit on their own checkout; null for anyone else and a guest's checkout. */
282
+ storeCredit: checkoutStoreCredit.nullable(),
283
+ /** The total less what the gift cards and store credit pay: what is left to pay by a payment method. 0: nothing is due, `place` places it paid. */
284
+ amountDue: mntNonNegative,
285
+ /** Stock as a cart's, a bundle's per component; MERCHANDISE_PRICE_CHANGED while a line's price is not its `quotedUnitPrice` (a bundle's once, on its first component). */
286
+ warnings: z.array(z.object({
287
+ code: z.enum(CHECKOUT_WARNINGS),
288
+ lineId: liteId("basket_line")
289
+ }))
290
+ });
291
+ const opened = { token: checkoutToken };
292
+ storefrontProcedure.meta(write).route({
293
+ summary: "Start a checkout from a cart",
294
+ description: "Takes the cart into checkout, its lines fixed from now, each line's price as the buyer sees it now kept as its quoted price (a bundle's, and each of its components' share; a bundle is bought as its variant holds it now); answers a new checkout token, shown in this answer only (an earlier one stops opening), and the checkout priced. Refused: a token that opens no cart, NOT_FOUND `{ resource: \"cart\" }`; a cart with no line, PRECONDITION_FAILED `sales.cart_empty`; one being paid, `sales.cart_paying`. A line out of stock is warned, not refused. Any cart write after this ends the checkout. `attribution` is the buyer's ad ids, kept on the cart for the order's purchase conversion: Meta's `fbp` and `fbc` (a field sent before the same id in `cookies`), GA4's client id and the sessions of the shop's own GA4 streams (nothing of GA4 for a shop without a GA4 pixel), each kept only in its vendor's shape; what survives replaces what the cart held, and nothing surviving keeps it."
295
+ }).input(z.object({
296
+ cartToken,
297
+ attribution: marketingAttribution.optional()
298
+ })).output(z.object({
299
+ token: z.string(),
300
+ checkout: storefrontCheckout
301
+ }));
302
+ storefrontProcedure.meta(read).route({
303
+ summary: "Read a checkout",
304
+ description: "The checkout the token opens, priced now with the delivery offered and selected as it stands. NOT_FOUND `{ resource: \"checkout\" }` when it opens none."
305
+ }).input(z.object(opened)).output(storefrontCheckout);
306
+ storefrontProcedure.meta(write).route({
307
+ summary: "Set the checkout's contact",
308
+ description: "Names what it changes: an email or phone left out is kept, null clears it; `receipt` left out is kept. The contact needs an email or a phone (VALIDATION_FAILED `email_or_phone_required` on `email`)."
309
+ }).input(z.object({
310
+ ...opened,
311
+ email: typedEmail.nullable().optional(),
312
+ phone: e164Phone.nullable().optional(),
313
+ receipt: checkoutReceipt.optional()
314
+ })).output(storefrontCheckout);
315
+ storefrontProcedure.meta(write).route({
316
+ summary: "Set the checkout's ship-to address",
317
+ description: "Replaces the address whole (a field left out is empty); null removes it. An area geo lacks is NOT_FOUND `{ resource: \"area\", id }`. The delivery options are quoted again for it: the choice is kept while still offered, else the cheapest that is not a pickup. `saveToCustomer` asks placement to add the address to the buyer's address book (skipped when the book holds the same address or 5 already): only the signed-in buyer whose checkout it is may ask, anyone else is PRECONDITION_FAILED `sales.checkout_not_yours`."
318
+ }).input(z.object({
319
+ ...opened,
320
+ address: checkoutAddressInput.nullable()
321
+ })).output(storefrontCheckout);
322
+ storefrontProcedure.meta(write).route({
323
+ summary: "Choose the checkout's delivery",
324
+ description: "Selects one of the options the checkout offers now. One it does not offer is DELIVERY_OPTION_UNAVAILABLE and changes nothing."
325
+ }).errors({ DELIVERY_OPTION_UNAVAILABLE: {
326
+ status: 409,
327
+ data: z.object({ handle: z.string() })
328
+ } }).input(z.object({
329
+ ...opened,
330
+ handle: z.string().min(1).max(200)
331
+ })).output(storefrontCheckout);
332
+ storefrontProcedure.meta(write).route({
333
+ summary: "Enter or remove the checkout's discount code",
334
+ description: "As the cart's: kept upper-cased whether or not it applies, `discountCode.error` saying why it takes nothing; null removes it. The cart shows the same code."
335
+ }).input(z.object({
336
+ ...opened,
337
+ code: z.string().trim().min(1).max(64).regex(DISCOUNT_CODE_PATTERN).transform((code) => code.toUpperCase()).nullable()
338
+ })).output(storefrontCheckout);
339
+ storefrontProcedure.meta(write).route({
340
+ summary: "Set the checkout's note",
341
+ description: `The buyer's note, as the cart's: trimmed, at most ${CART_NOTE_MAX} characters, null or blank clears it.`
342
+ }).input(z.object({
343
+ ...opened,
344
+ note: z.string().trim().max(CART_NOTE_MAX).nullable().transform((note) => note || null)
345
+ })).output(storefrontCheckout);
346
+ storefrontProcedure.meta(write).route({
347
+ summary: "Accept the checkout's price changes",
348
+ description: "Takes each line's price now as its quoted price: the MERCHANDISE_PRICE_CHANGED warnings go. The buyer has seen the new prices (the checkout already charges them). Each gift card's `amount` becomes its `quotedAmount` too, as on every write."
349
+ }).input(z.object(opened)).output(storefrontCheckout);
350
+ storefrontProcedure.meta(write).route({
351
+ summary: "Sign the buyer out of the checkout",
352
+ description: "Revokes the session sent in `x-buyer-session` and, when the checkout is that buyer's, makes it a guest's: the customer, the contact, the address, the delivery chosen and the gift cards go; the lines, the note and the discount code stay (legacy started a new guest checkout from the same cart). Another customer's checkout or a guest's is left as it is. UNAUTHENTICATED without a signed-in buyer; PRECONDITION_FAILED `sales.checkout_paying` while a payment is under way, when nothing changes."
353
+ }).input(z.object(opened)).output(storefrontCheckout);
354
+ storefrontProcedure.meta(write).route({
355
+ summary: "Apply a gift card to the checkout",
356
+ description: `Applies the shop's gift card the code opens, after those already applied (one already applied is answered as it is). Nothing is taken from the card until the order is placed or a Cody Pay payment starts. A code that opens no card the buyer can spend now (none, switched off, expired, nothing left on it, not yet sent) is GIFT_CARD_UNAVAILABLE, the same for each; past 5 cards, LIMIT_EXCEEDED. A card the buyer can spend is then refused, applying nothing, with PRECONDITION_FAILED \`sales.gift_card_tender_not_allowed\` when the checkout sells a gift card (store value does not buy store value), \`payments.nothing_due\` when the cards before it leave nothing to pay; a card applied earlier pays 0 while the total no longer needs it. A code that opens no card is a guess: 10 an hour per buyer address and 200 per shop, counted before the code is looked up (RATE_LIMITED past either, the right code too), only failed ones kept.`
357
+ }).errors({
358
+ GIFT_CARD_UNAVAILABLE: {
359
+ status: 422,
360
+ data: z.object({})
361
+ },
362
+ LIMIT_EXCEEDED: {
363
+ status: 422,
364
+ data: z.object({ limit: z.int() })
365
+ }
366
+ }).input(z.object({
367
+ ...opened,
368
+ code: giftCardCode
369
+ })).output(storefrontCheckout);
370
+ storefrontProcedure.meta(write).route({
371
+ summary: "Remove a gift card from the checkout",
372
+ description: "Takes the gift card off the checkout; the cards after it cover what it paid as far as their balances go. A card not applied changes nothing."
373
+ }).input(z.object({
374
+ ...opened,
375
+ giftCardId: liteId("gift_card")
376
+ })).output(storefrontCheckout);
377
+ storefrontProcedure.meta(write).route({
378
+ summary: "Pay with store credit",
379
+ description: "Applies the signed-in buyer's store credit to their checkout: it pays what the gift cards leave, as far as the credit goes (whole tögrög when it pays part). Nothing is taken until the order is placed or a Cody Pay payment starts. UNAUTHENTICATED without a buyer; PRECONDITION_FAILED `sales.checkout_not_yours` on another's checkout, `sales.store_credit_disabled` when the shop does not let buyers spend it, `sales.store_credit_gift_card` when the checkout sells a gift card, `sales.checkout_paying` while a payment is under way; NO_STORE_CREDIT when it would pay nothing."
380
+ }).errors({ NO_STORE_CREDIT: {
381
+ status: 422,
382
+ data: z.object({})
383
+ } }).input(z.object(opened)).output(storefrontCheckout);
384
+ storefrontProcedure.meta(write).route({
385
+ summary: "Stop paying with store credit",
386
+ description: "Takes the store credit off the checkout, whoever reads it; one without changes nothing. PRECONDITION_FAILED `sales.checkout_paying` while a payment is under way."
387
+ }).input(z.object(opened)).output(storefrontCheckout);
388
+ /** What the checkout is missing before it can become an order. */
389
+ const PLACEMENT_DETAILS = [
390
+ "contact",
391
+ "email",
392
+ "address",
393
+ "phone",
394
+ "receipt"
395
+ ];
396
+ /**
397
+ * Why a checkout cannot become an order as it stands, decided under its
398
+ * lock with every price, the delivery and the discounts taken again:
399
+ * nothing is written, and the checkout read after shows what changed.
400
+ */
401
+ const placementErrors = {
402
+ /** A line's variant was deleted or left the store. */
403
+ MERCHANDISE_UNAVAILABLE: {
404
+ status: 409,
405
+ data: z.object({ lineId: liteId("basket_line") })
406
+ },
407
+ /** Prices moved since the buyer last saw them: `drift.accept`, then try again. */
408
+ PRICE_CHANGED: {
409
+ status: 409,
410
+ data: z.object({ lineIds: z.array(liteId("basket_line")) })
411
+ },
412
+ /** A line's units are no longer there to hold. */
413
+ INSUFFICIENT_INVENTORY: {
414
+ status: 409,
415
+ data: z.object({ lineId: liteId("basket_line") })
416
+ },
417
+ /** Something ships and no delivery is chosen. */
418
+ DELIVERY_REQUIRED: {
419
+ status: 409,
420
+ data: z.object({})
421
+ },
422
+ /** The chosen delivery is no longer offered as it was quoted (its price or routing moved). */
423
+ DELIVERY_CHANGED: {
424
+ status: 409,
425
+ data: z.object({})
426
+ },
427
+ /** The entered code applied when quoted and no longer does: remove it or try again. */
428
+ DISCOUNT_REFUSED: {
429
+ status: 409,
430
+ data: z.object({ error: z.enum(CART_DISCOUNT_ERRORS) })
431
+ },
432
+ /** A gift card pays other than the checkout last showed (its balance moved, it was switched off or expired): read the checkout, `drift.accept`, then try again. Nothing was taken from any card. */
433
+ GIFT_CARD_CHANGED: {
434
+ status: 409,
435
+ data: z.object({ giftCardIds: z.array(liteId("gift_card")) })
436
+ },
437
+ /** The store credit pays other than the checkout last showed (the credit moved, the buyer's session is not the checkout's, the shop stopped it, or the checkout sells a gift card): read the checkout, then try again. Nothing was taken. */
438
+ STORE_CREDIT_CHANGED: {
439
+ status: 409,
440
+ data: z.object({})
441
+ },
442
+ /**
443
+ * `contact`: neither an email nor a phone; `email`: a download needs one;
444
+ * `address`: a shipped order needs its first line and area; `phone`: an
445
+ * order handed over needs one (the contact's or the address's).
446
+ */
447
+ DETAILS_REQUIRED: {
448
+ status: 422,
449
+ data: z.object({ detail: z.enum(PLACEMENT_DETAILS) })
450
+ }
451
+ };
452
+ /** The ways a checkout may be paid: through Cody Pay's hosted page, or off it (cash on delivery, or the merchant's own way). */
453
+ const CHECKOUT_PAYMENT_METHODS = [
454
+ "cody_pay",
455
+ "cash_on_delivery",
456
+ "manual"
457
+ ];
458
+ /** How a placed order's money came: a checkout's way, a gift card the checkout applied, or the buyer's store credit. */
459
+ const ORDER_PAYMENT_METHODS = [
460
+ ...CHECKOUT_PAYMENT_METHODS,
461
+ "gift_card",
462
+ "store_credit"
463
+ ];
464
+ const ORDER_PAYMENT_STATUSES = [
465
+ "pending",
466
+ "partially_paid",
467
+ "paid",
468
+ "partially_refunded",
469
+ "refunded",
470
+ "voided"
471
+ ];
472
+ /** A bundle on an order (legacy `lineGroups`): what the buyer bought as one line, over its component lines. */
473
+ const storefrontOrderLineGroup = z.object({
474
+ id: liteId("order_line_group"),
475
+ /** The bundle's product title, as sold. */
476
+ title: z.string(),
477
+ variantTitle: z.string().nullable(),
478
+ /** Bundles bought. */
479
+ quantity: z.int().positive(),
480
+ /** One bundle's price as sold. */
481
+ unitPrice: mntNonNegative,
482
+ compareAtUnitPrice: mntNonNegative.nullable(),
483
+ /** `unitPrice` × `quantity`: its components' together. */
484
+ subtotal: mntNonNegative,
485
+ /** What the discounts took off its components together. */
486
+ discount: mntNonNegative
487
+ });
488
+ /** A bundle on the thank-you page: with the picture its row shows. */
489
+ const placedLineGroup = storefrontOrderLineGroup.extend({
490
+ /** Its variant's first image, else its product's (a video's poster); null without one. */
491
+ image: imageSchema.nullable() });
492
+ /** An order as its buyer sees it on the thank-you page. */
493
+ const storefrontOrder = z.object({
494
+ id: liteId("order"),
495
+ /** Shown as `#<number>`. */
496
+ number: z.int().positive(),
497
+ placedAt: z.iso.datetime(),
498
+ email: z.string().nullable(),
499
+ phone: z.string().nullable(),
500
+ /** The signed-in buyer's own first name, for the greeting: only when the session reading it is the order's customer; null otherwise. */
501
+ customer: z.object({ firstName: z.string().nullable() }).nullable(),
502
+ /** The buyer's note to the shop. */
503
+ note: z.string().nullable(),
504
+ /** Who the eBarimt receipt is made out to: a person, or an organization by its register. */
505
+ receipt: z.discriminatedUnion("kind", [z.object({ kind: z.literal("personal") }), z.object({
506
+ kind: z.literal("organization"),
507
+ organizationRegister: z.string()
508
+ })]),
509
+ /** The order's eBarimt receipt: the one in force, else (none issued) the latest attempt; null while none was asked for. */
510
+ fiscalReceipt: z.object({
511
+ status: z.enum([
512
+ "pending",
513
+ "issued",
514
+ "failed",
515
+ "voided",
516
+ "void_failed"
517
+ ]),
518
+ ddtd: z.string().nullable(),
519
+ lottery: z.string().nullable(),
520
+ qrData: z.string().nullable(),
521
+ /** As the tax authority dated it ("yyyy-MM-dd HH:mm:ss"). */
522
+ issuedDate: z.string().nullable()
523
+ }).nullable(),
524
+ /** The code the buyer gives the shop's driver at the door: while a driver of the shop may still bring it; null otherwise. */
525
+ deliveryCode: z.string().nullable(),
526
+ /** Every discount the order took, in the order taken, with what it took off. */
527
+ discountApplications: z.array(z.object({
528
+ title: z.string(),
529
+ code: z.string().nullable(),
530
+ amount: mntNonNegative
531
+ })),
532
+ /** Its money: `pending` until something is captured, `paid` once all of it is. */
533
+ paymentStatus: z.enum(ORDER_PAYMENT_STATUSES),
534
+ /** The payments taken or awaited, one per gift card that paid; none for an order with nothing to pay. */
535
+ payments: z.array(z.object({
536
+ method: z.enum(ORDER_PAYMENT_METHODS),
537
+ status: z.enum([
538
+ "pending",
539
+ "awaiting_payment",
540
+ "captured"
541
+ ]),
542
+ amount: mntNonNegative,
543
+ /** A gift card's: the code's last four characters. */
544
+ giftCard: z.object({ codeSuffix: z.string() }).nullable()
545
+ })),
546
+ lines: z.array(z.object({
547
+ id: liteId("order_line"),
548
+ productTitle: z.string(),
549
+ variantTitle: z.string().nullable(),
550
+ sku: z.string().nullable(),
551
+ quantity: z.int().positive(),
552
+ unitPrice: mntNonNegative,
553
+ /** What its discounts took off it. */
554
+ discount: mntNonNegative,
555
+ /** Its variant's first image, else its product's (a video's poster); null without one. */
556
+ image: imageSchema.nullable(),
557
+ /** The custom attributes a buyer sees (none hidden, none named with a leading underscore). */
558
+ customAttributes: z.array(z.object({
559
+ name: z.string(),
560
+ value: z.string()
561
+ })),
562
+ /** On a bundle's component: the bundle, as `lineGroups` holds it; its `unitPrice` is its share, its `quantity` real units. */
563
+ lineGroup: placedLineGroup.optional(),
564
+ /** On a bundle's component: its units in one bundle. */
565
+ bundleItemQuantity: z.int().positive().optional()
566
+ })),
567
+ /** The bundles bought; their component lines are in `lines`. */
568
+ lineGroups: z.array(placedLineGroup),
569
+ delivery: z.object({
570
+ method: z.enum([
571
+ "ship",
572
+ "pickup",
573
+ "none"
574
+ ]),
575
+ /** As the buyer chose it; null when nothing is delivered. */
576
+ title: z.string().nullable(),
577
+ address: checkoutAddress.omit({
578
+ area: true,
579
+ instructions: true
580
+ }).extend({ areaId: liteId("area") }).nullable(),
581
+ pickupLocationId: liteId("location").nullable()
582
+ }),
583
+ subtotal: mntNonNegative,
584
+ discount: mntNonNegative,
585
+ shipping: mntNonNegative,
586
+ shippingDiscount: mntNonNegative,
587
+ vat: mntNonNegative,
588
+ taxesIncluded: z.boolean(),
589
+ total: mntNonNegative,
590
+ /** What was captured so far. */
591
+ paid: mntNonNegative
592
+ });
593
+ storefrontProcedure.meta(read).route({
594
+ summary: "The ways a checkout may be paid",
595
+ description: "`cody_pay` when the shop collects through Cody Pay (`pay`); `cash_on_delivery` when it takes cash on delivery or at pickup, else `manual` when it has no Cody Pay either (`place`: the merchant collects their own way). Nothing due (`amountDue` 0 on `checkout.get`: a total of 0, or gift cards paying all of it) needs no method: `place` places it paid whatever the shop takes. NOT_FOUND `{ resource: \"checkout\" }` when the token opens none."
596
+ }).input(z.object(opened)).output(z.object({ methods: z.array(z.enum(CHECKOUT_PAYMENT_METHODS)) }));
597
+ storefrontProcedure.meta(write).route({
598
+ summary: "Place the order, paid off Cody Pay",
599
+ description: "Turns the checkout into an order at once: each gift card's share taken from it (a captured `gift_card` payment each), the store credit's from the buyer's credit (a captured `store_credit` payment), the rest (`amountDue`) owed by cash on delivery (or at pickup) when the shop takes it, else by the merchant's own way; an order with nothing due is born paid. Everything is priced again under the checkout's lock and compared with what the buyer was shown (the placement errors), the stock held for each line and passed to the order. Refused with PRECONDITION_FAILED `payments.payment_required` when something is due and the shop collects through Cody Pay and takes no cash on delivery (use `pay`), `sales.gift_card_tender_not_allowed` when the checkout sells a gift card and a gift card pays part of it (one applied before the gift card was added to the cart: remove it), `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial. The checkout token then reads the order (`order`)."
600
+ }).errors(placementErrors).input(z.object(opened)).output(storefrontOrder);
601
+ storefrontProcedure.meta(write).route({
602
+ summary: "Pay the checkout through Cody Pay",
603
+ description: "Checks the checkout as `place` does, holds its stock, its gift cards' shares (taken from the cards now, given back if the charge ends unpaid) and its store credit (held from the buyer's credit until the charge ends: spent when it is paid, given back when it is not), and opens a Cody Pay charge for the rest (`amountDue`): answers the hosted page to send the buyer to. The checkout is `paying` from then until Cody Pay reports the charge: paid places the order (the token then reads it, `order`), expired gives the checkout back. Refused with PRECONDITION_FAILED `payments.cody_pay_unavailable` when the shop does not collect through Cody Pay, `payments.nothing_due` when nothing is due (use `place`), `payments.amount_not_whole_tugrik` for an amount due under one tögrög (Cody Pay charges whole tögrög: an amount due with möngö is charged its whole tögrög, the möngö dropped, and the charge paid pays the order in full),`sales.gift_card_tender_not_allowed` as `place`, `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial; NO_PAYMENT_METHOD when the shop has every Cody Pay method switched off (nothing is held); PROVIDER_UNAVAILABLE when Cody Pay does not open the charge (the checkout is given back)."
604
+ }).errors({
605
+ ...placementErrors,
606
+ NO_PAYMENT_METHOD: {
607
+ status: 409,
608
+ data: z.object({})
609
+ },
610
+ PROVIDER_UNAVAILABLE: {
611
+ status: 503,
612
+ data: z.object({})
613
+ }
614
+ }).input(z.object(opened)).output(z.object({ payment: z.object({
615
+ id: liteId("payment"),
616
+ /** Cody Pay's hosted page: send the buyer there. */
617
+ checkoutUrl: z.string(),
618
+ expiresAt: z.iso.datetime()
619
+ }) }));
620
+ storefrontProcedure.meta(read).route({
621
+ summary: "Read the order a checkout became",
622
+ description: "The order the checkout token's checkout was placed as, for 30 days after: the thank-you page. NOT_FOUND `{ resource: \"order\" }` while it is not placed (a checkout still `paying` reads with `get`) or past those days. Answered whatever the shop's status: the buyer keeps the confirmation of an order placed before the shop closed."
623
+ }).input(z.object(opened)).output(storefrontOrder);
624
+ export { CHECKOUT_PAYMENT_METHODS, CHECKOUT_WARNINGS, COURIER_PROMPTS, ORDER_PAYMENT_METHODS, ORDER_PAYMENT_STATUSES, PLACEMENT_DETAILS, checkoutAddress, checkoutAddressInput, checkoutGiftCard, checkoutLineGroup, checkoutReceipt, checkoutSavedAddress, checkoutStoreCredit, checkoutToken, deliveryOption, storefrontCheckout, storefrontCheckoutLine, storefrontOrder, storefrontOrderLineGroup };
@@ -0,0 +1,3 @@
1
+ import { RpcClient } from "../../../contract-kit/src/client.js";
2
+ import { StorefrontContract } from "./index.js";
3
+ export type StorefrontClient = RpcClient<StorefrontContract>;
@@ -0,0 +1,6 @@
1
+ import { createRpcClient } from "../../../contract-kit/src/client.js";
2
+ // ../api-contract/src/storefront/client.ts
3
+ function createStorefrontClient(options) {
4
+ return createRpcClient("storefront", "omit", options);
5
+ }
6
+ export { createStorefrontClient };
@@ -0,0 +1,3 @@
1
+ import "../custom-data.js";
2
+ import "../../../contract-kit/src/index.js";
3
+ import "zod";
@@ -0,0 +1,2 @@
1
+ import "../custom-data.js";
2
+ import "zod";
@@ -0,0 +1,27 @@
1
+ import { liteId } from "../ids.js";
2
+ import { metafieldReferences, metafieldValue, metaobjectFieldValue } from "../custom-data.js";
3
+ import { productMedia } from "../media.js";
4
+ import { z } from "zod";
5
+ // ../api-contract/src/storefront/custom-data.ts
6
+ /** A metafield key as a theme names it: `custom.material`. */
7
+ const metafieldRef = z.string().regex(/^[a-z][a-z0-9_-]*\.[a-z][a-z0-9_-]*$/);
8
+ /**
9
+ * What the values name, as the merchant's references, a file carrying what
10
+ * a product's media carries: `image` for an image, `video` (with its
11
+ * poster) for a video, else neither; `url` the file itself (null until it
12
+ * is ready).
13
+ */
14
+ const storefrontReferences = metafieldReferences.extend({ files: z.array(metafieldReferences.shape.files.element.extend({
15
+ image: productMedia.shape.image,
16
+ video: productMedia.shape.video
17
+ })) });
18
+ z.object({
19
+ values: z.array(metafieldValue),
20
+ /** The fields of each entry a value references. */
21
+ entries: z.array(z.object({
22
+ id: liteId("metaobject"),
23
+ fields: z.array(metaobjectFieldValue)
24
+ })),
25
+ references: storefrontReferences
26
+ });
27
+ export { metafieldRef, storefrontReferences };