@uniformdev/canvas 19.35.3-alpha.82 → 19.36.1-alpha.7

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
@@ -271,6 +271,9 @@ var require_retry2 = __commonJS({
271
271
  // src/CanvasClient.ts
272
272
  import { ApiClient } from "@uniformdev/context/api";
273
273
 
274
+ // src/enhancement/createLimitPolicy.ts
275
+ import { ApiClientError } from "@uniformdev/context/api";
276
+
274
277
  // ../../node_modules/.pnpm/p-retry@5.1.2/node_modules/p-retry/index.js
275
278
  var import_retry = __toESM(require_retry2(), 1);
276
279
  var networkErrorMsgs = /* @__PURE__ */ new Set([
@@ -446,7 +449,17 @@ function createLimitPolicy({
446
449
  }
447
450
  if (retry2) {
448
451
  const retryFunc = currentFunc;
449
- currentFunc = () => pRetry(retryFunc, retry2);
452
+ currentFunc = () => pRetry(retryFunc, {
453
+ ...retry2,
454
+ onFailedAttempt: async (error) => {
455
+ if (retry2.onFailedAttempt) {
456
+ await retry2.onFailedAttempt(error);
457
+ }
458
+ if (error instanceof ApiClientError && typeof error.statusCode === "number" && error.statusCode.toString().startsWith("4")) {
459
+ throw error;
460
+ }
461
+ }
462
+ });
450
463
  }
451
464
  return currentFunc();
452
465
  };
@@ -1642,6 +1655,80 @@ var RouteClient = class extends ApiClient6 {
1642
1655
  }
1643
1656
  };
1644
1657
 
1658
+ // src/utils/bindVariables.ts
1659
+ function bindVariables({
1660
+ variables,
1661
+ value,
1662
+ errorPrefix = "Variable",
1663
+ handleBinding
1664
+ }) {
1665
+ let boundCount = 0;
1666
+ const errors = [];
1667
+ const defaultHandleBinding = (variableName, variables2, errors2) => {
1668
+ const variableValue = variables2[variableName];
1669
+ if (variableValue === void 0) {
1670
+ errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1671
+ return "";
1672
+ }
1673
+ return variableValue;
1674
+ };
1675
+ const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1676
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1677
+ boundCount++;
1678
+ return variableValue;
1679
+ });
1680
+ return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1681
+ }
1682
+
1683
+ // src/utils/bindVariablesToObject.ts
1684
+ import { produce } from "immer";
1685
+ function bindVariablesToObject(options) {
1686
+ return bindVariablesToObjectRecursive(options);
1687
+ }
1688
+ function bindVariablesToObjectRecursive({
1689
+ value,
1690
+ recursivePath,
1691
+ ...bindVariablesOptions
1692
+ }) {
1693
+ let boundCount = 0;
1694
+ const errors = [];
1695
+ if (typeof value === "string") {
1696
+ return bindVariables({ ...bindVariablesOptions, value });
1697
+ }
1698
+ if (typeof value !== "object" || value === null) {
1699
+ return { boundCount: 0, result: value };
1700
+ }
1701
+ const result = produce(value, (draft) => {
1702
+ Object.entries(draft).forEach(([property, oldValue]) => {
1703
+ const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1704
+ if (typeof oldValue === "string") {
1705
+ const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1706
+ if (oldValue !== bindResult.result || bindResult.errors) {
1707
+ boundCount += bindResult.boundCount;
1708
+ draft[property] = bindResult.result;
1709
+ if (bindResult.errors) {
1710
+ errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1711
+ }
1712
+ }
1713
+ return;
1714
+ }
1715
+ const childBind = bindVariablesToObjectRecursive({
1716
+ ...bindVariablesOptions,
1717
+ value: oldValue,
1718
+ recursivePath: currentObjectPath
1719
+ });
1720
+ if (childBind.boundCount || childBind.errors) {
1721
+ boundCount += childBind.boundCount;
1722
+ draft[property] = childBind.result;
1723
+ if (childBind.errors) {
1724
+ errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1725
+ }
1726
+ }
1727
+ });
1728
+ });
1729
+ return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1730
+ }
1731
+
1645
1732
  // src/utils/createApiEnhancer.ts
1646
1733
  var createUniformApiEnhancer = ({ apiUrl }) => {
1647
1734
  return async (message) => {
@@ -1689,8 +1776,8 @@ var getParameterAttributes = ({
1689
1776
  [ATTRIBUTE_PARAMETER_VALUE]: String(value != null ? value : ""),
1690
1777
  [ATTRIBUTE_PARAMETER_TYPE]: type,
1691
1778
  [ATTRIBUTE_PLACEHOLDER]: placeholder,
1692
- [ATTRIBUTE_MULTILINE]: type === "text" ? isMultiline : void 0,
1693
- contentEditable: type === "text" ? isEditable : void 0
1779
+ [ATTRIBUTE_MULTILINE]: isMultiline,
1780
+ contentEditable: isEditable
1694
1781
  };
1695
1782
  };
1696
1783
 
@@ -1731,183 +1818,9 @@ function mapSlotToTestVariations(slot) {
1731
1818
  });
1732
1819
  }
1733
1820
 
1734
- // src/utils/variables/parseVariableExpression.ts
1735
- var escapeCharacter = "\\";
1736
- var variablePrefix = "${";
1737
- var variableSuffix = "}";
1738
- function parseVariableExpression(serialized, onToken) {
1739
- let bufferStartIndex = 0;
1740
- let bufferEndIndex = 0;
1741
- let tokenCount = 0;
1742
- const handleToken = (token, type) => {
1743
- tokenCount++;
1744
- return onToken == null ? void 0 : onToken(token, type);
1745
- };
1746
- let state = "text";
1747
- for (let index = 0; index < serialized.length; index++) {
1748
- const char = serialized[index];
1749
- if (bufferStartIndex > bufferEndIndex) {
1750
- bufferEndIndex = bufferStartIndex;
1751
- }
1752
- if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1753
- if (serialized[index - 1] === escapeCharacter) {
1754
- bufferEndIndex -= escapeCharacter.length;
1755
- if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1756
- return tokenCount;
1757
- }
1758
- bufferStartIndex = index;
1759
- bufferEndIndex = index + 1;
1760
- continue;
1761
- }
1762
- state = "variable";
1763
- if (bufferEndIndex > bufferStartIndex) {
1764
- if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1765
- return tokenCount;
1766
- }
1767
- bufferStartIndex = bufferEndIndex;
1768
- }
1769
- index += variablePrefix.length - 1;
1770
- bufferStartIndex += variablePrefix.length;
1771
- continue;
1772
- }
1773
- if (char === variableSuffix && state === "variable") {
1774
- state = "text";
1775
- if (bufferEndIndex > bufferStartIndex) {
1776
- if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1777
- return tokenCount;
1778
- }
1779
- bufferStartIndex = bufferEndIndex + variableSuffix.length;
1780
- }
1781
- continue;
1782
- }
1783
- bufferEndIndex++;
1784
- }
1785
- if (bufferEndIndex > bufferStartIndex) {
1786
- if (state === "variable") {
1787
- state = "text";
1788
- bufferStartIndex -= variablePrefix.length;
1789
- }
1790
- handleToken(serialized.substring(bufferStartIndex), state);
1791
- }
1792
- return tokenCount;
1793
- }
1794
-
1795
- // src/utils/variables/bindVariables.ts
1796
- function bindVariables({
1797
- variables,
1798
- value,
1799
- errorPrefix = "Variable",
1800
- handleBinding
1801
- }) {
1802
- let boundCount = 0;
1803
- let tokenCount = 0;
1804
- const errors = [];
1805
- const defaultHandleBinding = (variableName, variables2, errors2) => {
1806
- const variableValue = variables2[variableName];
1807
- if (variableValue === void 0) {
1808
- errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1809
- return "";
1810
- }
1811
- return variableValue;
1812
- };
1813
- const result = [];
1814
- parseVariableExpression(value, (token, tokenType) => {
1815
- tokenCount++;
1816
- if (tokenType === "text") {
1817
- result.push(token);
1818
- return;
1819
- }
1820
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1821
- boundCount++;
1822
- result.push(variableValue);
1823
- });
1824
- return {
1825
- result: tokenCount === 1 ? result[0] : result.join(""),
1826
- boundCount,
1827
- errors: errors.length > 0 ? errors : void 0
1828
- };
1829
- }
1830
-
1831
- // src/utils/variables/bindVariablesToObject.ts
1832
- import { produce } from "immer";
1833
-
1834
- // src/utils/variables/createVariableReference.ts
1835
- function createVariableReference(variableName) {
1836
- return `\${${variableName}}`;
1837
- }
1838
-
1839
- // src/utils/variables/bindVariablesToObject.ts
1840
- function bindVariablesToObject(options) {
1841
- return bindVariablesToObjectRecursive(options);
1842
- }
1843
- function bindVariablesToObjectRecursive({
1844
- value,
1845
- recursivePath,
1846
- ...bindVariablesOptions
1847
- }) {
1848
- let boundCount = 0;
1849
- const errors = [];
1850
- if (typeof value === "string") {
1851
- return bindVariables({ ...bindVariablesOptions, value });
1852
- }
1853
- if (typeof value !== "object" || value === null) {
1854
- return { boundCount: 0, result: value };
1855
- }
1856
- const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1857
- if (richTextNodeResult !== void 0) {
1858
- return richTextNodeResult;
1859
- }
1860
- const result = produce(value, (draft) => {
1861
- Object.entries(draft).forEach(([property, oldValue]) => {
1862
- const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1863
- if (typeof oldValue === "string") {
1864
- const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1865
- if (oldValue !== bindResult.result || bindResult.errors) {
1866
- boundCount += bindResult.boundCount;
1867
- draft[property] = bindResult.result;
1868
- if (bindResult.errors) {
1869
- errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1870
- }
1871
- }
1872
- return;
1873
- }
1874
- const childBind = bindVariablesToObjectRecursive({
1875
- ...bindVariablesOptions,
1876
- value: oldValue,
1877
- recursivePath: currentObjectPath
1878
- });
1879
- if (childBind.boundCount || childBind.errors) {
1880
- boundCount += childBind.boundCount;
1881
- draft[property] = childBind.result;
1882
- if (childBind.errors) {
1883
- errors.push(...childBind.errors);
1884
- }
1885
- }
1886
- });
1887
- });
1888
- return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1889
- }
1890
- function handleRichTextNodeBinding(object, options) {
1891
- if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
1892
- const value = createVariableReference(object.reference);
1893
- const bindResult = bindVariables({ ...options, value });
1894
- const bindResultAsTextNode = {
1895
- detail: 0,
1896
- format: 0,
1897
- mode: "normal",
1898
- style: "",
1899
- text: bindResult.result,
1900
- type: "text",
1901
- version: 1
1902
- };
1903
- return { ...bindResult, result: bindResultAsTextNode };
1904
- }
1905
- return void 0;
1906
- }
1907
-
1908
1821
  // src/index.ts
