@shop-on/theme-server 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/dist/account-state.js +1 -2
  2. package/dist/account.d.ts +1 -3
  3. package/dist/account.js +1 -3
  4. package/dist/activity.d.ts +1 -3
  5. package/dist/activity.js +1 -2
  6. package/dist/analytics-proxy.d.ts +8 -14
  7. package/dist/analytics-proxy.js +111 -28
  8. package/dist/analytics-script-proxy.d.ts +1 -5
  9. package/dist/analytics-script-proxy.js +4 -14
  10. package/dist/api-contract/src/addresses.js +32 -0
  11. package/dist/api-contract/src/areas.js +49 -0
  12. package/dist/api-contract/src/custom-data.d.ts +31 -0
  13. package/dist/api-contract/src/custom-data.js +303 -0
  14. package/dist/api-contract/src/ids.js +110 -0
  15. package/dist/api-contract/src/image.js +65 -0
  16. package/dist/api-contract/src/limits.js +44 -0
  17. package/dist/api-contract/src/media.js +13 -0
  18. package/dist/api-contract/src/pickup.js +11 -0
  19. package/dist/api-contract/src/recommendations.js +4 -0
  20. package/dist/api-contract/src/shops.js +27 -0
  21. package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
  22. package/dist/api-contract/src/storefront/analytics.js +138 -0
  23. package/dist/api-contract/src/storefront/areas.d.ts +2 -0
  24. package/dist/api-contract/src/storefront/cart.d.ts +132 -0
  25. package/dist/api-contract/src/storefront/cart.js +248 -0
  26. package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
  27. package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
  28. package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
  29. package/dist/api-contract/src/storefront/checkout.js +624 -0
  30. package/dist/api-contract/src/storefront/client.d.ts +3 -0
  31. package/dist/api-contract/src/storefront/client.js +6 -0
  32. package/dist/api-contract/src/storefront/content.d.ts +3 -0
  33. package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
  34. package/dist/api-contract/src/storefront/custom-data.js +27 -0
  35. package/dist/api-contract/src/storefront/customer.d.ts +2 -0
  36. package/dist/api-contract/src/storefront/customer.js +261 -0
  37. package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
  38. package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
  39. package/dist/api-contract/src/storefront/forms.d.ts +2 -0
  40. package/dist/api-contract/src/storefront/forms.js +82 -0
  41. package/dist/api-contract/src/storefront/index.d.ts +20956 -0
  42. package/dist/api-contract/src/storefront/orders.d.ts +2 -0
  43. package/dist/api-contract/src/storefront/policies.d.ts +2 -0
  44. package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
  45. package/dist/api-contract/src/storefront/procedure.js +29 -0
  46. package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
  47. package/dist/api-contract/src/storefront/search.d.ts +3 -0
  48. package/dist/api-contract/src/storefront/seo.d.ts +2 -0
  49. package/dist/api-contract/src/storefront/seo.js +132 -0
  50. package/dist/api-contract/src/storefront/shop.d.ts +2 -0
  51. package/dist/api-contract/src/storefront/shop.js +101 -0
  52. package/dist/api-contract/src/taxonomy.js +5 -0
  53. package/dist/api-contract/src/text.js +15 -0
  54. package/dist/api-contract/src/theme-documents.js +13 -0
  55. package/dist/api-contract/src/theme-translations.d.ts +14 -0
  56. package/dist/api-contract/src/theme-translations.js +30 -0
  57. package/dist/api-contract/src/video.js +19 -0
  58. package/dist/availability.d.ts +6 -3
  59. package/dist/availability.js +11 -5
  60. package/dist/blog.d.ts +53 -59
  61. package/dist/blog.js +197 -497
  62. package/dist/cache.d.ts +5 -6
  63. package/dist/cache.js +1 -2
  64. package/dist/canonical-host.d.ts +4 -6
  65. package/dist/canonical-host.js +5 -6
  66. package/dist/card.js +159 -0
  67. package/dist/cart-customer.d.ts +1 -3
  68. package/dist/cart-customer.js +64 -105
  69. package/dist/cart-mutations.d.ts +43 -77
  70. package/dist/cart-mutations.js +106 -232
  71. package/dist/cart-recommendations.d.ts +1 -3
  72. package/dist/cart-recommendations.js +1 -2
  73. package/dist/cart-session.d.ts +1 -3
  74. package/dist/cart-session.js +1 -2
  75. package/dist/cart.d.ts +63 -224
  76. package/dist/cart.js +70 -197
  77. package/dist/checkout.d.ts +13 -17
  78. package/dist/checkout.js +75 -101
  79. package/dist/coalesce.js +57 -0
  80. package/dist/collection.d.ts +89 -298
  81. package/dist/collection.js +210 -656
  82. package/dist/contact.d.ts +20 -10
  83. package/dist/contact.js +36 -30
  84. package/dist/content-version.d.ts +37 -21
  85. package/dist/content-version.js +64 -42
  86. package/dist/contract-kit/src/client.d.ts +12 -0
  87. package/dist/contract-kit/src/client.js +156 -0
  88. package/dist/contract-kit/src/errors.d.ts +1 -0
  89. package/dist/contract-kit/src/errors.js +81 -0
  90. package/dist/contract-kit/src/ids.d.ts +1 -0
  91. package/dist/contract-kit/src/ids.js +118 -0
  92. package/dist/contract-kit/src/index.d.ts +7 -0
  93. package/dist/contract-kit/src/money.d.ts +1 -0
  94. package/dist/contract-kit/src/money.js +10 -0
  95. package/dist/contract-kit/src/pagination.d.ts +1 -0
  96. package/dist/contract-kit/src/procedure.d.ts +69 -0
  97. package/dist/contract-kit/src/procedure.js +16 -0
  98. package/dist/contract-kit/src/realtime.d.ts +1 -0
  99. package/dist/cookie-secret.js +1 -2
  100. package/dist/csrf.js +1 -2
  101. package/dist/customer-handoff.d.ts +1 -3
  102. package/dist/customer-handoff.js +10 -25
  103. package/dist/customer-session.d.ts +1 -3
  104. package/dist/customer-session.js +1 -2
  105. package/dist/customer.d.ts +2 -3
  106. package/dist/customer.js +27 -44
  107. package/dist/files.d.ts +49 -10
  108. package/dist/files.js +106 -89
  109. package/dist/fonts.d.ts +3 -5
  110. package/dist/fonts.js +16 -35
  111. package/dist/html.d.ts +1 -3
  112. package/dist/html.js +1 -2
  113. package/dist/liquid.d.ts +4 -10
  114. package/dist/liquid.js +25 -32
  115. package/dist/listing.js +79 -0
  116. package/dist/marketing-cookies.d.ts +1 -3
  117. package/dist/marketing-cookies.js +1 -2
  118. package/dist/menu.d.ts +46 -155
  119. package/dist/menu.js +51 -61
  120. package/dist/metafields.d.ts +9 -5
  121. package/dist/metafields.js +10 -38
  122. package/dist/metaobject.d.ts +10 -5
  123. package/dist/metaobject.js +43 -66
  124. package/dist/money.d.ts +7 -0
  125. package/dist/money.js +18 -0
  126. package/dist/newsletter.d.ts +19 -10
  127. package/dist/newsletter.js +31 -25
  128. package/dist/page.d.ts +13 -17
  129. package/dist/page.js +49 -105
  130. package/dist/password-session.d.ts +1 -3
  131. package/dist/password-session.js +1 -2
  132. package/dist/password.d.ts +25 -18
  133. package/dist/password.js +41 -59
  134. package/dist/policy.d.ts +7 -7
  135. package/dist/policy.js +19 -55
  136. package/dist/product.d.ts +42 -67
  137. package/dist/product.js +144 -644
  138. package/dist/read-ahead.d.ts +18 -0
  139. package/dist/read-ahead.js +32 -0
  140. package/dist/recommendations.d.ts +6 -12
  141. package/dist/recommendations.js +36 -60
  142. package/dist/review.d.ts +134 -94
  143. package/dist/review.js +257 -469
  144. package/dist/search.d.ts +59 -64
  145. package/dist/search.js +170 -469
  146. package/dist/seo-context.d.ts +1 -3
  147. package/dist/seo-context.js +4 -5
  148. package/dist/seo-documents.d.ts +50 -0
  149. package/dist/seo-documents.js +149 -0
  150. package/dist/shop-seo.d.ts +3 -5
  151. package/dist/shop-seo.js +7 -26
  152. package/dist/shop.d.ts +38 -42
  153. package/dist/shop.js +37 -51
  154. package/dist/store-availability.d.ts +12 -14
  155. package/dist/store-availability.js +18 -56
  156. package/dist/storefront.js +199 -0
  157. package/dist/structured-data.d.ts +3 -28
  158. package/dist/structured-data.js +19 -49
  159. package/dist/trace.js +1 -2
  160. package/dist/translations.d.ts +26 -31
  161. package/dist/translations.js +55 -28
  162. package/package.json +16 -14
  163. package/dist/graphql-env.d.ts +0 -22778
  164. package/dist/graphql.d.ts +0 -64
  165. package/dist/graphql.js +0 -344
  166. package/dist/schema.storefront.graphql +0 -10483
  167. package/dist/seo-proxy.d.ts +0 -11
  168. package/dist/seo-proxy.js +0 -26
