@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.5

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 (204) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +98 -10
  3. package/dist/abi/canvas-live.d.ts +49 -0
  4. package/dist/abi/canvas-live.js +88 -0
  5. package/dist/abi/cart.d.ts +48 -0
  6. package/dist/abi/cart.js +321 -0
  7. package/dist/abi/convert.d.ts +192 -0
  8. package/dist/abi/convert.js +670 -0
  9. package/dist/abi/data-api.d.ts +15 -0
  10. package/dist/abi/data-api.js +198 -0
  11. package/dist/abi/jcs.d.ts +6 -0
  12. package/dist/abi/jcs.js +28 -0
  13. package/dist/abi/page-data.d.ts +209 -0
  14. package/dist/abi/page-data.js +583 -0
  15. package/dist/abi/page.d.ts +23 -0
  16. package/dist/abi/page.js +46 -0
  17. package/dist/abi/referrals.d.ts +23 -0
  18. package/dist/abi/referrals.js +89 -0
  19. package/dist/abi/rewards.d.ts +27 -0
  20. package/dist/abi/rewards.js +82 -0
  21. package/dist/abi/seo.d.ts +47 -0
  22. package/dist/abi/seo.js +68 -0
  23. package/dist/abi/shell.d.ts +22 -0
  24. package/dist/abi/shell.js +10 -0
  25. package/dist/abi/wire.d.ts +38 -0
  26. package/dist/abi/wire.js +84 -0
  27. package/dist/build.d.ts +113 -0
  28. package/dist/build.js +294 -0
  29. package/dist/client.d.ts +19 -0
  30. package/dist/client.js +76 -0
  31. package/dist/contract.d.ts +6 -0
  32. package/dist/contract.js +6 -0
  33. package/dist/headless/ai.d.ts +132 -0
  34. package/dist/headless/ai.js +99 -0
  35. package/dist/headless/hooks.d.ts +273 -0
  36. package/dist/headless/hooks.js +533 -0
  37. package/dist/headless/i18n.d.ts +13 -0
  38. package/dist/headless/i18n.js +45 -0
  39. package/dist/headless/index.d.ts +36 -0
  40. package/dist/headless/index.js +38 -0
  41. package/dist/headless/money.d.ts +25 -0
  42. package/dist/headless/money.js +61 -0
  43. package/dist/headless/page.d.ts +43 -0
  44. package/dist/headless/page.js +53 -0
  45. package/dist/headless/platform.d.ts +55 -0
  46. package/dist/headless/platform.js +77 -0
  47. package/dist/headless/primitives/cart.d.ts +90 -0
  48. package/dist/headless/primitives/cart.js +153 -0
  49. package/dist/headless/primitives/collection.d.ts +68 -0
  50. package/dist/headless/primitives/collection.js +80 -0
  51. package/dist/headless/primitives/product.d.ts +101 -0
  52. package/dist/headless/primitives/product.js +117 -0
  53. package/dist/headless/primitives/storefront.d.ts +106 -0
  54. package/dist/headless/primitives/storefront.js +186 -0
  55. package/dist/headless/primitives/util.d.ts +19 -0
  56. package/dist/headless/primitives/util.js +42 -0
  57. package/dist/headless/strings.d.ts +9 -0
  58. package/dist/headless/strings.js +186 -0
  59. package/dist/headless/structured-data.d.ts +31 -0
  60. package/dist/headless/structured-data.js +57 -0
  61. package/dist/headless/theme.d.ts +99 -0
  62. package/dist/headless/theme.js +54 -0
  63. package/dist/index.d.ts +20 -0
  64. package/dist/index.js +18 -553
  65. package/dist/runtime/data/StoreProvider.d.ts +56 -0
  66. package/dist/runtime/data/StoreProvider.js +308 -0
  67. package/dist/runtime/data/handle.d.ts +14 -0
  68. package/dist/runtime/data/handle.js +16 -0
  69. package/dist/runtime/data/routes.d.ts +87 -0
  70. package/dist/runtime/data/routes.js +154 -0
  71. package/dist/runtime/data/to-ui.d.ts +258 -0
  72. package/dist/runtime/data/to-ui.js +820 -0
  73. package/dist/runtime/entries/register.d.ts +9 -0
  74. package/dist/runtime/entries/register.js +9 -0
  75. package/dist/runtime/entries/sections.d.ts +16 -0
  76. package/dist/runtime/entries/sections.js +19 -0
  77. package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
  78. package/dist/runtime/kit/SectionTree.js +100 -0
  79. package/dist/runtime/kit/analytics.d.ts +63 -0
  80. package/dist/runtime/kit/analytics.js +233 -0
  81. package/dist/runtime/kit/cart.d.ts +186 -0
  82. package/dist/runtime/kit/cart.js +545 -0
  83. package/dist/{types → runtime/kit}/composition.d.ts +1 -1
  84. package/dist/runtime/kit/composition.js +3 -0
  85. package/dist/{types → runtime/kit}/config.d.ts +2 -2
  86. package/dist/runtime/kit/config.js +6 -0
  87. package/dist/runtime/kit/consent.d.ts +29 -0
  88. package/dist/runtime/kit/consent.js +159 -0
  89. package/dist/runtime/kit/contract/content.d.ts +164 -0
  90. package/dist/runtime/kit/contract/content.js +83 -0
  91. package/dist/runtime/kit/contract/field-types.d.ts +290 -0
  92. package/dist/runtime/kit/contract/field-types.js +129 -0
  93. package/dist/runtime/kit/contract/groups.d.ts +230 -0
  94. package/dist/runtime/kit/contract/groups.js +402 -0
  95. package/dist/runtime/kit/contract/index.d.ts +33 -0
  96. package/dist/runtime/kit/contract/index.js +33 -0
  97. package/dist/runtime/kit/contract/transform.d.ts +82 -0
  98. package/dist/runtime/kit/contract/transform.js +133 -0
  99. package/dist/runtime/kit/contract/validate.d.ts +67 -0
  100. package/dist/runtime/kit/contract/validate.js +388 -0
  101. package/dist/runtime/kit/data.d.ts +636 -0
  102. package/dist/runtime/kit/data.js +1117 -0
  103. package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
  104. package/dist/runtime/kit/defineSection.js +10 -0
  105. package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
  106. package/dist/runtime/kit/dynamic-source.js +143 -0
  107. package/dist/runtime/kit/index.d.ts +24 -0
  108. package/dist/runtime/kit/index.js +24 -0
  109. package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
  110. package/dist/runtime/kit/jsx-to-json.js +28 -0
  111. package/dist/{types → runtime/kit}/mount.d.ts +25 -4
  112. package/dist/runtime/kit/mount.js +124 -0
  113. package/dist/{types → runtime/kit}/registry.d.ts +1 -1
  114. package/dist/runtime/kit/registry.js +12 -0
  115. package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
  116. package/dist/runtime/kit/ssg.js +56 -0
  117. package/dist/runtime/kit/storefront.d.ts +566 -0
  118. package/dist/runtime/kit/storefront.js +723 -0
  119. package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
  120. package/dist/runtime/kit/theme-context.js +25 -0
  121. package/dist/runtime/kit/types.d.ts +142 -0
  122. package/dist/runtime/kit/types.js +1 -0
  123. package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
  124. package/dist/runtime/platform/TrackingPixels.js +63 -0
  125. package/dist/runtime/platform/environment.d.ts +68 -0
  126. package/dist/runtime/platform/environment.js +85 -0
  127. package/dist/runtime/platform/locale.d.ts +27 -0
  128. package/dist/runtime/platform/locale.js +42 -0
  129. package/dist/runtime/platform/route-analytics.d.ts +15 -0
  130. package/dist/runtime/platform/route-analytics.js +72 -0
  131. package/dist/runtime/platform/safe-href.d.ts +25 -0
  132. package/dist/runtime/platform/safe-href.js +69 -0
  133. package/dist/runtime/platform/safe-values.d.ts +37 -0
  134. package/dist/runtime/platform/safe-values.js +140 -0
  135. package/dist/runtime/render/SectionRenderer.d.ts +4 -0
  136. package/dist/runtime/render/SectionRenderer.js +111 -0
  137. package/dist/runtime/render/listing-query.d.ts +47 -0
  138. package/dist/runtime/render/listing-query.js +114 -0
  139. package/dist/runtime/render/localized-copy.d.ts +86 -0
  140. package/dist/runtime/render/localized-copy.js +129 -0
  141. package/dist/runtime/render/product-listing.d.ts +11 -0
  142. package/dist/runtime/render/product-listing.js +240 -0
  143. package/dist/runtime/render/section-actions.d.ts +48 -0
  144. package/dist/runtime/render/section-actions.js +170 -0
  145. package/dist/runtime/render/starter.d.ts +11 -0
  146. package/dist/runtime/render/starter.js +81 -0
  147. package/dist/runtime/seo/head.d.ts +87 -0
  148. package/dist/runtime/seo/head.js +224 -0
  149. package/dist/runtime/seo/structured-data.d.ts +22 -0
  150. package/dist/runtime/seo/structured-data.js +115 -0
  151. package/dist/runtime/shell/PageShell.d.ts +18 -0
  152. package/dist/runtime/shell/PageShell.js +113 -0
  153. package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
  154. package/dist/runtime/shell/RuntimeBridge.js +85 -0
  155. package/dist/runtime/shell/overlay-state.d.ts +31 -0
  156. package/dist/runtime/shell/overlay-state.js +59 -0
  157. package/dist/runtime/shell/preview-mode.d.ts +2 -0
  158. package/dist/runtime/shell/preview-mode.js +11 -0
  159. package/dist/runtime/shell/spa-router.d.ts +14 -0
  160. package/dist/runtime/shell/spa-router.js +76 -0
  161. package/dist/runtime/shell/url-redirects.d.ts +6 -0
  162. package/dist/runtime/shell/url-redirects.js +37 -0
  163. package/dist/runtime/slots.d.ts +74 -0
  164. package/dist/runtime/slots.js +16 -0
  165. package/dist/runtime.d.ts +34 -0
  166. package/dist/runtime.js +40 -0
  167. package/dist/sdk/index.d.ts +24 -0
  168. package/dist/sdk/index.js +24 -0
  169. package/dist/sdk/referrals.d.ts +62 -0
  170. package/dist/sdk/referrals.js +20 -0
  171. package/dist/sdk/rewards.d.ts +85 -0
  172. package/dist/sdk/rewards.js +29 -0
  173. package/dist/sdk/runtime-context.d.ts +135 -0
  174. package/dist/sdk/runtime-context.js +73 -0
  175. package/dist/sdk/schema.d.ts +197 -0
  176. package/dist/sdk/schema.js +121 -0
  177. package/dist/sdk/server-render.d.ts +8 -0
  178. package/dist/sdk/server-render.js +12 -0
  179. package/dist/sdk/store-lookup.d.ts +27 -0
  180. package/dist/sdk/store-lookup.js +29 -0
  181. package/dist/sdk/store.d.ts +115 -0
  182. package/dist/sdk/store.js +1 -0
  183. package/dist/sdk/sx.d.ts +40 -0
  184. package/dist/sdk/sx.js +112 -0
  185. package/dist/sdk/theme.d.ts +152 -0
  186. package/dist/sdk/theme.js +10 -0
  187. package/dist/sdk/types.d.ts +470 -0
  188. package/dist/sdk/types.js +7 -0
  189. package/dist/server.d.ts +65 -0
  190. package/dist/server.js +370 -0
  191. package/docs/headless-api.md +220 -0
  192. package/package.json +77 -23
  193. package/dist/index.js.map +0 -1
  194. package/dist/ssg-BZ5FYTw7.js +0 -1802
  195. package/dist/ssg-BZ5FYTw7.js.map +0 -1
  196. package/dist/ssg.js +0 -8
  197. package/dist/ssg.js.map +0 -1
  198. package/dist/types/cart.d.ts +0 -86
  199. package/dist/types/data.d.ts +0 -357
  200. package/dist/types/editor.d.ts +0 -5
  201. package/dist/types/index.d.ts +0 -20
  202. package/dist/types/preview-bridge.d.ts +0 -9
  203. package/dist/types/storefront.d.ts +0 -333
  204. package/dist/types/types.d.ts +0 -44
