@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
package/dist/menu.d.ts CHANGED
@@ -1,156 +1,47 @@
1
- import { ResultOf } from "./graphql.js";
2
- //#region src/menu.d.ts
3
- declare const MenuQuery: import("gql.tada").TadaDocumentNode<{
4
- menu: {
5
- handle: string;
6
- id: string;
7
- items: {
8
- nodes: {
9
- children: {
10
- children: {
11
- id: string;
12
- itemType: "ARTICLE" | "BLOG" | "COLLECTION" | "HTTP" | "PAGE" | "POLICY" | "PRODUCT";
13
- resource: {
14
- __typename?: "ShopArticle" | undefined;
15
- } | {
16
- __typename?: "ShopBlog" | undefined;
17
- } | {
18
- __typename?: "ShopCollection" | undefined;
19
- handle: string;
20
- image: {
21
- __typename: "ShopFont";
22
- alt: string | null;
23
- id: string;
24
- thumbhash: string | null;
25
- } | {
26
- __typename: "ShopMediaImage";
27
- alt: string | null;
28
- focalPoint: {
29
- x: number;
30
- y: number;
31
- } | null;
32
- height: number | null;
33
- id: string;
34
- thumbUrl: string | null;
35
- thumbhash: string | null;
36
- url: string | null;
37
- url2x: string | null;
38
- url3x: string | null;
39
- width: number | null;
40
- } | {
41
- __typename: "ShopVideo";
42
- alt: string | null;
43
- id: string;
44
- thumbhash: string | null;
45
- } | null;
46
- } | {
47
- __typename?: "ShopPage" | undefined;
48
- } | {
49
- __typename?: "ShopPolicy" | undefined;
50
- } | {
51
- __typename?: "ShopProduct" | undefined;
52
- } | null;
53
- title: string;
54
- url: string | null;
55
- }[];
56
- id: string;
57
- itemType: "ARTICLE" | "BLOG" | "COLLECTION" | "HTTP" | "PAGE" | "POLICY" | "PRODUCT";
58
- resource: {
59
- __typename?: "ShopArticle" | undefined;
60
- } | {
61
- __typename?: "ShopBlog" | undefined;
62
- } | {
63
- __typename?: "ShopCollection" | undefined;
64
- handle: string;
65
- image: {
66
- __typename: "ShopFont";
67
- alt: string | null;
68
- id: string;
69
- thumbhash: string | null;
70
- } | {
71
- __typename: "ShopMediaImage";
72
- alt: string | null;
73
- focalPoint: {
74
- x: number;
75
- y: number;
76
- } | null;
77
- height: number | null;
78
- id: string;
79
- thumbUrl: string | null;
80
- thumbhash: string | null;
81
- url: string | null;
82
- url2x: string | null;
83
- url3x: string | null;
84
- width: number | null;
85
- } | {
86
- __typename: "ShopVideo";
87
- alt: string | null;
88
- id: string;
89
- thumbhash: string | null;
90
- } | null;
91
- } | {
92
- __typename?: "ShopPage" | undefined;
93
- } | {
94
- __typename?: "ShopPolicy" | undefined;
95
- } | {
96
- __typename?: "ShopProduct" | undefined;
97
- } | null;
98
- title: string;
99
- url: string | null;
100
- }[];
101
- id: string;
102
- itemType: "ARTICLE" | "BLOG" | "COLLECTION" | "HTTP" | "PAGE" | "POLICY" | "PRODUCT";
103
- resource: {
104
- __typename?: "ShopArticle" | undefined;
105
- } | {
106
- __typename?: "ShopBlog" | undefined;
107
- } | {
108
- __typename?: "ShopCollection" | undefined;
109
- handle: string;
110
- image: {
111
- __typename: "ShopFont";
112
- alt: string | null;
113
- id: string;
114
- thumbhash: string | null;
115
- } | {
116
- __typename: "ShopMediaImage";
117
- alt: string | null;
118
- focalPoint: {
119
- x: number;
120
- y: number;
121
- } | null;
122
- height: number | null;
123
- id: string;
124
- thumbUrl: string | null;
125
- thumbhash: string | null;
126
- url: string | null;
127
- url2x: string | null;
128
- url3x: string | null;
129
- width: number | null;
130
- } | {
131
- __typename: "ShopVideo";
132
- alt: string | null;
133
- id: string;
134
- thumbhash: string | null;
135
- } | null;
136
- } | {
137
- __typename?: "ShopPage" | undefined;
138
- } | {
139
- __typename?: "ShopPolicy" | undefined;
140
- } | {
141
- __typename?: "ShopProduct" | undefined;
142
- } | null;
143
- title: string;
144
- url: string | null;
145
- }[];
146
- };
147
- title: string;
148
- } | null;
149
- }, {
1
+ import { ImageFileInfo } from "./files.js";
2
+ /** What a menu link leads to, read now; a URL link, and one whose destination is gone, has none. */
3
+ export type MenuResource = {
4
+ kind: "product" | "page" | "blog";
5
+ id: string;
150
6
  handle: string;
151
- }, void>;
152
- export type MenuData = NonNullable<ResultOf<typeof MenuQuery>["menu"]>;
153
- export type MenuItemData = MenuData["items"]["nodes"][number];
154
- export declare function fetchMenu(handle: string, locale?: string): Promise<MenuData | null>;
155
- //#endregion
156
- export {};
7
+ title: string;
8
+ } | {
9
+ kind: "collection";
10
+ id: string;
11
+ handle: string;
12
+ title: string;
13
+ image: ImageFileInfo | null;
14
+ } | {
15
+ kind: "article";
16
+ id: string;
17
+ handle: string;
18
+ title: string;
19
+ blogHandle: string;
20
+ } | {
21
+ kind: "policy";
22
+ handle: string;
23
+ title: string;
24
+ };
25
+ export type MenuLinkKind = "url" | "product" | "collection" | "page" | "blog" | "article" | "policy";
26
+ export type MenuItemData = {
27
+ id: string;
28
+ title: string;
29
+ /** Where it leads: a URL link's own, else its destination's storefront path.
30
+ * Null when the destination is gone (deleted, or a policy not written): the
31
+ * link keeps its place and its children and is drawn as text. */
32
+ url: string | null;
33
+ kind: MenuLinkKind;
34
+ resource: MenuResource | null;
35
+ /** The links under it (three levels in all). */
36
+ items: MenuItemData[];
37
+ };
38
+ export type MenuData = {
39
+ id: string;
40
+ handle: string;
41
+ title: string;
42
+ items: MenuItemData[];
43
+ };
44
+ /** A menu with every link, in the read's language (titles,
45
+ * and the resources' own). Null for a handle the shop lacks, and on a failed
46
+ * read: a header renders without it. */
47
+ export declare function fetchMenu(handle: string, locale?: string): Promise<MenuData | null>;
package/dist/menu.js CHANGED
@@ -1,71 +1,61 @@
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/menu.ts
5
- const MenuItemFragment = graphql(`
6
- fragment MenuItemFields on ShopMenuItem @_unmask {
7
- id
8
- title
9
- url
10
- itemType
11
- resource {
12
- ... on ShopCollection {
13
- handle
14
- image {
15
- __typename
16
- id
17
- alt
18
- thumbhash
19
- ... on ShopMediaImage {
20
- url(transform: { maxWidth: 640 })
21
- url2x: url(transform: { maxWidth: 640, scale: 2 })
22
- url3x: url(transform: { maxWidth: 640, scale: 3 })
23
- thumbUrl: url(transform: { maxWidth: 80 })
24
- width
25
- height
26
- focalPoint {
27
- x
28
- y
29
- }
30
- }
31
- }
32
- }
33
- }
34
- }
35
- `);
36
- const MenuQuery = graphql(`
37
- query StorefrontMenu($handle: String!) {
38
- menu(handle: $handle) {
39
- id
40
- handle
41
- title
42
- items(first: 20) {
43
- nodes {
44
- ...MenuItemFields
45
- children {
46
- ...MenuItemFields
47
- children {
48
- ...MenuItemFields
49
- }
50
- }
51
- }
52
- }
53
- }
54
- }
55
- `, [MenuItemFragment]);
3
+ import { imageFileInfo } from "./files.js";
4
+ import { orFallback, orNotFound, storefront } from "./storefront.js";
5
+ // src/menu.ts
6
+ /** The width a collection link's image is drawn at: an image-led mega menu's
7
+ * tile (Horizon's `collection_images` / `featured_collections` submenus). */
8
+ const MENU_COLLECTION_IMAGE_WIDTH = 640;
9
+ function toResource(resource) {
10
+ switch (resource.kind) {
11
+ case "collection": return {
12
+ kind: "collection",
13
+ id: resource.id,
14
+ handle: resource.handle,
15
+ title: resource.title,
16
+ image: resource.image ? imageFileInfo(resource.image, MENU_COLLECTION_IMAGE_WIDTH) : null
17
+ };
18
+ case "article": return { ...resource };
19
+ case "policy": return {
20
+ kind: "policy",
21
+ handle: resource.handle,
22
+ title: resource.title
23
+ };
24
+ default: return {
25
+ kind: resource.kind,
26
+ id: resource.id,
27
+ handle: resource.handle,
28
+ title: resource.title
29
+ };
30
+ }
31
+ }
32
+ function toItem(link) {
33
+ return {
34
+ id: link.id,
35
+ title: link.title,
36
+ url: link.url,
37
+ kind: link.kind,
38
+ resource: link.resource ? toResource(link.resource) : null,
39
+ items: link.items.map(toItem)
40
+ };
41
+ }
56
42
  const fetchMenuCached = memoTTL(async (handle, locale) => {
57
- return (await request(MenuQuery, { handle }, locale ? { acceptLanguage: locale } : void 0)).menu ?? null;
43
+ const menu = await orNotFound(storefront(locale).menus.get({ handle }));
44
+ if (!menu) return null;
45
+ return {
46
+ id: menu.id,
47
+ handle: menu.handle,
48
+ title: menu.title,
49
+ items: menu.items.map(toItem)
50
+ };
58
51
  }, {
59
52
  key: (handle, locale) => `${handle}|${locale ?? ""}`,
60
53
  version: getContentVersion
61
54
  });
