@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,28 +1,34 @@
1
- import { graphql, request } from "./graphql.js";
2
- //#region src/newsletter.ts
3
- const NewsletterSubscribeMutation = graphql(`
4
- mutation NewsletterSubscribe($input: NewsletterSubscribeInput!) {
5
- newsletterSubscribe(input: $input) {
6
- subscribed
7
- userErrors {
8
- code
9
- field
10
- message
11
- }
12
- }
13
- }
14
- `);
15
- /** Subscribe `email`. A refusal comes back as the API's first user error; a
16
- * transport or GraphQL failure throws. */
1
+ import { errorCode, invalidField, storefront } from "./storefront.js";
2
+ // src/newsletter.ts
3
+ /** Subscribe `email`. The token goes only when the form has one: a shop
4
+ * without a widget asks for none. Any other failure throws. */
17
5
  async function subscribeNewsletter(input) {
18
- const payload = (await request(NewsletterSubscribeMutation, { input })).newsletterSubscribe;
19
- if (payload?.subscribed) return { ok: true };
20
- const error = payload?.userErrors[0];
21
- return {
22
- ok: false,
23
- code: error?.code ?? "UNKNOWN",
24
- message: error?.message ?? null
25
- };
6
+ try {
7
+ await storefront().newsletter.subscribe({
8
+ email: input.email,
9
+ ...input.turnstileToken ? { turnstileToken: input.turnstileToken } : {}
10
+ });
11
+ return { ok: true };
12
+ } catch (error) {
13
+ const code = errorCode(error);
14
+ if (code === "RATE_LIMITED") {
15
+ const { retryAfterSeconds } = error.data;
16
+ return {
17
+ ok: false,
18
+ code,
19
+ retryAfterSeconds
20
+ };
21
+ }
22
+ if (code === "VALIDATION_FAILED") return {
23
+ ok: false,
24
+ code,
25
+ field: invalidField(error)
26
+ };
27
+ if (code === "TURNSTILE_FAILED") return {
28
+ ok: false,
29
+ code
30
+ };
31
+ throw error;
32
+ }
26
33
  }
27
- //#endregion
28
34
  export { subscribeNewsletter };
package/dist/page.d.ts CHANGED
@@ -1,36 +1,33 @@
1
1
  import { MetafieldMap } from "./metafields.js";
2
- //#region src/page.d.ts
3
2
  export type PageData = {
4
3
  id: string;
5
4
  handle: string;
6
5
  title: string;
7
6
  bodyHtml: string;
8
7
  bodyPlain: string;
8
+ /** `page.<suffix>`; null for the default template (`page`). */
9
9
  themeTemplate: string | null;
10
- storefrontThemeTemplate: string;
11
- publishedAt: string | null;
10
+ publishedAt: string;
12
11
  updatedAt: string;
13
12
  seo: {
14
13
  title: string;
15
14
  description: string;
16
15
  };
17
16
  };
18
- /** Single-page fetch keyed by URL handle. Returns null when the page is
19
- * missing or not publicly visible — the route loader maps that to a 404. */
20
- export declare const fetchPage: (handle: string, locale?: string) => Promise<PageData | null>;
17
+ /** A page by handle or id. Null when it is missing, not visible to a buyer,
18
+ * or the read failed: the route loader maps that to a 404. */
19
+ export declare function fetchPage(ref: string, locale?: string): Promise<PageData | null>;
21
20
  export type PageRef = {
22
21
  title: string;
23
22
  bodyHtml: string;
24
23
  };