1909
- import { ApiClientError } from "@uniformdev/context/api";
1910
- var CanvasClientError = ApiClientError;
1822
+ import { ApiClientError as ApiClientError2 } from "@uniformdev/context/api";
1823
+ var CanvasClientError = ApiClientError2;
1911
1824
  export {
1912
1825
  ATTRIBUTE_COMPONENT_ID,
1913
1826
  ATTRIBUTE_MULTILINE,
@@ -1915,7 +1828,7 @@ export {
1915
1828
  ATTRIBUTE_PARAMETER_TYPE,
1916
1829
  ATTRIBUTE_PARAMETER_VALUE,
1917
1830
  ATTRIBUTE_PLACEHOLDER,
1918
- ApiClientError,
1831
+ ApiClientError2 as ApiClientError,
1919
1832
  BatchEntry,
1920
1833
  CANVAS_DRAFT_STATE,
1921
1834
  CANVAS_ENRICHMENT_TAG_PARAM,
@@ -1963,7 +1876,6 @@ export {
1963
1876
  createEventBus,
1964
1877
  createLimitPolicy,
1965
1878
  createUniformApiEnhancer,
1966
- createVariableReference,
1967
1879
  enhance,
1968
1880
  extractLocales,
1969
1881
  generateHash,
@@ -1989,7 +1901,6 @@ export {
1989
1901
  mapSlotToPersonalizedVariations,
1990
1902
  mapSlotToTestVariations,
1991
1903
  nullLimitPolicy,
1992
- parseVariableExpression,
1993
1904
  subscribeToComposition,
1994
1905
  unstable_CompositionRelationshipClient,
1995
1906
  walkComponentTree
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas",
3
- "version": "19.35.3-alpha.82+4bc341093",
3
+ "version": "19.36.1-alpha.7+859ce3f29",
4
4
  "description": "Common functionality and types for Uniform Canvas",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -38,7 +38,7 @@
38
38
  "pusher-js": "8.2.0"
39
39
  },
40
40
  "dependencies": {
41
- "@uniformdev/context": "19.35.3-alpha.82+4bc341093",
41
+ "@uniformdev/context": "19.36.1-alpha.7+859ce3f29",
42
42
  "immer": "9.0.21"
43
43
  },
44
44
  "files": [
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "4bc341093bc946900df2646fe53eca4bcddc693c"
50
+ "gitHead": "859ce3f295642c78d0308620f988a51ce933bd64"
51
51
  }