@contentstack/studio-registry 1.3.0 → 1.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.
Files changed (27) hide show
  1. package/dist/cjs/index.cjs +216 -66
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/esm/component-registry/registry.constant.js +1 -0
  4. package/dist/esm/component-registry/registry.constant.js.map +1 -1
  5. package/dist/esm/data-binder/cslp.js +4 -1
  6. package/dist/esm/data-binder/cslp.js.map +1 -1
  7. package/dist/esm/data-binder/retrieve-data.js +74 -18
  8. package/dist/esm/data-binder/retrieve-data.js.map +1 -1
  9. package/dist/esm/data-binder/retrieve-data.util.js +50 -51
  10. package/dist/esm/data-binder/retrieve-data.util.js.map +1 -1
  11. package/dist/esm/data-binder/strategies/contentstack.js +19 -0
  12. package/dist/esm/data-binder/strategies/contentstack.js.map +1 -0
  13. package/dist/esm/data-binder/strategies/dataSourceCapabilities.js +12 -0
  14. package/dist/esm/data-binder/strategies/dataSourceCapabilities.js.map +1 -0
  15. package/dist/esm/data-binder/strategies/query.js +17 -0
  16. package/dist/esm/data-binder/strategies/query.js.map +1 -0
  17. package/dist/esm/data-binder/strategies/repeater.js +18 -0
  18. package/dist/esm/data-binder/strategies/repeater.js.map +1 -0
  19. package/dist/esm/data-binder/strategies/template.js +16 -0
  20. package/dist/esm/data-binder/strategies/template.js.map +1 -0
  21. package/dist/esm/data-binder/strategies/utils.js +20 -0
  22. package/dist/esm/data-binder/strategies/utils.js.map +1 -0
  23. package/dist/esm/data-binder/strategy-registry.js +20 -0
  24. package/dist/esm/data-binder/strategy-registry.js.map +1 -0
  25. package/dist/esm/index.d.ts +5 -2
  26. package/dist/esm/index.js +1 -1
  27. package/package.json +7 -7
@@ -64,6 +64,7 @@ const DATA_SOURCES = {
64
64
  TEMPLATE: "template",
65
65
  CONTENTSTACK: "contentstack",
66
66
  REPEATER: "repeater",
67
+ QUERY: "contentstack_queries",
67
68
  SYMBOL_PROPS: "symbol_props",
68
69
  };