25
- /** Single-page fetch keyed by Relay GID (a page_reference metafield value).
26
- * `page(id:)` applies the same `public_visible` gate as the handle path, so a
27
- * missing/draft/cross-shop page returns null — the caller falls back. */
28
- export declare const fetchPageById: (id: string, locale?: string) => Promise<PageRef | null>;
29
- /** The page's storefront metafields keyed `"namespace.key"` — the
30
- * `page_metafield` dynamic source. Its own read, not a field on `fetchPage`,
31
- * so a page whose template binds none never asks for them. `{}` on a miss or
32
- * a failure, so the literal renders. */
33
- export declare const fetchPageMetafields: (ref: string, locale?: string) => Promise<MetafieldMap>;
24
+ /** A page by its id (a page metafield's value), as a reference renders it;
25
+ * null when a buyer may not see it, and the caller falls back. */
26
+ export declare function fetchPageById(id: string, locale?: string): Promise<PageRef | null>;
27
+ /** The page's storefront metafields keyed `"namespace.key"`: the
28
+ * `page_metafield` dynamic source, off the same read as the page. `{}` on a
29
+ * miss or a failure, so the literal renders. */
30
+ export declare function fetchPageMetafields(ref: string, locale?: string): Promise<MetafieldMap>;
34
31
  /** The route CMS page as the theme loader's dynamic sources read it
35
32
  * (`DynamicSourcePage` in `@shop-on/theme-loader`): the title off the page
36
33
  * read the theme already made, and a lazy provider the loader calls only when
@@ -39,5 +36,4 @@ export declare const fetchPageMetafields: (ref: string, locale?: string) => Prom
39
36
  export declare function routePageSource(page: Pick<PageData, "handle" | "title">, locale?: string): {
40
37
  title: string;
41
38
  getMetafields: () => Promise<MetafieldMap>;
42
- };
43
- //#endregion
39
+ };
package/dist/page.js CHANGED
@@ -1,118 +1,63 @@
1
+ import { liteId } from "./api-contract/src/ids.js";
1
2
  import { memoTTL } from "./cache.js";
2
- import { graphql, request } from "./graphql.js";
3
3
  import { getContentVersion } from "./content-version.js";
4
- import { metafieldsReader } from "./metafields.js";
5
- //#region src/page.ts
6
- function parseRef(ref) {
7
- return ref.startsWith("gid://") ? { id: ref } : { handle: ref };
4
+ import { customDataMap } from "./metafields.js";
5
+ import { orFallback, orNotFound, storefront } from "./storefront.js";
6
+ // src/page.ts
7
+ /** A page by handle, or by id when the ref is a page id (a `page` setting
8
+ * bound to a page metafield holds a `page_…` TypeID). */
9
+ function pageRef(ref) {
10
+ const id = liteId("page").safeParse(ref);
11
+ return id.success ? { pageId: id.data } : { handle: ref };
8
12
  }
