@webstudio-is/react-sdk 0.132.0 → 0.134.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/lib/index.js CHANGED
@@ -787,6 +787,7 @@ var decodeDataSourceVariable = (name) => {
787
787
  var generateExpression = ({
788
788
  expression,
789
789
  dataSources,
790
+ usedDataSources,
790
791
  scope
791
792
  }) => {
792
793
  return validateExpression(expression, {
@@ -798,6 +799,7 @@ var generateExpression = ({
798
799
  const depId = decodeDataSourceVariable(identifier);
799
800
  const dep = depId ? dataSources.get(depId) : void 0;
800
801
  if (dep) {
802
+ usedDataSources?.set(dep.id, dep);
801
803
  return scope.getName(dep.id, dep.name);
802
804
  }
803
805
  return identifier;
@@ -1335,7 +1337,8 @@ import { parseComponentName } from "@webstudio-is/sdk";
1335
1337
  var generateAction = ({
1336
1338
  scope,
1337
1339
  prop,
1338
- dataSources
1340
+ dataSources,
1341
+ usedDataSources
1339
1342
  }) => {
1340
1343
  const setters = /* @__PURE__ */ new Set();
1341
1344
  let args = [];
@@ -1352,6 +1355,7 @@ var generateAction = ({
1352
1355
  const depId = decodeDataSourceVariable(identifier);
1353
1356
  const dep = depId ? dataSources.get(depId) : void 0;
1354
1357
  if (dep) {
1358
+ usedDataSources.set(dep.id, dep);
1355
1359
  if (assignee) {
1356
1360
  setters.add(dep);
1357
1361
  }
@@ -1387,7 +1391,8 @@ var generateAction = ({
1387
1391
  var generatePropValue = ({
1388
1392
  scope,
1389
1393
  prop,
1390
- dataSources
1394
+ dataSources,
1395
+ usedDataSources
1391
1396
  }) => {
1392
1397
  if (prop.type === "asset" || prop.type === "page") {
1393
1398
  return;
@@ -1400,17 +1405,19 @@ var generatePropValue = ({
1400
1405
  if (dataSource === void 0) {
1401
1406
  return;
1402
1407
  }
1408
+ usedDataSources.set(dataSource.id, dataSource);
1403
1409
  return scope.getName(dataSource.id, dataSource.name);
1404
1410
  }
1405
1411
  if (prop.type === "expression") {
1406
1412
  return generateExpression({
1407
1413
  expression: prop.value,
1408
1414
  dataSources,
1415
+ usedDataSources,
1409
1416
  scope
1410
1417
  });
1411
1418
  }
1412
1419
  if (prop.type === "action") {
1413
- return generateAction({ scope, prop, dataSources });
1420
+ return generateAction({ scope, prop, dataSources, usedDataSources });
1414
1421
  }
1415
1422
  prop;
1416
1423
  };
@@ -1419,6 +1426,7 @@ var generateJsxElement = ({
1419
1426
  instance,
1420
1427
  props,
1421
1428
  dataSources,
1429
+ usedDataSources,
1422
1430
  indexesWithinAncestors,
1423
1431
  children,
1424
1432
  classesMap
@@ -1443,7 +1451,12 @@ ${indexAttribute}="${index}"`;
1443
1451
  if (prop.instanceId !== instance.id) {
1444
1452
  continue;
1445
1453
  }
1446
- const propValue = generatePropValue({ scope, prop, dataSources });
1454
+ const propValue = generatePropValue({
1455
+ scope,
1456
+ prop,
1457
+ dataSources,
1458
+ usedDataSources
1459
+ });
1447
1460
  if (prop.name === showAttribute) {
1448
1461
  if (propValue === "true") {
1449
1462
  continue;
@@ -1523,6 +1536,7 @@ var generateJsxChildren = ({
1523
1536
  instances,
1524
1537
  props,
1525
1538
  dataSources,
1539
+ usedDataSources,
1526
1540
  indexesWithinAncestors,
1527
1541
  classesMap
1528
1542
  }) => {
@@ -1538,6 +1552,7 @@ var generateJsxChildren = ({
1538
1552
  const expression = generateExpression({
1539
1553
  expression: child.value,
1540
1554
  dataSources,
1555
+ usedDataSources,
1541
1556
  scope
1542
1557
  });
1543
1558
  generatedChildren = `{${expression}}
@@ -1555,6 +1570,7 @@ var generateJsxChildren = ({
1555
1570
  instance,
1556
1571
  props,
1557
1572
  dataSources,
1573
+ usedDataSources,
1558
1574
  indexesWithinAncestors,
1559
1575
  classesMap,
1560
1576
  children: generateJsxChildren({
@@ -1564,6 +1580,7 @@ var generateJsxChildren = ({
1564
1580
  instances,
1565
1581
  props,
1566
1582
  dataSources,
1583
+ usedDataSources,
1567
1584
  indexesWithinAncestors
1568
1585
  })
1569
1586
  });
@@ -1588,17 +1605,36 @@ var generateWebstudioComponent = ({
1588
1605
  if (instance === void 0) {
1589
1606
  return "";
1590
1607
  }
1608
+ const usedDataSources = /* @__PURE__ */ new Map();
1609
+ const generatedJsx = generateJsxElement({
1610
+ scope,
1611
+ instance,
1612
+ props,
1613
+ dataSources,
1614
+ usedDataSources,
1615
+ indexesWithinAncestors,
1616
+ classesMap,
1617
+ children: generateJsxChildren({
1618
+ scope,
1619
+ children: instance.children,
1620
+ instances,
1621
+ props,
1622
+ dataSources,
1623
+ usedDataSources,
1624
+ indexesWithinAncestors,
1625
+ classesMap
1626
+ })
1627
+ });
1591
1628
  let generatedProps = "";
1592
1629
  if (parameters.length > 0) {
1593
1630
  let generatedPropsValue = "{ ";
1594
1631
  let generatedPropsType = "{ ";
1595
1632
  for (const parameter of parameters) {
1596
- const dataSource = dataSources.get(parameter.value);
1597
- if (dataSource === void 0) {
1598
- continue;
1633
+ const dataSource = usedDataSources.get(parameter.value);
1634
+ if (dataSource) {
1635
+ const valueName = scope.getName(dataSource.id, dataSource.name);
1636
+ generatedPropsValue += `${parameter.name}: ${valueName}, `;
1599
1637
  }
1600
- const valueName = scope.getName(dataSource.id, dataSource.name);
1601
- generatedPropsValue += `${parameter.name}: ${valueName}, `;
1602
1638
  generatedPropsType += `${parameter.name}: any; `;
1603
1639
  }
1604
1640
  generatedPropsValue += `}`;
@@ -1606,7 +1642,7 @@ var generateWebstudioComponent = ({
1606
1642
  generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
1607
1643
  }
1608
1644
  let generatedDataSources = "";
1609
- for (const dataSource of dataSources.values()) {
1645
+ for (const dataSource of usedDataSources.values()) {
1610
1646
  if (dataSource.type === "variable") {
1611
1647
  const valueName = scope.getName(dataSource.id, dataSource.name);
1612
1648
  const setterName = scope.getName(
@@ -1629,23 +1665,6 @@ var generateWebstudioComponent = ({
1629
1665
  `;
1630
1666
  }
1631
1667
  }
1632
- const generatedJsx = generateJsxElement({
1633
- scope,
1634
- instance,
1635
- props,
1636
- dataSources,
1637
- indexesWithinAncestors,
1638
- classesMap,
1639
- children: generateJsxChildren({
1640
- scope,
1641
- children: instance.children,
1642
- instances,
1643
- props,
1644
- dataSources,
1645
- indexesWithinAncestors,
1646
- classesMap
1647
- })
1648
- });
1649
1668
  let generatedComponent = "";
1650
1669
  generatedComponent += `const ${name} = (${generatedProps}) => {
1651
1670
  `;
@@ -1663,25 +1682,11 @@ var generateResourcesLoader = ({
1663
1682
  dataSources,
1664
1683
  resources
1665
1684
  }) => {
1666
- let generatedVariables = "";
1667
1685
  let generatedOutput = "";
1668
1686
  let generatedLoaders = "";
1669
1687
  let hasResources = false;
1688
+ const usedDataSources = /* @__PURE__ */ new Map();
1670
1689
  for (const dataSource of dataSources.values()) {
1671
- if (dataSource.type === "variable") {
1672
- const name = scope.getName(dataSource.id, dataSource.name);
1673
- const value = JSON.stringify(dataSource.value.value);
1674
- generatedVariables += `let ${name} = ${value}
1675
- `;
1676
- }
1677
- if (dataSource.type === "parameter") {
1678
- if (dataSource.id !== page.pathVariableId) {
1679
- continue;
1680
- }
1681
- const name = scope.getName(dataSource.id, dataSource.name);
1682
- generatedVariables += `const ${name} = _props.params
1683
- `;
1684
- }
1685
1690
  if (dataSource.type === "resource") {
1686
1691
  const resource = resources.get(dataSource.resourceId);
1687
1692
  if (resource === void 0) {
@@ -1700,6 +1705,7 @@ var generateResourcesLoader = ({
1700
1705
  const url = generateExpression({
1701
1706
  expression: resource.url,
1702
1707
  dataSources,
1708
+ usedDataSources,
1703
1709
  scope
1704
1710
  });
1705
1711
  generatedLoaders += `url: ${url},
@@ -1712,6 +1718,7 @@ var generateResourcesLoader = ({
1712
1718
  const value = generateExpression({
1713
1719
  expression: header.value,
1714
1720
  dataSources,
1721
+ usedDataSources,
1715
1722
  scope
1716
1723
  });
1717
1724
  generatedLoaders += `{ name: "${header.name}", value: ${value} },
@@ -1723,12 +1730,30 @@ var generateResourcesLoader = ({
1723
1730
  const body = generateExpression({
1724
1731
  expression: resource.body,
1725
1732
  dataSources,
1733
+ usedDataSources,
1726
1734
  scope
1727
1735
  });
1728
1736
  generatedLoaders += `body: ${body},
1729
1737
  `;
1730
1738
  }
1731
1739
  generatedLoaders += `}),
1740
+ `;
1741
+ }
1742
+ }
1743
+ let generatedVariables = "";
1744
+ for (const dataSource of usedDataSources.values()) {
1745
+ if (dataSource.type === "variable") {
1746
+ const name = scope.getName(dataSource.id, dataSource.name);
1747
+ const value = JSON.stringify(dataSource.value.value);
1748
+ generatedVariables += `let ${name} = ${value}
1749
+ `;
1750
+ }
1751
+ if (dataSource.type === "parameter") {
1752
+ if (dataSource.id !== page.pathParamsDataSourceId) {
1753
+ continue;
1754
+ }
1755
+ const name = scope.getName(dataSource.id, dataSource.name);
1756
+ generatedVariables += `const ${name} = _props.params
1732
1757
  `;
1733
1758
  }
1734
1759
  }
@@ -1741,8 +1766,8 @@ var generateResourcesLoader = ({
1741
1766
  `;
1742
1767
  generated += `export const loadResources = async (_props: { params: Params }) => {
1743
1768
  `;
1769
+ generated += generatedVariables;
1744
1770
  if (hasResources) {
1745
- generated += generatedVariables;
1746
1771
  generated += `const [
1747
1772
  `;
1748
1773
  generated += generatedOutput;
@@ -1763,25 +1788,33 @@ var generateResourcesLoader = ({
1763
1788
  };
1764
1789
 
1765
1790
  // src/page-meta-generator.ts
1791
+ import {
1792
+ createScope
1793
+ } from "@webstudio-is/sdk";
1766
1794
  var generatePageMeta = ({
1767
- scope,
1795
+ globalScope,
1768
1796
  page,
1769
1797
  dataSources
1770
1798
  }) => {
1799
+ const localScope = createScope(["params", "resources"]);
1800
+ const usedDataSources = /* @__PURE__ */ new Map();
1771
1801
  const titleExpression = generateExpression({
1772
1802
  expression: page.title,
1773
1803
  dataSources,
1774
- scope
1804
+ usedDataSources,
1805
+ scope: localScope
1775
1806
  });
1776
1807
  const descriptionExpression = generateExpression({
1777
1808
  expression: page.meta.description ?? "undefined",
1778
1809
  dataSources,
1779
- scope
1810
+ usedDataSources,
1811
+ scope: localScope
1780
1812
  });
1781
1813
  const excludePageFromSearchExpression = generateExpression({
1782
1814
  expression: page.meta.excludePageFromSearch ?? "undefined",
1783
1815
  dataSources,
1784
- scope
1816
+ usedDataSources,
1817
+ scope: localScope
1785
1818
  });
1786
1819
  const socialImageAssetIdExpression = JSON.stringify(
1787
1820
  page.meta.socialImageAssetId
@@ -1789,10 +1822,53 @@ var generatePageMeta = ({
1789
1822
  const socialImageUrlExpression = generateExpression({
1790
1823
  expression: page.meta.socialImageUrl ?? "undefined",
1791
1824
  dataSources,
1792
- scope
1825
+ usedDataSources,
1826
+ scope: localScope
1827
+ });
1828
+ const statusExpression = generateExpression({
1829
+ expression: page.meta.status ?? "undefined",
1830
+ dataSources,
1831
+ usedDataSources,
1832
+ scope: localScope
1833
+ });
1834
+ const redirectExpression = generateExpression({
1835
+ expression: page.meta.redirect ?? "undefined",
1836
+ dataSources,
1837
+ usedDataSources,
1838
+ scope: localScope
1793
1839
  });
1840
+ let customExpression = "";
1841
+ customExpression += `[
1842
+ `;
1843
+ for (const customMeta of page.meta.custom ?? []) {
1844
+ if (customMeta.property.trim().length === 0) {
1845
+ continue;
1846
+ }
1847
+ const propertyExpression = JSON.stringify(customMeta.property);
1848
+ const contentExpression = generateExpression({
1849
+ expression: customMeta.content,
1850
+ dataSources,
1851
+ usedDataSources,
1852
+ scope: localScope
1853
+ });
1854
+ customExpression += ` {
1855
+ `;
1856
+ customExpression += ` property: ${propertyExpression},
1857
+ `;
1858
+ customExpression += ` content: ${contentExpression},
1859
+ `;
1860
+ customExpression += ` },
1861
+ `;
1862
+ }
1863
+ customExpression += ` ]`;
1794
1864
  let generated = "";
1795
- generated += `export const getPageMeta = ({}: {
1865
+ generated += `export const getPageMeta = ({
1866
+ `;
1867
+ generated += ` params,
1868
+ `;
1869
+ generated += ` resources,
1870
+ `;
1871
+ generated += `}: {
1796
1872
  `;
1797
1873
  generated += ` params: Record<string, undefined | string>;
1798
1874
  `;
@@ -1800,6 +1876,33 @@ var generatePageMeta = ({
1800
1876
  `;
1801
1877
  generated += `}): PageMeta => {
1802
1878
  `;
1879
+ for (const dataSource of usedDataSources.values()) {
1880
+ if (dataSource.type === "variable") {
1881
+ const valueName = localScope.getName(dataSource.id, dataSource.name);
1882
+ const initialValueString = JSON.stringify(dataSource.value.value);
1883
+ generated += ` let ${valueName} = ${initialValueString}
1884
+ `;
1885
+ continue;
1886
+ }
1887
+ if (dataSource.type === "parameter") {
1888
+ if (dataSource.id === page.pathParamsDataSourceId) {
1889
+ const valueName = localScope.getName(dataSource.id, dataSource.name);
1890
+ generated += ` let ${valueName} = params
1891
+ `;
1892
+ }
1893
+ continue;
1894
+ }
1895
+ if (dataSource.type === "resource") {
1896
+ const valueName = localScope.getName(dataSource.id, dataSource.name);
1897
+ const resourceName = globalScope.getName(
1898
+ dataSource.resourceId,
1899
+ dataSource.name
1900
+ );
1901
+ generated += ` let ${valueName} = resources.${resourceName}
1902
+ `;
1903
+ continue;
1904
+ }
1905
+ }
1803
1906
  generated += ` return {
1804
1907
  `;
1805
1908
  generated += ` title: ${titleExpression},
@@ -1812,30 +1915,11 @@ var generatePageMeta = ({
1812
1915
  `;
1813
1916
  generated += ` socialImageUrl: ${socialImageUrlExpression},
1814
1917
  `;
1815
- generated += ` custom: [
1918
+ generated += ` status: ${statusExpression},
1816
1919
  `;
1817
- if (page.meta.custom) {
1818
- for (const customMeta of page.meta.custom) {
1819
- if (customMeta.property.trim().length === 0) {
1820
- continue;
1821
- }
1822
- const propertyExpression = JSON.stringify(customMeta.property);
1823
- const contentExpression = generateExpression({
1824
- scope,
1825
- dataSources,
1826
- expression: customMeta.content
1827
- });
1828
- generated += ` {
1829
- `;
1830
- generated += ` property: ${propertyExpression},
1831
- `;
1832
- generated += ` content: ${contentExpression},
1920
+ generated += ` redirect: ${redirectExpression},
1833
1921
  `;
1834
- generated += ` },
1835
- `;
1836
- }
1837
- }
1838
- generated += ` ],
1922
+ generated += ` custom: ${customExpression},
1839
1923
  `;
1840
1924
  generated += ` };
1841
1925
  `;
@@ -1,6 +1,6 @@
1
1
  import type { Instances, Instance, Props, Scope, DataSources, Prop } from "@webstudio-is/sdk";
2
2
  import type { IndexesWithinAncestors } from "./instance-utils";
3
- export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, classesMap, }: {
3
+ export declare const generateJsxElement: ({ scope, instance, props, dataSources, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
4
4
  scope: Scope;
5
5
  instance: Instance;
6
6
  props: Map<string, {
@@ -117,11 +117,44 @@ export declare const generateJsxElement: ({ scope, instance, props, dataSources,
117
117
  resourceId: string;
118
118
  scopeInstanceId?: string | undefined;
119
119
  }>;
120
+ usedDataSources: Map<string, {
121
+ value: {
122
+ value: number;
123
+ type: "number";
124
+ } | {
125
+ value: string;
126
+ type: "string";
127
+ } | {
128
+ value: boolean;
129
+ type: "boolean";
130
+ } | {
131
+ value: string[];
132
+ type: "string[]";
133
+ } | {
134
+ type: "json";
135
+ value?: unknown;
136
+ };
137
+ type: "variable";
138
+ name: string;
139
+ id: string;
140
+ scopeInstanceId?: string | undefined;
141
+ } | {
142
+ type: "parameter";
143
+ name: string;
144
+ id: string;
145
+ scopeInstanceId?: string | undefined;
146
+ } | {
147
+ type: "resource";
148
+ name: string;
149
+ id: string;
150
+ resourceId: string;
151
+ scopeInstanceId?: string | undefined;
152
+ }>;
120
153
  indexesWithinAncestors: IndexesWithinAncestors;
121
154
  children: string;
122
155
  classesMap?: Map<string, string[]> | undefined;
123
156
  }) => string;
124
- export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
157
+ export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, usedDataSources, indexesWithinAncestors, classesMap, }: {
125
158
  scope: Scope;
126
159
  children: Instance["children"];
127
160
  instances: Map<string, {
@@ -254,6 +287,39 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
254
287
  resourceId: string;
255
288
  scopeInstanceId?: string | undefined;
256
289
  }>;
290
+ usedDataSources: Map<string, {
291
+ value: {
292
+ value: number;
293
+ type: "number";
294
+ } | {
295
+ value: string;
296
+ type: "string";
297
+ } | {
298
+ value: boolean;
299
+ type: "boolean";
300
+ } | {
301
+ value: string[];
302
+ type: "string[]";
303
+ } | {
304
+ type: "json";
305
+ value?: unknown;
306
+ };
307
+ type: "variable";
308
+ name: string;
309
+ id: string;
310
+ scopeInstanceId?: string | undefined;
311
+ } | {
312
+ type: "parameter";
313
+ name: string;
314
+ id: string;
315
+ scopeInstanceId?: string | undefined;
316
+ } | {
317
+ type: "resource";
318
+ name: string;
319
+ id: string;
320
+ resourceId: string;
321
+ scopeInstanceId?: string | undefined;
322
+ }>;
257
323
  indexesWithinAncestors: IndexesWithinAncestors;
258
324
  classesMap?: Map<string, string[]> | undefined;
259
325
  }) => string;
@@ -14,7 +14,7 @@ export declare const validateExpression: (code: string, options?: {
14
14
  }) => string;
15
15
  export declare const encodeDataSourceVariable: (id: string) => string;
16
16
  export declare const decodeDataSourceVariable: (name: string) => string | undefined;
17
- export declare const generateExpression: ({ expression, dataSources, scope, }: {
17
+ export declare const generateExpression: ({ expression, dataSources, usedDataSources, scope, }: {
18
18
  expression: string;
19
19
  dataSources: Map<string, {
20
20
  value: {
@@ -49,6 +49,39 @@ export declare const generateExpression: ({ expression, dataSources, scope, }: {
49
49
  resourceId: string;
50
50
  scopeInstanceId?: string | undefined;
51
51
  }>;
52
+ usedDataSources: Map<string, {
53
+ value: {
54
+ value: number;
55
+ type: "number";
56
+ } | {
57
+ value: string;
58
+ type: "string";
59
+ } | {
60
+ value: boolean;
61
+ type: "boolean";
62
+ } | {
63
+ value: string[];
64
+ type: "string[]";
65
+ } | {
66
+ type: "json";
67
+ value?: unknown;
68
+ };
69
+ type: "variable";
70
+ name: string;
71
+ id: string;
72
+ scopeInstanceId?: string | undefined;
73
+ } | {
74
+ type: "parameter";
75
+ name: string;
76
+ id: string;
77
+ scopeInstanceId?: string | undefined;
78
+ } | {
79
+ type: "resource";
80
+ name: string;
81
+ id: string;
82
+ resourceId: string;
83
+ scopeInstanceId?: string | undefined;
84
+ }>;
52
85
  scope: Scope;
53
86
  }) => string;
54
87
  export declare const executeExpression: (expression: undefined | string) => any;
@@ -1,17 +1,19 @@
1
- import type { Asset, DataSources, Page, Scope } from "@webstudio-is/sdk";
1
+ import { type Asset, type DataSources, type Page, type Scope } from "@webstudio-is/sdk";
2
2
  export type PageMeta = {
3
3
  title: string;
4
4
  description?: string;
5
5
  excludePageFromSearch?: boolean;
6
6
  socialImageAssetId?: Asset["id"];
7
7
  socialImageUrl?: string;
8
+ status?: number;
9
+ redirect?: string;
8
10
  custom: Array<{
9
11
  property: string;
10
12
  content: string;
11
13
  }>;
12
14
  };
13
- export declare const generatePageMeta: ({ scope, page, dataSources, }: {
14
- scope: Scope;
15
+ export declare const generatePageMeta: ({ globalScope, page, dataSources, }: {
16
+ globalScope: Scope;
15
17
  page: Page;
16
18
  dataSources: Map<string, {
17
19
  value: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.132.0",
3
+ "version": "0.134.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -33,11 +33,11 @@
33
33
  "jsep": "^1.3.8",
34
34
  "nanoid": "^5.0.1",
35
35
  "title-case": "^4.1.0",
36
- "@webstudio-is/fonts": "0.132.0",
37
- "@webstudio-is/css-engine": "0.132.0",
38
- "@webstudio-is/image": "0.132.0",
39
- "@webstudio-is/icons": "^0.132.0",
40
- "@webstudio-is/sdk": "0.132.0"
36
+ "@webstudio-is/css-engine": "0.134.0",
37
+ "@webstudio-is/image": "0.134.0",
38
+ "@webstudio-is/sdk": "0.134.0",
39
+ "@webstudio-is/fonts": "0.134.0",
40
+ "@webstudio-is/icons": "^0.134.0"
41
41
  },
42
42
  "exports": {
43
43
  ".": {