69
70
  const INTERNAL_COMPONENT_SECTIONS = {
@@ -5173,6 +5174,9 @@ class JSONRTERegistry {
5173
5174
  * @returns The path of the CSLP path.
5174
5175
  */
5175
5176
  function getCslpPath(binding) {
5177
+ if (typeof binding !== "string") {
5178
+ return "";
5179
+ }
5176
5180
  const bindingParts = binding.split(".");
5177
5181
  const lastPart = bindingParts.pop();
5178
5182
  const cslpPath = bindingParts.join(".") + ".$." + lastPart;
@@ -5194,6 +5198,14 @@ const getCslpTag = ({ bindedField, data, }) => {
5194
5198
  return cslpValue || {};
5195
5199
  };
5196
5200
 
5201
+ const dataSourceCapabilities = {
5202
+ [DATA_SOURCES.CONTENTSTACK]: { supportsCslp: true },
5203
+ [DATA_SOURCES.COMPONENT_PROPS]: { supportsCslp: true },
5204
+ [DATA_SOURCES.TEMPLATE]: { supportsCslp: true },
5205
+ [DATA_SOURCES.REPEATER]: { supportsCslp: true },
5206
+ [DATA_SOURCES.QUERY]: { supportsCslp: true }, // Verified: Query supports CSLP now
5207
+ };
5208
+
5197
5209
  /**
5198
5210
  * This transforms the provided data into the format defined by the schema.
5199
5211
  *
@@ -5293,6 +5305,92 @@ function removeCurrentBindingKey(entry, bindingMap, currentBindingKey) {
5293
5305
  return [key, newValue];
5294
5306
  }
5295
5307
 
5308
+ class DataSourceStrategyRegistry {
5309
+ static instance;
5310
+ strategies = new Map();
5311
+ constructor() { }
5312
+ static getInstance() {
5313
+ if (!DataSourceStrategyRegistry.instance) {
5314
+ DataSourceStrategyRegistry.instance = new DataSourceStrategyRegistry();
5315
+ }
5316
+ return DataSourceStrategyRegistry.instance;
5317
+ }
5318
+ register(type, strategy) {
5319
+ this.strategies.set(type, strategy);
5320
+ }
5321
+ get(type) {
5322
+ return this.strategies.get(type);
5323
+ }
5324
+ }
5325
+
5326
+ /**
5327
+ * Extracts the binding path string from a Contentstack-like binding object.
5328
+ * Flattens nested path objects into a dot-notation string.
5329
+ */
5330
+ const getBindingStringForCS = (binding) => {
5331
+ //TODO: fix this types and data source uid
5332
+ const path = binding.value.path;
5333
+ const flattenPath = (path) => {
5334
+ if (!path || Object.keys(path).length === 0)
5335
+ return "";
5336
+ const key = Object.keys(path)[0];
5337
+ const value = path[key];
5338
+ const flattenValue = flattenPath(value);
5339
+ return flattenValue ? `${key}.${flattenValue}` : key;
5340
+ };
5341
+ return flattenPath(path);
5342
+ };
5343
+
5344
+ const getContentstackBindingString = (binding) => {
5345
+ // STORED AT dataSources.contentstack.ct_uid:euid.<bindingString>
5346
+ const { _content_type_uid, uid } = binding.value;
5347
+ const entryKey = `${_content_type_uid}:${uid}`;
5348
+ return [
5349
+ DATA_SOURCES_PREFIX,
5350
+ DATA_SOURCES.CONTENTSTACK,
5351
+ entryKey,
5352
+ getBindingStringForCS(binding),
5353
+ ]
5354
+ .filter(Boolean)
5355
+ .join(".");
5356
+ };
5357
+
5358
+ const getQueryBindingString = (binding) => {
5359
+ // STORED AT dataSources.query.<queryUID>.<bindingString>
5360
+ // Binding value structure: { queryUID: string, path: CSBindingPath }
5361
+ const { queryUID } = binding.value;
5362
+ const suffix = getBindingStringForCS(binding);
5363
+ // Keep the full path including "data" and "count" - the schema structure is the source of truth
5364
+ const finalPath = [DATA_SOURCES_PREFIX, DATA_SOURCES.QUERY, queryUID, suffix]
5365
+ .filter(Boolean)
5366
+ .join(".");
5367
+ return finalPath;
5368
+ };
5369
+
5370
+ const getRepeaterBindingString = (binding) => {
5371
+ const { repeaterUID } = binding.value;
5372
+ return [
5373
+ DATA_SOURCES_PREFIX,
5374
+ DATA_SOURCES.REPEATER,
5375
+ repeaterUID,
5376
+ "context",
5377
+ getBindingStringForCS(binding),
5378
+ ]
5379
+ .filter(Boolean)
5380
+ .join(".");
5381
+ };
5382
+
5383
+ const getTemplateBindingString = (binding) => {
5384
+ // STORED AT dataSources.template.<bindingString>
5385
+ return [
5386
+ DATA_SOURCES_PREFIX,
5387
+ DATA_SOURCES.TEMPLATE,
5388
+ getBindingStringForCS(binding),
5389
+ ]
5390
+ .filter(Boolean)
5391
+ .join(".");
5392
+ };
5393
+
5296
5394
  /**
5297
5395
  * Retrieves the default static value for a given property type.
5298
5396
  *
@@ -5365,59 +5463,53 @@ function getPlaceHolderComponent(placeholders, prop, type) {
5365
5463
  : getDefaultPropStaticValue(type);
5366
5464
  return placeholderComponent;
5367
5465
  }
5368
- const getBindingStringForCS = (binding) => {
5369
- //TODO: fix this types and data source uid
5370
- const path = binding.value.path;
5371
- const flattenPath = (path) => {
5372
- if (Object.keys(path).length === 0)
5373
- return "";
5374
- const key = Object.keys(path)[0];
5375
- const value = path[key];
5376
- const flattenValue = flattenPath(value);
5377
- return flattenValue ? `${key}.${flattenValue}` : key;
5378
- };
5379
- return flattenPath(path);
5380
- };
5466
+ // Register default strategies (Side-effect initialization)
5467
+ const registry = DataSourceStrategyRegistry.getInstance();
5468
+ registry.register(DATA_SOURCES.CONTENTSTACK, {
5469
+ getBindingString: (binding) => getContentstackBindingString(binding),
5470
+ });
5471
+ registry.register(DATA_SOURCES.TEMPLATE, {
5472
+ getBindingString: (binding) => getTemplateBindingString(binding),
5473
+ });
5474
+ registry.register(DATA_SOURCES.QUERY, {
5475
+ getBindingString: (binding) => getQueryBindingString(binding),
5476
+ convertPath: (path) => path.split("."),
5477
+ });
5478
+ registry.register(DATA_SOURCES.REPEATER, {
5479
+ getBindingString: (binding) => getRepeaterBindingString(binding),
5480
+ });
5481
+ registry.register(DATA_SOURCES.COMPONENT_PROPS, {
5482
+ getBindingString: (binding) => [DATA_SOURCES.COMPONENT_PROPS, binding.value].filter(Boolean).join("."),
5483
+ });
5484
+ registry.register(DATA_SOURCES.STATIC_VALUE, {
5485
+ getBindingString: (binding) => [DATA_SOURCES.STATIC_VALUE, binding.value].filter(Boolean).join("."),
5486
+ });
5487
+ registry.register(DATA_SOURCES.SYMBOL_PROPS, {
5488
+ getBindingString: (binding) => [DATA_SOURCES.SYMBOL_PROPS, binding.value].filter(Boolean).join("."),
5489
+ });
5381
5490
  const getBindingString = (binding) => {
5382
- if (binding.type === DATA_SOURCES.TEMPLATE) {
5383
- // STORED AT dataSources.template.<bindingString>
5384
- return [
5385
- DATA_SOURCES_PREFIX,
5386
- DATA_SOURCES.TEMPLATE,
5387
- getBindingStringForCS(binding),
5388
- ].join(".");
5491
+ const strategy = DataSourceStrategyRegistry.getInstance().get(binding.type);
5492
+ if (strategy) {
5493
+ return strategy.getBindingString(binding);
5389
5494
  }
5390
- if (binding.type === DATA_SOURCES.CONTENTSTACK) {
5391
- // STORED AT dataSources.contentstack.ct_uid:euid.<bindingString>
5392
- const { _content_type_uid, uid } = binding.value;
5393
- const entryKey = `${_content_type_uid}:${uid}`;
5394
- return [
5395
- DATA_SOURCES_PREFIX,
5396
- DATA_SOURCES.CONTENTSTACK,
5397
- entryKey,
5398
- getBindingStringForCS(binding),
5399
- ].join(".");
5400
- }
5401
- if (binding.type === DATA_SOURCES.REPEATER) {
5402
- const { repeaterUID } = binding.value;
5403
- return [
5404
- DATA_SOURCES_PREFIX,
5405
- DATA_SOURCES.REPEATER,
5406
- repeaterUID,
5407
- "context",
5408
- getBindingStringForCS(binding),
5409
- ].join(".");
5410
- }
5411
- if (binding.type === DATA_SOURCES.COMPONENT_PROPS) {
5412
- return [DATA_SOURCES.COMPONENT_PROPS, binding.value].join(".");
5413
- }
5414
- if (binding.type === DATA_SOURCES.STATIC_VALUE) {
5415
- return [DATA_SOURCES.STATIC_VALUE, binding.value].join(".");
5495
+ // Fallback for simple string bindings
5496
+ return binding.value;
5497
+ };
5498
+ const convertBindingPath = (bindingType, path) => {
5499
+ const strategy = DataSourceStrategyRegistry.getInstance().get(bindingType);
5500
+ if (strategy && strategy.convertPath) {
5501
+ return strategy.convertPath(path);
5416
5502
  }
5417
- if (binding.type === DATA_SOURCES.SYMBOL_PROPS) {
5418
- return [DATA_SOURCES.SYMBOL_PROPS, binding.value].join(".");
5503
+ return path;
5504
+ };
5505
+ const getFlattenedData = (data) => {
5506
+ if (data && typeof data === "object" && !Array.isArray(data)) {
5507
+ const keys = Object.keys(data).filter((key) => key !== "$" && key !== "_metadata");
5508
+ if (keys.length === 1) {
5509
+ return getFlattenedData(data[keys[0]]);
5510
+ }
5419
5511
  }
5420
- return binding.value;
5512
+ return data;
5421
5513
  };
5422
5514
  const resolveMetadataBindings = (node, data) => {
5423
5515
  if (!node.metadata?.bindings)
@@ -5442,7 +5534,7 @@ const getRepeaterContextPath = (binding) => {
5442
5534
  function isDataMatchingPropType(data, type) {
5443
5535
  const dataType = getDataType(data);
5444
5536
  const map = {
5445
- string: dataType === "string",
5537
+ string: dataType === "string" || dataType === "number" || dataType === "boolean",
5446
5538
  boolean: dataType === "boolean",
5447
5539
  number: dataType === "number",
5448
5540
  object: dataType === "object",
@@ -5504,13 +5596,34 @@ function retrievePropValue(args) {
5504
5596
  return;
5505
5597
  }
5506
5598
  const nodePropConfig = nodeProps[field];
5507
- const { binding: bindedField } = nodePropConfig || {};
5599
+ const { binding: bindedField, value: staticValue } = nodePropConfig || {};
5508
5600
  //TODO: Get proper data for design
5509
5601
  const placeholderComponent = getPlaceHolderComponent(placeholders, prop, type);
5510
- const fallbackValue = defaultValue ?? placeholderComponent;
5602
+ const fallbackValue = staticValue !== undefined
5603
+ ? staticValue
5604
+ : (defaultValue ?? placeholderComponent);
5511
5605
  const dataPath = bindedField && getBindingString(bindedField);
5512
5606
  if ((type === "array" || type === "object") && dataPath) {
5513
- const bindedData = get(data, dataPath);
5607
+ let pathForGet = dataPath;
5608
+ // Use strategy-based path conversion (Agnostic)
5609
+ if (bindedField?.type && typeof dataPath === "string") {
5610
+ pathForGet = convertBindingPath(bindedField.type, dataPath);
5611
+ }
5612
+ let bindedData = get(data, pathForGet);
5613
+ // Strictly handle QueryResult { entries: Entry[], count: number } structure.
5614
+ // If we're expecting an array and the bindedData matches the QueryResult pattern,
5615
+ // we extract the entries array.
5616
+ if (type === "array" &&
5617
+ bindedData &&
5618
+ typeof bindedData === "object" &&
5619
+ !Array.isArray(bindedData) &&
5620
+ "entries" in bindedData &&
5621
+ Array.isArray(bindedData.entries)) {
5622
+ bindedData = bindedData.entries;
5623
+ }
5624
+ // Note: If bindedData is a direct array [], it will fall through.
5625
+ // As per the requirement "expects {result: [], count: number} only and no direct []",
5626
+ // we are now enforcing the unwrapping for the expected object structure.
5514
5627
  const bindingMap = nodePropConfig.bindingMap;
5515
5628
  const isBindingMapEmpty = Object.keys(bindingMap || {}).length === 0;
5516
5629
  const transformedData = !isBindingMapEmpty
@@ -5518,11 +5631,23 @@ function retrievePropValue(args) {
5518
5631
  : bindedData;
5519
5632
  resolvedData = transformedData ?? fallbackValue;
5520
5633
  if (type === "array") {
5521
- const cslpObj = getCslpTag({ bindedField: dataPath, data });
5522
- if (cslpObj["data-cslp"]) {
5523
- mappedData[`$${field}Items`] = transformedData.map((_, idx) => {
5524
- return { "data-cslp": `${cslpObj["data-cslp"]}.$${idx}` };
5525
- });
5634
+ const isCSLPAbleType = bindedField?.type &&
5635
+ dataSourceCapabilities[bindedField.type]?.supportsCslp;
5636
+ if (cslp.appendTags &&
5637
+ isCSLPAbleType &&
5638
+ Array.isArray(transformedData)) {
5639
+ const cslpObj = getCslpTag({ bindedField: dataPath, data });
5640
+ if (cslpObj["data-cslp"]) {
5641
+ mappedData[`$${field}Items`] = transformedData.map((_, idx) => {
5642
+ return { "data-cslp": `${cslpObj["data-cslp"]}.$${idx}` };
5643
+ });
5644
+ }
5645
+ }
5646
+ if (isCompositionBeingEdited &&
5647
+ isCSLPAbleType &&
5648
+ Array.isArray(resolvedData) &&
5649
+ resolvedData.length === 0) {
5650
+ resolvedData = [{}];
5526
5651
  }
5527
5652
  }
5528
5653
  }
@@ -5533,6 +5658,7 @@ function retrievePropValue(args) {
5533
5658
  // the binding path will be like: dataSources.repeater.{uid}.context.{fieldName}
5534
5659
  // But context IS the primitive value, so accessing context.fieldName returns undefined.
5535
5660
  // In this case, we should return the context itself.
5661
+ let handledRepeaterContext = false;
5536
5662
  if (dataPath.includes("dataSources.repeater")) {
5537
5663
  const pathParts = dataPath.split(".");
5538
5664
  // Check if path is: dataSources.repeater.{uid}.context.{fieldName}
@@ -5545,10 +5671,37 @@ function retrievePropValue(args) {
5545
5671
  const isPrimitive = typeof contextValue !== "object" || contextValue === null;
5546
5672
  if (isPrimitive) {
5547
5673
  resolvedData = contextValue;
5674
+ handledRepeaterContext = true;
5548
5675
  }
5549
5676
  }
5550
5677
  }
5551
5678
  }
5679
+ // Unwrap single-key objects (common in Contentstack Modular Blocks)
5680
+ // We only do this if we are not explicitly expecting an object or array.
5681
+ // Skip if we already handled the repeater context case above.
5682
+ if (type !== "object" && type !== "array" && !handledRepeaterContext) {
5683
+ resolvedData = getFlattenedData(resolvedData);
5684
+ // Fallback to static value if the resolved data is still not matching the expected primitive type
5685
+ // This ensures that if a binding returns an object (e.g. during a structural change), we don't crash React
5686
+ // and instead show the static value entered by the user.
5687
+ if (!isDataMatchingPropType(resolvedData, type)) {
5688
+ if (type === "string") {
5689
+ const actualType = getDataType(resolvedData);
5690
+ if (actualType === "object" || actualType === "array") {
5691
+ resolvedData =
5692
+ "This content may not display correctly in this view. Please contact your development team to update the component so it can support this type of content.";
5693
+ }
5694
+ else {
5695
+ resolvedData =
5696
+ staticValue !== undefined ? staticValue : fallbackValue;
5697
+ }
5698
+ }
5699
+ else {
5700
+ resolvedData =
5701
+ staticValue !== undefined ? staticValue : fallbackValue;
5702
+ }
5703
+ }
5704
+ }
5552
5705
  if (type === "choice") {
5553
5706
  if (!prop.multiSelect) {
5554
5707
  // we are explicitly checking for single select choice type to ensure we don't have a mismatch in the data type.
@@ -5559,7 +5712,7 @@ function retrievePropValue(args) {
5559
5712
  }
5560
5713
  }
5561
5714
  else {
5562
- resolvedData = fallbackValue;
5715
+ resolvedData = staticValue !== undefined ? staticValue : fallbackValue;
5563
5716
  // TODO: THIS IS A TEMPORARY FIX TO HANDLE THE CASE WHERE THE DATA TYPE IS NOT MATCHING THE PROP TYPE. WE WILL REMOVE THIS ELSE BLOCK.
5564
5717
  if (type === "choice") {
5565
5718
  if (!prop.multiSelect) {
@@ -5606,13 +5759,8 @@ function retrievePropValue(args) {
5606
5759
  if (type === "json_rte") {
5607
5760
  mappedData["jsonConfig"] = jsonRTE;
5608
5761
  }
5609
- const cslpAbleTypes = [
5610
- DATA_SOURCES.CONTENTSTACK,
5611
- DATA_SOURCES.COMPONENT_PROPS,
5612
- DATA_SOURCES.TEMPLATE,
5613
- DATA_SOURCES.REPEATER,
5614
- ];
5615
- const isCSLPAbleType = bindedField?.type && cslpAbleTypes.includes(bindedField.type);
5762
+ const isCSLPAbleType = bindedField?.type &&
5763
+ dataSourceCapabilities[bindedField.type]?.supportsCslp;
5616
5764
  if (cslp.appendTags && dataPath && isCSLPAbleType) {
5617
5765
  const cslpValue = getCslpTag({ bindedField: dataPath, data });
5618
5766
  if (cslpValue) {
@@ -5726,12 +5874,14 @@ exports.JSONRTERegistry = JSONRTERegistry;
5726
5874
  exports.STYLE_SECTIONS_OPTIONS = STYLE_SECTIONS_OPTIONS;
5727
5875
  exports.STYLE_SECTION_CATEGORIES = STYLE_SECTION_CATEGORIES;
5728
5876
  exports.SYSTEM_COMPONENTS = SYSTEM_COMPONENTS;
5877
+ exports.convertBindingPath = convertBindingPath;
5729
5878
  exports.getBindingString = getBindingString;
5730
5879
  exports.getCslpPath = getCslpPath;
5731
5880
  exports.getCslpTag = getCslpTag;
5732
5881
  exports.getDataType = getDataType;
5733
5882
  exports.getDefaultPropStaticValue = getDefaultPropStaticValue;
5734
5883
  exports.getDesignClassesNames = getDesignClassesNames;
5884
+ exports.getFlattenedData = getFlattenedData;
5735
5885
  exports.getPlaceHolderComponent = getPlaceHolderComponent;
5736
5886
  exports.getRepeaterBinding = getRepeaterBinding;
5737
5887
  exports.getRepeaterContextPath = getRepeaterContextPath;