@numueg/theme-sdk 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -4,6 +4,400 @@ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
4
 
5
5
  // src/types/theme.ts
6
6
  var MAX_BLOCK_DEPTH = 5;
7
+
8
+ // src/validation/index.ts
9
+ var THEME_CONTRACT_VERSION = 1;
10
+ var SDK_VERSION = "0.4.0" ;
11
+ var REQUIRED_TEMPLATES = [
12
+ "home",
13
+ "product",
14
+ "collection",
15
+ "cart",
16
+ "page",
17
+ "search",
18
+ "404"
19
+ ];
20
+ var KNOWN_TEMPLATES = /* @__PURE__ */ new Set([
21
+ ...REQUIRED_TEMPLATES,
22
+ "blog",
23
+ "article",
24
+ "policies",
25
+ "password",
26
+ "account",
27
+ "checkout"
28
+ ]);
29
+ var KNOWN_SETTING_TYPES = /* @__PURE__ */ new Set([
30
+ "text",
31
+ "textarea",
32
+ "richtext",
33
+ "number",
34
+ "range",
35
+ "color",
36
+ "checkbox",
37
+ "select",
38
+ "radio",
39
+ "font",
40
+ "image_picker",
41
+ "url",
42
+ "product",
43
+ "product_list",
44
+ "collection",
45
+ "collection_list",
46
+ "header",
47
+ "paragraph",
48
+ "html",
49
+ "date",
50
+ "time",
51
+ "video_picker",
52
+ "color_scheme",
53
+ "page_picker",
54
+ "blog_picker",
55
+ "link_list_picker",
56
+ "variant_picker",
57
+ "file_upload",
58
+ "icon_picker",
59
+ "icon"
60
+ ]);
61
+ var PRESENTATIONAL_SETTING_TYPES = /* @__PURE__ */ new Set([
62
+ "header",
63
+ "paragraph",
64
+ "html"
65
+ ]);
66
+ var SECTION_TYPE_RE = /^[a-z][a-z0-9_-]*$/;
67
+ var THEME_ID_RE = /^[a-z0-9][a-z0-9_-]*[a-z0-9]$/;
68
+ var SEMVER_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z-.]+)?(?:\+[0-9A-Za-z-.]+)?$/;
69
+ function isObject(v) {
70
+ return typeof v === "object" && v !== null && !Array.isArray(v);
71
+ }
72
+ function isNonEmptyString(v) {
73
+ return typeof v === "string" && v.trim().length > 0;
74
+ }
75
+ var IssueBag = class {
76
+ constructor() {
77
+ this.issues = [];
78
+ }
79
+ err(code, message, path) {
80
+ this.issues.push({ level: "error", code, message, path });
81
+ }
82
+ warn(code, message, path) {
83
+ this.issues.push({ level: "warning", code, message, path });
84
+ }
85
+ result() {
86
+ return {
87
+ valid: !this.issues.some((i) => i.level === "error"),
88
+ issues: this.issues
89
+ };
90
+ }
91
+ };
92
+ function collectPresetSectionTypes(manifest) {
93
+ const types = /* @__PURE__ */ new Set();
94
+ const presets = manifest.presets;
95
+ if (!isObject(presets)) return types;
96
+ const buckets = [presets.templates, presets.section_groups];
97
+ for (const bucket of buckets) {
98
+ if (!isObject(bucket)) continue;
99
+ for (const entry of Object.values(bucket)) {
100
+ if (!isObject(entry)) continue;
101
+ const sections = entry.sections;
102
+ const instances = Array.isArray(sections) ? sections : isObject(sections) ? Object.values(sections) : [];
103
+ for (const inst of instances) {
104
+ if (isObject(inst) && isNonEmptyString(inst.type)) types.add(inst.type);
105
+ }
106
+ }
107
+ }
108
+ return types;
109
+ }
110
+ function validateSettingInto(bag, setting, pathPrefix, seenIds) {
111
+ if (!isObject(setting)) {
112
+ bag.err("schema.setting.invalid", "Setting must be an object.", pathPrefix);
113
+ return;
114
+ }
115
+ const type = setting.type;
116
+ if (!isNonEmptyString(type)) {
117
+ bag.err("schema.setting.type.missing", "Setting is missing a `type`.", pathPrefix);
118
+ } else if (!KNOWN_SETTING_TYPES.has(type)) {
119
+ bag.warn(
120
+ "schema.setting.type.unknown",
121
+ `Unknown setting type "${type}" \u2014 the customizer will fall back to a text input.`,
122
+ pathPrefix
123
+ );
124
+ }
125
+ const presentational = isNonEmptyString(type) && PRESENTATIONAL_SETTING_TYPES.has(type);
126
+ if (!presentational) {
127
+ if (!isNonEmptyString(setting.id)) {
128
+ bag.err("schema.setting.id.missing", "Setting is missing an `id`.", pathPrefix);
129
+ } else {
130
+ if (seenIds.has(setting.id)) {
131
+ bag.err(
132
+ "schema.setting.id.duplicate",
133
+ `Duplicate setting id "${setting.id}".`,
134
+ pathPrefix
135
+ );
136
+ }
137
+ seenIds.add(setting.id);
138
+ }
139
+ if (!isNonEmptyString(setting.label)) {
140
+ bag.warn("schema.setting.label.missing", "Setting has no `label`.", pathPrefix);
141
+ }
142
+ }
143
+ if (type === "select" || type === "radio") {
144
+ const options = setting.options;
145
+ if (!Array.isArray(options) || options.length === 0) {
146
+ bag.err(
147
+ "schema.setting.options.missing",
148
+ `"${type}" setting "${String(setting.id)}" must declare a non-empty \`options\` array.`,
149
+ pathPrefix
150
+ );
151
+ }
152
+ }
153
+ if (type === "range") {
154
+ if (typeof setting.min !== "number" || typeof setting.max !== "number") {
155
+ bag.err(
156
+ "schema.setting.range.bounds",
157
+ `"range" setting "${String(setting.id)}" must declare numeric \`min\` and \`max\`.`,
158
+ pathPrefix
159
+ );
160
+ } else if (setting.min >= setting.max) {
161
+ bag.err(
162
+ "schema.setting.range.order",
163
+ `"range" setting "${String(setting.id)}" has min >= max.`,
164
+ pathPrefix
165
+ );
166
+ }
167
+ }
168
+ }
169
+ function validateSectionSchema(schema, opts = {}) {
170
+ const bag = new IssueBag();
171
+ const where = opts.filenameType ? `${opts.filenameType}.json` : "section schema";
172
+ if (!isObject(schema)) {
173
+ bag.err("schema.invalid", "Section schema must be a JSON object.", where);
174
+ return bag.result();
175
+ }
176
+ const type = schema.type;
177
+ if (!isNonEmptyString(type)) {
178
+ bag.err("schema.type.missing", "Section schema is missing a `type`.", where);
179
+ } else {
180
+ if (!SECTION_TYPE_RE.test(type)) {
181
+ bag.err(
182
+ "schema.type.format",
183
+ `Section type "${type}" must match ${SECTION_TYPE_RE} (lowercase, start with a letter).`,
184
+ where
185
+ );
186
+ }
187
+ if (opts.filenameType && type !== opts.filenameType) {
188
+ bag.err(
189
+ "schema.type.filename_mismatch",
190
+ `Schema type "${type}" must equal its filename "${opts.filenameType}" (component-filename = schema-type convention).`,
191
+ where
192
+ );
193
+ }
194
+ }
195
+ if (!isNonEmptyString(schema.name)) {
196
+ bag.err("schema.name.missing", "Section schema is missing a `name`.", where);
197
+ }
198
+ if (schema.settings === void 0) {
199
+ bag.warn("schema.settings.missing", "Section schema has no `settings`.", where);
200
+ } else if (!Array.isArray(schema.settings)) {
201
+ bag.err("schema.settings.invalid", "`settings` must be an array.", where);
202
+ } else {
203
+ const seen = /* @__PURE__ */ new Set();
204
+ schema.settings.forEach(
205
+ (s, i) => validateSettingInto(bag, s, `${where}.settings[${i}]`, seen)
206
+ );
207
+ }
208
+ if (schema.blocks !== void 0) {
209
+ if (!Array.isArray(schema.blocks)) {
210
+ bag.err("schema.blocks.invalid", "`blocks` must be an array.", where);
211
+ } else {
212
+ schema.blocks.forEach((b, i) => {
213
+ if (!isObject(b) || !isNonEmptyString(b.type)) {
214
+ bag.err("schema.block.type.missing", `Block[${i}] is missing a \`type\`.`, where);
215
+ return;
216
+ }
217
+ if (!SECTION_TYPE_RE.test(b.type)) {
218
+ bag.err(
219
+ "schema.block.type.format",
220
+ `Block type "${b.type}" must match ${SECTION_TYPE_RE}.`,
221
+ where
222
+ );
223
+ }
224
+ if (Array.isArray(b.settings)) {
225
+ const seen = /* @__PURE__ */ new Set();
226
+ b.settings.forEach(
227
+ (s, j) => validateSettingInto(bag, s, `${where}.blocks[${i}].settings[${j}]`, seen)
228
+ );
229
+ }
230
+ });
231
+ }
232
+ }
233
+ return bag.result();
234
+ }
235
+ function validateSettingsAgainstSchema(settings, schema, pathPrefix = "settings") {
236
+ const bag = new IssueBag();
237
+ if (!isObject(settings)) {
238
+ bag.err("instance.settings.invalid", "Section settings must be an object.", pathPrefix);
239
+ return bag.result();
240
+ }
241
+ const defs = Array.isArray(schema.settings) ? schema.settings : [];
242
+ const byId = /* @__PURE__ */ new Map();
243
+ for (const d of defs) if (isNonEmptyString(d?.id)) byId.set(d.id, d);
244
+ for (const [key, value] of Object.entries(settings)) {
245
+ const def = byId.get(key);
246
+ if (!def) {
247
+ bag.warn(
248
+ "instance.setting.unknown",
249
+ `Setting "${key}" is not declared in the "${String(schema.type)}" schema.`,
250
+ `${pathPrefix}.${key}`
251
+ );
252
+ continue;
253
+ }
254
+ if (value === null || value === void 0) continue;
255
+ const p = `${pathPrefix}.${key}`;
256
+ if ((def.type === "select" || def.type === "radio") && Array.isArray(def.options)) {
257
+ const allowed = def.options.map((o) => o.value);
258
+ if (typeof value === "string" && !allowed.includes(value)) {
259
+ bag.err(
260
+ "instance.setting.option.invalid",
261
+ `"${key}" = "${value}" is not one of: ${allowed.join(", ")}.`,
262
+ p
263
+ );
264
+ }
265
+ }
266
+ if (def.type === "range" && typeof value === "number") {
267
+ if (typeof def.min === "number" && value < def.min) {
268
+ bag.err("instance.setting.range.under", `"${key}" = ${value} is below min ${def.min}.`, p);
269
+ }
270
+ if (typeof def.max === "number" && value > def.max) {
271
+ bag.err("instance.setting.range.over", `"${key}" = ${value} is above max ${def.max}.`, p);
272
+ }
273
+ }
274
+ if (def.type === "checkbox" && typeof value !== "boolean") {
275
+ bag.warn("instance.setting.type.mismatch", `"${key}" should be a boolean.`, p);
276
+ }
277
+ if ((def.type === "number" || def.type === "range") && typeof value !== "number") {
278
+ bag.warn("instance.setting.type.mismatch", `"${key}" should be a number.`, p);
279
+ }
280
+ }
281
+ return bag.result();
282
+ }
283
+ function validateManifestCore(bag, m) {
284
+ if (!isNonEmptyString(m.id)) {
285
+ bag.err("manifest.id.missing", "theme.json is missing `id`.", "id");
286
+ } else if (!THEME_ID_RE.test(m.id)) {
287
+ bag.err(
288
+ "manifest.id.format",
289
+ `Theme id "${m.id}" must be lowercase alphanumeric with dashes/underscores (no leading/trailing separator).`,
290
+ "id"
291
+ );
292
+ }
293
+ const name = m.name;
294
+ const nameOk = isNonEmptyString(name) || isObject(name) && Object.values(name).some((v) => isNonEmptyString(v));
295
+ if (!nameOk) {
296
+ bag.err("manifest.name.missing", "theme.json is missing a non-empty `name`.", "name");
297
+ }
298
+ if (!isNonEmptyString(m.version)) {
299
+ bag.err("manifest.version.missing", "theme.json is missing `version`.", "version");
300
+ } else if (!SEMVER_RE.test(m.version)) {
301
+ bag.err("manifest.version.invalid", `Version "${m.version}" is not valid semver.`, "version");
302
+ }
303
+ if (!isNonEmptyString(m.author)) {
304
+ bag.err("manifest.author.missing", "theme.json is missing `author`.", "author");
305
+ }
306
+ if (m.min_sdk_version !== void 0 && !SEMVER_RE.test(String(m.min_sdk_version))) {
307
+ bag.warn("manifest.min_sdk_version.invalid", "`min_sdk_version` is not valid semver.", "min_sdk_version");
308
+ }
309
+ }
310
+ function validateManifest(manifest, ctx = {}) {
311
+ const bag = new IssueBag();
312
+ if (!isObject(manifest)) {
313
+ bag.err("manifest.invalid", "theme.json must be a JSON object.", "theme.json");
314
+ return bag.result();
315
+ }
316
+ validateManifestCore(bag, manifest);
317
+ const presets = manifest.presets;
318
+ if (!isObject(presets) || Object.keys(presets).length === 0) {
319
+ bag.warn("manifest.presets.empty", "theme.json has no presets \u2014 merchants start with an empty page.", "presets");
320
+ } else {
321
+ if (ctx.sectionTypes) {
322
+ for (const type of collectPresetSectionTypes(manifest)) {
323
+ if (!ctx.sectionTypes.has(type)) {
324
+ bag.err(
325
+ "manifest.preset.unknown_section",
326
+ `Preset references section type "${type}" with no schemas/sections/${type}.json.`,
327
+ "presets"
328
+ );
329
+ }
330
+ }
331
+ }
332
+ const templates = isObject(presets.templates) ? presets.templates : {};
333
+ for (const t of REQUIRED_TEMPLATES) {
334
+ if (!(t in templates)) {
335
+ bag.warn(
336
+ "manifest.template.missing",
337
+ `No preset for required template "${t}" \u2014 the storefront will fall back to its built-in.`,
338
+ `presets.templates.${t}`
339
+ );
340
+ }
341
+ }
342
+ }
343
+ return bag.result();
344
+ }
345
+ function validateBuiltManifest(builtManifest, importMap, opts = {}) {
346
+ const bag = new IssueBag();
347
+ if (!isObject(builtManifest)) {
348
+ bag.err("built.invalid", "manifest.json must be a JSON object.", "manifest.json");
349
+ return bag.result();
350
+ }
351
+ validateManifestCore(bag, builtManifest);
352
+ const schemas = builtManifest.section_schemas;
353
+ const shipped = new Set(isObject(schemas) ? Object.keys(schemas) : []);
354
+ if (!isObject(schemas)) {
355
+ bag.warn("built.section_schemas.missing", "manifest.json has no `section_schemas`.", "section_schemas");
356
+ }
357
+ for (const type of collectPresetSectionTypes(builtManifest)) {
358
+ if (!shipped.has(type)) {
359
+ bag.err(
360
+ "built.preset.unknown_section",
361
+ `Preset references section type "${type}" not present in section_schemas.`,
362
+ "presets"
363
+ );
364
+ }
365
+ }
366
+ if (isObject(schemas)) {
367
+ for (const [type, schema] of Object.entries(schemas)) {
368
+ const r = validateSectionSchema(schema, { filenameType: type });
369
+ for (const issue of r.issues) bag.issues.push(issue);
370
+ }
371
+ }
372
+ if (!isNonEmptyString(builtManifest.plugin_version)) {
373
+ bag.warn("built.plugin_version.missing", "manifest.json has no `plugin_version`.", "plugin_version");
374
+ }
375
+ if (importMap !== void 0) {
376
+ if (!isObject(importMap)) {
377
+ bag.err("importmap.invalid", "import-map.json must be a JSON object.", "import-map.json");
378
+ } else {
379
+ const cv = importMap.contract_version;
380
+ if (typeof cv !== "number") {
381
+ bag.warn(
382
+ "importmap.contract_version.missing",
383
+ "import-map.json has no numeric `contract_version` \u2014 built by an older plugin.",
384
+ "contract_version"
385
+ );
386
+ } else if (typeof opts.hostContractVersion === "number" && cv > opts.hostContractVersion) {
387
+ bag.err(
388
+ "importmap.contract_version.incompatible",
389
+ `Theme built for contract v${cv}; this platform supports up to v${opts.hostContractVersion}.`,
390
+ "contract_version"
391
+ );
392
+ }
393
+ }
394
+ }
395
+ return bag.result();
396
+ }
397
+ function mergeResults(...results) {
398
+ const issues = results.flatMap((r) => r.issues);
399
+ return { valid: !issues.some((i) => i.level === "error"), issues };
400
+ }
7
401
  var ShopContext = createContext(null);
