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

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 (210) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +125 -10
  3. package/dist/abi/canvas-live.d.ts +67 -0
  4. package/dist/abi/canvas-live.js +154 -0
  5. package/dist/abi/cart.d.ts +59 -0
  6. package/dist/abi/cart.js +387 -0
  7. package/dist/abi/convert.d.ts +344 -0
  8. package/dist/abi/convert.js +1278 -0
  9. package/dist/abi/data-api.d.ts +15 -0
  10. package/dist/abi/data-api.js +209 -0
  11. package/dist/abi/jcs.d.ts +6 -0
  12. package/dist/abi/jcs.js +28 -0
  13. package/dist/abi/labels.d.ts +3 -0
  14. package/dist/abi/labels.js +34 -0
  15. package/dist/abi/page-data.d.ts +256 -0
  16. package/dist/abi/page-data.js +678 -0
  17. package/dist/abi/page.d.ts +28 -0
  18. package/dist/abi/page.js +57 -0
  19. package/dist/abi/referrals.d.ts +23 -0
  20. package/dist/abi/referrals.js +89 -0
  21. package/dist/abi/rewards.d.ts +27 -0
  22. package/dist/abi/rewards.js +82 -0
  23. package/dist/abi/seo.d.ts +47 -0
  24. package/dist/abi/seo.js +68 -0
  25. package/dist/abi/shell.d.ts +23 -0
  26. package/dist/abi/shell.js +10 -0
  27. package/dist/abi/state-pack.d.ts +17 -0
  28. package/dist/abi/state-pack.js +43 -0
  29. package/dist/abi/wire.d.ts +38 -0
  30. package/dist/abi/wire.js +84 -0
  31. package/dist/build.d.ts +116 -0
  32. package/dist/build.js +310 -0
  33. package/dist/client.d.ts +19 -0
  34. package/dist/client.js +87 -0
  35. package/dist/contract.d.ts +6 -0
  36. package/dist/contract.js +6 -0
  37. package/dist/headless/ai.d.ts +132 -0
  38. package/dist/headless/ai.js +99 -0
  39. package/dist/headless/hooks.d.ts +276 -0
  40. package/dist/headless/hooks.js +540 -0
  41. package/dist/headless/i18n.d.ts +13 -0
  42. package/dist/headless/i18n.js +45 -0
  43. package/dist/headless/index.d.ts +37 -0
  44. package/dist/headless/index.js +38 -0
  45. package/dist/headless/money.d.ts +25 -0
  46. package/dist/headless/money.js +61 -0
  47. package/dist/headless/page.d.ts +43 -0
  48. package/dist/headless/page.js +53 -0
  49. package/dist/headless/platform.d.ts +55 -0
  50. package/dist/headless/platform.js +77 -0
  51. package/dist/headless/primitives/cart.d.ts +90 -0
  52. package/dist/headless/primitives/cart.js +153 -0
  53. package/dist/headless/primitives/collection.d.ts +68 -0
  54. package/dist/headless/primitives/collection.js +80 -0
  55. package/dist/headless/primitives/product.d.ts +101 -0
  56. package/dist/headless/primitives/product.js +117 -0
  57. package/dist/headless/primitives/storefront.d.ts +106 -0
  58. package/dist/headless/primitives/storefront.js +186 -0
  59. package/dist/headless/primitives/util.d.ts +19 -0
  60. package/dist/headless/primitives/util.js +42 -0
  61. package/dist/headless/strings.d.ts +9 -0
  62. package/dist/headless/strings.js +190 -0
  63. package/dist/headless/structured-data.d.ts +31 -0
  64. package/dist/headless/structured-data.js +57 -0
  65. package/dist/headless/theme.d.ts +99 -0
  66. package/dist/headless/theme.js +54 -0
  67. package/dist/index.d.ts +20 -0
  68. package/dist/index.js +18 -553
  69. package/dist/runtime/data/StoreProvider.d.ts +84 -0
  70. package/dist/runtime/data/StoreProvider.js +373 -0
  71. package/dist/runtime/data/handle.d.ts +14 -0
  72. package/dist/runtime/data/handle.js +16 -0
  73. package/dist/runtime/data/routes.d.ts +87 -0
  74. package/dist/runtime/data/routes.js +158 -0
  75. package/dist/runtime/data/to-ui.d.ts +281 -0
  76. package/dist/runtime/data/to-ui.js +977 -0
  77. package/dist/runtime/entries/register.d.ts +9 -0
  78. package/dist/runtime/entries/register.js +9 -0
  79. package/dist/runtime/entries/sections.d.ts +16 -0
  80. package/dist/runtime/entries/sections.js +19 -0
  81. package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
  82. package/dist/runtime/kit/SectionTree.js +100 -0
  83. package/dist/runtime/kit/analytics.d.ts +63 -0
  84. package/dist/runtime/kit/analytics.js +250 -0
  85. package/dist/runtime/kit/cart.d.ts +215 -0
  86. package/dist/runtime/kit/cart.js +545 -0
  87. package/dist/{types → runtime/kit}/composition.d.ts +1 -1
  88. package/dist/runtime/kit/composition.js +3 -0
  89. package/dist/{types → runtime/kit}/config.d.ts +2 -2
  90. package/dist/runtime/kit/config.js +6 -0
  91. package/dist/runtime/kit/consent.d.ts +29 -0
  92. package/dist/runtime/kit/consent.js +159 -0
  93. package/dist/runtime/kit/contract/content.d.ts +199 -0
  94. package/dist/runtime/kit/contract/content.js +106 -0
  95. package/dist/runtime/kit/contract/field-types.d.ts +337 -0
  96. package/dist/runtime/kit/contract/field-types.js +206 -0
  97. package/dist/runtime/kit/contract/groups.d.ts +230 -0
  98. package/dist/runtime/kit/contract/groups.js +402 -0
  99. package/dist/runtime/kit/contract/index.d.ts +33 -0
  100. package/dist/runtime/kit/contract/index.js +33 -0
  101. package/dist/runtime/kit/contract/transform.d.ts +82 -0
  102. package/dist/runtime/kit/contract/transform.js +133 -0
  103. package/dist/runtime/kit/contract/validate.d.ts +75 -0
  104. package/dist/runtime/kit/contract/validate.js +480 -0
  105. package/dist/runtime/kit/data.d.ts +720 -0
  106. package/dist/runtime/kit/data.js +1117 -0
  107. package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
  108. package/dist/runtime/kit/defineSection.js +10 -0
  109. package/dist/{types → runtime/kit}/dynamic-source.d.ts +40 -18
  110. package/dist/runtime/kit/dynamic-source.js +294 -0
  111. package/dist/runtime/kit/index.d.ts +24 -0
  112. package/dist/runtime/kit/index.js +24 -0
  113. package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
  114. package/dist/runtime/kit/jsx-to-json.js +28 -0
  115. package/dist/{types → runtime/kit}/mount.d.ts +25 -4
  116. package/dist/runtime/kit/mount.js +124 -0
  117. package/dist/{types → runtime/kit}/registry.d.ts +1 -1
  118. package/dist/runtime/kit/registry.js +12 -0
  119. package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
  120. package/dist/runtime/kit/ssg.js +56 -0
  121. package/dist/runtime/kit/storefront.d.ts +639 -0
  122. package/dist/runtime/kit/storefront.js +723 -0
  123. package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
  124. package/dist/runtime/kit/theme-context.js +25 -0
  125. package/dist/runtime/kit/types.d.ts +147 -0
  126. package/dist/runtime/kit/types.js +1 -0
  127. package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
  128. package/dist/runtime/platform/TrackingPixels.js +63 -0
  129. package/dist/runtime/platform/environment.d.ts +68 -0
  130. package/dist/runtime/platform/environment.js +85 -0
  131. package/dist/runtime/platform/locale.d.ts +27 -0
  132. package/dist/runtime/platform/locale.js +42 -0
  133. package/dist/runtime/platform/route-analytics.d.ts +15 -0
  134. package/dist/runtime/platform/route-analytics.js +72 -0
  135. package/dist/runtime/platform/safe-href.d.ts +25 -0
  136. package/dist/runtime/platform/safe-href.js +69 -0
  137. package/dist/runtime/platform/safe-values.d.ts +37 -0
  138. package/dist/runtime/platform/safe-values.js +140 -0
  139. package/dist/runtime/render/SectionRenderer.d.ts +4 -0
  140. package/dist/runtime/render/SectionRenderer.js +166 -0
  141. package/dist/runtime/render/listing-query.d.ts +52 -0
  142. package/dist/runtime/render/listing-query.js +122 -0
  143. package/dist/runtime/render/localized-copy.d.ts +86 -0
  144. package/dist/runtime/render/localized-copy.js +129 -0
  145. package/dist/runtime/render/product-listing.d.ts +11 -0
  146. package/dist/runtime/render/product-listing.js +243 -0
  147. package/dist/runtime/render/section-actions.d.ts +66 -0
  148. package/dist/runtime/render/section-actions.js +202 -0
  149. package/dist/runtime/render/starter.d.ts +11 -0
  150. package/dist/runtime/render/starter.js +81 -0
  151. package/dist/runtime/seo/head.d.ts +115 -0
  152. package/dist/runtime/seo/head.js +281 -0
  153. package/dist/runtime/seo/structured-data.d.ts +83 -0
  154. package/dist/runtime/seo/structured-data.js +252 -0
  155. package/dist/runtime/shell/PageShell.d.ts +18 -0
  156. package/dist/runtime/shell/PageShell.js +113 -0
  157. package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
  158. package/dist/runtime/shell/RuntimeBridge.js +93 -0
  159. package/dist/runtime/shell/overlay-state.d.ts +31 -0
  160. package/dist/runtime/shell/overlay-state.js +59 -0
  161. package/dist/runtime/shell/preview-mode.d.ts +2 -0
  162. package/dist/runtime/shell/preview-mode.js +11 -0
  163. package/dist/runtime/shell/spa-router.d.ts +14 -0
  164. package/dist/runtime/shell/spa-router.js +76 -0
  165. package/dist/runtime/shell/url-redirects.d.ts +6 -0
  166. package/dist/runtime/shell/url-redirects.js +37 -0
  167. package/dist/runtime/slots.d.ts +74 -0
  168. package/dist/runtime/slots.js +16 -0
  169. package/dist/runtime.d.ts +34 -0
  170. package/dist/runtime.js +40 -0
  171. package/dist/sdk/app-block.d.ts +29 -0
  172. package/dist/sdk/app-block.js +22 -0
  173. package/dist/sdk/index.d.ts +26 -0
  174. package/dist/sdk/index.js +26 -0
  175. package/dist/sdk/referrals.d.ts +62 -0
  176. package/dist/sdk/referrals.js +20 -0
  177. package/dist/sdk/rewards.d.ts +85 -0
  178. package/dist/sdk/rewards.js +29 -0
  179. package/dist/sdk/runtime-context.d.ts +202 -0
  180. package/dist/sdk/runtime-context.js +73 -0
  181. package/dist/sdk/schema.d.ts +206 -0
  182. package/dist/sdk/schema.js +121 -0
  183. package/dist/sdk/server-render.d.ts +8 -0
  184. package/dist/sdk/server-render.js +12 -0
  185. package/dist/sdk/store-lookup.d.ts +27 -0
  186. package/dist/sdk/store-lookup.js +29 -0
  187. package/dist/sdk/store.d.ts +149 -0
  188. package/dist/sdk/store.js +5 -0
  189. package/dist/sdk/sx.d.ts +40 -0
  190. package/dist/sdk/sx.js +112 -0
  191. package/dist/sdk/theme.d.ts +191 -0
  192. package/dist/sdk/theme.js +10 -0
  193. package/dist/sdk/types.d.ts +757 -0
  194. package/dist/sdk/types.js +7 -0
  195. package/dist/server.d.ts +111 -0
  196. package/dist/server.js +533 -0
  197. package/docs/headless-api.md +220 -0
  198. package/package.json +76 -22
  199. package/dist/index.js.map +0 -1
  200. package/dist/ssg-BZ5FYTw7.js +0 -1802
  201. package/dist/ssg-BZ5FYTw7.js.map +0 -1
  202. package/dist/ssg.js +0 -8
  203. package/dist/ssg.js.map +0 -1
  204. package/dist/types/cart.d.ts +0 -86
  205. package/dist/types/data.d.ts +0 -357
  206. package/dist/types/editor.d.ts +0 -5
  207. package/dist/types/index.d.ts +0 -20
  208. package/dist/types/preview-bridge.d.ts +0 -9
  209. package/dist/types/storefront.d.ts +0 -333
  210. package/dist/types/types.d.ts +0 -44
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `<AppBlock>`: how a theme renders a block of an installed app (theme-abi-v2.md §7.2 "App blocks (`@app`)").
3
+ *
4
+ * A section or block that lists `'@app'` in its accepted blocks receives the app's nodes among its saved blocks, with
5
+ * a type `app:<appHandle>/<blockHandle>` (`isAppBlock`). The theme places each where it wants and renders it with this
6
+ * component, which writes only the platform placeholder:
7
+ *
8
+ * <div data-tq-app-block="<type>" data-tq-node="<id>" data-tq-type="<type>" class="tq-app-block">
9
+ * <script type="application/json">{settings}</script>
10
+ * </div>
11
+ *
12
+ * The gateway adds the app's stylesheet and script (served from the shop's own origin, with integrity) and the
13
+ * platform mount script to the page; the app's script fills the placeholder in the browser. The settings are inert
14
+ * JSON: every `<`, `>` and `&` is written as a \u escape, so no value can end the element. No `style` attribute.
15
+ */
16
+ import type { SavedBlock } from './store.js';
17
+ /** Whether a saved block is a block of an installed app (render it with `<AppBlock>`). */
18
+ export declare function isAppBlock(block: {
19
+ type?: unknown;
20
+ } | null | undefined): boolean;
21
+ /** The settings as JSON that is safe inside `<script type="application/json">`. */
22
+ export declare function appBlockSettingsJson(settings: Readonly<Record<string, unknown>> | undefined): string;
23
+ export interface AppBlockProps {
24
+ block: SavedBlock;
25
+ /** Extra class names for the theme's own layout (spacing, grid placement). */
26
+ className?: string;
27
+ }
28
+ /** The placeholder of one app block; nothing for a node that is not an app block. */
29
+ export declare function AppBlock({ block, className }: AppBlockProps): JSX.Element | null;
@@ -0,0 +1,22 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { isAppBlockType } from '../runtime/kit/contract/content.js';
3
+ /** Whether a saved block is a block of an installed app (render it with `<AppBlock>`). */
4
+ export function isAppBlock(block) {
5
+ return !!block && isAppBlockType(block.type);
6
+ }
7
+ /** The settings as JSON that is safe inside `<script type="application/json">`. */
8
+ export function appBlockSettingsJson(settings) {
9
+ return JSON.stringify(settings ?? {})
10
+ .replace(/</g, '\\u003c')
11
+ .replace(/>/g, '\\u003e')
12
+ .replace(/&/g, '\\u0026')
13
+ .replace(/\u2028/g, '\\u2028')
14
+ .replace(/\u2029/g, '\\u2029');
15
+ }
16
+ /** The placeholder of one app block; nothing for a node that is not an app block. */
17
+ export function AppBlock({ block, className }) {
18
+ if (!isAppBlock(block))
19
+ return null;
20
+ const id = typeof block.id === 'string' && block.id ? block.id : undefined;
21
+ return (_jsx("div", { "data-tq-app-block": block.type, ...(id ? { 'data-tq-node': id } : {}), "data-tq-type": block.type, className: className ? `tq-app-block ${className}` : 'tq-app-block', suppressHydrationWarning: true, children: _jsx("script", { type: "application/json", dangerouslySetInnerHTML: { __html: appBlockSettingsJson(block.settings) } }) }));
22
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @tanqory/theme-sdk — the public API a Tanqory theme is written against.
3
+ *
4
+ * schema defineSchema(), field.*, resolveSettings() — a section's persisted settings
5
+ * theme defineTheme(), defineSection() — how a theme describes itself
6
+ * runtime useThemeContext(), useThemeActions(), useSearchSuggestions()
7
+ * types Product, Collection, Cart, Menu, … — the data a theme renders
8
+ * rewards useRewards(), earnPoints(): the store's loyalty program, browser-only
9
+ * referrals useReferrals(), shareTargets(): the shopper's referral link and the friend offer, browser-only
10
+ * store StoreContext + imageFor / menuFor / collectionFor / productFor … —
11
+ * read what the runtime put in the context
12
+ *
13
+ * Nothing here fetches or knows how the platform works.
14
+ */
15
+ export * from './schema.js';
16
+ export * from './types.js';
17
+ export * from './store.js';
18
+ export * from './store-lookup.js';
19
+ export { AppBlock, isAppBlock, appBlockSettingsJson, type AppBlockProps } from './app-block.js';
20
+ export { APP_BLOCKS } from '../runtime/kit/contract/content.js';
21
+ export * from './theme.js';
22
+ export * from './runtime-context.js';
23
+ export { ServerRenderProvider, useServerRender } from './server-render.js';
24
+ export * from './rewards.js';
25
+ export * from './referrals.js';
26
+ export { sx, cx as sxJoin, collectStyles, STYLE_RULES_ID } from './sx.js';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @tanqory/theme-sdk — the public API a Tanqory theme is written against.
3
+ *
4
+ * schema defineSchema(), field.*, resolveSettings() — a section's persisted settings
5
+ * theme defineTheme(), defineSection() — how a theme describes itself
6
+ * runtime useThemeContext(), useThemeActions(), useSearchSuggestions()
7
+ * types Product, Collection, Cart, Menu, … — the data a theme renders
8
+ * rewards useRewards(), earnPoints(): the store's loyalty program, browser-only
9
+ * referrals useReferrals(), shareTargets(): the shopper's referral link and the friend offer, browser-only
10
+ * store StoreContext + imageFor / menuFor / collectionFor / productFor … —
11
+ * read what the runtime put in the context
12
+ *
13
+ * Nothing here fetches or knows how the platform works.
14
+ */
15
+ export * from './schema.js';
16
+ export * from './types.js';
17
+ export * from './store.js';
18
+ export * from './store-lookup.js';
19
+ export { AppBlock, isAppBlock, appBlockSettingsJson } from './app-block.js';
20
+ export { APP_BLOCKS } from '../runtime/kit/contract/content.js';
21
+ export * from './theme.js';
22
+ export * from './runtime-context.js';
23
+ export { ServerRenderProvider, useServerRender } from './server-render.js';
24
+ export * from './rewards.js';
25
+ export * from './referrals.js';
26
+ export { sx, cx as sxJoin, collectStyles, STYLE_RULES_ID } from './sx.js';
@@ -0,0 +1,62 @@
1
+ /** PERCENTAGE: `value` is a percent; FIXED_AMOUNT: an amount in `currency`. */
2
+ export interface ReferralOffer {
3
+ type: 'PERCENTAGE' | 'FIXED_AMOUNT' | string;
4
+ value: string;
5
+ currency: string | null;
6
+ }
7
+ export interface ReferralReward {
8
+ type: 'PERCENTAGE_CODE' | 'FIXED_CODE' | 'STORE_CREDIT' | 'POINTS' | string;
9
+ value: string;
10
+ points: number;
11
+ currency: string | null;
12
+ }
13
+ /** The signed-in shopper's own link (they are the advocate). */
14
+ export interface ReferralState {
15
+ enabled: true;
16
+ code: string;
17
+ shareUrl: string;
18
+ offer: ReferralOffer;
19
+ reward: ReferralReward;
20
+ stats: {
21
+ clicks: number;
22
+ friendOrders: number;
23
+ pending: number;
24
+ rewarded: number;
25
+ };
26
+ }
27
+ /** What the `?ref=` link the shopper arrived on offers them (a new customer). */
28
+ export interface FriendOffer {
29
+ enabled: true;
30
+ code: string;
31
+ offer: ReferralOffer;
32
+ minimumOrder: string | null;
33
+ advocateFirstName: string | null;
34
+ /** The friend's own code once claimed by email (kept for the session). */
35
+ claimedCode: string | null;
36
+ }
37
+ export type ClaimResult = {
38
+ ok: true;
39
+ code: string;
40
+ } | {
41
+ ok: false;
42
+ error: 'email' | 'self' | 'failed';
43
+ };
44
+ /**
45
+ * `state` is null until known, for a guest and with referrals off; `friend` without a `?ref=` code this session,
46
+ * for a returning customer and with referrals off. `claim` (checkout then applies the friend's code) and `invite`
47
+ * (emails the link to a friend) never reject.
48
+ */
49
+ export interface ReferralsValue {
50
+ state: ReferralState | null;
51
+ friend: FriendOffer | null;
52
+ claim(email: string): Promise<ClaimResult>;
53
+ invite(email: string): Promise<boolean>;
54
+ }
55
+ /** Provided by the theme runtime. Themes never render it. */
56
+ export declare const ReferralsContext: import("react").Context<ReferralsValue>;
57
+ export declare const useReferrals: () => ReferralsValue;
58
+ /** One tap per app: each target carries the link (and the message where the app takes text). */
59
+ export declare function shareTargets(url: string, text: string): {
60
+ id: 'line' | 'whatsapp' | 'messenger' | 'x';
61
+ href: string;
62
+ }[];
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The store's referral program as the shopper sees it (army #7751): the advocate's link, share targets and stats
3
+ * for the Referral tab, and the friend offer a `?ref=CODE` visit shows. The runtime provides it in the browser
4
+ * (lib/abi/referrals.tsx); on the server, in the editor and with referrals off both states are `null`.
5
+ */
6
+ import { createContext, useContext } from 'react';
7
+ const OFF = { state: null, friend: null, claim: async () => ({ ok: false, error: 'failed' }), invite: async () => false };
8
+ /** Provided by the theme runtime. Themes never render it. */
9
+ export const ReferralsContext = createContext(OFF);
10
+ export const useReferrals = () => useContext(ReferralsContext);
11
+ /** One tap per app: each target carries the link (and the message where the app takes text). */
12
+ export function shareTargets(url, text) {
13
+ const u = encodeURIComponent(url);
14
+ return [
15
+ { id: 'line', href: `https://social-plugins.line.me/lineit/share?url=${u}&text=${encodeURIComponent(text)}` },
16
+ { id: 'whatsapp', href: `https://wa.me/?text=${encodeURIComponent(`${text} ${url}`)}` },
17
+ { id: 'messenger', href: `fb-messenger://share/?link=${u}` },
18
+ { id: 'x', href: `https://x.com/intent/post?text=${encodeURIComponent(text)}&url=${u}` },
19
+ ];
20
+ }
@@ -0,0 +1,85 @@
1
+ export interface RewardsWay {
2
+ id: string;
3
+ kind: string;
4
+ name: string;
5
+ /** Fixed points for the action; null for an order, which earns at `program.earnRates`. */
6
+ points: number | null;
7
+ completed: boolean;
8
+ }
9
+ export interface RewardsReward {
10
+ id: string;
11
+ name: string;
12
+ kind: string;
13
+ pointsCost: number;
14
+ /** The member can turn points into this code now. */
15
+ affordable: boolean;
16
+ pointsNeeded: number;
17
+ tierLocked: boolean;
18
+ limitReached: boolean;
19
+ }
20
+ export interface RewardsCode {
21
+ code: string;
22
+ rewardName: string;
23
+ kind: string;
24
+ expiresAt: string | null;
25
+ }
26
+ export interface RewardsTier {
27
+ basis: 'POINTS' | 'SPEND' | string;
28
+ currency: string | null;
29
+ current: {
30
+ name: string;
31
+ earnMultiplier: number;
32
+ } | null;
33
+ next: {
34
+ name: string;
35
+ threshold: number;
36
+ } | null;
37
+ progress: {
38
+ qualifying: number;
39
+ remaining: number | null;
40
+ } | null;
41
+ }
42
+ export interface RewardsState {
43
+ enabled: true;
44
+ signedIn: boolean;
45
+ /** Signed in and in the program (a signed-in shopper may still have to join). */
46
+ member?: boolean;
47
+ /** `earnRates`: points per one unit of a currency, by ISO code. */
48
+ program: {
49
+ name: string;
50
+ earnRates: Readonly<Record<string, number>>;
51
+ };
52
+ balance?: {
53
+ available: number;
54
+ pending: number;
55
+ lifetimeEarned: number;
56
+ };
57
+ tier?: RewardsTier | null;
58
+ rewards?: readonly RewardsReward[];
59
+ codes?: readonly RewardsCode[];
60
+ ways: readonly RewardsWay[];
61
+ }
62
+ export type RedeemResult = {
63
+ ok: true;
64
+ code: string;
65
+ } | {
66
+ ok: false;
67
+ error: 'insufficient' | 'failed';
68
+ };
69
+ export interface RewardsValue {
70
+ /** null until known, and for a store with loyalty off. */
71
+ state: RewardsState | null;
72
+ /** Turns points into a code; the state is refreshed on success. Never rejects. */
73
+ redeem(rewardId: string): Promise<RedeemResult>;
74
+ /** Joins the program (a signed-in shopper who is not a member yet). Never rejects. */
75
+ join(): Promise<boolean>;
76
+ }
77
+ /** Provided by the theme runtime. Themes never render it. */
78
+ export declare const RewardsContext: import("react").Context<RewardsValue>;
79
+ /** The loyalty program, the shopper's balance and the redeem / join actions. */
80
+ export declare function useRewards(): RewardsValue;
81
+ /**
82
+ * Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
83
+ * rounded down. null when the program does not earn in that currency (the hint is then not shown).
84
+ */
85
+ export declare function earnPoints(amount: number, currencyCode: string, state: RewardsState | null): number | null;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The store's loyalty program as the shopper sees it (army #7749): what the rewards launcher and
3
+ * the "Earn N points" hints render from, and the two things a shopper can ask of it.
4
+ *
5
+ * The theme runtime provides it after the page has loaded in the browser (lib/abi/rewards.tsx);
6
+ * on the server, in the editor and for a store with loyalty off it is `null`, so nothing renders.
7
+ */
8
+ import { createContext, useContext } from 'react';
9
+ const OFF = { state: null, redeem: async () => ({ ok: false, error: 'failed' }), join: async () => false };
10
+ /** Provided by the theme runtime. Themes never render it. */
11
+ export const RewardsContext = createContext(OFF);
12
+ /** The loyalty program, the shopper's balance and the redeem / join actions. */
13
+ export function useRewards() {
14
+ return useContext(RewardsContext);
15
+ }
16
+ /**
17
+ * Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
18
+ * rounded down. null when the program does not earn in that currency (the hint is then not shown).
19
+ */
20
+ export function earnPoints(amount, currencyCode, state) {
21
+ if (!state)
22
+ return null;
23
+ const rate = Number(state.program.earnRates[currencyCode]);
24
+ if (!(rate > 0) || !(amount > 0))
25
+ return null;
26
+ const multiplier = Number(state.tier?.current?.earnMultiplier) > 0 ? Number(state.tier.current.earnMultiplier) : 1;
27
+ // Rounded to 6 places first so 0.1 x 3 style float noise never drops a whole point.
28
+ return Math.floor(Math.round(amount * rate * multiplier * 1e6) / 1e6);
29
+ }
@@ -0,0 +1,202 @@
1
+ import type { Cart, Link, Shop, Product, GiftCardBalance, CartLineAttribute } from './types.js';
2
+ export type ThemeMode = 'storefront' | 'editor';
3
+ export type OverlayName = 'cart' | 'search' | 'account';
4
+ export interface ThemeToast {
5
+ id: string;
6
+ message: string;
7
+ tone: 'info' | 'error';
8
+ }
9
+ /**
10
+ * Why the store refused a line's last quantity change, when it said: `cart_limit_exceeded` is the
11
+ * merchant's "Maximum quantity per product"; `maxQuantity` is that limit when known.
12
+ */
13
+ export interface CartLineRefusal {
14
+ code: 'cart_limit_exceeded';
15
+ maxQuantity?: number;
16
+ }
17
+ export interface ThemeContextValue {
18
+ /** `editor` inside the Studio canvas: empty sections draw their setup frame. */
19
+ mode: ThemeMode;
20
+ /** BCP 47 language of the page. */
21
+ locale: string;
22
+ /** The store's theme settings, including the editor's unsaved live values. */
23
+ settings: Readonly<Record<string, unknown>>;
24
+ /** The shopper's cart (null until known). */
25
+ cart: Cart | null;
26
+ /** Where Checkout goes. */
27
+ checkoutUrl: string;
28
+ /** Which page-level overlay is open. */
29
+ overlay: OverlayName | null;
30
+ /** The cart drawer was opened by an add, not by the shopper. */
31
+ justAddedToCart: boolean;
32
+ /** Lines with a request in flight / whose last request failed. */
33
+ busyLines: readonly string[];
34
+ failedLines: readonly string[];
35
+ /** Failed lines the store said why for (a subset of `failedLines`), by line id. */
36
+ refusedLines: Readonly<Record<string, CartLineRefusal>>;
37
+ toasts: readonly ThemeToast[];
38
+ /** The cookie banner should be shown (the platform decided consent is needed and the shopper has not answered). */
39
+ consentRequired: boolean;
40
+ /** The store's privacy policy page, when it is published; null otherwise (or not known yet). */
41
+ privacyPolicyUrl: string | null;
42
+ /** The shop as sections see it (name, cookie banner, taxes, free-shipping threshold); absent outside a store. */
43
+ shop?: Shop;
44
+ /**
45
+ * Where a platform form posts on this page (`<form data-tq-form method="post">`, Theme ABI §1) — for page-level
46
+ * forms outside a section (a pop-up's sign-up). Absent where the platform takes no form posts.
47
+ */
48
+ formEndpoint?: string;
49
+ /** The outcome of the platform form POST this page answers, when it answers one (any form id). */
50
+ formResult?: {
51
+ formId: string;
52
+ status: 'ok' | 'invalid' | 'rate_limited' | 'unavailable' | 'expired';
53
+ email?: string;
54
+ discountCode?: string;
55
+ giftCard?: GiftCardBalance;
56
+ };
57
+ }
58
+ /**
59
+ * How applying a discount code went (`ThemeActions.cart.applyDiscountCode`). Kept equal to the runtime's
60
+ * `DiscountCodeResult` (runtime/kit/cart.tsx).
61
+ */
62
+ export type DiscountCodeResult = 'applied' | 'not_applicable' | 'failed';
63
+ /**
64
+ * Why the last code `applyDiscountCode` answered `not_applicable` for does not apply (`ThemeActions.cart.lastDiscountRefusal`),
65
+ * when the platform said why (board 35 #14). Kept equal to the runtime's `DiscountCodeRefusal` (runtime/kit/cart.tsx).
66
+ */
67
+ export type DiscountCodeRefusal = 'not_found' | 'inactive' | 'usage_limit' | 'already_used' | 'sign_in' | 'not_eligible' | 'no_items' | 'minimum_quantity' | 'minimum_subtotal' | 'delivery' | 'not_combinable';
68
+ /**
69
+ * What stock let the last `add` put in the cart, when the platform cut it (theme-abi Cart-2, the store's
70
+ * MERCHANDISE_OUT_OF_STOCK / MERCHANDISE_NOT_ENOUGH_STOCK cart warnings): `out_of_stock` — none could be added, the
71
+ * variant is sold out; `not_enough_stock` — `added` (0 or more) were added, all that is in stock. Kept equal to the
72
+ * runtime's `CartAddStock` (runtime/kit/cart.tsx).
73
+ */
74
+ export type CartAddStock = {
75
+ code: 'out_of_stock';
76
+ } | {
77
+ code: 'not_enough_stock';
78
+ added: number;
79
+ };
80
+ export interface ConsentDecision {
81
+ analytics: boolean;
82
+ marketing: boolean;
83
+ }
84
+ /** What an add may carry beside the variant and quantity. */
85
+ export interface AddToCartOptions {
86
+ /** The selling plan the shopper picked (`Product.sellingPlanGroups[].plans[].id`): the line is a subscription. */
87
+ sellingPlanId?: string;
88
+ /**
89
+ * The line's custom attributes (NEW-PROD-12) — a gift card's recipient. Entries whose key or value is not a string
90
+ * are dropped. Carried by kits that set `ThemeActions.cart.lineAttributes`.
91
+ */
92
+ attributes?: readonly CartLineAttribute[];
93
+ /**
94
+ * The theme shows a stock cut itself (it reads `ThemeActions.cart.lastAddStock()` where the button is): the kit then
95
+ * does not show its own toast for it. Without it the kit says it in a toast. Either way such an add never opens the
96
+ * cart.
97
+ */
98
+ showsStock?: true;
99
+ }
100
+ export interface ThemeActions {
101
+ cart: {
102
+ /**
103
+ * Resolves true when the line is in the cart; never rejects. Opens the cart on success, unless stock cut the add
104
+ * (`lastAddStock`). `options.attributes`
105
+ * (NEW-PROD-12) are the line's custom attributes — a gift card's recipient (`Recipient email`, `Recipient name`,
106
+ * `Message`, `Send on`); the platform stores them on the line and the order reads them. Malformed entries are dropped.
107
+ */
108
+ add(variantId: string, quantity: number, options?: AddToCartOptions): Promise<boolean>;
109
+ /**
110
+ * What stock let the last `add` put in the cart when the platform cut it (Cart-2), else null. A getter: read it right
111
+ * after `add` resolves. `add` resolved false for `out_of_stock` and for `not_enough_stock` with nothing added; the
112
+ * cart was not opened in either case, nor for a partial add. Absent on kits without it.
113
+ */
114
+ lastAddStock?(): CartAddStock | null;
115
+ /**
116
+ * `true` on a kit whose `add` carries `options.attributes` to the line (NEW-PROD-12); absent on older kits, which
117
+ * drop them. A theme checks it before offering a form whose answers must reach the line (a gift card's recipient).
118
+ */
119
+ lineAttributes?: true;
120
+ setQuantity(lineId: string, quantity: number): Promise<void>;
121
+ remove(lineId: string): Promise<void>;
122
+ setNote(note: string): Promise<void>;
123
+ open(): void;
124
+ /**
125
+ * Put a discount code on the cart, beside the codes already there; resolves how it went, never rejects.
126
+ * `applied`: the store applies it (the cart's totals already show it); an earlier code it does not combine with
127
+ * was taken off (`lastDiscountReplaced`). `not_applicable`: the store does not apply it to this cart (unknown,
128
+ * expired, a minimum not met, does not combine) — it was taken off again and the other codes stay; `failed`: the
129
+ * request did not go through.
130
+ * Optional: absent on hosts without cart discount codes — a theme must check before offering a code field.
131
+ */
132
+ applyDiscountCode?(code: string): Promise<DiscountCodeResult>;
133
+ /** Why the code the last `applyDiscountCode` refused does not apply, when the platform said why; else null. A getter: read it right after `applyDiscountCode` resolves. */
134
+ lastDiscountRefusal?(): DiscountCodeRefusal | null;
135
+ /** The earlier codes the last `applyDiscountCode` took off because the new code does not combine with them ([] when none). A getter. */
136
+ lastDiscountReplaced?(): readonly string[];
137
+ /** Take a discount code off the cart. Resolves true once it is gone; never rejects. Present with `applyDiscountCode`. */
138
+ removeDiscountCode?(code: string): Promise<boolean>;
139
+ };
140
+ search: {
141
+ open(): void;
142
+ /** Go to the results page for a query. */
143
+ go(query: string): void;
144
+ /** Product suggestions for a query (predictive search). */
145
+ suggest(query: string): Promise<SearchSuggestions>;
146
+ };
147
+ localization: {
148
+ /** Switch market (prices and currency follow). */
149
+ changeCountry(countryCode: string): void;
150
+ /**
151
+ * Apply a Region & language choice: `?country=` when the country changed, `?locale=` when the
152
+ * language changed (`current` is what the page shows now). Optional for older hosts.
153
+ */
154
+ changeLocale?(next: {
155
+ countryCode?: string;
156
+ languageCode?: string;
157
+ }, current?: {
158
+ countryCode?: string;
159
+ languageCode?: string;
160
+ }): void;
161
+ };
162
+ consent: {
163
+ decide(choice: ConsentDecision): void;
164
+ /** Show the cookie banner again, so the shopper can change an earlier choice. */
165
+ open(): void;
166
+ };
167
+ overlays: {
168
+ close(): void;
169
+ };
170
+ toasts: {
171
+ show(message: string, tone?: ThemeToast['tone']): void;
172
+ dismiss(id: string): void;
173
+ };
174
+ /** Go to a storefront URL (soft navigation when on). */
175
+ navigate(href: string): void;
176
+ }
177
+ export interface SearchSuggestions {
178
+ products: Product[];
179
+ collections: Link[];
180
+ articles: Link[];
181
+ /** All matches (the results page count). */
182
+ total: number;
183
+ }
184
+ export interface ThemeRuntimeValue {
185
+ context: ThemeContextValue;
186
+ actions: ThemeActions;
187
+ }
188
+ /** Provided by the theme runtime. Themes never render it. */
189
+ export declare const ThemeRuntimeContext: import("react").Context<ThemeRuntimeValue>;
190
+ /** Page state the theme can render from (cart, open overlay, mode, locale, settings). */
191
+ export declare function useThemeContext(): ThemeContextValue;
192
+ /** The platform actions a theme can trigger (cart, search, localization, consent, toasts). */
193
+ export declare function useThemeActions(): ThemeActions;
194
+ /**
195
+ * Predictive search for a query, debounced. Results are tagged with the query
196
+ * they answered: a slow answer for "shirt" never renders under "hat", and
197
+ * `loading` stays true until the CURRENT query has one.
198
+ */
199
+ export declare function useSearchSuggestions(query: string, debounceMs?: number): {
200
+ results: SearchSuggestions | null;
201
+ loading: boolean;
202
+ };
@@ -0,0 +1,73 @@
1
+ /**
2
+ * What a theme can read from and ask of the platform while it renders.
3
+ *
4
+ * The theme runtime provides these; a theme only calls the hooks. Nothing here
5
+ * exposes how it is done — no API clients, no cart engine, no session.
6
+ */
7
+ import { createContext, useContext, useEffect, useRef, useState } from 'react';
8
+ const NOOP_ACTIONS = {
9
+ cart: { add: async () => false, setQuantity: async () => { }, remove: async () => { }, setNote: async () => { }, open: () => { } },
10
+ search: { open: () => { }, go: () => { }, suggest: async () => ({ products: [], collections: [], articles: [], total: 0 }) },
11
+ localization: { changeCountry: () => { } },
12
+ consent: { decide: () => { }, open: () => { } },
13
+ overlays: { close: () => { } },
14
+ toasts: { show: () => { }, dismiss: () => { } },
15
+ navigate: () => { },
16
+ };
17
+ const DETACHED = {
18
+ context: {
19
+ mode: 'storefront',
20
+ locale: 'en',
21
+ settings: {},
22
+ cart: null,
23
+ checkoutUrl: '/checkout',
24
+ overlay: null,
25
+ justAddedToCart: false,
26
+ busyLines: [],
27
+ failedLines: [],
28
+ refusedLines: {},
29
+ toasts: [],
30
+ consentRequired: false,
31
+ privacyPolicyUrl: null,
32
+ },
33
+ actions: NOOP_ACTIONS,
34
+ };
35
+ /** Provided by the theme runtime. Themes never render it. */
36
+ export const ThemeRuntimeContext = createContext(DETACHED);
37
+ /** Page state the theme can render from (cart, open overlay, mode, locale, settings). */
38
+ export function useThemeContext() {
39
+ return useContext(ThemeRuntimeContext).context;
40
+ }
41
+ /** The platform actions a theme can trigger (cart, search, localization, consent, toasts). */
42
+ export function useThemeActions() {
43
+ return useContext(ThemeRuntimeContext).actions;
44
+ }
45
+ /**
46
+ * Predictive search for a query, debounced. Results are tagged with the query
47
+ * they answered: a slow answer for "shirt" never renders under "hat", and
48
+ * `loading` stays true until the CURRENT query has one.
49
+ */
50
+ export function useSearchSuggestions(query, debounceMs = 250) {
51
+ const { search } = useThemeActions();
52
+ const [answer, setAnswer] = useState(null);
53
+ const timer = useRef(null);
54
+ const term = query.trim();
55
+ useEffect(() => {
56
+ if (timer.current)
57
+ clearTimeout(timer.current);
58
+ if (!term)
59
+ return;
60
+ timer.current = setTimeout(() => {
61
+ search
62
+ .suggest(term)
63
+ .then((results) => setAnswer({ term, results }))
64
+ .catch(() => setAnswer({ term, results: { products: [], collections: [], articles: [], total: 0 } }));
65
+ }, debounceMs);
66
+ return () => {
67
+ if (timer.current)
68
+ clearTimeout(timer.current);
69
+ };
70
+ }, [term, search, debounceMs]);
71
+ const fresh = answer && answer.term === term ? answer.results : null;
72
+ return { results: term ? fresh : null, loading: Boolean(term) && !fresh };
73
+ }