@shop-on/theme-server 0.10.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
@@ -1,4 +1,3 @@
1
- //#region src/seo-context.d.ts
2
1
  export interface SeoRequestContext {
3
2
  origin: string;
4
3
  siteName: string;
@@ -10,5 +9,4 @@ export declare function resolveSeoContext(): Promise<SeoRequestContext>;
10
9
  * without the shop name (kito's hreflang cluster).
11
10
  */
12
11
  export declare function resolveCanonicalOrigin(): Promise<string>;
13
- export declare function getPublicOrigin(fallback?: string): string;
14
- //#endregion
12
+ export declare function getPublicOrigin(fallback?: string): string;
@@ -1,14 +1,14 @@
1
- import { StorefrontConfigError } from "./graphql.js";
2
- import { fetchShopInfo } from "./shop.js";
3
1
  import { canonicalOrigin } from "./canonical-host.js";
2
+ import { StorefrontConfigError } from "./storefront.js";
3
+ import { fetchShopInfo } from "./shop.js";
4
4
  import { getRequestUrl } from "@tanstack/react-start/server";
5
- //#region src/seo-context.ts
5
+ // src/seo-context.ts
6
6
  async function resolveSeoContext() {
7
7
  const [shop, requestOrigin] = await Promise.all([safeFetchShop(), readOrigin()]);
8
8
  return {
9
9
  origin: canonicalOrigin({
10
10
  requestOrigin,
11
- primaryFqdn: shop?.primaryDomain?.fqdn
11
+ primaryHostname: shop?.primaryDomain?.hostname
12
12
  }),
13
13
  siteName: shop?.name ?? ""
14
14
  };
@@ -43,5 +43,4 @@ function getPublicOrigin(fallback = "") {
43
43
  function readOrigin() {
44
44
  return getPublicOrigin();
45
45
  }
46
- //#endregion
47
46
  export { getPublicOrigin, resolveCanonicalOrigin, resolveSeoContext };
@@ -0,0 +1,50 @@
1
+ import { StorefrontClient } from "./api-contract/src/storefront/client.js";
2
+ export type SeoKind = "robots" | "sitemap";
3
+ export interface SeoDocument {
4
+ status: number;
5
+ body: string;
6
+ contentType: string;
7
+ cacheControl: string | null;
8
+ /** With a 503: when to ask again, in seconds. */
9
+ retryAfter?: number;
10
+ }
11
+ type SitemapIndex = Awaited<ReturnType<StorefrontClient["seo"]["sitemap"]["index"]>>;
12
+ type SitemapFile = Awaited<ReturnType<StorefrontClient["seo"]["sitemap"]["file"]>>;
13
+ /**
14
+ * robots.txt (legacy Shop::Seo::RobotsTxt). Locked: everything disallowed,
15
+ * the merchant's lines left out, so they cannot `Allow:` a closed store back
16
+ * in. Open: the buyer's surfaces disallowed (`ROBOTS_DISALLOWED_PATHS`), the
17
+ * sitemap on `origin` when there is one (the fallback has none it can
18
+ * trust), then the merchant's lines, which the api checked when they were
19
+ * saved.
20
+ */
21
+ export declare function renderRobotsTxt({ locked, origin, append }: {
22
+ locked: boolean;
23
+ origin: string | null;
24
+ append: string | null;
25
+ }): string;
26
+ /** The sitemap index (legacy SitemapXml#sitemapindex): one `<sitemap>` per file, its query's `&` escaped. */
27
+ export declare function renderSitemapIndex({ origin, files }: SitemapIndex): string;
28
+ /**
29
+ * A sitemap file (legacy SitemapXml#urlset), hreflang as Google reads it:
30
+ * each language's URL is its own `<url>`, carrying the whole alternate set
31
+ * and `x-default` (the primary language's URL); a shop in one language has
32
+ * no alternates and no `xmlns:xhtml`. `<lastmod>` only when known.
33
+ */
34
+ export declare function renderSitemapFile({ origin, locales, entries }: SitemapFile): string;
35
+ /**
36
+ * The document for `kind` and `search`, the canonical sitemap query
37
+ * (`kind=K&page=N`, or empty for the index: `normalizeSitemapQuery`).
38
+ * Within `TIMEOUT_MS`, else rejected: the route answers its fallback.
39
+ */
40
+ export declare function getSeoDocument(kind: SeoKind, search: string): Promise<SeoDocument>;
41
+ /** What `robotsHandler` and `sitemapHandler` (`@shop-on/theme-routes`) take. */
42
+ export declare const seoDocuments: {
43
+ getSeoDocument: typeof getSeoDocument;
44
+ /** robots.txt when the api cannot answer: the disallowed paths, no `Sitemap:` line (no origin it can trust). */
45
+ robotsFallback: string;
46
+ /** The sitemap's kinds and highest page, as the api takes them. */
47
+ sitemapKinds: readonly ["static", "products", "collections", "pages", "articles"];
48
+ sitemapPageMax: number;
49
+ };
50
+ export {};
@@ -0,0 +1,149 @@
1
+ import { SITEMAP_KINDS, SITEMAP_PAGE_MAX } from "./api-contract/src/limits.js";
2
+ import { ROBOTS_DISALLOWED_PATHS } from "./api-contract/src/storefront/seo.js";
3
+ import { memoTTL } from "./cache.js";
4
+ import { getContentVersion } from "./content-version.js";
5
+ import { escapeHtmlAttr } from "./html.js";
6
+ import { errorCode, storefront } from "./storefront.js";
7
+ import { localizedUrl } from "@shop-on/theme-runtime/seo";
8
+ // src/seo-documents.ts
9
+ /** The whole read's budget, the version lookup included: a hung api must not hang a crawler. */
10
+ const TIMEOUT_MS = 2e3;
11
+ const TEXT = "text/plain; charset=utf-8";
12
+ const XML = "application/xml; charset=utf-8";
13
+ const SITEMAP_NS = "http://www.sitemaps.org/schemas/sitemap/0.9";
14
+ const XHTML_NS = "http://www.w3.org/1999/xhtml";
15
+ /**
16
+ * robots.txt (legacy Shop::Seo::RobotsTxt). Locked: everything disallowed,
17
+ * the merchant's lines left out, so they cannot `Allow:` a closed store back
18
+ * in. Open: the buyer's surfaces disallowed (`ROBOTS_DISALLOWED_PATHS`), the
19
+ * sitemap on `origin` when there is one (the fallback has none it can
20
+ * trust), then the merchant's lines, which the api checked when they were
21
+ * saved.
22
+ */
23
+ function renderRobotsTxt({ locked, origin, append }) {
24
+ if (locked) return "User-agent: *\nDisallow: /\n";
25
+ const lines = ["User-agent: *", ...ROBOTS_DISALLOWED_PATHS.map((path) => `Disallow: ${path}`)];
26
+ if (origin) lines.push("", `Sitemap: ${origin}/sitemap.xml`);
27
+ if (append) lines.push("", append);
28
+ return `${lines.join("\n")}\n`;
29
+ }
30
+ /** The sitemap index (legacy SitemapXml#sitemapindex): one `<sitemap>` per file, its query's `&` escaped. */
31
+ function renderSitemapIndex({ origin, files }) {
32
+ const body = files.flatMap(({ kind, pages }) => Array.from({ length: pages }, (_, at) => ` <sitemap><loc>${escapeHtmlAttr(`${origin}/sitemap.xml?kind=${kind}&page=${at + 1}`)}</loc></sitemap>\n`)).join("");
33
+ return `<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="${SITEMAP_NS}">\n${body}</sitemapindex>\n`;
34
+ }
35
+ /**
36
+ * A sitemap file (legacy SitemapXml#urlset), hreflang as Google reads it:
37
+ * each language's URL is its own `<url>`, carrying the whole alternate set
38
+ * and `x-default` (the primary language's URL); a shop in one language has
39
+ * no alternates and no `xmlns:xhtml`. `<lastmod>` only when known.
40
+ */
41
+ function renderSitemapFile({ origin, locales, entries }) {
42
+ const [primary = ""] = locales;
43
+ const several = locales.length > 1;
44
+ const url = (path, locale) => localizedUrl(origin, path, locale, primary);
45
+ const link = (hreflang, href) => ` <xhtml:link rel="alternate" hreflang="${escapeHtmlAttr(hreflang)}" href="${escapeHtmlAttr(href)}"/>\n`;
46
+ const body = entries.flatMap(({ path, updatedAt }) => {
47
+ const lastmod = updatedAt ? ` <lastmod>${escapeHtmlAttr(updatedAt)}</lastmod>\n` : "";
48
+ const alternates = several ? locales.map((locale) => link(locale, url(path, locale))).join("") + link("x-default", url(path, primary)) : "";
49
+ return (several ? locales : [primary]).map((locale) => ` <url>\n <loc>${escapeHtmlAttr(url(path, locale))}</loc>\n${lastmod}${alternates} </url>\n`);
50
+ }).join("");
51
+ return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="${SITEMAP_NS}"${several ? ` xmlns:xhtml="${XHTML_NS}"` : ""}>\n${body}</urlset>\n`;
52
+ }
53
+ /**
54
+ * A refusal as the document's answer: NOT_FOUND (a locked shop's sitemap, a
55
+ * file past the last) and a refused input are a 404 with no body; the
56
+ * sitemap's rate class spent is a 503 a crawler retries. Anything else is
57
+ * thrown, so the route answers its fallback and nothing is memoised.
58
+ */
59
+ function refused(error) {
60
+ const code = errorCode(error);
61
+ if (code === "NOT_FOUND" || code === "VALIDATION_FAILED") return {
62
+ status: 404,
63
+ body: "",
64
+ contentType: TEXT,
65
+ cacheControl: null
66
+ };
67
+ if (code === "RATE_LIMITED") {
68
+ const { retryAfterSeconds } = error.data ?? {};
69
+ return {
70
+ status: 503,
71
+ body: "",
72
+ contentType: TEXT,
73
+ cacheControl: null,
74
+ retryAfter: retryAfterSeconds ?? 60
75
+ };
76
+ }
77
+ throw error;
78
+ }
79
+ /** Each call's own deadline: what is left of the read's budget is the race's (`getSeoDocument`). */
80
+ const call = { context: { timeoutMs: TIMEOUT_MS } };
81
+ async function readDocument(kind, search) {
82
+ const seo = storefront().seo;
83
+ try {
84
+ if (kind === "robots") {
85
+ const facts = await seo.robots({}, call);
86
+ return {
87
+ status: 200,
88
+ body: renderRobotsTxt(facts),
89
+ contentType: TEXT,
90
+ cacheControl: facts.locked ? "no-store" : "public, max-age=300"
91
+ };
92
+ }
93
+ const query = new URLSearchParams(search);
94
+ const sitemapKind = query.get("kind");
95
+ return {
96
+ status: 200,
97
+ body: sitemapKind === null ? renderSitemapIndex(await seo.sitemap.index({}, call)) : renderSitemapFile(await seo.sitemap.file({
98
+ kind: sitemapKind,
99
+ page: Number(query.get("page"))
100
+ }, call)),
101
+ contentType: XML,
102
+ cacheControl: "public, max-age=3600"
103
+ };
104
+ } catch (error) {
105
+ return refused(error);
106
+ }
107
+ }
108
+ const memoDocument = memoTTL(readDocument, {
109
+ key: (kind, search) => `${kind}?${search}`,
110
+ version: getContentVersion,
111
+ shouldCache: (doc) => doc.status === 200
112
+ });
113
+ const memoFile = memoTTL(readDocument, {
114
+ key: (kind, search) => `${kind}?${search}`,
115
+ version: getContentVersion,
116
+ shouldCache: (doc) => doc.status === 200,
117
+ ttlMs: 6e5,
118
+ maxEntries: 16
119
+ });
120
+ /**
121
+ * The document for `kind` and `search`, the canonical sitemap query
122
+ * (`kind=K&page=N`, or empty for the index: `normalizeSitemapQuery`).
123
+ * Within `TIMEOUT_MS`, else rejected: the route answers its fallback.
124
+ */
125
+ async function getSeoDocument(kind, search) {
126
+ const read = kind === "sitemap" && search !== "" ? memoFile : memoDocument;
127
+ let timer;
128
+ try {
129
+ return await Promise.race([read(kind, search), new Promise((_, reject) => {
130
+ timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`[seo-documents] ${kind} exceeded ${TIMEOUT_MS}ms`)), TIMEOUT_MS);
131
+ })]);
132
+ } finally {
133
+ clearTimeout(timer);
134
+ }
135
+ }
136
+ /** What `robotsHandler` and `sitemapHandler` (`@shop-on/theme-routes`) take. */
137
+ const seoDocuments = {
138
+ getSeoDocument,
139
+ /** robots.txt when the api cannot answer: the disallowed paths, no `Sitemap:` line (no origin it can trust). */
140
+ robotsFallback: renderRobotsTxt({
141
+ locked: false,
142
+ origin: null,
143
+ append: null
144
+ }),
145
+ /** The sitemap's kinds and highest page, as the api takes them. */
146
+ sitemapKinds: SITEMAP_KINDS,
147
+ sitemapPageMax: SITEMAP_PAGE_MAX
148
+ };
149
+ export { getSeoDocument, renderRobotsTxt, renderSitemapFile, renderSitemapIndex, seoDocuments };
@@ -1,12 +1,10 @@
1
- //#region src/shop-seo.d.ts
2
1
  export interface ShopSeo {
2
+ /** Empty when the merchant set none: the homepage's title is then the shop's name. */
3
3
  title: string;
4
+ /** Empty when the merchant set none: the homepage emits no description. */
4
5
  description: string;
5
6
  googleSiteVerification: string | null;
6
7
  bingSiteVerification: string | null;
7
8
  socialImageUrl: string | null;
8
9
  }
9
- /** Null when the merchant has never opened the SEO form — every consumer then
10
- * falls back to what it did before this existed. */
11
- export declare const fetchShopSeo: (locale: string | undefined) => Promise<ShopSeo | null>;
12
- //#endregion
10
+ export declare const fetchShopSeo: (locale: string | undefined) => Promise<ShopSeo>;
package/dist/shop-seo.js CHANGED
@@ -1,37 +1,18 @@
1
1
  import { memoTTL } from "./cache.js";
2
- import { graphql, request } from "./graphql.js";
3
2
  import { getContentVersion } from "./content-version.js";
4
- //#region src/shop-seo.ts
5
- const ShopSeoQuery = graphql(`
6
- query StorefrontShopSeo {
7
- shop {
8
- seo {
9
- seoTitle
10
- seoDescription
11
- googleSiteVerification
12
- bingSiteVerification
13
- socialImage {
14
- url
15
- }
16
- }
17
- }
18
- }
19
- `);
20
- /** Null when the merchant has never opened the SEO form — every consumer then
21
- * falls back to what it did before this existed. */
3
+ import { storefront } from "./storefront.js";
4
+ // src/shop-seo.ts
22
5
  const fetchShopSeo = memoTTL(async (locale) => {
23
- const seo = (await request(ShopSeoQuery, void 0, locale ? { acceptLanguage: locale } : void 0)).shop?.seo;
24
- if (!seo) return null;
6
+ const seo = await storefront(locale).seo.get({});
25
7
  return {
26
- title: seo.seoTitle,
27
- description: seo.seoDescription,
28
- googleSiteVerification: seo.googleSiteVerification ?? null,
29
- bingSiteVerification: seo.bingSiteVerification ?? null,
8
+ title: seo.title ?? "",
9
+ description: seo.description ?? "",
10
+ googleSiteVerification: seo.googleSiteVerification,
11
+ bingSiteVerification: seo.bingSiteVerification,
30
12
  socialImageUrl: seo.socialImage?.url ?? null
31
13
  };
32
14
  }, {
33
15
  key: (locale) => `shop-seo|${locale ?? ""}`,
34
16
  version: getContentVersion
35
17
  });
36
- //#endregion
37
18
  export { fetchShopSeo };
package/dist/shop.d.ts CHANGED
@@ -1,46 +1,42 @@
1
- import { ResultOf } from "./graphql.js";
2
- //#region src/shop.d.ts
3
- declare const ShopQuery: import("gql.tada").TadaDocumentNode<{
4
- shop: {
5
- accountUrl: string;
6
- checkoutUrl: string;
7
- currency: string;
8
- handle: string;
9
- id: string;
10
- locales: {
11
- locale: string;
12
- published: boolean;
13
- }[];
14
- name: string;
15
- passwordProtection: {
16
- bypassToken: string | null;
17
- enabled: boolean;
18
- passwordUpdatedAt: string | null;
19
- } | null;
20
- paymentMethods: {
21
- key: string;
22
- label: string;
23
- logoUrl: string | null;
24
- }[];
25
- primaryDomain: {
26
- fqdn: string;
27
- turnstileSitekey: string | null;
28
- } | null;
29
- primaryLocale: string;
30
- status: string;
31
- taxesIncluded: boolean;
32
- timezone: string;
33
- turnstileSitekey: string | null;
1
+ /** The shop as every page renders it (`shop.get`), without the gate's token. */
2
+ export type ShopInfo = {
3
+ id: string;
4
+ handle: string;
5
+ name: string;
6
+ /** Anything but `active` serves the unavailable page (`./availability`). */
7
+ status: "active" | "closed" | "suspended";
8
+ currency: string;
9
+ timezone: string;
10
+ primaryLocale: string;
11
+ /** The languages buyers get: the primary, then the published ones. */
12
+ locales: {
13
+ locale: string;
14
+ }[];
15
+ /** The canonical host (`./canonical-host`); null when the shop has none. */
16
+ primaryDomain: {
17
+ hostname: string;
34
18
  } | null;
35
- }, {}, void>;
36
- type ShopRecord = NonNullable<ResultOf<typeof ShopQuery>["shop"]>;
37
- type PasswordProtection = NonNullable<ShopRecord["passwordProtection"]>;
38
- export type ShopInfo = Omit<ShopRecord, "passwordProtection"> & {
39
- passwordProtection: Omit<PasswordProtection, "bypassToken"> | null;
19
+ passwordProtection: {
20
+ enabled: boolean;
21
+ /** Changes with the password: the unlock cookie carries it (`./password`). */
22
+ version: string | null;
23
+ };
24
+ taxesIncluded: boolean;
25
+ turnstileSitekey: string | null;
26
+ accountUrl: string;
27
+ checkoutUrl: string;
28
+ /** The payment marks: `logoUrl` null for a provider without one. */
29
+ paymentMethods: {
30
+ key: string;
31
+ label: string;
32
+ logoUrl: string | null;
33
+ }[];
40
34
  };
41
- export declare function fetchShopInfo(): Promise<ShopInfo | null>;
35
+ /** The gate's record: `bypassToken`, while enabled, opens the store before launch. */
36
+ export type PasswordProtection = ShopInfo["passwordProtection"] & {
37
+ bypassToken: string | null;
38
+ };
39
+ export declare function fetchShopInfo(): Promise<ShopInfo>;
42
40
  /** The gate's record, `bypassToken` included — for `readPasswordGate` only.
43
41
  * Never return it from a server fn. */
44
- export declare function fetchPasswordProtection(): Promise<PasswordProtection | null>;
45
- //#endregion
46
- export {};
42
+ export declare function fetchPasswordProtection(): Promise<PasswordProtection>;
package/dist/shop.js CHANGED
@@ -1,65 +1,51 @@
1
1
  import { memoTTL } from "./cache.js";
2
- import { graphql, request } from "./graphql.js";
3
2
  import { getContentVersion } from "./content-version.js";
4
- //#region src/shop.ts
5
- const ShopQuery = graphql(`
6
- query StorefrontShop {
7
- shop {
8
- id
9
- name
10
- handle
11
- status
12
- currency
13
- timezone
14
- primaryLocale
15
- taxesIncluded
16
- locales {
17
- locale
18
- published
19
- }
20
- turnstileSitekey
21
- accountUrl
22
- checkoutUrl
23
- primaryDomain {
24
- fqdn
25
- turnstileSitekey
26
- }
27
- paymentMethods {
28
- key
29
- label
30
- logoUrl
31
- }
32
- passwordProtection {
33
- enabled
34
- passwordUpdatedAt
35
- bypassToken
36
- }
37
- }
38
- }
39
- `);
3
+ import { storefront } from "./storefront.js";
4
+ // src/shop.ts
40
5
  const fetchShopRecord = memoTTL(async () => {
41
- return (await request(ShopQuery)).shop ?? null;
6
+ const shop = await storefront().shop.get();
7
+ const { enabled, version, bypassToken } = shop.passwordProtection;
8
+ return {
9
+ info: {
10
+ id: shop.id,
11
+ handle: shop.handle,
12
+ name: shop.name,
13
+ status: shop.status,
14
+ currency: shop.currency,
15
+ timezone: shop.timezone,
16
+ primaryLocale: shop.primaryLocale,
17
+ locales: shop.locales.filter(({ locale, published }) => published || locale === shop.primaryLocale).map(({ locale }) => ({ locale })),
18
+ primaryDomain: shop.primaryDomain && { hostname: shop.primaryDomain.hostname },
19
+ passwordProtection: {
20
+ enabled,
21
+ version
22
+ },
23
+ taxesIncluded: shop.taxesIncluded,
24
+ turnstileSitekey: shop.turnstileSitekey,
25
+ accountUrl: shop.accountUrl,
26
+ checkoutUrl: shop.checkoutUrl,
27
+ paymentMethods: shop.paymentMethods.map(({ key, label, logoUrl }) => ({
28
+ key,
29
+ label,
30
+ logoUrl
31
+ }))
32
+ },
33
+ passwordProtection: {
34
+ enabled,
35
+ version,
36
+ bypassToken
37
+ }
38
+ };
42
39
  }, {
43
40
  key: () => "",
44
41
  version: getContentVersion
45
42
  });
46
43
  async function fetchShopInfo() {
47
- const shop = await fetchShopRecord();
48
- if (!shop) return null;
49
- if (!shop.passwordProtection) return {
50
- ...shop,
51
- passwordProtection: null
52
- };
53
- const { bypassToken: _omitted, ...passwordProtection } = shop.passwordProtection;
54
- return {
55
- ...shop,
56
- passwordProtection
57
- };
44
+ return (await fetchShopRecord()).info;
58
45
  }
59
46
  /** The gate's record, `bypassToken` included — for `readPasswordGate` only.
60
47
  * Never return it from a server fn. */
61
48
  async function fetchPasswordProtection() {
62
- return (await fetchShopRecord())?.passwordProtection ?? null;
49
+ return (await fetchShopRecord()).passwordProtection;
63
50
  }
64
- //#endregion
65
51
  export { fetchPasswordProtection, fetchShopInfo };
@@ -1,24 +1,22 @@
1
- //#region src/store-availability.d.ts
2
- export type StoreAvailabilitySheetNode = {
1
+ /** One store a variant can be collected from (`products.pickupAvailability`). */
2
+ export type StoreAvailability = {
3
+ /** Untracked, or units available there. */
3
4
  available: boolean;
5
+ /** When it is ready, as a buyer reads it in the read's language ("Usually ready in 2 hours"). */
4
6
  pickUpTime: string;
5
- quantityAvailable: number | null;
7
+ /** Units available there; null when the variant is untracked. */
8
+ quantity: number | null;
6
9
  location: {
7
10
  id: string;
8
11
  name: string;
9
- formatted: string[];
12
+ /** The address, one line per entry, in the read's language; empty without one. */
13
+ addressLines: string[];
10
14
  };
11
15
  };
12
- export type StoreAvailabilitySheetPage = {
13
- totalCount: number;
14
- endCursor: string | null;
15
- hasNextPage: boolean;
16
- nodes: StoreAvailabilitySheetNode[];
17
- };
16
+ /** Where `variantId` can be collected: none for a variant that does not ship,
17
+ * whose product a buyer may not see, or that is no variant id. */
18
18
  export declare function fetchStoreAvailability(data: {
19
19
  variantId: string;
20
- first: number;
21
- after?: string;
20
+ /** The read's language (the address and the ready time); omitted, the shop's primary. */
22
21
  locale?: string;
23
- }): Promise<StoreAvailabilitySheetPage>;
24
- //#endregion
22
+ }): Promise<StoreAvailability[]>;
@@ -1,59 +1,21 @@
1
- import { graphql, request } from "./graphql.js";
2
- //#region src/store-availability.ts
3
- const FetchStoreAvailabilityQuery = graphql(`
4
- query FetchStoreAvailability($variantId: ID!, $first: Int!, $after: String, $locale: String) {
5
- productVariant(id: $variantId) {
6
- id
7
- storeAvailability(first: $first, after: $after) {
8
- totalCount
9
- pageInfo {
10
- endCursor
11
- hasNextPage
12
- }
13
- nodes {
14
- available
15
- pickUpTime
16
- quantityAvailable
17
- location {
18
- id
19
- name
20
- formatted(locale: $locale)
21
- }
22
- }
23
- }
24
- }
25
- }
26
- `);
27
- const EMPTY_PAGE = {
28
- totalCount: 0,
29
- endCursor: null,
30
- hasNextPage: false,
31
- nodes: []
32
- };
1
+ import { liteId } from "./api-contract/src/ids.js";
2
+ import { orNotFound, storefront } from "./storefront.js";
3
+ // src/store-availability.ts
4
+ const variantId = liteId("variant");
5
+ /** Where `variantId` can be collected: none for a variant that does not ship,
6
+ * whose product a buyer may not see, or that is no variant id. */
33
7
  async function fetchStoreAvailability(data) {
34
- const variant = (await request(FetchStoreAvailabilityQuery, {
35
- variantId: data.variantId,
36
- first: data.first,
37
- after: data.after ?? null,
38
- locale: data.locale ?? null
39
- }, data.locale ? { acceptLanguage: data.locale } : void 0)).productVariant;
40
- if (!variant) return EMPTY_PAGE;
41
- const conn = variant.storeAvailability;
42
- return {
43
- totalCount: conn.totalCount,
44
- endCursor: conn.pageInfo.endCursor ?? null,
45
- hasNextPage: conn.pageInfo.hasNextPage,
46
- nodes: conn.nodes.map((n) => ({
47
- available: n.available,
48
- pickUpTime: n.pickUpTime,
49
- quantityAvailable: n.quantityAvailable ?? null,
50
- location: {
51
- id: n.location.id,
52
- name: n.location.name,
53
- formatted: [...n.location.formatted]
54
- }
55
- }))
56
- };
8
+ const id = variantId.safeParse(data.variantId);
9
+ if (!id.success) return [];
10
+ return ((await orNotFound(storefront(data.locale).products.pickupAvailability({ variantId: id.data })))?.items ?? []).map((item) => ({
11
+ available: item.available,
12
+ pickUpTime: item.pickUpTime,
13
+ quantity: item.quantity,
14
+ location: {
15
+ id: item.location.id,
16
+ name: item.location.name,
17
+ addressLines: [...item.location.addressLines]
18
+ }
19
+ }));
57
20
  }
58
- //#endregion
59
21
  export { fetchStoreAvailability };