@@ -0,0 +1,198 @@
1
+ import * as convert from './convert.js';
2
+ function decode(handle) {
3
+ if (!handle.includes('%'))
4
+ return handle;
5
+ try {
6
+ return decodeURIComponent(handle);
7
+ }
8
+ catch {
9
+ return handle;
10
+ }
11
+ }
12
+ function predictive(r) {
13
+ return {
14
+ queries: [...r.queries],
15
+ products: r.products.map((p) => ({
16
+ handle: p.handle,
17
+ title: p.title,
18
+ price: p.priceRange.minVariantPrice,
19
+ featuredImage: convert.image(p.featuredImage),
20
+ })),
21
+ collections: r.collections.map((c) => ({ handle: c.handle, title: c.title, image: null })),
22
+ pages: [],
23
+ articles: [],
24
+ };
25
+ }
26
+ /** Nova's search sort ids (the `sort` URL param) → the ABI `catalog.search` order. Relevance (absent) is the default. */
27
+ const SEARCH_ORDER = {
28
+ 'price-asc': { sortKey: 'PRICE', reverse: false },
29
+ 'price-desc': { sortKey: 'PRICE', reverse: true },
30
+ 'title-asc': { sortKey: 'TITLE', reverse: false },
31
+ 'created-desc': { sortKey: 'CREATED_AT', reverse: true },
32
+ };
33
+ export const SEARCH_SORTS = ['relevance', ...Object.keys(SEARCH_ORDER)];
34
+ export function searchOrder(sort) {
35
+ return sort && Object.prototype.hasOwnProperty.call(SEARCH_ORDER, sort) ? SEARCH_ORDER[sort] : null;
36
+ }
37
+ /** The resource-cache key of a search (platform/runtime/data/StoreProvider.tsx keys its loads the same way). */
38
+ export const searchKey = (query, sort) => (searchOrder(sort) ? `search:${query}|${sort}` : `search:${query}`);
39
+ /** rev 2.34 `Shop.locations` (converted by `convert.shop`) → `DataApi.locations`; a store without them answers `[]`. */
40
+ export function storeLocations(shop) {
41
+ return (shop?.locations ?? []).map((l) => ({
42
+ id: l.id,
43
+ name: l.name,
44
+ code: null,
45
+ address: {
46
+ country: l.address.country ?? l.address.countryCode ?? null,
47
+ address: [l.address.address1, l.address.address2].filter(Boolean).join(', ') || null,
48
+ city: l.address.city ?? null,
49
+ province: l.address.province ?? null,
50
+ postalCode: l.address.zip ?? null,
51
+ phone: l.phone ?? null,
52
+ },
53
+ }));
54
+ }
55
+ export function createDataApi(pd, remote) {
56
+ const productBy = (h) => pd.products[h] ?? pd.products[decode(h)] ?? null;
57
+ const collectionBy = (h) => pd.collections[h] ?? pd.collections[decode(h)] ?? null;
58
+ const menuBy = (h) => pd.menus[h] ?? pd.menus[decode(h)] ?? null;
59
+ const allCollections = () => pd.collectionList ?? Object.values(pd.collections);
60
+ // The page's resource cache (platform/runtime/data/StoreProvider.tsx) starts from what the server
61
+ // loaded, keyed the way that cache keys its loads — so the first render (server and hydrating
62
+ // browser alike) shows it, and the browser does not ask the platform again for the same records.
63
+ const preloaded = {};
64
+ for (const [h, p] of Object.entries(pd.products))
65
+ if (p.variants?.length)
66
+ preloaded[`product:${h}`] = p;
67
+ for (const [h, c] of Object.entries(pd.collections))
68
+ preloaded[`collection-products:${h}`] = { products: c.products, filters: c.filters ?? [] };
69
+ for (const [h, b] of Object.entries(pd.blogs))
70
+ preloaded[`blog:${h}`] = b;
71
+ for (const [k, a] of Object.entries(pd.articles))
72
+ preloaded[`article:${k}`] = a;
73
+ if (pd.search)
74
+ preloaded[searchKey(pd.search.query, pd.search.sort)] = pd.search.result;
75
+ // rev 2.34: the shop's pickup locations, in DataApi.locations' shape; the store-locator reads them on first render
76
+ const locations = storeLocations(pd.shop);
77
+ preloaded.locations = locations;
78
+ for (const [h, list] of Object.entries(pd.recommendations)) {
79
+ const id = pd.products[h]?.id;
80
+ if (id)
81
+ preloaded[`recommendations:${id}`] = list;
82
+ }
83
+ const api = {
84
+ preloaded,
85
+ collectionByHandle: collectionBy,
86
+ allCollections,
87
+ productByHandle: productBy,
88
+ pageByHandle: (h) => pd.pages[h] ?? pd.pages[decode(h)] ?? null,
89
+ localization: pd.localization,
90
+ shop: pd.shop,
91
+ menu: menuBy,
92
+ locations: async () => locations,
93
+ ...(pd.policyHandle ? { policyHandle: pd.policyHandle } : {}),
94
+ getSnapshot: () => pd,
95
+ blogSync: (h) => pd.blogs[h] ?? pd.blogs[decode(h)] ?? null,
96
+ articleSync: (b, h) => pd.articles[`${b}/${h}`] ?? pd.articles[`${decode(b)}/${decode(h)}`] ?? null,
97
+ searchSync: (q, sort) => (pd.search && pd.search.query.trim().toLowerCase() === q.trim().toLowerCase() && (pd.search.sort ?? '') === (searchOrder(sort) ? (sort ?? '') : '') ? pd.search.result : null),
98
+ recommendationsSync: (h) => pd.recommendations[h] ?? null,
99
+ routeMissing: pd.routeMissing === true,
100
+ ...(pd.signedIn !== null ? { signedIn: pd.signedIn } : {}),
101
+ ...(pd.accountUrls ? { accountUrl: (path) => pd.accountUrls[path ?? 'profile'] } : {}),
102
+ ...(pd.formEndpoint ? { formEndpoint: pd.formEndpoint } : {}),
103
+ ...(pd.formResult ? { formResult: pd.formResult } : {}),
104
+ fetchMenu: async (handle) => {
105
+ const known = menuBy(handle);
106
+ if (known || !remote)
107
+ return known;
108
+ const list = (await remote.data('shop.menus', [[handle]]));
109
+ const m = list.find((x) => x.handle === handle);
110
+ return m ? convert.menu(m) : null;
111
+ },
112
+ // The cookie banner links the privacy policy only when the store has published it; a page other
113
+ // than that policy's own does not load it, so the browser asks once, when the banner shows.
114
+ fetchPolicy: async (handle) => {
115
+ const known = Object.values(pd.shop.policies ?? {}).find((p) => p?.handle === handle) ?? null;
116
+ if (known || !remote)
117
+ return known;
118
+ const p = (await remote.data('content.policy', [handle]));
119
+ return p ? convert.policy(p) : null;
120
+ },
121
+ fetchProduct: async (handle) => {
122
+ const known = productBy(handle);
123
+ // A card copy carries no variants; the product page needs them.
124
+ if ((known && known.variants?.length) || !remote)
125
+ return known;
126
+ // `collections` (ABI opt-in): the product page's breadcrumb names the product's collection.
127
+ const p = (await remote.data('catalog.product', [handle, { collections: true }]));
128
+ return p ? convert.product(p) : known;
129
+ },
130
+ listingSeed: (handle, filters) => {
131
+ const seed = pd.listingSeed;
132
+ return seed && (seed.handle === handle || seed.handle === decode(handle)) && seed.key === JSON.stringify(filters) ? { products: seed.products, pageInfo: seed.pageInfo } : null;
133
+ },
134
+ collectionProducts: async (handle, opts) => {
135
+ const known = collectionBy(handle);
136
+ if (!remote) {
137
+ return known
138
+ ? { products: known.products, filters: [], pageInfo: { hasNextPage: false, hasPreviousPage: false, startCursor: null, endCursor: null } }
139
+ : { products: [], filters: [], pageInfo: { hasNextPage: false, hasPreviousPage: false, startCursor: null, endCursor: null }, missing: true };
140
+ }
141
+ const sortKey = opts?.sortKey === 'COLLECTION_DEFAULT' ? undefined : opts?.sortKey;
142
+ const filters = convert.abiFilters(opts?.filters);
143
+ // The first unfiltered page carries the facets (ABI opt-in); later pages and filtered pages reuse them.
144
+ const facets = !opts?.after && filters.length === 0;
145
+ const c = (await remote.data('catalog.collection', [handle, { productsFirst: opts?.first ?? 24, ...(opts?.after ? { after: opts.after } : {}), ...(sortKey ? { sortKey } : {}), ...(opts?.reverse ? { reverse: true } : {}), ...(filters.length ? { filters } : {}), ...(facets ? { facets: true } : {}) }]));
146
+ if (!c)
147
+ return { products: [], filters: [], pageInfo: { hasNextPage: false, hasPreviousPage: false, startCursor: null, endCursor: null }, missing: true };
148
+ return {
149
+ products: c.products.nodes.map(convert.product),
150
+ filters: convert.filters(c.filters),
151
+ pageInfo: { hasNextPage: c.products.pageInfo.hasNextPage, hasPreviousPage: Boolean(opts?.after), startCursor: opts?.after ?? null, endCursor: c.products.pageInfo.endCursor ?? null },
152
+ };
153
+ },
154
+ blogByHandle: async (handle) => {
155
+ const known = pd.blogs[handle] ?? pd.blogs[decode(handle)] ?? null;
156
+ if (known || !remote)
157
+ return known;
158
+ const b = (await remote.data('content.blog', [handle]));
159
+ return b ? convert.blog(b) : null;
160
+ },
161
+ articleByHandle: async (blogHandle, handle) => {
162
+ const known = pd.articles[`${blogHandle}/${handle}`] ?? pd.articles[`${decode(blogHandle)}/${decode(handle)}`] ?? null;
163
+ if (known || !remote)
164
+ return known;
165
+ const a = (await remote.data('content.article', [blogHandle, handle]));
166
+ return a ? { ...convert.article(a), blogHandle } : null;
167
+ },
168
+ ...(remote
169
+ ? {
170
+ consent: {
171
+ get: async () => (await remote.data('consent.get', [])),
172
+ set: async (input) => {
173
+ const env = await remote.action('consent.set', [input]);
174
+ if (!env.ok)
175
+ throw new Error(env.error.code);
176
+ },
177
+ },
178
+ search: async (query, opts) => {
179
+ const types = (opts?.types ?? ['PRODUCT', 'PAGE', 'ARTICLE']).map((t) => t.toLowerCase());
180
+ const order = searchOrder(opts?.sort);
181
+ const r = (await remote.data('catalog.search', [{ query, ...(opts?.first ? { first: opts.first } : {}), ...(opts?.after ? { after: opts.after } : {}), types, ...(order ? { sortKey: order.sortKey, ...(order.reverse ? { reverse: true } : {}) } : {}) }]));
182
+ return convert.searchResults(r);
183
+ },
184
+ predictiveSearch: async (query, opts) => predictive((await remote.data('catalog.predictive', [query, opts?.limit ?? 4]))),
185
+ productRecommendations: async (productId) => {
186
+ const handle = Object.values(pd.products).find((p) => p.id === productId)?.handle;
187
+ if (!handle)
188
+ return [];
189
+ const list = (await remote.data('catalog.recommendations', [handle, 'related', 4]));
190
+ return list.map(convert.product);
191
+ },
192
+ }
193
+ : pd.search
194
+ ? { search: async () => pd.search.result }
195
+ : {}),
196
+ };
197
+ return api;
198
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * RFC 8785 JSON Canonicalization for JSON values (null, boolean, string, finite number, array,
3
+ * plain object). The browser wire encodes `/_tq/data` arguments as JCS so one call has one cache
4
+ * key (theme-abi-v2.md §4). Same algorithm as scripts/abi/jcs.mjs, which writes manifest.json.
5
+ */
6
+ export declare function canonicalize(value: unknown): string;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * RFC 8785 JSON Canonicalization for JSON values (null, boolean, string, finite number, array,
3
+ * plain object). The browser wire encodes `/_tq/data` arguments as JCS so one call has one cache
4
+ * key (theme-abi-v2.md §4). Same algorithm as scripts/abi/jcs.mjs, which writes manifest.json.
5
+ */
6
+ export function canonicalize(value) {
7
+ if (value === null)
8
+ return 'null';
9
+ switch (typeof value) {
10
+ case 'boolean':
11
+ return value ? 'true' : 'false';
12
+ case 'number':
13
+ if (!Number.isFinite(value))
14
+ throw new TypeError('JCS: non-finite number');
15
+ return JSON.stringify(value);
16
+ case 'string':
17
+ return JSON.stringify(value);
18
+ case 'object': {
19
+ if (Array.isArray(value))
20
+ return `[${value.map(canonicalize).join(',')}]`;
21
+ const o = value;
22
+ const keys = Object.keys(o).filter((k) => o[k] !== undefined).sort();
23
+ return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalize(o[k])}`).join(',')}}`;
24
+ }
25
+ default:
26
+ throw new TypeError(`JCS: unsupported ${typeof value}`);
27
+ }
28
+ }
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Everything one page render reads, as ONE serialisable record.
3
+ *
4
+ * The server fills it from `ctx.data` (theme-abi-v2.md §3) and embeds it in the page; the browser
5
+ * rebuilds the identical `DataApi` from the embedded copy, so the hydrated tree starts from the
6
+ * bytes the server rendered. Nothing here is cached across renders: a `PageData` is created inside
7
+ * `render()` and dropped with it (§1 multi-tenant isolates, suite 9).
8
+ *
9
+ * Data plan (§3 counting, §8 limits — 4 calls / 2 rounds hard, 1 / 1 budget):
10
+ * round 1 snapshot({ resolve: 'all' }) — shop, menus, the route's resource, and every
11
+ * `resolve: true` setting the platform resolves into `refs`
12
+ * round 2 only what round 1 did not answer, all started together: one `shop.menus([...])`,
13
+ * `catalog.collections()` when a section lists every collection, then one
14
+ * `catalog.collection` / `catalog.product` per unresolved ref — never past the hard
15
+ * call limit. A ref the budget cannot cover renders as "not found", the section's own
16
+ * empty state; it is logged, never retried.
17
+ */
18
+ import type { RenderContext, Cart as AbiCart, Policy as AbiPolicy } from '@tanqory/theme-abi';
19
+ import type { ContentNode, SectionDef } from '../runtime/kit/types.js';
20
+ import type { Collection, Localization, Page, Product } from '../runtime/kit/data.js';
21
+ import type { Article, Blog, Menu, SearchResults, Shop } from '../runtime/kit/storefront.js';
22
+ /**
23
+ * The listing state of this request (`?sort=` and `?filter.*=`, platform/runtime/render/listing-query.ts):
24
+ * only on the collection route. `sort` may also arrive as the ABI's query-derived route param.
25
+ * Cache-safe for the same reason as the search query: a request with a query string is neither served
26
+ * from nor stored in the shared cache (§5.1).
27
+ */
28
+ export declare function listingQueryOfRequest(ctx: Pick<RenderContext, 'route' | 'request'>): string;
29
+ /**
30
+ * The search query of this request: the `q` route param (theme-abi-v2.md §2.1), else — for a
31
+ * gateway that does not forward it yet — the `q` of the request's own query string. Only on the
32
+ * search route. Shopper-typed text: every place that prints it escapes it (React does).
33
+ * Reading the raw query is cache-safe: a request with any query string is neither served from nor
34
+ * stored in the shared cache (§5.1).
35
+ */
36
+ export declare function searchQueryOf(ctx: Pick<RenderContext, 'route' | 'request'>): string | null;
37
+ /**
38
+ * The search page's sort (`?sort=`, the ABI's query-derived `sort` route param on the search kind):
39
+ * a sort id lib/abi/data-api.ts `searchOrder` knows, else null (relevance). Only on the search route.
40
+ */
41
+ export declare function searchSortOf(ctx: Pick<RenderContext, 'route' | 'request'>): string | null;
42
+ /**
43
+ * The variant a product URL names (`/products/<handle>?variant=<id>`): a shared link, or checkout's way
44
+ * back to the product. Only on the product route; the section opens on it only when it is a variant
45
+ * of that product (ProductDetails ignores any other id). Read from the request's own query string:
46
+ * a request with a query is never served from or stored in the shared cache (§5.1).
47
+ */
48
+ export declare function variantOf(ctx: Pick<RenderContext, 'route' | 'request'>): string | null;
49
+ /**
50
+ * Why a cart permalink (`/cart/<variant>:<qty>`) landed on the cart page: the router's
51
+ * `?cart_link=` reason (apps/storefront/router/cart-permalink.mjs), else null. Only on the cart
52
+ * route, and only a reason the router sends. The cart route is a private response already (§5).
53
+ */
54
+ export declare function cartLinkOf(ctx: Pick<RenderContext, 'route' | 'request'>): string | null;
55
+ /**
56
+ * The route params the page renders with: the route's own, plus `q` (`searchQueryOf`), its `sort`
57
+ * (`searchSortOf`) and `cart_link` (`cartLinkOf`) when only the request's query string has them.
58
+ * They travel in the page state, so the hydrating browser builds the same location.
59
+ */
60
+ export declare function pageParamsOf(ctx: Pick<RenderContext, 'route' | 'request'>, searchQuery: string | null, searchSort?: string | null): Readonly<Record<string, string>>;
61
+ export interface PageData {
62
+ /** `ctx.route.kind` this record was loaded for. */
63
+ route: string;
64
+ shop: Shop;
65
+ menus: Record<string, Menu>;
66
+ products: Record<string, Product>;
67
+ collections: Record<string, Collection>;
68
+ /** Every collection, when a section on this page lists them; null when not loaded. */
69
+ collectionList: Collection[] | null;
70
+ pages: Record<string, Page>;
71
+ blogs: Record<string, Blog>;
72
+ /** key `${blogHandle}/${articleHandle}` */
73
+ articles: Record<string, Article>;
74
+ /** `sort`: the non-relevance sort id the result was loaded with (absent = relevance). */
75
+ search: {
76
+ query: string;
77
+ sort?: string;
78
+ result: SearchResults;
79
+ } | null;
80
+ /**
81
+ * The first filtered page of the route's collection, when the request's URL names a filter the
82
+ * server could answer in one request (a shared filtered link). `key` is that request's filter objects
83
+ * as JSON — what the listing asks `listingSeed` for. Null otherwise.
84
+ */
85
+ listingSeed: {
86
+ handle: string;
87
+ key: string;
88
+ products: Product[];
89
+ pageInfo: {
90
+ hasNextPage: boolean;
91
+ endCursor: string | null;
92
+ };
93
+ } | null;
94
+ /** product handle → recommended products, when the page shows recommendations. */
95
+ recommendations: Record<string, Product[]>;
96
+ policyHandle: string | null;
97
+ /** Account BFF URLs for the account slot (§4), only on the customer route. No customer data. */
98
+ accountUrls: {
99
+ orders: string;
100
+ addresses: string;
101
+ profile: string;
102
+ } | null;
103
+ /** The shopper's cart — only on the cart route (forces a private response). */
104
+ cart: AbiCart | null;
105
+ localization: Localization;
106
+ /** `request.isSignedIn` when the header shows the account menu (varied on), else null. */
107
+ signedIn: boolean | null;
108
+ /** Resource handles the plan wanted but could not load inside the limits. */
109
+ unresolved: string[];
110
+ /**
111
+ * The route addresses a resource by handle and the platform has no such resource (product,
112
+ * collection, page, blog), or no published policy for a policy route. The page answers 404 and the section renders its not-found state
113
+ * from this same flag, so the server and the hydrating browser agree.
114
+ */
115
+ routeMissing: boolean;
116
+ /**
117
+ * The platform's form endpoint on this host (theme-abi §1 native forms): the route's own path, posted
118
+ * `<form data-tq-form="<id>" method="post">`. Set by `render()`; absent on a page loaded without one.
119
+ */
120
+ formEndpoint?: string | null;
121
+ /** The outcome of the native form POST this render answers (`render()`), else null. */
122
+ formResult?: NativeFormResult | null;
123
+ }
124
+ /** What a declared form's POST came to: `ctx.actions.form.submit`'s answer, or why it never ran. */
125
+ export type NativeFormStatus = 'ok' | 'invalid' | 'rate_limited' | 'unavailable' | 'expired';
126
+ export interface NativeFormResult {
127
+ formId: string;
128
+ status: NativeFormStatus;
129
+ /** The address the shopper typed, kept on a failure so they need not retype it. */
130
+ email?: string;
131
+ }
132
+ type Need = {
133
+ kind: 'collection' | 'product' | 'menu' | 'page' | 'blog';
134
+ handle: string;
135
+ ref: string;
136
+ };
137
+ /**
138
+ * Data-plan requests that do not depend on section types (`loadPageData` `plan`). Each flag only adds a read the
139
+ * section conventions would also make (`pageNeeds`): the market list, the sign-in state, the route product's
140
+ * recommendations, the store's collection list.
141
+ */
142
+ export interface PagePlanOptions {
143
+ /** Read the shopper's country and list the store's countries (the page then varies on `x-tq-country`). */
144
+ localization?: boolean;
145
+ /** Read `request.isSignedIn` (the page then varies on it). */
146
+ account?: boolean;
147
+ /** On a product page, load the product's recommendations in the server render. */
148
+ recommendations?: boolean;
149
+ /** Load the store's collection list. */
150
+ collections?: boolean;
151
+ /** Data calls the caller makes beside the plan in the same render; the plan stays inside the limit with them. */
152
+ reservedCalls?: number;
153
+ }
154
+ /** Resource settings on this page (effective value = node setting, else the attribute default). */
155
+ export declare function pageNeeds(nodes: readonly ContentNode[], defs: (type: string) => SectionDef | undefined): {
156
+ needs: Need[];
157
+ listsCollections: boolean;
158
+ showsLocale: boolean;
159
+ showsAccount: boolean;
160
+ showsRecommendations: boolean;
161
+ headerMenus: string[];
162
+ };
163
+ /**
164
+ * `ctx.locale` → Nova's Localization. `locale.country` is a personalising input (§2): it is read
165
+ * only when the page shows the country switcher, and the caller hints `vary: ['x-tq-country']`
166
+ * for exactly that case. Otherwise the country stays blank and nothing on the page depends on it.
167
+ */
168
+ export declare function localizationOf(ctx: RenderContext, shipsTo: readonly string[], readCountry: boolean): Localization;
169
+ /** One active market as the platform names it (`ctx.locale.markets`, an optional ABI field). */
170
+ export interface LocaleMarket {
171
+ id: string;
172
+ currency: string;
173
+ countries: readonly string[];
174
+ languages: readonly string[];
175
+ }
176
+ /** `ctx.locale.markets`, read defensively: only well-formed entries; [] when the platform sends none. */
177
+ export declare function marketsOf(ctx: Pick<RenderContext, 'locale'>): LocaleMarket[];
178
+ /** Load a page's data through `ctx.data`. */
179
+ export declare function loadPageData(ctx: RenderContext, nodes: readonly ContentNode[], defs: (type: string) => SectionDef | undefined,
180
+ /** Menu handles named by GLOBAL settings (header/footer menu pickers), not by a node. */
181
+ settingMenus?: readonly string[],
182
+ /**
183
+ * The search query this page renders (`searchQueryOf`). When the snapshot answered a different
184
+ * query — a gateway that does not forward `q` as a route param answers the empty one — the
185
+ * search is run once in round 2, so the server HTML holds the results the browser would show.
186
+ */
187
+ searchQuery?: string | null,
188
+ /** The search sort of this request (`searchSortOf`); null = relevance. */
189
+ searchSort?: string | null,
190
+ /** The collection listing's URL state (`listingQueryOfRequest`); '' = none. */
191
+ listingQuery?: string,
192
+ /**
193
+ * What the theme asks the plan for regardless of its section types (`RenderOptions.storefront.plan`), and the
194
+ * data calls the caller makes beside this plan in round 1 (`reservedCalls`). Absent: the plan reads only the
195
+ * section conventions (`pageNeeds`), exactly as before.
196
+ */
197
+ plan?: PagePlanOptions): Promise<PageData>;
198
+ /** An unknown (or not yet published) resource on a resource route is a real 404. Exported for tests. */
199
+ export declare function routeMissingOf(route: RenderContext['route'], found: {
200
+ products: Record<string, unknown>;
201
+ collections: Record<string, unknown>;
202
+ pages: Record<string, unknown>;
203
+ blogs: Record<string, unknown>;
204
+ articles: Record<string, {
205
+ handle?: string;
206
+ }>;
207
+ policies: readonly AbiPolicy[];
208
+ }): boolean;
209
+ export {};