@@ -0,0 +1,138 @@
1
+ import { mntNonNegative } from "../../../contract-kit/src/money.js";
2
+ import { liteId } from "../ids.js";
3
+ import { ANALYTICS_NUMBER_MAX, ANALYTICS_URL_SENT_MAX } from "../limits.js";
4
+ import { RECOMMENDATION_INTENTS } from "../recommendations.js";
5
+ import { storefrontProcedure } from "./procedure.js";
6
+ import { z } from "zod";
7
+ // ../api-contract/src/storefront/analytics.ts
8
+ /**
9
+ * A token a buyer's browser mints (a visit, a visitor, an event): a
10
+ * UUIDv7, not a wire id. Its time is when it was minted, and the log's key:
11
+ * a beacon sent again lands on the same row.
12
+ */
13
+ const analyticsToken = z.uuidv7();
14
+ /** No text the log keeps may hold a NUL: the database's text refuses one. */
15
+ const NO_NUL = /^[^\u0000]*$/;
16
+ /** A count or a 0-based place in a list. */
17
+ const count = z.int().min(0).max(ANALYTICS_NUMBER_MAX);
18
+ const handle = z.string().min(1).max(255).regex(NO_NUL);
19
+ /** A listing a card was shown in: a collection's page, or the search page. */
20
+ const analyticsProductList = z.object({
21
+ kind: z.enum(["collection", "search"]),
22
+ collectionId: liteId("collection").optional()
23
+ });
24
+ /** One event of `name`, under its own `id`. */
25
+ const event = (name, shape) => z.object({
26
+ id: analyticsToken,
27
+ name: z.literal(name),
28
+ ...shape
29
+ });
30
+ const recommendation = {
31
+ /** The recommended product. */
32
+ productId: liteId("prod"),
33
+ productHandle: handle.optional(),
34
+ /** The product whose page showed the rail. */
35
+ referenceProductId: liteId("prod"),
36
+ referenceProductHandle: handle.optional(),
37
+ intent: z.enum(RECOMMENDATION_INTENTS),
38
+ /** The card's 0-based place in the rail. */
39
+ index: count.optional()
40
+ };
41
+ const cartLine = {
42
+ productId: liteId("prod"),
43
+ variantId: liteId("variant"),
44
+ quantity: z.int().min(1).max(ANALYTICS_NUMBER_MAX)
45
+ };
46
+ /**
47
+ * The events a storefront records, by `name`: GA4's ecommerce events. The
48
+ * checkout's (`checkout_*_submitted`, `payment_initiated`,
49
+ * `checkout_completed`) are the checkout app's, not a storefront's:
50
+ * `analytics.checkout.events.record` (checkout-analytics.ts).
51
+ */
52
+ const storefrontEvent = z.discriminatedUnion("name", [
53
+ event("search_submitted", {
54
+ /** As the buyer typed it, trimmed. */
55
+ query: z.string().trim().min(1).max(200).regex(NO_NUL),
56
+ /** How many products matched; none when not known. */
57
+ resultCount: count.optional()
58
+ }),
59
+ event("collection_viewed", {
60
+ collectionId: liteId("collection"),
61
+ collectionHandle: handle.optional()
62
+ }),
63
+ event("product_list_viewed", {
64
+ list: analyticsProductList,
65
+ resultCount: count.optional(),
66
+ /** The cards shown, each with its 0-based place. */
67
+ items: z.array(z.object({
68
+ productId: liteId("prod"),
69
+ index: count
70
+ })).max(100)
71
+ }),
72
+ event("product_clicked", {
73
+ productId: liteId("prod"),
74
+ productHandle: handle.optional(),
75
+ list: analyticsProductList.optional(),
76
+ /** The card's 0-based place in its list. */
77
+ index: count.optional()
78
+ }),
79
+ event("product_viewed", {
80
+ productId: liteId("prod"),
81
+ productHandle: handle.optional()
82
+ }),
83
+ event("recommendation_shown", recommendation),
84
+ event("recommendation_clicked", recommendation),
85
+ event("product_added_to_cart", cartLine),
86
+ event("product_removed_from_cart", cartLine),
87
+ event("cart_viewed", {
88
+ lineCount: count,
89
+ totalQuantity: count,
90
+ /** In möngö. */
91
+ subtotal: mntNonNegative
92
+ }),
93
+ event("checkout_started", {
94
+ totalQuantity: count,
95
+ /** In möngö. */
96
+ subtotal: mntNonNegative
97
+ })
98
+ ]);
99
+ const pageUrl = z.string().max(ANALYTICS_URL_SENT_MAX);
100
+ /** A marketing hint of the buyer's request: past what is kept it is dropped, not refused. */
101
+ const hint = z.string().max(ANALYTICS_URL_SENT_MAX);
102
+ /**
103
+ * What the shop's pixels' server-side conversions match the buyer by, from
104
+ * the buyer's request: Meta's `_fbp` and `_fbc`, and the allowlisted ad
105
+ * cookies as one `name=value; …` string. Checked by the api: a value not in
106
+ * its vendor's shape is dropped, never refused.
107
+ */
108
+ const marketingAttribution = z.object({
109
+ fbp: hint.optional(),
110
+ fbc: hint.optional(),
111
+ cookies: hint.optional()
112
+ });
113
+ storefrontProcedure.meta({
114
+ permission: "storefront.read",
115
+ rateLimit: "storefront-analytics"
116
+ }).route({
117
+ summary: "Record a visit",
118
+ description: "Logs a buyer's visit as it begins: the browser's visit and visitor tokens (UUIDv7), the page it landed on and the page that referred it (each kept only as an http or https URL of at most 2,048 characters; the referrer not when it is on the landing page's host). The landing page's `utm_*` values, the referrer's host, the buyer's network (its address cut to /24 or /48) and its device, browser and OS (from the request's User-Agent) are derived here. A visit is one row: sent again, it is not written again, and the first one's landing page stays. `recorded` is whether this call wrote it: false too for a bot's user agent, or a visit token minted more than a day from the api's clock. VALIDATION_FAILED for a token that is not a UUIDv7."
119
+ }).input(z.object({
120
+ visitId: analyticsToken,
121
+ visitorId: analyticsToken,
122
+ landingUrl: pageUrl.optional(),
123
+ referrerUrl: pageUrl.optional()
124
+ })).output(z.object({ recorded: z.boolean() }));
125
+ storefrontProcedure.meta({
126
+ permission: "storefront.read",
127
+ rateLimit: "storefront-analytics"
128
+ }).route({
129
+ summary: "Record events",
130
+ description: `Logs up to 20 funnel events of a visit, each under the id the browser minted (a UUIDv7, whose time is the event's): an event sent again is not written again. \`recorded\` is how many this call wrote: an event already written, one whose id was minted more than a day from the api's clock, and every event of a bot's user agent are not. Ids are not looked up. VALIDATION_FAILED for an event of another name or shape, or a token that is not a UUIDv7; the whole call then writes nothing.`
131
+ }).input(z.object({
132
+ visitId: analyticsToken,
133
+ visitorId: analyticsToken,
134
+ events: z.array(storefrontEvent).min(1).max(20),
135
+ /** The buyer's attribution, and the page the beacon came from (its Referer). */
136
+ marketing: marketingAttribution.extend({ eventSourceUrl: hint.optional() }).optional()
137
+ })).output(z.object({ recorded: z.int().min(0) }));
138
+ export { analyticsProductList, analyticsToken, marketingAttribution, storefrontEvent };
@@ -0,0 +1,2 @@
1
+ import "../../../contract-kit/src/index.js";
2
+ import "zod";
@@ -0,0 +1,132 @@
1
+ import "../../../contract-kit/src/index.js";
2
+ import { z } from "zod";
3
+ export declare const storefrontCart: z.ZodObject<{
4
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`basket_${string}`>, "id:basket", "inout">;
5
+ expiresAt: z.ZodISODateTime;
6
+ note: z.ZodNullable<z.ZodString>;
7
+ totalQuantity: z.ZodInt;
8
+ lines: z.ZodArray<z.ZodObject<{
9
+ unitPrice: z.ZodInt;
10
+ compareAtPrice: z.ZodNullable<z.ZodInt>;
11
+ subtotal: z.ZodInt;
12
+ discount: z.ZodInt;
13
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`basket_line_${string}`>, "id:basket_line", "inout">;
14
+ quantity: z.ZodInt;
15
+ customAttributes: z.ZodArray<z.ZodObject<{
16
+ name: z.ZodString;
17
+ value: z.ZodString;
18
+ }, z.core.$strip>>;
19
+ available: z.ZodBoolean;
20
+ quantityAvailable: z.ZodNullable<z.ZodInt>;
21
+ variant: z.ZodObject<{
22
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`variant_${string}`>, "id:variant", "inout">;
23
+ title: z.ZodString;
24
+ sku: z.ZodNullable<z.ZodString>;
25
+ selectedOptions: z.ZodArray<z.ZodObject<{
26
+ name: z.ZodString;
27
+ value: z.ZodString;
28
+ }, z.core.$strip>>;
29
+ requiresShipping: z.ZodBoolean;
30
+ image: z.ZodNullable<z.ZodObject<{
31
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`file_${string}`>, "id:file", "inout">;
32
+ url: z.ZodString;
33
+ sized: z.ZodNullable<z.ZodString>;
34
+ width: z.ZodInt;
35
+ height: z.ZodInt;
36
+ alt: z.ZodNullable<z.ZodString>;
37
+ thumbhash: z.ZodNullable<z.ZodString>;
38
+ focalPoint: z.ZodNullable<z.ZodObject<{
39
+ x: z.ZodNumber;
40
+ y: z.ZodNumber;
41
+ }, z.core.$strip>>;
42
+ }, z.core.$strip>>;
43
+ }, z.core.$strip>;
44
+ product: z.ZodObject<{
45
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`prod_${string}`>, "id:prod", "inout">;
46
+ handle: z.ZodString;
47
+ url: z.ZodString;
48
+ title: z.ZodString;
49
+ vendor: z.ZodNullable<z.ZodString>;
50
+ image: z.ZodNullable<z.ZodObject<{
51
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`file_${string}`>, "id:file", "inout">;
52
+ url: z.ZodString;
53
+ sized: z.ZodNullable<z.ZodString>;
54
+ width: z.ZodInt;
55
+ height: z.ZodInt;
56
+ alt: z.ZodNullable<z.ZodString>;
57
+ thumbhash: z.ZodNullable<z.ZodString>;
58
+ focalPoint: z.ZodNullable<z.ZodObject<{
59
+ x: z.ZodNumber;
60
+ y: z.ZodNumber;
61
+ }, z.core.$strip>>;
62
+ }, z.core.$strip>>;
63
+ }, z.core.$strip>;
64
+ bundleItems: z.ZodOptional<z.ZodArray<z.ZodObject<{
65
+ quantity: z.ZodInt;
66
+ variant: z.ZodObject<{
67
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`variant_${string}`>, "id:variant", "inout">;
68
+ title: z.ZodString;
69
+ image: z.ZodNullable<z.ZodObject<{
70
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`file_${string}`>, "id:file", "inout">;
71
+ url: z.ZodString;
72
+ sized: z.ZodNullable<z.ZodString>;
73
+ width: z.ZodInt;
74
+ height: z.ZodInt;
75
+ alt: z.ZodNullable<z.ZodString>;
76
+ thumbhash: z.ZodNullable<z.ZodString>;
77
+ focalPoint: z.ZodNullable<z.ZodObject<{
78
+ x: z.ZodNumber;
79
+ y: z.ZodNumber;
80
+ }, z.core.$strip>>;
81
+ }, z.core.$strip>>;
82
+ }, z.core.$strip>;
83
+ product: z.ZodObject<{
84
+ id: z.core.$ZodBranded<z.ZodTemplateLiteral<`prod_${string}`>, "id:prod", "inout">;
85
+ handle: z.ZodString;
86
+ title: z.ZodString;
87
+ }, z.core.$strip>;
88
+ }, z.core.$strip>>>;
89
+ }, z.core.$strip>>;
90
+ discountCode: z.ZodNullable<z.ZodObject<{
91
+ code: z.ZodString;
92
+ error: z.ZodNullable<z.ZodEnum<{
93
+ DISCOUNT_EXPIRED: "DISCOUNT_EXPIRED";
94
+ DISCOUNT_INACTIVE: "DISCOUNT_INACTIVE";
95
+ DISCOUNT_MINIMUM_NOT_MET: "DISCOUNT_MINIMUM_NOT_MET";
96
+ DISCOUNT_NOT_ELIGIBLE: "DISCOUNT_NOT_ELIGIBLE";
97
+ DISCOUNT_USAGE_LIMIT_REACHED: "DISCOUNT_USAGE_LIMIT_REACHED";
98
+ }>>;
99
+ atCheckout: z.ZodBoolean;
100
+ }, z.core.$strip>>;
101
+ discountApplications: z.ZodArray<z.ZodObject<{
102
+ source: z.ZodEnum<{
103
+ automatic: "automatic";
104
+ code: "code";
105
+ }>;
106
+ class: z.ZodEnum<{
107
+ order: "order";
108
+ product: "product";
109
+ shipping: "shipping";
110
+ }>;
111
+ title: z.ZodString;
112
+ code: z.ZodNullable<z.ZodString>;
113
+ amount: z.ZodInt;
114
+ allocations: z.ZodArray<z.ZodObject<{
115
+ lineId: z.ZodNullable<z.core.$ZodBranded<z.ZodTemplateLiteral<`basket_line_${string}`>, "id:basket_line", "inout">>;
116
+ amount: z.ZodInt;
117
+ }, z.core.$strip>>;
118
+ }, z.core.$strip>>;
119
+ subtotal: z.ZodInt;
120
+ discount: z.ZodInt;
121
+ vat: z.ZodInt;
122
+ taxesIncluded: z.ZodBoolean;
123
+ total: z.ZodInt;
124
+ warnings: z.ZodArray<z.ZodObject<{
125
+ code: z.ZodEnum<{
126
+ MERCHANDISE_NOT_ENOUGH_STOCK: "MERCHANDISE_NOT_ENOUGH_STOCK";
127
+ MERCHANDISE_OUT_OF_STOCK: "MERCHANDISE_OUT_OF_STOCK";
128
+ }>;
129
+ lineId: z.core.$ZodBranded<z.ZodTemplateLiteral<`basket_line_${string}`>, "id:basket_line", "inout">;
130
+ }, z.core.$strip>>;
131
+ }, z.core.$strip>;
132
+ export type StorefrontCart = z.infer<typeof storefrontCart>;
@@ -0,0 +1,248 @@
1
+ import { mntNonNegative } from "../../../contract-kit/src/money.js";
2
+ import { liteId } from "../ids.js";
3
+ import { imageSchema } from "../image.js";
4
+ import { CART_LINE_QUANTITY_MAX, CART_NOTE_MAX, DISCOUNT_CODE_PATTERN, LINE_ATTRIBUTE_VALUE_MAX } from "../limits.js";
5
+ import { storefrontProcedure } from "./procedure.js";
6
+ import { z } from "zod";
7
+ // ../api-contract/src/storefront/cart.ts
8
+ const read = {
9
+ permission: "storefront.read",
10
+ rateLimit: "storefront"
11
+ };
12
+ const write = {
13
+ permission: "storefront.read",
14
+ rateLimit: "storefront-cart"
15
+ };
16
+ /** The token `cart.create` answered; the cart's only key. */
17
+ const cartToken = z.string().min(1).max(64);
18
+ const quantity = z.int().min(1).max(CART_LINE_QUANTITY_MAX);
19
+ /**
20
+ * A line's custom attributes as the buyer sent them (legacy
21
+ * Shop::LineItemAttributes): a name trimmed, a blank one dropped, a name
22
+ * sent twice kept once with its last value, `visible` true unless false.
23
+ * Hidden ones are kept for the merchant and never answered to a buyer.
24
+ */
25
+ const lineAttributes = z.array(z.object({
26
+ name: z.string().trim().max(100),
27
+ value: z.string().max(LINE_ATTRIBUTE_VALUE_MAX),
28
+ visible: z.boolean().default(true)
29
+ })).max(20).transform((attributes) => [...new Map(attributes.filter((a) => a.name !== "").map((a) => [a.name, a])).values()]);
30
+ /** A refusal that leaves the cart as it was, naming the variant the call named whose stock refused it (a bundle's, for its items). */
31
+ const variantRefusal = z.object({ variantId: liteId("variant") });
32
+ const pricedLine = {
33
+ /** The variant's price now. */
34
+ unitPrice: mntNonNegative,
35
+ /** Its compare-at price, only above the price (on sale). */
36
+ compareAtPrice: mntNonNegative.nullable(),
37
+ /** The unit price × quantity. */
38
+ subtotal: mntNonNegative,
39
+ /** What the cart's discounts take off this line. */
40
+ discount: mntNonNegative
41
+ };
42
+ const storefrontCartLine = z.object({
43
+ id: liteId("basket_line"),
44
+ quantity: z.int().positive(),
45
+ /** The visible custom attributes, in the order sent. */
46
+ customAttributes: z.array(z.object({
47
+ name: z.string(),
48
+ value: z.string()
49
+ })),
50
+ ...pricedLine,
51
+ /** Whether the whole cart's demand on this variant can be sold now: false once its product is not on the store. */
52
+ available: z.boolean(),
53
+ /** Units available across the shop's active locations; null when nothing bounds them. */
54
+ quantityAvailable: z.int().nonnegative().nullable(),
55
+ variant: z.object({
56
+ id: liteId("variant"),
57
+ /** Its option values' names, joined by " / "; empty without options. */
58
+ title: z.string(),
59
+ sku: z.string().nullable(),
60
+ selectedOptions: z.array(z.object({
61
+ name: z.string(),
62
+ value: z.string()
63
+ })),
64
+ requiresShipping: z.boolean(),
65
+ /** Its first media's image (a video's poster); null without one. */
66
+ image: imageSchema.nullable()
67
+ }),
68
+ product: z.object({
69
+ id: liteId("prod"),
70
+ handle: z.string(),
71
+ /** `/products/<handle>`. */
72
+ url: z.string(),
73
+ title: z.string(),
74
+ vendor: z.string().nullable(),
75
+ /** Its first media's image; null without one. */
76
+ image: imageSchema.nullable()
77
+ }),
78
+ /**
79
+ * A bundle's line only (absent on any other): what one bundle holds, each
80
+ * item's variant and its units per bundle, in the bundle's order; an item
81
+ * whose product buyers cannot see now is left out. The line itself is the
82
+ * bundle: its variant, price, quantity (in bundles), attributes and
83
+ * availability (every item's, the whole cart's demand counted) are the
84
+ * bundle's.
85
+ */
86
+ bundleItems: z.array(z.object({
87
+ quantity: z.int().positive(),
88
+ variant: z.object({
89
+ id: liteId("variant"),
90
+ title: z.string(),
91
+ image: imageSchema.nullable()
92
+ }),
93
+ product: z.object({
94
+ id: liteId("prod"),
95
+ handle: z.string(),
96
+ title: z.string()
97
+ })
98
+ })).optional()
99
+ });
100
+ const CART_DISCOUNT_ERRORS = [
101
+ "DISCOUNT_NOT_ELIGIBLE",
102
+ "DISCOUNT_INACTIVE",
103
+ "DISCOUNT_EXPIRED",
104
+ "DISCOUNT_MINIMUM_NOT_MET",
105
+ "DISCOUNT_USAGE_LIMIT_REACHED"
106
+ ];
107
+ const CART_WARNINGS = ["MERCHANDISE_OUT_OF_STOCK", "MERCHANDISE_NOT_ENOUGH_STOCK"];
108
+ /** Whose a cart is, as `cart.get` tells the signed-in buyer (or nobody). */
109
+ const CART_OWNERS = [
110
+ "guest",
111
+ "you",
112
+ "other"
113
+ ];
114
+ const storefrontCart = z.object({
115
+ id: liteId("basket"),
116
+ /** Set when the cart was made, never extended. */
117
+ expiresAt: z.iso.datetime(),
118
+ note: z.string().nullable(),
119
+ /** The lines' quantities together, a bundle's in bundles. */
120
+ totalQuantity: z.int().nonnegative(),
121
+ /** In the order added. A line whose variant was deleted is left out (the next write removes it). */
122
+ lines: z.array(storefrontCartLine),
123
+ /**
124
+ * The code the buyer entered, kept whether or not it applies: `error` why
125
+ * it takes nothing now (only four reasons are told apart, so codes cannot be
126
+ * enumerated), null while it applies; `atCheckout` for a free-delivery
127
+ * code, which waits for a delivery to be chosen.
128
+ */
129
+ discountCode: z.object({
130
+ code: z.string(),
131
+ error: z.enum(CART_DISCOUNT_ERRORS).nullable(),
132
+ atCheckout: z.boolean()
133
+ }).nullable(),
134
+ /** Every discount the cart takes, in the order taken. */
135
+ discountApplications: z.array(z.object({
136
+ source: z.enum(["code", "automatic"]),
137
+ class: z.enum([
138
+ "product",
139
+ "order",
140
+ "shipping"
141
+ ]),
142
+ title: z.string(),
143
+ code: z.string().nullable(),
144
+ amount: mntNonNegative,
145
+ allocations: z.array(z.object({
146
+ lineId: liteId("basket_line").nullable(),
147
+ amount: mntNonNegative
148
+ }))
149
+ })),
150
+ /** The lines' subtotals together. */
151
+ subtotal: mntNonNegative,
152
+ /** What the discounts take off the lines. */
153
+ discount: mntNonNegative,
154
+ /** The VAT on the lines after their discounts (no delivery is chosen in a cart). */
155
+ vat: mntNonNegative,
156
+ /** Whether the VAT is inside the prices (else `total` adds it). */
157
+ taxesIncluded: z.boolean(),
158
+ /** Subtotal less discount, plus the VAT unless included. */
159
+ total: mntNonNegative,
160
+ /** A line whose variant cannot sell what the cart asks of it now (legacy's cart warnings). */
161
+ warnings: z.array(z.object({
162
+ code: z.enum(CART_WARNINGS),
163
+ lineId: liteId("basket_line")
164
+ }))
165
+ });
166
+ const cartErrors = {
167
+ /** Selling `quantity` more would ask past what the variant can sell. */
168
+ INSUFFICIENT_INVENTORY: {
169
+ status: 409,
170
+ data: variantRefusal
171
+ } };
172
+ storefrontProcedure.meta(write).route({
173
+ summary: "Open a cart",
174
+ description: "A new, empty cart and its token, shown in this answer only: the storefront keeps it (its cookie) and names the cart by it from then on. The cart lives 30 days from now, never extended. With a signed-in buyer (`x-buyer-session`) the cart is theirs; one who has a live cart already is CONFLICT `{ resource: \"cart\" }`: claim it (`cart.claim`)."
175
+ }).input(z.object({})).output(z.object({
176
+ token: z.string(),
177
+ cart: storefrontCart
178
+ }));
179
+ storefrontProcedure.meta(read).route({
180
+ summary: "Read a cart",
181
+ description: "The cart the token opens, priced now, and whose it is: `guest` (nobody's), `you` (the signed-in buyer's, `x-buyer-session`) or `other` (another customer's, or any customer's when nobody is signed in). NOT_FOUND `{ resource: \"cart\" }` when it opens none (unknown, expired, placed or merged)."
182
+ }).input(z.object({ token: cartToken })).output(storefrontCart.extend({ owner: z.enum(CART_OWNERS) }));
183
+ storefrontProcedure.meta(write).route({
184
+ summary: "Claim the signed-in buyer's cart",
185
+ description: "The signed-in buyer's live cart (on another device, or behind a cleared cookie) with a new token, shown in this answer only; the token before it opens nothing from now on. The cart's expiry is not extended. Null `token` and `cart` when they have none: open one with `cart.create`. UNAUTHENTICATED without a signed-in buyer."
186
+ }).input(z.object({})).output(z.object({
187
+ token: z.string().nullable(),
188
+ cart: storefrontCart.nullable()
189
+ }));
190
+ storefrontProcedure.meta(write).route({
191
+ summary: "Make a guest cart the signed-in buyer's",
192
+ description: "The cart the token opens becomes the signed-in buyer's and keeps its token. Their live cart before it, if any, is merged into it and ends (merged): a line (or a bundle) of the same variant and attributes is raised by the other's quantity, the rest added, and its note kept only when this cart has none. A cart already theirs is answered unchanged. Refused, nothing changed: UNAUTHENTICATED without a signed-in buyer; NOT_FOUND `{ resource: \"cart\" }` as `cart.get`; PRECONDITION_FAILED `sales.cart_other_customer` for another customer's cart, `sales.cart_paying` while either cart is being paid; a merged line whose variant a buyer may no longer see NOT_FOUND `{ resource: \"variant\", id }`; past 100 lines LIMIT_EXCEEDED; a variant whose merged demand it cannot sell INSUFFICIENT_INVENTORY."
193
+ }).errors({
194
+ ...cartErrors,
195
+ LIMIT_EXCEEDED: {
196
+ status: 422,
197
+ data: z.object({ limit: z.int() })
198
+ }
199
+ }).input(z.object({ token: cartToken })).output(storefrontCart);
200
+ storefrontProcedure.meta(write).route({
201
+ summary: "Add lines to a cart",
202
+ description: `Adds each line's quantity of its variant, with its custom attributes, all or nothing. A variant already on a line with the same attributes (in any order) raises that line; other attributes make a line of their own. A bundle's variant is one line (\`bundleItems\` lists what it holds), raised the same way; its items' stock is asked for bundles × units each, with any other line of the same variant. Refused: a variant a buyer may not see, NOT_FOUND \`{ resource: "variant", id }\`; past 100 lines (a bundle is one), LIMIT_EXCEEDED; a variant whose whole demand in the cart it cannot sell, INSUFFICIENT_INVENTORY naming it (a bundle whose item cannot, or whose item's product buyers cannot see, naming the bundle's variant).`
203
+ }).errors({
204
+ ...cartErrors,
205
+ LIMIT_EXCEEDED: {
206
+ status: 422,
207
+ data: z.object({ limit: z.int() })
208
+ }
209
+ }).input(z.object({
210
+ token: cartToken,
211
+ lines: z.array(z.object({
212
+ variantId: liteId("variant"),
213
+ quantity,
214
+ customAttributes: lineAttributes.default([])
215
+ })).min(1).max(100)
216
+ })).output(storefrontCart);
217
+ storefrontProcedure.meta(write).route({
218
+ summary: "Change cart lines' quantities",
219
+ description: "Sets each named line's quantity, all or nothing; a line named twice is VALIDATION_FAILED, a line the cart lacks NOT_FOUND `{ resource: \"cart_line\", id }`. A bundle's line sets the bundles, its items' units with them. A quantity raised past what its variant (a bundle's items) can sell is INSUFFICIENT_INVENTORY; a lowered one is never refused. Remove a line with `cart.lines.remove`."
220
+ }).errors(cartErrors).input(z.object({
221
+ token: cartToken,
222
+ lines: z.array(z.object({
223
+ lineId: liteId("basket_line"),
224
+ quantity
225
+ })).min(1).max(100).refine((lines) => new Set(lines.map((l) => l.lineId)).size === lines.length, { message: "a line is named twice" })
226
+ })).output(storefrontCart);
227
+ storefrontProcedure.meta(write).route({
228
+ summary: "Remove cart lines",
229
+ description: "Removes the named lines (a bundle's with everything it holds), all or nothing: a line the cart lacks is NOT_FOUND `{ resource: \"cart_line\", id }`."
230
+ }).input(z.object({
231
+ token: cartToken,
232
+ lineIds: z.array(liteId("basket_line")).min(1).max(100)
233
+ })).output(storefrontCart);
234
+ storefrontProcedure.meta(write).route({
235
+ summary: "Set the cart's note",
236
+ description: `The buyer's note (a gift message, delivery instructions), trimmed, at most ${CART_NOTE_MAX} characters; null or blank clears it. The checkout shows it.`
237
+ }).input(z.object({
238
+ token: cartToken,
239
+ note: z.string().trim().max(CART_NOTE_MAX).nullable().transform((note) => note || null)
240
+ })).output(storefrontCart);
241
+ storefrontProcedure.meta(write).route({
242
+ summary: "Enter or remove the cart's discount code",
243
+ description: "Keeps the code (trimmed, upper-cased; one a cart) whether or not it applies now: the cart's `discountCode` says why it takes nothing, and every read prices it afresh. Null removes it. A code no discount could have (letters, digits, `_` and `-`, at most 64) is VALIDATION_FAILED."
244
+ }).input(z.object({
245
+ token: cartToken,
246
+ code: z.string().trim().min(1).max(64).regex(DISCOUNT_CODE_PATTERN).transform((code) => code.toUpperCase()).nullable()
247
+ })).output(storefrontCart);
248
+ export { CART_DISCOUNT_ERRORS, CART_OWNERS, CART_WARNINGS, cartToken, lineAttributes, storefrontCart, storefrontCartLine };
@@ -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 "../../../contract-kit/src/index.js";
2
+ import "zod";
@@ -0,0 +1,2 @@
1
+ import "../../../contract-kit/src/index.js";
2
+ import "zod";