55
+ /** A menu with every link, in the read's language (titles,
56
+ * and the resources' own). Null for a handle the shop lacks, and on a failed
57
+ * read: a header renders without it. */
62
58
  async function fetchMenu(handle, locale) {
63
- try {
64
- return await fetchMenuCached(handle, locale);
65
- } catch (err) {
66
- console.warn(`[fetchMenu] failed for handle="${handle}":`, err);
67
- return null;
68
- }
59
+ return orFallback(fetchMenuCached(handle, locale), null, "fetchMenu");
69
60
  }
70
- //#endregion
71
61
  export { fetchMenu };
@@ -1,7 +1,11 @@
1
- //#region src/metafields.d.ts
2
- /** A resource's storefront metafields keyed `"namespace.key"`. */
1
+ /** A value as JSON holds it: what a metafield's value is on the wire, and
2
+ * what a server function's answer may carry. */
3
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {
4
+ [key: string]: JsonValue;
5
+ };
6
+ /** A resource's storefront metafields keyed `"namespace.key"`, each its
7
+ * value type's name and its value (theme-contract's `CustomDataValue`). */
3
8
  export type MetafieldMap = Record<string, {
4
9
  type: string;
5
- value: string;
6
- }>;
7
- //#endregion
10
+ value: JsonValue;
11
+ }>;
@@ -1,43 +1,15 @@
1
- import { memoTTL } from "./cache.js";
2
- import { getContentVersion } from "./content-version.js";
3
- //#region src/metafields.ts
4
- /** Metafield nodes as the `"namespace.key"`-keyed map every reader uses. */
5
- function toMetafieldMap(nodes) {
6
- return Object.fromEntries(nodes.map((m) => [`${m.namespace}.${m.key}`, {
1
+ // src/metafields.ts
2
+ /** The values of a storefront procedure's custom data (`storefrontCustomData`)
3
+ * as the `"namespace.key"`-keyed map every reader uses. */
4
+ function customDataMap(data) {
5
+ return Object.fromEntries(data.values.map((m) => [`${m.namespace}.${m.key}`, {
7
6
  type: m.type,
8
7
  value: m.value
9
8
  }]));
10
9
  }
11
- /** A `LIST_*` field's stored JSON array as strings; `[]` when it is not one. */
12
- function parseListValues(raw) {
13
- try {
14
- const arr = JSON.parse(raw);
15
- if (!Array.isArray(arr)) return [];
16
- return arr.filter((x) => x != null).map(String);
17
- } catch {
18
- return [];
19
- }
10
+ /** The cache-key segment for a list of `namespace.key` names: sorted, so two
11
+ * callers naming the same keys in another order share one entry. */
12
+ function metafieldKeysKey(keys) {
13
+ return keys?.length ? [...keys].sort().join(",") : "";
20
14
  }
21
- /** The cached metafield read behind each route resource's `*_metafield`
22
- * dynamic source. `read` returns the resource's metafield nodes, or `null`
23
- * when the resource is missing. A resource with no published fields is `{}`
24
- * and IS cached; a missing resource or a failed read is retried next time.
25
- * Either way the caller gets `{}`, so the template renders its literal. */
26
- function metafieldsReader(name, read) {
27
- const cached = memoTTL(async (ref, locale) => {
28
- try {
29
- const nodes = await read(ref, locale);
30
- return nodes ? toMetafieldMap(nodes) : null;
31
- } catch (err) {
32
- console.warn(`[${name}] failed:`, err);
33
- return null;
34
- }
35
- }, {
36
- key: (ref, locale) => `${name}|${ref}|${locale ?? ""}`,
37
- shouldCache: (v) => v !== null,
38
- version: getContentVersion
39
- });
40
- return async (ref, locale) => await cached(ref, locale) ?? {};
41
- }
42
- //#endregion
43
- export { metafieldsReader, parseListValues, toMetafieldMap };
15
+ export { customDataMap, metafieldKeysKey };
@@ -1,10 +1,14 @@
1
1
  import { FileInfo } from "./files.js";
2
- //#region src/metaobject.d.ts
2
+ import { JsonValue } from "./metafields.js";
3
3
  export type MetaobjectFieldData = {
4
4
  key: string;
5
+ /** The value type, snake_case: `color`, `single_line_text`, `rich_text`… */
5
6
  type: string;
6
- value: string | null;
7
- values?: string[];
7
+ /** The type's wire value: a list's an array, a rich text's its node tree, a
8
+ * reference's the target's id. */
9
+ value: JsonValue;
10
+ /** A `file_reference` field's file, resolved; null when it is gone or is
11
+ * neither an image nor a video. */
8
12
  file?: FileInfo | null;
9
13
  };
10
14
  export type MetaobjectData = {
@@ -14,5 +18,6 @@ export type MetaobjectData = {
14
18
  display_name: string;
15
19
  fields: Record<string, MetaobjectFieldData>;
16
20
  };
17
- export declare function fetchMetaobject(id: string): Promise<MetaobjectData | null>;
18
- //#endregion
21
+ /** A metaobject entry by id; null for one a buyer may not read, an id that is
22
+ * no entry's (no call is made), and on a failed read. */
23
+ export declare function fetchMetaobject(id: string): Promise<MetaobjectData | null>;
@@ -1,84 +1,61 @@
1
+ import { liteId } from "./api-contract/src/ids.js";
1
2
  import { memoTTL } from "./cache.js";
2
- import { graphql, request } from "./graphql.js";
3
+ import { coalescedReads } from "./coalesce.js";
3
4
  import { getContentVersion } from "./content-version.js";
4
- import { toFileInfo } from "./files.js";
5
- import { parseListValues } from "./metafields.js";
6
- //#region src/metaobject.ts
7
- const FetchMetaobjectQuery = graphql(`
8
- query FetchMetaobject($id: ID!) {
9
- metaobject(id: $id) {
10
- id
11
- handle
12
- type
13
- displayName
14
- fields {
15
- key
16
- type
17
- value
18
- reference {
19
- __typename
20
- ... on ShopMediaImage {
21
- id
22
- alt
23
- thumbhash
24
- width
25
- height
26
- url(transform: { maxWidth: 256 })
27
- url2x: url(transform: { maxWidth: 256, scale: 2 })
28
- url3x: url(transform: { maxWidth: 256, scale: 3 })
29
- thumbUrl: url(transform: { maxWidth: 64 })
30
- focalPoint {
31
- x
32
- y
33
- }
34
- }
35
- ... on ShopVideo {
36
- id
37
- alt
38
- url
39
- hlsUrl
40
- width
41
- height
42
- duration
43
- }
44
- }
45
- }
46
- }
47
- }
48
- `);
49
- const fetchMetaobjectCached = memoTTL(async (id) => {
50
- const m = (await request(FetchMetaobjectQuery, { id })).metaobject;
51
- if (!m) return null;
5
+ import { imageFileInfo, videoFileInfo } from "./files.js";
6
+ import { orFallback, storefront } from "./storefront.js";
7
+ // src/metaobject.ts
8
+ /** The width a field's referenced image (and a video's poster) is drawn at. */
9
+ const FIELD_FILE_WIDTH = 256;
10
+ function fieldFile(id, files) {
11
+ const file = typeof id === "string" ? files.find((f) => f.id === id) : void 0;
12
+ if (file?.image) return imageFileInfo(file.image, FIELD_FILE_WIDTH);
13
+ if (file?.video) return videoFileInfo(file.video, FIELD_FILE_WIDTH);
14
+ return null;
15
+ }
16
+ function toMetaobject({ entry, files }) {
52
17
  const fields = {};
53
- for (const f of m.fields) {
54
- const entry = {
18
+ for (const f of entry.fields) {
19
+ const field = {
55
20
  key: f.key,
56
21
  type: f.type,
57
- value: f.value ?? null
22
+ value: f.value
58
23
  };
59
- if (f.type.startsWith("LIST_")) entry.values = f.value ? parseListValues(f.value) : [];
60
- if (f.type === "FILE_REFERENCE") entry.file = toFileInfo(f.reference, { baseWidth: 256 });
61
- fields[f.key] = entry;
24
+ if (f.type === "file_reference" && !f.list) field.file = fieldFile(f.value, files);
25
+ fields[f.key] = field;
62
26
  }
63
27
  return {
64
- id: m.id,
65
- handle: m.handle,
66
- type: m.type,
67
- display_name: m.displayName,
28
+ id: entry.id,
29
+ handle: entry.handle,
30
+ type: entry.type,
31
+ display_name: entry.displayName,
68
32
  fields
69
33
  };
34
+ }
35
+ /** Entry reads of this request's tick share a `metaobjects.entries` call;
36
+ * each carries the call's referenced files. */
37
+ const coalescedEntry = coalescedReads({
38
+ max: 50,
39
+ matches: ({ entry }, ref) => entry.id === ref
40
+ });
41
+ const fetchMetaobjectCached = memoTTL(async (id) => {
42
+ const found = await coalescedEntry("", id, async (refs) => {
43
+ const answer = await storefront().metaobjects.entries({ refs: refs.map((ref) => ({ metaobjectId: liteId("metaobject").parse(ref) })) });
44
+ return answer.items.map((entry) => ({
45
+ entry,
46
+ files: answer.references.files
47
+ }));
48
+ });
49
+ return found && toMetaobject(found);
70
50
  }, {
71
51
  key: (id) => `metaobject|${id}`,
72
52
  shouldCache: (v) => v !== null,
73
53
  version: getContentVersion
74
54
  });
55
+ /** A metaobject entry by id; null for one a buyer may not read, an id that is
56
+ * no entry's (no call is made), and on a failed read. */
75
57
  async function fetchMetaobject(id) {
76
- try {
77
- return await fetchMetaobjectCached(id);
78
- } catch (err) {
79
- console.warn("[fetchMetaobject] failed:", err);
80
- return null;
81
- }
58
+ if (!liteId("metaobject").safeParse(id).success) return null;
59
+ return orFallback(fetchMetaobjectCached(id), null, "fetchMetaobject");
82
60
  }
83
- //#endregion
84
61
  export { fetchMetaobject };
@@ -0,0 +1,7 @@
1
+ import { Money } from "@shop-on/theme-runtime/money";
2
+ /** An api amount (möngö) as the `Money` a theme reads. */
3
+ export declare function toMoney(mnt: number): Money;
4
+ /** A buyer's amount in tögrög (a price filter's bound) as the api's möngö,
5
+ * to the nearest möngö; null for one the api would refuse (negative, not a
6
+ * number, or past what a möngö amount holds). */
7
+ export declare function toMnt(amount: number): number | null;
package/dist/money.js ADDED
@@ -0,0 +1,18 @@
1
+ // src/money.ts
2
+ /** MNT is the only currency the api holds. */
3
+ const CURRENCY = "MNT";
4
+ /** An api amount (möngö) as the `Money` a theme reads. */
5
+ function toMoney(mnt) {
6
+ return {
7
+ amount: mnt / 100,
8
+ currencyCode: CURRENCY
9
+ };
10
+ }
11
+ /** A buyer's amount in tögrög (a price filter's bound) as the api's möngö,
12
+ * to the nearest möngö; null for one the api would refuse (negative, not a
13
+ * number, or past what a möngö amount holds). */
14
+ function toMnt(amount) {
15
+ const mnt = Math.round(amount * 100);
16
+ return Number.isSafeInteger(mnt) && mnt >= 0 ? mnt : null;
17
+ }
18
+ export { toMnt, toMoney };
@@ -1,17 +1,26 @@
1
- //#region src/newsletter.d.ts
2
- /** A refusal carries the API's first user error: its `code`, and its `message`
3
- * — English, `null` when the API gave none — for a theme that shows it. */
1
+ /** Why the api did not record the sign-up, for the form to word:
2
+ * `TURNSTILE_FAILED` (the bot check did not pass), `VALIDATION_FAILED`
3
+ * (`field` the input it names, `email`), `RATE_LIMITED` (too many sign-ups
4
+ * from this buyer's address, for this email or at this shop; nothing was
5
+ * recorded, so the form says to try later, never that it worked). */
4
6
  export type SubscribeNewsletterResult = {
5
7
  ok: true;
6
8
  } | {
7
9
  ok: false;
8
- code: string;
9
- message: string | null;
10
+ code: "TURNSTILE_FAILED";
11
+ } | {
12
+ ok: false;
13
+ code: "VALIDATION_FAILED";
14
+ field: string | null;
15
+ } | {
16
+ ok: false;
17
+ code: "RATE_LIMITED";
18
+ retryAfterSeconds: number;
10
19
  };
11
- /** Subscribe `email`. A refusal comes back as the API's first user error; a
12
- * transport or GraphQL failure throws. */
20
+ /** Subscribe `email`. The token goes only when the form has one: a shop
21
+ * without a widget asks for none. Any other failure throws. */
13
22
  export declare function subscribeNewsletter(input: {
14
23
  email: string;
15
- turnstileToken: string;
16
- }): Promise<SubscribeNewsletterResult>;
17
- //#endregion
24
+ /** The widget's token; empty or absent when the shop has no widget. */
25
+ turnstileToken?: string;
26
+ }): Promise<SubscribeNewsletterResult>;