9
- const FetchPageQuery = graphql(`
10
- query FetchPage($handle: String, $id: ID) {
11
- page(handle: $handle, id: $id) {
12
- id
13
- handle
14
- title
15
- bodyHtml
16
- # Plain-text body capped at the meta-description storage cap —
17
- # Rails strip_tags + entity decode + word-boundary truncate.
18
- bodyPlain: body(truncateAt: 320)
19
- themeTemplate
20
- storefrontThemeTemplate
21
- publishedAt
22
- updatedAt
23
- seo {
24
- title
25
- description
26
- }
27
- }
28
- }
29
- `);
30
- const fetchPageCached = memoTTL(async (handle, locale) => {
31
- const p = (await request(FetchPageQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).page;
13
+ /** One `pages.get` behind every read of a page: the route's page, a page
14
+ * setting's, and the page's metafields. Null while a buyer may not see it
15
+ * (NOT_FOUND) and on a failed read, which is not cached. */
16
+ const fetchPageRecord = memoTTL(async (ref, locale) => {
17
+ const p = await orFallback(orNotFound(storefront(locale).pages.get(pageRef(ref))), null, "fetchPage");
32
18
  if (!p) return null;
33
19
  return {
34
- id: p.id,
35
- handle: p.handle,
36
- title: p.title,
37
- bodyHtml: p.bodyHtml,
38
- bodyPlain: p.bodyPlain,
39
- themeTemplate: p.themeTemplate ?? null,
40
- storefrontThemeTemplate: p.storefrontThemeTemplate,
41
- publishedAt: p.publishedAt ?? null,
42
- updatedAt: p.updatedAt,
43
- seo: {
44
- title: p.seo.title,
45
- description: p.seo.description
46
- }
20
+ page: {
21
+ id: p.id,
22
+ handle: p.handle,
23
+ title: p.title,
24
+ bodyHtml: p.bodyHtml,
25
+ bodyPlain: p.bodyText,
26
+ themeTemplate: p.themeTemplate,
27
+ publishedAt: p.publishedAt,
28
+ updatedAt: p.updatedAt,
29
+ seo: {
30
+ title: p.seoTitle,
31
+ description: p.seoDescription
32
+ }
33
+ },
34
+ metafields: customDataMap(p.metafields)
47
35
  };
48
36
  }, {
49
- key: (handle, locale) => `page|${handle}|${locale ?? ""}`,
37
+ key: (ref, locale) => `page|${ref}|${locale ?? ""}`,
50
38
  shouldCache: (v) => v !== null,
51
39
  version: getContentVersion
52
40
  });
53
- /** Single-page fetch keyed by URL handle. Returns null when the page is
54
- * missing or not publicly visible — the route loader maps that to a 404. */
55
- const fetchPage = async (handle, locale) => {
56
- try {
57
- return await fetchPageCached(handle, locale);
58
- } catch (err) {
59
- console.warn("[fetchPage] failed:", err);
60
- return null;
61
- }
62
- };
63
- const FetchPageByIdQuery = graphql(`
64
- query FetchPageById($id: ID!) {
65
- page(id: $id) {
66
- title
67
- bodyHtml
68
- }
69
- }
70
- `);
71
- const fetchPageByIdCached = memoTTL(async (id, locale) => {
72
- const p = (await request(FetchPageByIdQuery, { id }, locale ? { acceptLanguage: locale } : void 0)).page;
73
- if (!p) return null;
74
- return {
75
- title: p.title,
76
- bodyHtml: p.bodyHtml
41
+ /** A page by handle or id. Null when it is missing, not visible to a buyer,
42
+ * or the read failed: the route loader maps that to a 404. */
43
+ async function fetchPage(ref, locale) {
44
+ return (await fetchPageRecord(ref, locale))?.page ?? null;
45
+ }
46
+ /** A page by its id (a page metafield's value), as a reference renders it;
47
+ * null when a buyer may not see it, and the caller falls back. */
48
+ async function fetchPageById(id, locale) {
49
+ const page = await fetchPage(id, locale);
50
+ return page && {
51
+ title: page.title,
52
+ bodyHtml: page.bodyHtml
77
53
  };
78
- }, {
79
- key: (id, locale) => `page-id|${id}|${locale ?? ""}`,
80
- shouldCache: (v) => v !== null,
81
- version: getContentVersion
82
- });
83
- /** Single-page fetch keyed by Relay GID (a page_reference metafield value).
84
- * `page(id:)` applies the same `public_visible` gate as the handle path, so a
85
- * missing/draft/cross-shop page returns null — the caller falls back. */
86
- const fetchPageById = async (id, locale) => {
87
- try {
88
- return await fetchPageByIdCached(id, locale);
89
- } catch (err) {
90
- console.warn("[fetchPageById] failed:", err);
91
- return null;
92
- }
93
- };
94
- const FetchPageMetafieldsQuery = graphql(`
95
- query FetchPageMetafields($handle: String, $id: ID) {
96
- page(handle: $handle, id: $id) {
97
- # Storefront-visible only (the API gates on the storefront scope).
98
- metafields(first: 50) {
99
- nodes {
100
- namespace
101
- key
102
- type
103
- value
104
- }
105
- }
106
- }
107
- }
108
- `);
109
- /** The page's storefront metafields keyed `"namespace.key"` — the
110
- * `page_metafield` dynamic source. Its own read, not a field on `fetchPage`,
111
- * so a page whose template binds none never asks for them. `{}` on a miss or
112
- * a failure, so the literal renders. */
113
- const fetchPageMetafields = metafieldsReader("fetchPageMetafields", async (handle, locale) => {
114
- return (await request(FetchPageMetafieldsQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).page?.metafields.nodes ?? null;
115
- });
54
+ }
55
+ /** The page's storefront metafields keyed `"namespace.key"`: the
56
+ * `page_metafield` dynamic source, off the same read as the page. `{}` on a
57
+ * miss or a failure, so the literal renders. */
58
+ async function fetchPageMetafields(ref, locale) {
59
+ return (await fetchPageRecord(ref, locale))?.metafields ?? {};
60
+ }
116
61
  /** The route CMS page as the theme loader's dynamic sources read it
117
62
  * (`DynamicSourcePage` in `@shop-on/theme-loader`): the title off the page
118
63
  * read the theme already made, and a lazy provider the loader calls only when
@@ -124,5 +69,4 @@ function routePageSource(page, locale) {
124
69
  getMetafields: () => fetchPageMetafields(page.handle, locale)
125
70
  };
126
71
  }
127
- //#endregion
128
72
  export { fetchPage, fetchPageById, fetchPageMetafields, routePageSource };
@@ -1,4 +1,3 @@
1
- //#region src/password-session.d.ts
2
1
  export interface PasswordSessionData {
3
2
  v?: string;
4
3
  }
@@ -6,5 +5,4 @@ export declare const readPasswordSession: () => Promise<PasswordSessionData | un
6
5
  export declare const writePasswordSession: (data: {
7
6
  v: string;
8
7
  }) => Promise<void>;
9
- export declare const clearPasswordSession: () => Promise<void>;
10
- //#endregion
8
+ export declare const clearPasswordSession: () => Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import { derivedCookieSecret } from "./cookie-secret.js";
2
2
  import { createServerOnlyFn } from "@tanstack/react-start";
3
- //#region src/password-session.ts
3
+ // src/password-session.ts
4
4
  const TTL_SECONDS = 2592e3;
5
5
  const DOMAIN_LABEL = "password";
6
6
  const sessionConfig = createServerOnlyFn(async () => {
@@ -36,5 +36,4 @@ const writePasswordSession = createServerOnlyFn(async (data) => {
36
36
  const clearPasswordSession = createServerOnlyFn(async () => {
37
37
  await (await loadSession()).clear();
38
38
  });
39
- //#endregion
40
39
  export { clearPasswordSession, readPasswordSession, writePasswordSession };
@@ -1,6 +1,5 @@
1
- //#region src/password.d.ts
2
1
  /** Whether this visitor is locked out: protection is on and their cookie does
3
- * not carry the current `passwordUpdatedAt`, so changing the password
2
+ * not carry the current password `version`, so changing the password
4
3
  * invalidates every outstanding unlock with no server-side state. A matching
5
4
  * `?key=` (the admin's "View shop" links, the screenshot job) mints the cookie
6
5
  * as a successful unlock does; `keyConsumed` tells the caller to strip it from
@@ -10,29 +9,37 @@ export declare function readPasswordGate(key: string | undefined): Promise<{
10
9
  locked: boolean;
11
10
  keyConsumed: boolean;
12
11
  }>;
13
- /** A refusal carries the API's first user error: its `code`
14
- * (`INVALID_PASSWORD`, `RATE_LIMITED`, `TURNSTILE_FAILED`, `NOT_ENABLED`) for
15
- * the form to word, and its `message` — English, `null` when the API gave
16
- * none — for a theme that shows it. */
12
+ /** Why the api kept the store locked, as the password pages word it:
13
+ * `INVALID_PASSWORD` (not the store's password), `TURNSTILE_FAILED` (the bot
14
+ * check did not pass), `RATE_LIMITED` (too many attempts: from this buyer's
15
+ * address, or at this shop; `retryAfterSeconds` until the next is counted),
16
+ * `NOT_ENABLED` (the merchant opened the store meanwhile: nothing left to
17
+ * unlock, so the page reloads into the store). */
17
18
  export type UnlockStorefrontResult = {
18
19
  ok: true;
19
20
  } | {
20
21
  ok: false;
21
- code: string;
22
- message: string | null;
22
+ code: "INVALID_PASSWORD" | "TURNSTILE_FAILED" | "NOT_ENABLED";
23
+ } | {
24
+ ok: false;
25
+ code: "RATE_LIMITED";
26
+ retryAfterSeconds: number;
23
27
  };
24
- /** Check the password (Turnstile and a per-IP rate limit live in the API) and,
25
- * when it is right, write the unlock cookie. The version is re-read UNCACHED
26
- * first: a buyer entering a just-changed password must never get a stale one
27
- * stamped, which would bounce them straight back to the gate. A transport or
28
- * GraphQL failure throws. */
28
+ /** Check the password with the api (`passwordProtection.unlock`: Turnstile,
29
+ * then the gate, then the password; wrong guesses counted per buyer address
30
+ * and per shop) and, when it is right, write the unlock cookie with the
31
+ * version the api answered: the current one, so a buyer entering a
32
+ * just-changed password is never stamped a stale one and bounced back to the
33
+ * gate. The token goes only when the form has one: a shop without a widget
34
+ * asks for none. Any other failure throws. */
29
35
  export declare function unlockStorefront(input: {
30
36
  password: string;
31
37
  turnstileToken: string;
32
38
  }): Promise<UnlockStorefrontResult>;
33
- /** The merchant's coming-soon message (Preferences), translated for `locale`
34
- * — Liquid's `shop.password_message`. Read uncached, so a just-edited message
39
+ /** The merchant's coming-soon message (Preferences) — Liquid's
40
+ * `shop.password_message`; in `locale`'s language when the merchant translated
41
+ * it, else the shop's primary language (the api's translation overlay). Read uncached, so a just-edited message
35
42
  * shows at once; the password page is rare and cheap. `null` when unset or
36
- * unreadable: the page must render without it. */
37
- export declare function fetchPasswordMessage(locale?: string): Promise<string | null>;
38
- //#endregion
43
+ * unreadable: the page must render without it (a refused storefront key still
44
+ * throws, as every loader does). */
45
+ export declare function fetchPasswordMessage(locale?: string): Promise<string | null>;
package/dist/password.js CHANGED
@@ -1,9 +1,9 @@
1
- import { graphql, request } from "./graphql.js";
2
- import { fetchPasswordProtection } from "./shop.js";
3
1
  import { readPasswordSession, writePasswordSession } from "./password-session.js";
4
- //#region src/password.ts
2
+ import { fetchPasswordProtection } from "./shop.js";
3
+ import { errorCode, orFallback, storefront } from "./storefront.js";
4
+ // src/password.ts
5
5
  /** Whether this visitor is locked out: protection is on and their cookie does
6
- * not carry the current `passwordUpdatedAt`, so changing the password
6
+ * not carry the current password `version`, so changing the password
7
7
  * invalidates every outstanding unlock with no server-side state. A matching
8
8
  * `?key=` (the admin's "View shop" links, the screenshot job) mints the cookie
9
9
  * as a successful unlock does; `keyConsumed` tells the caller to strip it from
@@ -11,81 +11,63 @@ import { readPasswordSession, writePasswordSession } from "./password-session.js
11
11
  * `fetchPasswordProtection` is memoised, so the per-request cost is near-free. */
12
12
  async function readPasswordGate(key) {
13
13
  const protection = await fetchPasswordProtection();
14
- if (!protection?.enabled || !protection.passwordUpdatedAt) return {
14
+ if (!protection.enabled || !protection.version) return {
15
15
  locked: false,
16
16
  keyConsumed: false
17
17
  };
18
18
  if (key && protection.bypassToken && key === protection.bypassToken) {
19
- await writePasswordSession({ v: protection.passwordUpdatedAt });
19
+ await writePasswordSession({ v: protection.version });
20
20
  return {
21
21
  locked: false,
22
22
  keyConsumed: true
23
23
  };
24
24
  }
25
25
  return {
26
- locked: (await readPasswordSession())?.v !== protection.passwordUpdatedAt,
26
+ locked: (await readPasswordSession())?.v !== protection.version,
27
27
  keyConsumed: false
28
28
  };
29
29
  }
30
- const UnlockMutation = graphql(`
31
- mutation StorefrontPasswordUnlock($input: StorefrontPasswordUnlockInput!) {
32
- storefrontPasswordUnlock(input: $input) {
33
- ok
34
- userErrors {
35
- code
36
- message
37
- }
38
- }
39
- }
40
- `);
41
- const VersionQuery = graphql(`
42
- query StorefrontPasswordVersion {
43
- shop {
44
- passwordProtection {
45
- passwordUpdatedAt
46
- }
47
- }
48
- }
49
- `);
50
- /** Check the password (Turnstile and a per-IP rate limit live in the API) and,
51
- * when it is right, write the unlock cookie. The version is re-read UNCACHED
52
- * first: a buyer entering a just-changed password must never get a stale one
53
- * stamped, which would bounce them straight back to the gate. A transport or
54
- * GraphQL failure throws. */
30
+ /** Check the password with the api (`passwordProtection.unlock`: Turnstile,
31
+ * then the gate, then the password; wrong guesses counted per buyer address
32
+ * and per shop) and, when it is right, write the unlock cookie with the
33
+ * version the api answered: the current one, so a buyer entering a
34
+ * just-changed password is never stamped a stale one and bounced back to the
35
+ * gate. The token goes only when the form has one: a shop without a widget
36
+ * asks for none. Any other failure throws. */
55
37
  async function unlockStorefront(input) {
56
- const payload = (await request(UnlockMutation, { input })).storefrontPasswordUnlock;
57
- if (!payload?.ok) {
58
- const error = payload?.userErrors[0];
59
- return {
38
+ let version;
39
+ try {
40
+ ({version} = await storefront().passwordProtection.unlock({
41
+ password: input.password,
42
+ ...input.turnstileToken ? { turnstileToken: input.turnstileToken } : {}
43
+ }));
44
+ } catch (error) {
45
+ const code = errorCode(error);
46
+ if (code === "RATE_LIMITED") {
47
+ const { retryAfterSeconds } = error.data;
48
+ return {
49
+ ok: false,
50
+ code,
51
+ retryAfterSeconds
52
+ };
53
+ }
54
+ if (code === "INVALID_PASSWORD" || code === "TURNSTILE_FAILED" || code === "NOT_ENABLED") return {
60
55
  ok: false,
61
- code: error?.code ?? "UNKNOWN",
62
- message: error?.message ?? null
56
+ code
63
57
  };
58
+ throw error;
64
59
  }
65
- const version = (await request(VersionQuery)).shop?.passwordProtection?.passwordUpdatedAt;
66
- if (version) await writePasswordSession({ v: version });
60
+ await writePasswordSession({ v: version });
67
61
  return { ok: true };
68
62
  }
69
- const MessageQuery = graphql(`
70
- query StorefrontPasswordMessage {
71
- shop {
72
- passwordProtection {
73
- message
74
- }
75
- }
76
- }
77
- `);
78
- /** The merchant's coming-soon message (Preferences), translated for `locale`
79
- * — Liquid's `shop.password_message`. Read uncached, so a just-edited message
63
+ /** The merchant's coming-soon message (Preferences) — Liquid's
64
+ * `shop.password_message`; in `locale`'s language when the merchant translated
65
+ * it, else the shop's primary language (the api's translation overlay). Read uncached, so a just-edited message
80
66
  * shows at once; the password page is rare and cheap. `null` when unset or
81
- * unreadable: the page must render without it. */
67
+ * unreadable: the page must render without it (a refused storefront key still
68
+ * throws, as every loader does). */
82
69
  async function fetchPasswordMessage(locale) {
83
- try {
84
- return (await request(MessageQuery, {}, locale ? { acceptLanguage: locale } : void 0)).shop?.passwordProtection?.message ?? null;
85
- } catch (err) {
86
- console.warn("[fetchPasswordMessage] failed:", err);
87
- return null;
88
- }
70
+ const { message } = await orFallback(storefront(locale).passwordProtection.message({}), { message: null }, "fetchPasswordMessage");
71
+ return message;
89
72
  }
90
- //#endregion
91
73
  export { fetchPasswordMessage, readPasswordGate, unlockStorefront };
package/dist/policy.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- //#region src/policy.d.ts
2
1
  export type PolicyData = {
3
2
  handle: string;
4
3
  title: string;
@@ -6,15 +5,16 @@ export type PolicyData = {
6
5
  url: string;
7
6
  updatedAt: string;
8
7
  };
9
- /** Single-policy fetch keyed by handle (the URL slug). Returns null when the
10
- * policy slot is unconfigured — the route loader maps that to a 404. */
8
+ /** Single-policy fetch keyed by handle (the URL slug). Null when the shop has
9
+ * not written that policy — the route loader maps that to a 404. Any other
10
+ * failure is thrown: the route's error, never a 404 a crawler would keep. */
11
11
  export declare const fetchPolicy: (handle: string, locale?: string) => Promise<PolicyData | null>;
12
12
  export type PolicyLink = {
13
13
  handle: string;
14
14
  title: string;
15
15
  url: string;
16
16
  };
17
- /** Every configured policy, for a footer link list. An unconfigured shop
18
- * returns `[]`, which the consuming block renders as nothing at all. */
19
- export declare const fetchShopPolicies: (locale?: string) => Promise<PolicyLink[]>;
20
- //#endregion
17
+ /** Every policy the shop has written, for a footer link list. A shop with none
18
+ * returns `[]`, which the consuming block renders as nothing at all; so does
19
+ * a failed read, since the page renders without it. */
20
+ export declare const fetchShopPolicies: (locale?: string) => Promise<PolicyLink[]>;
package/dist/policy.js CHANGED
@@ -1,58 +1,29 @@
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/policy.ts
5
- const FetchPolicyQuery = graphql(`
6
- query FetchPolicy($handle: String!) {
7
- shop {
8
- shopPolicy(handle: $handle) {
9
- handle
10
- title
11
- bodyHtml
12
- url
13
- updatedAt
14
- }
15
- }
16
- }
17
- `);
3
+ import { orFallback, orNotFound, storefront } from "./storefront.js";
4
+ // src/policy.ts
18
5
  const fetchPolicyCached = memoTTL(async (handle, locale) => {
19
- const p = (await request(FetchPolicyQuery, { handle }, locale ? { acceptLanguage: locale } : void 0)).shop?.shopPolicy;
20
- if (!p) return null;
6
+ const policy = await orNotFound(storefront(locale).policies.get({ handle }));
7
+ if (!policy) return null;
21
8
  return {
22
- handle: p.handle,
23
- title: p.title,
24
- bodyHtml: p.bodyHtml,
25
- url: p.url,
26
- updatedAt: p.updatedAt
9
+ handle: policy.handle,
10
+ title: policy.title,
11
+ bodyHtml: policy.bodyHtml,
12
+ url: policy.url,
13
+ updatedAt: policy.updatedAt
27
14
  };
28
15
  }, {
29
16
  key: (handle, locale) => `policy|${handle}|${locale ?? ""}`,
30
17
  shouldCache: (v) => v !== null,
31
18
  version: getContentVersion
32
19
  });
33
- /** Single-policy fetch keyed by handle (the URL slug). Returns null when the
34
- * policy slot is unconfigured — the route loader maps that to a 404. */
35
- const fetchPolicy = async (handle, locale) => {
36
- try {
37
- return await fetchPolicyCached(handle, locale);
38
- } catch (err) {
39
- console.warn("[fetchPolicy] failed:", err);
40
- return null;
41
- }
42
- };
43
- const FetchShopPoliciesQuery = graphql(`
44
- query FetchShopPolicies {
45
- shop {
46
- shopPolicies {
47
- handle
48
- title
49
- url
50
- }
51
- }
52
- }
53
- `);
20
+ /** Single-policy fetch keyed by handle (the URL slug). Null when the shop has
21
+ * not written that policy — the route loader maps that to a 404. Any other
22
+ * failure is thrown: the route's error, never a 404 a crawler would keep. */
23
+ const fetchPolicy = (handle, locale) => fetchPolicyCached(handle, locale);
54
24
  const fetchShopPoliciesCached = memoTTL(async (locale) => {
55
- return ((await request(FetchShopPoliciesQuery, {}, locale ? { acceptLanguage: locale } : void 0)).shop?.shopPolicies ?? []).map((p) => ({
25
+ const { items } = await storefront(locale).policies.list({});
26
+ return items.map((p) => ({
56
27
  handle: p.handle,
57
28
  title: p.title,
58
29
  url: p.url
@@ -61,15 +32,8 @@ const fetchShopPoliciesCached = memoTTL(async (locale) => {
61
32
  key: (locale) => `shop-policies|${locale ?? ""}`,
62
33
  version: getContentVersion
63
34
  });
64
- /** Every configured policy, for a footer link list. An unconfigured shop
65
- * returns `[]`, which the consuming block renders as nothing at all. */
66
- const fetchShopPolicies = async (locale) => {
67
- try {
68
- return await fetchShopPoliciesCached(locale);
69
- } catch (err) {
70
- console.warn("[fetchShopPolicies] failed:", err);
71
- return [];
72
- }
73
- };
74
- //#endregion
35
+ /** Every policy the shop has written, for a footer link list. A shop with none
36
+ * returns `[]`, which the consuming block renders as nothing at all; so does
37
+ * a failed read, since the page renders without it. */
38
+ const fetchShopPolicies = (locale) => orFallback(fetchShopPoliciesCached(locale), [], "fetchShopPolicies");
75
39
  export { fetchPolicy, fetchShopPolicies };