8
402
  var ProductContext = createContext(null);
9
403
  var CollectionContext = createContext(null);
@@ -526,9 +920,8 @@ function toNavigationItem(raw, locale) {
526
920
  url: raw.url || "/",
527
921
  resource_type: mapResourceType(raw.type),
528
922
  resource_handle: raw.resource_id ?? null,
529
- children: (raw.children ?? []).map(
530
- (child) => toNavigationItem(child, locale)
531
- )
923
+ target_visible: raw.target_visible !== false,
924
+ children: (raw.children ?? []).map((child) => toNavigationItem(child, locale)).filter((child) => child.target_visible)
532
925
  };
533
926
  }
534
927
  function useNavigation(handle, options) {
@@ -538,7 +931,8 @@ function useNavigation(handle, options) {
538
931
  const hostProvidedAny = !!navMap && Object.keys(navMap).length > 0;
539
932
  const rawItems = handle ? navMap?.[handle] : void 0;
540
933
  const hostItems = useMemo(() => {
541
- if (rawItems) return rawItems.map((it) => toNavigationItem(it, locale));
934
+ if (rawItems)
935
+ return rawItems.map((it) => toNavigationItem(it, locale)).filter((it) => it.target_visible);
542
936
  if (hostProvidedAny) return [];
543
937
  return null;
544
938
  }, [rawItems, hostProvidedAny, locale]);
@@ -1475,7 +1869,15 @@ function normalizeCartFromServer(cart) {
1475
1869
  subtotal: toMajor(cart.subtotal),
1476
1870
  total: toMajor(cart.total),
1477
1871
  ...cart.discount_amount != null ? { discount_amount: toMajor(cart.discount_amount) } : {},
1478
- items: Array.isArray(cart.items) ? cart.items.map((it) => ({ ...it, price: toMajor(it.price) })) : []
1872
+ items: Array.isArray(cart.items) ? cart.items.map((it) => {
1873
+ const raw = it;
1874
+ return {
1875
+ ...it,
1876
+ name: raw.name || raw.product_name || "",
1877
+ price: toMajor(raw.price ?? raw.unit_price),
1878
+ variant_name: raw.variant_name ?? void 0
1879
+ };
1880
+ }) : []
1479
1881
  };
1480
1882
  }
1481
1883
  function unwrapCart(json) {
@@ -3547,6 +3949,6 @@ function buildLocaleBundle(modules) {
3547
3949
  return bundle;
3548
3950
  }
3549
3951
 
3550
- export { AddToCartButton, Block, CartContext, CollectionCard, CollectionContext, CollectionProvider, CurrencySwitcher, CustomerContext, EditableImage, EditableText, Form, ICON_NAMES, Icon, IconMap, Image, Link, LocaleSwitcher, LocalizationContext, MAX_BLOCK_DEPTH, Money, NavigationContext, NuMuProvider, PageContext, ProductCard, ProductContext, ProductProvider, RichText, Section, SectionContext, ShopContext, ThemeSettingsContext, applyGlobalStyleTokens, applyImageTransform, asImageTransform, assetUrl, availableValues, buildLocaleBundle, buildThemeElement, clearSdkSingleton, collectBlocks, collectSections, computeGlobalStyleTokens, defaultVariant, defineBlock, defineSection, defineThemeEntry, dynamicSource, findVariantByOptions, flattenMessages, focalSrc, getReactSingleton, getSdkSingleton, isDefinedBlock, isDefinedSection, isDynamicSource, isSdkAvailable, mountTheme, pickTranslations, registerReactSingleton, registerSdkSingleton, resolveDynamicValue, resolveFontStack, resolveSettingsMap, resolveSizeChart, resolveSourcePath, resolveThemeSettings, sanitizeHtml, useAnalytics, useApp, useCart, useCheckout, useCollection, useCollectionOptional, useCollections, useCurrency, useCurrentTemplate, useCustomer, useCustomerActions, useCustomerAddresses, useDirection, useFieldTranslation, useGiftCardBalance, useImage, useLocale, useLocalization, useMoney, useNavigation, useNumberFormat, useOrder, useOrders, usePage, useProduct, useProductOptional, useProductSizeChart, useProducts, useRelatedProducts, useReorder, useResolvedSettings, useSearch, useSection, useSectionOptional, useShippingRates, useShop, useThemeSettings, useTranslation, useVariantSelection, useWishlist };
3952
+ export { AddToCartButton, Block, CartContext, CollectionCard, CollectionContext, CollectionProvider, CurrencySwitcher, CustomerContext, EditableImage, EditableText, Form, ICON_NAMES, Icon, IconMap, Image, KNOWN_SETTING_TYPES, KNOWN_TEMPLATES, Link, LocaleSwitcher, LocalizationContext, MAX_BLOCK_DEPTH, Money, NavigationContext, NuMuProvider, PageContext, ProductCard, ProductContext, ProductProvider, REQUIRED_TEMPLATES, RichText, SDK_VERSION, Section, SectionContext, ShopContext, THEME_CONTRACT_VERSION, ThemeSettingsContext, applyGlobalStyleTokens, applyImageTransform, asImageTransform, assetUrl, availableValues, buildLocaleBundle, buildThemeElement, clearSdkSingleton, collectBlocks, collectSections, computeGlobalStyleTokens, defaultVariant, defineBlock, defineSection, defineThemeEntry, dynamicSource, findVariantByOptions, flattenMessages, focalSrc, getReactSingleton, getSdkSingleton, isDefinedBlock, isDefinedSection, isDynamicSource, isSdkAvailable, mergeResults, mountTheme, pickTranslations, registerReactSingleton, registerSdkSingleton, resolveDynamicValue, resolveFontStack, resolveSettingsMap, resolveSizeChart, resolveSourcePath, resolveThemeSettings, sanitizeHtml, useAnalytics, useApp, useCart, useCheckout, useCollection, useCollectionOptional, useCollections, useCurrency, useCurrentTemplate, useCustomer, useCustomerActions, useCustomerAddresses, useDirection, useFieldTranslation, useGiftCardBalance, useImage, useLocale, useLocalization, useMoney, useNavigation, useNumberFormat, useOrder, useOrders, usePage, useProduct, useProductOptional, useProductSizeChart, useProducts, useRelatedProducts, useReorder, useResolvedSettings, useSearch, useSection, useSectionOptional, useShippingRates, useShop, useThemeSettings, useTranslation, useVariantSelection, useWishlist, validateBuiltManifest, validateManifest, validateSectionSchema, validateSettingsAgainstSchema };
3551
3953
  //# sourceMappingURL=index.mjs.map
3552
3954
  //# sourceMappingURL=index.mjs.map