@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/checkout.js CHANGED
@@ -1,119 +1,93 @@
1
- import { graphql, request } from "./graphql.js";
2
- import { fetchShopInfo } from "./shop.js";
3
1
  import { useCartSession } from "./cart-session.js";
2
+ import { readCustomerSession } from "./customer-session.js";
3
+ import { escapeHtmlAttr } from "./html.js";
4
4
  import { parseMarketingCookies } from "./marketing-cookies.js";
5
- import { createServerOnlyFn } from "@tanstack/react-start";
6
- import { CHECKOUT_STATE_FORMAT, isLoopback, markResponsePrivate } from "@shop-on/customer-runtime/handoff-state";
7
- //#region src/checkout.ts
8
- const CartCheckoutMutation = graphql(`
9
- mutation CartCheckout($input: CartCheckoutInput!) {
10
- cartCheckout(input: $input) {
11
- checkoutUrl
12
- userErrors {
13
- code
14
- field
15
- message
16
- }
17
- }
18
- }
19
- `);
20
- const readMarketingAttribution = createServerOnlyFn(async () => {
21
- try {
22
- const { getRequest } = await import("@tanstack/react-start/server");
23
- const cookies = parseMarketingCookies(getRequest().headers.get("cookie"));
24
- return Object.keys(cookies).length > 0 ? cookies : null;
25
- } catch (err) {
26
- console.warn("[checkout] marketing attribution unavailable:", err);
27
- return null;
28
- }
29
- });
30
- function publicCheckoutUrl(origin) {
31
- const url = new URL("/checkout", origin);
32
- if (!isLoopback(url.hostname)) url.protocol = "https:";
33
- return url.toString();
34
- }
35
- /**
36
- * The theme's `/checkout`, both passes. `state` is the URL's `checkout_state`,
37
- * a REQUIRED key even when `undefined`: a caller that never passed it would
38
- * bounce theme ↔ `/begin` forever, and this way that is a compile error.
39
- *
40
- * - No state: the URL of checkout's `/begin`, which binds this browser and
41
- * returns the buyer here with one. Mints nothing.
42
- * - A state: mint the cart handoff with it (`mintCheckoutHandoff`).
43
- *
44
- * The customer-session token is injected: forwarded as `X-Customer-Session` on
45
- * the mint so Rails folds the buyer into the handoff payload in one trip.
46
- */
47
- function startCheckoutHandoff(opts) {
48
- return opts.state === void 0 ? beginCheckoutUrl() : mintCheckoutHandoff({
49
- state: opts.state,
50
- customerToken: opts.customerToken
51
- });
52
- }
53
- async function beginCheckoutUrl() {
5
+ import { fetchShopInfo } from "./shop.js";
6
+ import { buyerStorefront, errorCode, storefront } from "./storefront.js";
7
+ // src/checkout.ts
8
+ /** Whether this buyer has a cart to check out, and where to go with it: the theme's `/checkout`. */
9
+ async function checkoutEntry() {
54
10
  if (!(await useCartSession()).data.token) return {
55
11
  ok: false,
56
12
  reason: "no_cart"
57
13
  };
58
- await markResponsePrivate();
59
- const shop = await fetchShopInfo();
60
- if (!shop) throw new Error("[checkout] shop info unavailable from Rails");
61
14
  const { getPublicOrigin } = await import("./seo-context.js");
62
- const url = new URL(`${shop.checkoutUrl}/begin`);
63
- url.searchParams.set("return_to", publicCheckoutUrl(getPublicOrigin()));
64
15
  return {
65
16
  ok: true,
66
- redirectTo: url.toString()
17
+ redirectTo: new URL("/checkout", getPublicOrigin()).toString()
67
18
  };
68
19
  }
20
+ const PRIVATE_HEADERS = {
21
+ "Cache-Control": "no-store",
22
+ "Referrer-Policy": "no-referrer"
23
+ };
24
+ function redirectTo(location) {
25
+ return new Response(null, {
26
+ status: 303,
27
+ headers: {
28
+ Location: location,
29
+ ...PRIVATE_HEADERS
30
+ }
31
+ });
32
+ }
69
33
  /**
70
- * Read this shop's `cart_session` cookie, call Rails `cartCheckout`, and return
71
- * the URL on the checkout app that consumes it, `checkout_state` appended.
72
- *
73
- * `no_cart` covers "no cart cookie" and Rails' `NOT_FOUND` (cart digest miss /
74
- * cross-shop) / `CART_EMPTY`; `bad_state` a state of the wrong shape (never
75
- * bounced to `/begin` again, so it can't loop). Any other userError code is
76
- * unexpected and throws (matching `request`'s retry-once-then-throw contract
77
- * for top-level GraphQL errors); the calling route sends the buyer home rather
78
- * than render on a URL holding a state.
34
+ * The theme's `GET /{-$locale}/checkout`. A preview (`?preview`) is a 404;
35
+ * a buyer with no cart, an unknown or expired one, or an empty one goes to
36
+ * the storefront's home; a cart already being paid goes to the checkout
37
+ * app, whose cookie still opens its checkout. Anything else the api refuses
38
+ * is thrown (the theme's 500).
79
39
  */
80
- async function mintCheckoutHandoff(opts) {
40
+ async function handleCheckoutGet(request, { locale }) {
41
+ if (new URL(request.url).searchParams.has("preview")) return new Response("Not Found", { status: 404 });
42
+ const home = locale ? `/${locale}` : "/";
81
43
  const cartToken = (await useCartSession()).data.token;
82
- if (!cartToken) return {
83
- ok: false,
84
- reason: "no_cart"
85
- };
86
- await markResponsePrivate();
87
- if (!CHECKOUT_STATE_FORMAT.test(opts.state)) {
88
- console.warn(JSON.stringify({
89
- event: "theme_checkout_bad_state",
90
- ts: Date.now()
44
+ if (!cartToken) return redirectTo(home);
45
+ const shop = await fetchShopInfo();
46
+ const attribution = parseMarketingCookies(request.headers.get("cookie"));
47
+ let token;
48
+ try {
49
+ ({token} = await storefront(locale).checkout.start({
50
+ cartToken,
51
+ ...Object.keys(attribution).length > 0 ? { attribution } : {}
91
52
  }));
92
- return {
93
- ok: false,
94
- reason: "bad_state"
95
- };
53
+ } catch (error) {
54
+ const code = errorCode(error);
55
+ const condition = error.data?.condition;
56
+ if (code === "NOT_FOUND" || condition === "sales.cart_empty") return redirectTo(home);
57
+ if (condition === "sales.cart_paying") return redirectTo(shop.checkoutUrl);
58
+ throw error;
96
59
  }
97
- const attribution = await readMarketingAttribution();
98
- const result = await request(CartCheckoutMutation, { input: attribution ? {
99
- cartToken,
100
- attribution
101
- } : { cartToken } }, opts.customerToken ? { customerSessionToken: opts.customerToken } : void 0);
102
- const checkoutUrl = result.cartCheckout?.checkoutUrl;
103
- if (!checkoutUrl) {
104
- const code = result.cartCheckout?.userErrors[0]?.code;
105
- if (code === "NOT_FOUND" || code === "CART_EMPTY") return {
106
- ok: false,
107
- reason: "no_cart"
108
- };
109
- throw new Error(`[checkout] cartCheckout returned no URL (code=${code ?? "UNKNOWN"})`);
60
+ const handoff = await customerHandoff();
61
+ return new Response(renderCheckoutHandoffForm(`${shop.checkoutUrl}/begin`, token, handoff), {
62
+ status: 200,
63
+ headers: {
64
+ "Content-Type": "text/html; charset=utf-8",
65
+ "Cache-Control": "no-store",
66
+ "Referrer-Policy": "strict-origin"
67
+ }
68
+ });
69
+ }
70
+ /** A handoff token for the signed-in buyer's session, or null for a guest or a session the api refuses. */
71
+ async function customerHandoff() {
72
+ const session = await readCustomerSession();
73
+ if (!session?.token) return null;
74
+ try {
75
+ return (await buyerStorefront(session.token).customer.handoff.create({})).token;
76
+ } catch (error) {
77
+ if (errorCode(error) === "UNAUTHENTICATED") return null;
78
+ throw error;
110
79
  }
111
- const bound = new URL(checkoutUrl);
112
- bound.searchParams.set("checkout_state", opts.state);
113
- return {
114
- ok: true,
115
- redirectTo: bound.toString()
116
- };
117
80
  }
118
- //#endregion
119
- export { startCheckoutHandoff };
81
+ /** The page that POSTs the checkout token, and a signed-in buyer's handoff token, to the checkout app's `/begin`. Pure. */
82
+ function renderCheckoutHandoffForm(beginUrl, token, customerHandoff) {
83
+ const handoff = customerHandoff ? `\n <input type="hidden" name="customer_handoff" value="${escapeHtmlAttr(customerHandoff)}">` : "";
84
+ return `<!DOCTYPE html>
85
+ <html><body>
86
+ <form action="${escapeHtmlAttr(beginUrl)}" method="POST">
87
+ <input type="hidden" name="token" value="${escapeHtmlAttr(token)}">${handoff}
88
+ <noscript><button type="submit">Continue</button></noscript>
89
+ </form>
90
+ <script>document.forms[0].submit()<\/script>
91
+ </body></html>`;
92
+ }
93
+ export { checkoutEntry, handleCheckoutGet, renderCheckoutHandoffForm };
@@ -0,0 +1,57 @@
1
+ import { requestSlot } from "./request-scope.js";
2
+ // src/coalesce.ts
3
+ /**
4
+ * A reader that coalesces same-tick reads into calls of `send`, matching
5
+ * each caller's item in the answer with `matches`. The first read of a group
6
+ * schedules its flush; every read of that group until then joins it, and the
7
+ * group's first `send` is the one used (a group names one question).
8
+ */
9
+ function coalescedReads(opts) {
10
+ const slot = Symbol("coalesced reads");
11
+ return (group, ref, send) => {
12
+ const pick = (items) => items.find((item) => opts.matches(item, ref)) ?? null;
13
+ const groups = requestSlot(slot, () => /* @__PURE__ */ new Map());
14
+ if (!groups) return send([ref]).then(pick);
15
+ let pending = groups.get(group);
16
+ if (!pending) {
17
+ const scheduled = {
18
+ send,
19
+ refs: /* @__PURE__ */ new Map()
20
+ };
21
+ pending = scheduled;
22
+ groups.set(group, scheduled);
23
+ setTimeout(() => {
24
+ groups.delete(group);
25
+ flush(scheduled, opts.max);
26
+ }, 0);
27
+ }
28
+ let entry = pending.refs.get(ref);
29
+ if (!entry) {
30
+ let resolve;
31
+ let reject;
32
+ const answer = new Promise((res, rej) => {
33
+ resolve = res;
34
+ reject = rej;
35
+ });
36
+ entry = {
37
+ resolve,
38
+ reject,
39
+ answer
40
+ };
41
+ pending.refs.set(ref, entry);
42
+ }
43
+ return entry.answer.then(pick);
44
+ };
45
+ }
46
+ function flush(pending, max) {
47
+ const refs = [...pending.refs.keys()];
48
+ for (let i = 0; i < refs.length; i += max) {
49
+ const chunk = refs.slice(i, i + max);
50
+ new Promise((res) => res(pending.send(chunk))).then((items) => {
51
+ for (const ref of chunk) pending.refs.get(ref).resolve(items);
52
+ }, (error) => {
53
+ for (const ref of chunk) pending.refs.get(ref).reject(error);
54
+ });
55
+ }
56
+ }
57
+ export { coalescedReads };