@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,116 @@
1
+ /**
2
+ * `@tanqory/theme-kit/build` — derive a theme's ABI v2 manifest from what the theme already declares: its
3
+ * `defineSection` / `defineBlock` schemas, its theme settings, its templates, and the `ai` roles of its sections
4
+ * (printed as `x-tanqory-ai`). Pure (no file system, no React): a theme's build script loads the theme, calls
5
+ * `buildManifest`, validates the result with `@tanqory/theme-abi` `validateManifest`, and writes the files.
6
+ *
7
+ * const { manifest, problems } = buildManifest({ name, version, theme, settings, templates, forms: { newsletter: true } })
8
+ *
9
+ * Capabilities: the kit's runtime needs the defaults below. `data:shop` is always declared (decision D6: the
10
+ * platform hands shop data to the browser snapshot only to a theme that declares it). Add more with
11
+ * `capabilities` (e.g. `data:inventory` to show stock counts).
12
+ */
13
+ import type { Field } from './sdk/schema.js';
14
+ import type { SettingSpec, ThemeDefinition } from './sdk/theme.js';
15
+ import { type AiProfile } from './headless/ai.js';
16
+ import { type Strings } from './headless/strings.js';
17
+ export { aiProfileOf, checkAiProfile, type AiProfile, type AiRole } from './headless/ai.js';
18
+ export { KIT_STRINGS, kitStrings } from './headless/strings.js';
19
+ /** RFC 8785 JSON canonicalization: `manifest.json` is its JCS bytes plus one trailing LF. */
20
+ export { canonicalize } from './abi/jcs.js';
21
+ /** What the kit's server render reads (`ctx.data`, `ctx.actions`). `data:shop` is always declared (D6). */
22
+ export declare const SERVER_CAPABILITIES: readonly string[];
23
+ /** What the kit's browser runtime calls over `/_tq/data` and `/_tq/actions`. `data:shop` is always declared (D6). */
24
+ export declare const BROWSER_CAPABILITIES: readonly string[];
25
+ /** The fields each platform form posts (`forms.<id>.fields`); `website` is the honeypot. */
26
+ export declare const FORM_FIELDS: Readonly<Record<'newsletter' | 'contact', readonly string[]>>;
27
+ export interface RegionSpec {
28
+ title: string;
29
+ scope: 'shared' | 'template';
30
+ accepts?: readonly string[];
31
+ min?: number;
32
+ max?: number;
33
+ }
34
+ /** Header (shared), the template's body, footer (shared): where `zone` header / main / footer place a section. */
35
+ export declare const DEFAULT_REGIONS: Readonly<Record<string, RegionSpec>>;
36
+ /** A value check for stored values (`@tanqory/theme-abi` `validateFieldValue`, or the registry-2 wrapper a theme uses). */
37
+ export type ValueCheck = (type: string, value: unknown, spec: Record<string, unknown>) => {
38
+ ok: boolean;
39
+ errors?: readonly {
40
+ message: string;
41
+ code?: string;
42
+ }[];
43
+ };
44
+ export interface BuildManifestInput {
45
+ /** Package-style theme name (`@scope/name`). */
46
+ name: string;
47
+ version: string;
48
+ theme: ThemeDefinition;
49
+ /** Theme settings (`defineSettings`), grouped by each setting's `group`. */
50
+ settings?: Readonly<Record<string, SettingSpec>>;
51
+ /** Template name → its route kind (`home`, `product`, …, `not_found`, `custom:<id>`) and regions in order. */
52
+ templates: Readonly<Record<string, {
53
+ route: string;
54
+ title?: string;
55
+ regions?: readonly string[];
56
+ }>>;
57
+ regions?: Readonly<Record<string, RegionSpec>>;
58
+ /** `custom:<id>` route paths. */
59
+ customRoutes?: Readonly<Record<string, {
60
+ path: string;
61
+ }>>;
62
+ /** Added to the kit's defaults. */
63
+ capabilities?: {
64
+ server?: readonly string[];
65
+ browser?: readonly string[];
66
+ };
67
+ /** Platform forms the theme renders (`<NewsletterForm>`, `<ContactForm>`). */
68
+ forms?: {
69
+ newsletter?: boolean | {
70
+ title: string;
71
+ };
72
+ contact?: boolean | {
73
+ title: string;
74
+ };
75
+ };
76
+ /** The theme's content document version (default 1). */
77
+ contentVersion?: number;
78
+ /**
79
+ * `engines["tanqory-field-types"]` (default "2": `asset:client/…` image values in defaults and presets; "3" adds
80
+ * `color_palette`, B.3).
81
+ */
82
+ fieldTypes?: '1' | '2' | '3';
83
+ /** The AI-compat profile; from the sections' `ai` roles by default, `false` for none. */
84
+ aiProfile?: AiProfile | false;
85
+ /** Checks every default and preset value (e.g. `validateFieldValue` from `@tanqory/theme-abi`). */
86
+ checkValue?: ValueCheck;
87
+ /** Extra top-level `x-*` members. */
88
+ extensions?: Readonly<Record<`x-${string}`, unknown>>;
89
+ }
90
+ export interface BuiltManifest {
91
+ manifest: Record<string, unknown>;
92
+ /** Contract problems found while deriving it; a build should refuse to ship with any. */
93
+ problems: string[];
94
+ }
95
+ /** "buttonLabel" → "Button label". */
96
+ export declare function humanize(key: string): string;
97
+ /** A settings-DSL field as a manifest attribute (registry field spec). */
98
+ export declare function fieldSpecOf(key: string, f: Field): Record<string, unknown>;
99
+ /** The manifest of a headless theme. */
100
+ export declare function buildManifest(input: BuildManifestInput): BuiltManifest;
101
+ interface Node {
102
+ id?: string;
103
+ type?: string;
104
+ settings?: Record<string, unknown>;
105
+ blocks?: readonly Node[];
106
+ }
107
+ /**
108
+ * Problems in starter content (a template's or region's sections): every node names a declared component, has an
109
+ * ABI node id, and holds only its component's attributes (each value through `checkValue` when given).
110
+ */
111
+ export declare function checkContent(where: string, nodes: readonly Node[], manifest: Record<string, unknown>, checkValue?: ValueCheck): string[];
112
+ /**
113
+ * The theme's locale files with the kit's words underneath: every key the kit's primitives use exists in every
114
+ * language the kit has words for, and the theme's own values win.
115
+ */
116
+ export declare function withKitStrings(locales: Readonly<Record<string, Strings>>): Record<string, Record<string, string>>;
package/dist/build.js ADDED
@@ -0,0 +1,310 @@
1
+ import { aiProfileOf, checkAiProfile } from './headless/ai.js';
2
+ import { KIT_STRINGS } from './headless/strings.js';
3
+ import { APP_BLOCKS, acceptsAppBlocks } from './runtime/kit/contract/content.js';
4
+ export { aiProfileOf, checkAiProfile } from './headless/ai.js';
5
+ export { KIT_STRINGS, kitStrings } from './headless/strings.js';
6
+ /** RFC 8785 JSON canonicalization: `manifest.json` is its JCS bytes plus one trailing LF. */
7
+ export { canonicalize } from './abi/jcs.js';
8
+ /** What the kit's server render reads (`ctx.data`, `ctx.actions`). `data:shop` is always declared (D6). */
9
+ export const SERVER_CAPABILITIES = [
10
+ 'data:shop',
11
+ 'data:localization',
12
+ 'data:catalog',
13
+ 'data:content',
14
+ 'data:search',
15
+ 'data:cart',
16
+ 'data:products.recommendations',
17
+ 'actions:cart',
18
+ 'actions:consent',
19
+ 'actions:form.submit',
20
+ ];
21
+ /** What the kit's browser runtime calls over `/_tq/data` and `/_tq/actions`. `data:shop` is always declared (D6). */
22
+ export const BROWSER_CAPABILITIES = [
23
+ 'data:shop',
24
+ 'data:localization',
25
+ 'data:catalog',
26
+ 'data:content',
27
+ 'data:search',
28
+ 'data:cart',
29
+ 'data:products.recommendations',
30
+ 'actions:cart',
31
+ 'actions:consent',
32
+ ];
33
+ /** The fields each platform form posts (`forms.<id>.fields`); `website` is the honeypot. */
34
+ export const FORM_FIELDS = {
35
+ newsletter: ['email', 'consent', 'website'],
36
+ contact: ['name', 'email', 'phone', 'message', 'website'],
37
+ };
38
+ /** Header (shared), the template's body, footer (shared): where `zone` header / main / footer place a section. */
39
+ export const DEFAULT_REGIONS = {
40
+ header: { title: 'Header', scope: 'shared' },
41
+ main: { title: 'Template', scope: 'template' },
42
+ footer: { title: 'Footer', scope: 'shared' },
43
+ };
44
+ const ZONE_REGION = { header: 'header', main: 'main', footer: 'footer' };
45
+ /** "buttonLabel" → "Button label". */
46
+ export function humanize(key) {
47
+ const words = key.replace(/[-_]+/g, ' ').replace(/([a-z0-9])([A-Z])/g, '$1 $2').trim().toLowerCase();
48
+ return words ? words[0].toUpperCase() + words.slice(1) : key;
49
+ }
50
+ const RESOLVED = new Set(['collection', 'product', 'page', 'blog', 'menu']);
51
+ /** A settings-DSL field as a manifest attribute (registry field spec). */
52
+ export function fieldSpecOf(key, f) {
53
+ const out = { type: f.control, label: f.label ?? humanize(key) };
54
+ if (f.info)
55
+ out.info = f.info;
56
+ if (f.placeholder)
57
+ out.placeholder = f.placeholder;
58
+ if (f.group)
59
+ out.group = f.group;
60
+ if (f.control === 'select') {
61
+ out.options = f.options.map((v) => ({ value: v, label: f.optionLabels?.[v] ?? humanize(v) }));
62
+ }
63
+ else if (f.control === 'range' || f.control === 'number') {
64
+ out.min = f.min;
65
+ out.max = f.max;
66
+ out.step = f.step;
67
+ if (f.control === 'range' && f.unit)
68
+ out.unit = f.unit;
69
+ }
70
+ if (RESOLVED.has(f.control))
71
+ out.resolve = true;
72
+ // "Not set" is an absent key: an empty default is no default.
73
+ if (f.default !== '' && f.default !== null && f.default !== undefined)
74
+ out.default = f.default;
75
+ return out;
76
+ }
77
+ function attributesOf(schema) {
78
+ const out = {};
79
+ for (const [k, f] of Object.entries(schema.fields))
80
+ if (!f.legacyHidden)
81
+ out[k] = fieldSpecOf(k, f);
82
+ return out;
83
+ }
84
+ /** A theme setting (`defineSettings`) as a manifest settings item. */
85
+ function settingItem(key, s) {
86
+ const out = { key, type: s.type, label: s.label ?? humanize(key) };
87
+ if (s.info)
88
+ out.info = s.info;
89
+ if (s.placeholder)
90
+ out.placeholder = s.placeholder;
91
+ if (s.options)
92
+ out.options = s.options.map((o) => ({ value: o.value, label: o.label }));
93
+ if (s.type === 'range' || s.type === 'number') {
94
+ if (s.min !== undefined)
95
+ out.min = s.min;
96
+ if (s.max !== undefined)
97
+ out.max = s.max;
98
+ if (s.step !== undefined)
99
+ out.step = s.step;
100
+ if (s.type === 'range' && s.unit)
101
+ out.unit = s.unit;
102
+ }
103
+ if (s.type === 'list') {
104
+ if (s.items)
105
+ out.items = { ...s.items };
106
+ if (s.min !== undefined)
107
+ out.min = s.min;
108
+ if (s.max !== undefined)
109
+ out.max = s.max;
110
+ }
111
+ // A palette's cap (registry 3, B.3): a lower `max` than 20 only.
112
+ if (s.type === 'color_palette' && s.max !== undefined)
113
+ out.max = s.max;
114
+ if (s.default !== '' && s.default !== null && s.default !== undefined)
115
+ out.default = s.default;
116
+ return out;
117
+ }
118
+ const cleanSettings = (settings) => Object.fromEntries(Object.entries(settings ?? {}).filter(([, v]) => v !== '' && v !== null && v !== undefined));
119
+ /** The manifest of a headless theme. */
120
+ export function buildManifest(input) {
121
+ const problems = [];
122
+ const check = (where, type, value, spec) => {
123
+ if (!input.checkValue)
124
+ return;
125
+ const r = input.checkValue(type, value, spec);
126
+ if (!r.ok)
127
+ problems.push(`${where} ${JSON.stringify(value).slice(0, 80)}: ${(r.errors ?? []).map((e) => `${e.message}${e.code ? ` (${e.code})` : ''}`).join('; ')}`);
128
+ };
129
+ // ── components ───────────────────────────────────────────────────────
130
+ const components = {};
131
+ const sections = input.theme.sections;
132
+ for (const s of sections) {
133
+ if (components[s.id])
134
+ problems.push(`sections: "${s.id}" is declared twice`);
135
+ const c = {
136
+ role: s.zone === 'main' ? 'section' : 'layout',
137
+ title: s.title ?? humanize(s.id),
138
+ attributes: attributesOf(s.schema),
139
+ regions: [ZONE_REGION[s.zone] ?? 'main'],
140
+ category: s.category,
141
+ };
142
+ if (s.description)
143
+ c.description = s.description;
144
+ if (s.icon)
145
+ c.icon = s.icon;
146
+ const blocks = s.blocks ?? [];
147
+ // theme-abi §7.2: `'@app'` in the section's schema blocks accepts the blocks of installed apps (never on layout).
148
+ const appBlocks = acceptsAppBlocks(s.schema.blocks);
149
+ if (appBlocks && s.zone !== 'main')
150
+ problems.push(`sections: "${s.id}" is a layout section and cannot accept app blocks ("${APP_BLOCKS}")`);
151
+ if (blocks.length || appBlocks)
152
+ c.children = [...blocks.map((b) => b.name), ...(appBlocks ? [APP_BLOCKS] : [])];
153
+ if (s.requiresContext?.length)
154
+ c.requires = [...s.requiresContext];
155
+ if (s.presets?.length) {
156
+ c.presets = s.presets.map((p, pi) => {
157
+ const settings = cleanSettings(p.settings);
158
+ return {
159
+ name: p.name,
160
+ ...(Object.keys(settings).length ? { settings } : {}),
161
+ ...(p.blocks?.length ? { blocks: p.blocks.map((b, bi) => ({ type: b.type, id: `${s.id}-preset${pi}-${bi}`, ...(Object.keys(cleanSettings(b.settings)).length ? { settings: cleanSettings(b.settings) } : {}) })) } : {}),
162
+ };
163
+ });
164
+ }
165
+ components[s.id] = c;
166
+ for (const b of blocks) {
167
+ const existing = components[b.name];
168
+ if (existing && existing.role !== 'block') {
169
+ problems.push(`blocks: "${b.name}" is also a section type`);
170
+ continue;
171
+ }
172
+ const parents = new Set([...(existing?.parents ?? []), ...(b.parents ?? []), s.id]);
173
+ components[b.name] = { role: 'block', title: b.title ?? humanize(b.name), attributes: attributesOf(b), parents: [...parents], ...(b.description ? { description: b.description } : {}), ...(b.requiresContext?.length ? { requires: [...b.requiresContext] } : {}) };
174
+ }
175
+ }
176
+ // Defaults and preset values against their attributes.
177
+ for (const [cid, c] of Object.entries(components)) {
178
+ const attrs = c.attributes;
179
+ for (const [k, spec] of Object.entries(attrs))
180
+ if (spec.default !== undefined)
181
+ check(`components.${cid}.attributes.${k}.default`, String(spec.type), spec.default, spec);
182
+ c.presets?.forEach((p, i) => {
183
+ for (const [k, v] of Object.entries(p.settings ?? {})) {
184
+ const spec = attrs[k];
185
+ if (!spec)
186
+ problems.push(`components.${cid}.presets[${i}].settings.${k}: not an attribute of "${cid}"`);
187
+ else
188
+ check(`components.${cid}.presets[${i}].settings.${k}`, String(spec.type), v, spec);
189
+ }
190
+ for (const b of p.blocks ?? []) {
191
+ const bc = components[b.type];
192
+ if (!bc)
193
+ problems.push(`components.${cid}.presets[${i}].blocks: "${b.type}" is not a block`);
194
+ else
195
+ for (const [k, v] of Object.entries(b.settings ?? {})) {
196
+ const spec = bc.attributes[k];
197
+ if (!spec)
198
+ problems.push(`components.${cid}.presets[${i}].blocks.${b.type}.${k}: not an attribute`);
199
+ else
200
+ check(`components.${cid}.presets[${i}].blocks.${b.type}.${k}`, String(spec.type), v, spec);
201
+ }
202
+ }
203
+ });
204
+ }
205
+ // ── settings ─────────────────────────────────────────────────────────
206
+ const groups = new Map();
207
+ for (const [key, spec] of Object.entries(input.settings ?? {})) {
208
+ const g = spec.group ?? 'General';
209
+ if (!groups.has(g))
210
+ groups.set(g, []);
211
+ const item = settingItem(key, spec);
212
+ if (item.default !== undefined)
213
+ check(`settingsSchema.${g}.${key}.default`, spec.type, item.default, item);
214
+ groups.get(g).push(item);
215
+ }
216
+ const settingsSchema = [...groups].map(([group, items]) => ({ group, items }));
217
+ // ── routes, templates, regions ───────────────────────────────────────
218
+ const regions = input.regions ?? DEFAULT_REGIONS;
219
+ const templates = {};
220
+ const routes = {};
221
+ for (const [name, t] of Object.entries(input.templates)) {
222
+ const order = t.regions ?? ['header', 'main', 'footer'];
223
+ for (const r of order)
224
+ if (!regions[r])
225
+ problems.push(`templates.${name}: region "${r}" is not declared`);
226
+ templates[name] = { route: t.route, regions: [...order], ...(t.title ? { title: t.title } : {}) };
227
+ // The unsuffixed template of a kind is its default (`product.bundle` is chosen by templateSuffix).
228
+ if (!name.includes('.') && !routes[t.route])
229
+ routes[t.route] = name;
230
+ }
231
+ for (const [name, t] of Object.entries(input.templates))
232
+ if (!routes[t.route])
233
+ routes[t.route] = name;
234
+ for (const kind of Object.keys(routes))
235
+ if (kind.startsWith('custom:') && !input.customRoutes?.[kind.slice(7)])
236
+ problems.push(`routes.${kind}: no customRoutes.${kind.slice(7)}.path`);
237
+ // ── capabilities, forms, AI ──────────────────────────────────────────
238
+ const uniq = (a, b = []) => [...new Set([...a, ...b])];
239
+ const capabilities = { server: uniq(SERVER_CAPABILITIES, input.capabilities?.server), browser: uniq(BROWSER_CAPABILITIES, input.capabilities?.browser) };
240
+ const forms = {};
241
+ for (const id of ['newsletter', 'contact']) {
242
+ const f = input.forms?.[id];
243
+ if (!f)
244
+ continue;
245
+ forms[id] = { title: typeof f === 'object' ? f.title : humanize(id), fields: [...FORM_FIELDS[id]] };
246
+ }
247
+ const profile = input.aiProfile === false ? null : (input.aiProfile ?? aiProfileOf(sections));
248
+ if (profile)
249
+ problems.push(...checkAiProfile(profile, components));
250
+ const manifest = {
251
+ abi: '2.0',
252
+ name: input.name,
253
+ version: input.version,
254
+ engines: { 'tanqory-abi': '^2.0', 'tanqory-field-types': input.fieldTypes ?? '2' },
255
+ capabilities,
256
+ client: { hydration: 'full' },
257
+ ...(Object.keys(forms).length ? { forms } : {}),
258
+ contentVersion: input.contentVersion ?? 1,
259
+ routes,
260
+ ...(input.customRoutes ? { customRoutes: input.customRoutes } : {}),
261
+ templates,
262
+ regions,
263
+ components,
264
+ settingsSchema,
265
+ ...(profile ? { 'x-tanqory-ai': profile } : {}),
266
+ ...(input.extensions ?? {}),
267
+ };
268
+ return { manifest, problems };
269
+ }
270
+ const NODE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
271
+ /**
272
+ * Problems in starter content (a template's or region's sections): every node names a declared component, has an
273
+ * ABI node id, and holds only its component's attributes (each value through `checkValue` when given).
274
+ */
275
+ export function checkContent(where, nodes, manifest, checkValue) {
276
+ const problems = [];
277
+ const components = (manifest.components ?? {});
278
+ const walk = (list, at) => list.forEach((n, i) => {
279
+ const here = `${at}[${i}]`;
280
+ const c = n.type ? components[n.type] : undefined;
281
+ if (!c)
282
+ return void problems.push(`${here}: component "${n.type}" is not declared`);
283
+ if (!n.id || !NODE_ID.test(n.id) || n.id.length > 64)
284
+ problems.push(`${here}: node id ${JSON.stringify(n.id)} does not match the ABI id pattern`);
285
+ for (const [k, v] of Object.entries(n.settings ?? {})) {
286
+ const spec = c.attributes?.[k];
287
+ if (!spec)
288
+ problems.push(`${here}.settings.${k}: not an attribute of "${n.type}"`);
289
+ else if (checkValue) {
290
+ const r = checkValue(String(spec.type), v, spec);
291
+ if (!r.ok)
292
+ problems.push(`${here}.settings.${k} ${JSON.stringify(v).slice(0, 80)}: ${(r.errors ?? []).map((e) => e.message).join('; ')}`);
293
+ }
294
+ }
295
+ if (n.blocks)
296
+ walk(n.blocks, `${here}.blocks`);
297
+ });
298
+ walk(nodes, where);
299
+ return problems;
300
+ }
301
+ /**
302
+ * The theme's locale files with the kit's words underneath: every key the kit's primitives use exists in every
303
+ * language the kit has words for, and the theme's own values win.
304
+ */
305
+ export function withKitStrings(locales) {
306
+ const out = {};
307
+ for (const [lang, map] of Object.entries(locales))
308
+ out[lang] = { ...(KIT_STRINGS[lang] ?? KIT_STRINGS.en), ...map };
309
+ return out;
310
+ }
@@ -0,0 +1,19 @@
1
+ import { type ThemeBundle } from './runtime/slots.js';
2
+ import type { ContentNode } from './runtime/kit/types.js';
3
+ type Settings = Record<string, unknown>;
4
+ export interface HydrateOptions {
5
+ /** The theme bundle the server entry was built from (the same object `createRender` got). */
6
+ bundle: ThemeBundle;
7
+ /**
8
+ * The same node transform the server entry applies (`RenderOptions.transformNodes`), so the
9
+ * hydrated tree is the one the server rendered. Default: unchanged.
10
+ */
11
+ transformNodes?(nodes: ContentNode[], settings: Settings): ContentNode[];
12
+ }
13
+ /**
14
+ * Hydrates the server-rendered page. Call it once, at module scope of the theme's browser entry.
15
+ * Does nothing on a page without the server's state block.
16
+ */
17
+ export declare function hydrate(options: HydrateOptions): void;
18
+ export type { ThemeBundle } from './runtime/slots.js';
19
+ export type { ContentNode } from './runtime/kit/types.js';
package/dist/client.js ADDED
@@ -0,0 +1,87 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * `@tanqory/theme-kit/client` — the Theme ABI v2 browser entry as a function: the one `isEntry`
4
+ * script of client/client-manifest.json calls `hydrate({ bundle })`.
5
+ *
6
+ * It hydrates exactly what `createRender` (./server) rendered: the page's content document and the
7
+ * data the server loaded are read back from the non-executable
8
+ * `<script type="application/json" id="tq-state">` block, so the first client render is built from
9
+ * the same bytes. Dynamic style values are class rules (sdk/sx.ts): the server block already holds
10
+ * the first render's, and a later one is inserted through CSSOM. After hydration the only requests
11
+ * are same-origin `/_tq/data/*` and `/_tq/actions/*` (abi/wire.ts).
12
+ *
13
+ * On the editor canvas (a preview render) the section nodes live in a store the platform's editor
14
+ * bridge feeds (`tq:canvas` events, announced with `tq:canvas-live`), so a setting edit re-renders
15
+ * its section without a reload (abi/canvas-live.ts). A theme does nothing to get this.
16
+ */
17
+ import { unpackPageData } from './abi/state-pack.js';
18
+ import { useSyncExternalStore } from 'react';
19
+ import { hydrateRoot } from 'react-dom/client';
20
+ import { setThemeBundle } from './runtime/slots.js';
21
+ import { sectionModules } from './runtime/entries/sections.js';
22
+ import { registerSections } from './runtime/kit/registry.js';
23
+ import { SectionTree } from './runtime/kit/SectionTree.js';
24
+ import { DataProvider } from './runtime/kit/data.js';
25
+ import { ThemeProvider } from './runtime/kit/theme-context.js';
26
+ import { PreviewModeProvider } from './runtime/shell/preview-mode.js';
27
+ import { localeStrings } from './runtime/platform/locale.js';
28
+ import { AbiShell } from './abi/shell.js';
29
+ import { createDataApi } from './abi/data-api.js';
30
+ import { AbiCartProvider } from './abi/cart.js';
31
+ import { AbiRewardsProvider, HOST_OFFERS_LOYALTY } from './abi/rewards.js';
32
+ import { keepRefCode } from './abi/referrals.js';
33
+ import { createAbiRemote } from './abi/wire.js';
34
+ import { connectCanvas, createCanvasStore, createSettingsStore } from './abi/canvas-live.js';
35
+ import { locationOf, nodesOf, notFoundTree } from './abi/page.js';
36
+ /**
37
+ * The editor canvas (a preview render): the section nodes live in a store the platform's editor
38
+ * bridge feeds, so a setting edit re-renders its section without a reload (abi/canvas-live.ts).
39
+ */
40
+ function LiveSectionTree({ store, settings, transform }) {
41
+ const nodes = useSyncExternalStore(store.subscribe, store.get, store.get);
42
+ return _jsx(SectionTree, { tree: transform(nodes, settings) });
43
+ }
44
+ /** The theme provider + shell with the editor's live theme settings on the canvas; the server's settings otherwise. */
45
+ function WithLiveSettings({ store, children }) {
46
+ return _jsx(_Fragment, { children: children(useSyncExternalStore(store.subscribe, store.get, store.get)) });
47
+ }
48
+ /**
49
+ * Hydrates the server-rendered page. Call it once, at module scope of the theme's browser entry.
50
+ * Does nothing on a page without the server's state block.
51
+ */
52
+ export function hydrate(options) {
53
+ const { bundle } = options;
54
+ const transform = options.transformNodes ?? ((n) => n);
55
+ setThemeBundle(bundle);
56
+ registerSections(Object.values(sectionModules()).map((m) => m.default));
57
+ const LOCALE_MAPS = Object.fromEntries(Object.entries(bundle.locales).map(([path, mod]) => [path.match(/\/([^/]+)\.json$/)?.[1] ?? 'en', mod.default]));
58
+ const el = document.getElementById('tq-state');
59
+ const root = document.getElementById('root');
60
+ if (!el || !root)
61
+ return;
62
+ const state = JSON.parse(el.textContent ?? '{}');
63
+ // the listing's products travel once (abi/state-pack.ts): put them back before anything reads the data
64
+ if (state.data)
65
+ unpackPageData(state.data);
66
+ const remote = createAbiRemote();
67
+ const data = createDataApi(state.data, remote);
68
+ const settings = state.settings ?? {};
69
+ const raw = nodesOf(state.page.regions);
70
+ const ui = state.ui ?? 'en';
71
+ const strings = localeStrings(ui, LOCALE_MAPS);
72
+ const location = locationOf(state.route.canonical, state.route.params ?? {}, state.listing ?? '');
73
+ const preview = state.preview === true;
74
+ // Preview only: never on a live storefront, whose page has no bridge to answer.
75
+ let canvas = null;
76
+ let liveSettings = null;
77
+ if (preview) {
78
+ canvas = createCanvasStore(raw);
79
+ liveSettings = createSettingsStore(settings);
80
+ connectCanvas(window, canvas, (type, detail) => new CustomEvent(type, { detail }), liveSettings);
81
+ }
82
+ // A `?ref=` visit keeps the friend's code for checkout even while the host has no referral ops.
83
+ if (!preview && !HOST_OFFERS_LOYALTY)
84
+ keepRefCode(window.location.search);
85
+ const page = (settings_) => (_jsx(ThemeProvider, { settings: settings_, locale: strings, children: _jsx(AbiCartProvider, { initial: state.data.cart, remote: remote, children: _jsx(AbiRewardsProvider, { remote: preview || !HOST_OFFERS_LOYALTY ? null : remote, children: _jsx(AbiShell, { location: location, locale: ui, contentLanguage: state.lang, settings: settings_, preview: preview, labels: state.labels, children: canvas ? _jsx(LiveSectionTree, { store: canvas, settings: settings_, transform: transform }) : _jsx(SectionTree, { tree: state.data.routeMissing ? notFoundTree(transform(raw, settings_)) : transform(raw, settings_) }) }) }) }) }));
86
+ hydrateRoot(root, _jsx(PreviewModeProvider, { value: preview, children: _jsx(DataProvider, { value: data, children: liveSettings ? _jsx(WithLiveSettings, { store: liveSettings, children: page }) : page(settings) }) }));
87
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@tanqory/theme-kit/contract` — the content contract the editor and the runtime share: page and
3
+ * section-group documents, roles, validation and the content-version transforms. Pure (no React,
4
+ * no DOM), so build scripts and the manifest generator can load it in Node.
5
+ */
6
+ export * from './runtime/kit/contract/index.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@tanqory/theme-kit/contract` — the content contract the editor and the runtime share: page and
3
+ * section-group documents, roles, validation and the content-version transforms. Pure (no React,
4
+ * no DOM), so build scripts and the manifest generator can load it in Node.
5
+ */
6
+ export * from './runtime/kit/contract/index.js';