@revisium/client 0.1.0 → 0.2.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.cjs CHANGED
@@ -668,16 +668,16 @@ var sdk_gen_exports = /* @__PURE__ */ __exportAll({
668
668
  createEndpoint: () => createEndpoint,
669
669
  createProject: () => createProject,
670
670
  createRevision: () => createRevision,
671
- createRow: () => createRow,
672
- createRows: () => createRows,
673
- createTable: () => createTable,
671
+ createRow: () => createRow$1,
672
+ createRows: () => createRows$1,
673
+ createTable: () => createTable$1,
674
674
  createUser: () => createUser,
675
675
  deleteBranch: () => deleteBranch,
676
676
  deleteEndpoint: () => deleteEndpoint,
677
677
  deleteProject: () => deleteProject,
678
- deleteRow: () => deleteRow,
679
- deleteRows: () => deleteRows,
680
- deleteTable: () => deleteTable,
678
+ deleteRow: () => deleteRow$1,
679
+ deleteRows: () => deleteRows$1,
680
+ deleteTable: () => deleteTable$1,
681
681
  draftRevision: () => draftRevision,
682
682
  endpointRelatives: () => endpointRelatives,
683
683
  endpoints: () => endpoints,
@@ -689,16 +689,16 @@ var sdk_gen_exports = /* @__PURE__ */ __exportAll({
689
689
  migrations: () => migrations,
690
690
  parentBranch: () => parentBranch,
691
691
  parentRevision: () => parentRevision,
692
- patchRow: () => patchRow,
692
+ patchRow: () => patchRow$1,
693
693
  patchRows: () => patchRows,
694
694
  project: () => project,
695
695
  projects: () => projects,
696
696
  readiness: () => readiness,
697
697
  removeUserFromOrganization: () => removeUserFromOrganization,
698
698
  removeUserFromProject: () => removeUserFromProject,
699
- renameRow: () => renameRow,
700
- renameTable: () => renameTable,
701
- revertChanges: () => revertChanges,
699
+ renameRow: () => renameRow$1,
700
+ renameTable: () => renameTable$1,
701
+ revertChanges: () => revertChanges$1,
702
702
  revision: () => revision,
703
703
  revisionChanges: () => revisionChanges,
704
704
  revisions: () => revisions,
@@ -722,9 +722,9 @@ var sdk_gen_exports = /* @__PURE__ */ __exportAll({
722
722
  tables: () => tables,
723
723
  updatePassword: () => updatePassword,
724
724
  updateProject: () => updateProject,
725
- updateRow: () => updateRow,
726
- updateRows: () => updateRows,
727
- updateTable: () => updateTable,
725
+ updateRow: () => updateRow$1,
726
+ updateRows: () => updateRows$1,
727
+ updateTable: () => updateTable$1,
728
728
  uploadFile: () => uploadFile,
729
729
  usersOrganization: () => usersOrganization,
730
730
  usersProject: () => usersProject
@@ -1054,7 +1054,7 @@ const createRevision = (options) => (options.client ?? client).post({
1054
1054
  /**
1055
1055
  * Discard all uncommitted changes
1056
1056
  */
1057
- const revertChanges = (options) => (options.client ?? client).post({
1057
+ const revertChanges$1 = (options) => (options.client ?? client).post({
1058
1058
  security: [{
1059
1059
  scheme: "bearer",
1060
1060
  type: "http"
@@ -1135,7 +1135,7 @@ const tables = (options) => (options.client ?? client).get({
1135
1135
  /**
1136
1136
  * Create a new table
1137
1137
  */
1138
- const createTable = (options) => (options.client ?? client).post({
1138
+ const createTable$1 = (options) => (options.client ?? client).post({
1139
1139
  security: [{
1140
1140
  scheme: "bearer",
1141
1141
  type: "http"
@@ -1235,7 +1235,7 @@ const applyMigrations = (options) => (options.client ?? client).post({
1235
1235
  /**
1236
1236
  * Delete a table
1237
1237
  */
1238
- const deleteTable = (options) => (options.client ?? client).delete({
1238
+ const deleteTable$1 = (options) => (options.client ?? client).delete({
1239
1239
  security: [{
1240
1240
  scheme: "bearer",
1241
1241
  type: "http"
@@ -1257,7 +1257,7 @@ const table = (options) => (options.client ?? client).get({
1257
1257
  /**
1258
1258
  * Update table schema using JSON Patch
1259
1259
  */
1260
- const updateTable = (options) => (options.client ?? client).patch({
1260
+ const updateTable$1 = (options) => (options.client ?? client).patch({
1261
1261
  security: [{
1262
1262
  scheme: "bearer",
1263
1263
  type: "http"
@@ -1283,7 +1283,7 @@ const tableCountRows = (options) => (options.client ?? client).get({
1283
1283
  /**
1284
1284
  * Delete multiple rows
1285
1285
  */
1286
- const deleteRows = (options) => (options.client ?? client).delete({
1286
+ const deleteRows$1 = (options) => (options.client ?? client).delete({
1287
1287
  security: [{
1288
1288
  scheme: "bearer",
1289
1289
  type: "http"
@@ -1313,7 +1313,7 @@ const rows = (options) => (options.client ?? client).post({
1313
1313
  /**
1314
1314
  * Create a new row
1315
1315
  */
1316
- const createRow = (options) => (options.client ?? client).post({
1316
+ const createRow$1 = (options) => (options.client ?? client).post({
1317
1317
  security: [{
1318
1318
  scheme: "bearer",
1319
1319
  type: "http"
@@ -1328,7 +1328,7 @@ const createRow = (options) => (options.client ?? client).post({
1328
1328
  /**
1329
1329
  * Create multiple rows
1330
1330
  */
1331
- const createRows = (options) => (options.client ?? client).post({
1331
+ const createRows$1 = (options) => (options.client ?? client).post({
1332
1332
  security: [{
1333
1333
  scheme: "bearer",
1334
1334
  type: "http"
@@ -1343,7 +1343,7 @@ const createRows = (options) => (options.client ?? client).post({
1343
1343
  /**
1344
1344
  * Replace data for multiple rows
1345
1345
  */
1346
- const updateRows = (options) => (options.client ?? client).put({
1346
+ const updateRows$1 = (options) => (options.client ?? client).put({
1347
1347
  security: [{
1348
1348
  scheme: "bearer",
1349
1349
  type: "http"
@@ -1428,7 +1428,7 @@ const tableForeignKeysTo = (options) => (options.client ?? client).get({
1428
1428
  /**
1429
1429
  * Rename a table
1430
1430
  */
1431
- const renameTable = (options) => (options.client ?? client).patch({
1431
+ const renameTable$1 = (options) => (options.client ?? client).patch({
1432
1432
  security: [{
1433
1433
  scheme: "bearer",
1434
1434
  type: "http"
@@ -1443,7 +1443,7 @@ const renameTable = (options) => (options.client ?? client).patch({
1443
1443
  /**
1444
1444
  * Delete a row
1445
1445
  */
1446
- const deleteRow = (options) => (options.client ?? client).delete({
1446
+ const deleteRow$1 = (options) => (options.client ?? client).delete({
1447
1447
  security: [{
1448
1448
  scheme: "bearer",
1449
1449
  type: "http"
@@ -1465,7 +1465,7 @@ const row = (options) => (options.client ?? client).get({
1465
1465
  /**
1466
1466
  * Patch row data using JSON Patch
1467
1467
  */
1468
- const patchRow = (options) => (options.client ?? client).patch({
1468
+ const patchRow$1 = (options) => (options.client ?? client).patch({
1469
1469
  security: [{
1470
1470
  scheme: "bearer",
1471
1471
  type: "http"
@@ -1480,7 +1480,7 @@ const patchRow = (options) => (options.client ?? client).patch({
1480
1480
  /**
1481
1481
  * Replace row data
1482
1482
  */
1483
- const updateRow = (options) => (options.client ?? client).put({
1483
+ const updateRow$1 = (options) => (options.client ?? client).put({
1484
1484
  security: [{
1485
1485
  scheme: "bearer",
1486
1486
  type: "http"
@@ -1539,7 +1539,7 @@ const rowForeignKeysTo = (options) => (options.client ?? client).get({
1539
1539
  /**
1540
1540
  * Rename a row
1541
1541
  */
1542
- const renameRow = (options) => (options.client ?? client).patch({
1542
+ const renameRow$1 = (options) => (options.client ?? client).patch({
1543
1543
  security: [{
1544
1544
  scheme: "bearer",
1545
1545
  type: "http"
@@ -1605,6 +1605,451 @@ const liveness = (options) => (options?.client ?? client).get({
1605
1605
  ...options
1606
1606
  });
1607
1607
 
1608
+ //#endregion
1609
+ //#region src/data-operations.ts
1610
+ function unwrap(result) {
1611
+ if (result.error) {
1612
+ const err = result.error;
1613
+ throw new Error(err.message ?? `API error: ${JSON.stringify(result.error)}`);
1614
+ }
1615
+ return result.data;
1616
+ }
1617
+ function assertDraft(ctx) {
1618
+ assertContext(ctx);
1619
+ if (!ctx.isDraft) throw new Error("Mutations are only allowed in draft revision. Use setContext({ revision: \"draft\" }).");
1620
+ }
1621
+ function assertContext(ctx) {
1622
+ if (!ctx.branch.organizationId) throw new Error("Context not set. Call setContext() first.");
1623
+ }
1624
+ async function getTables(ctx, options) {
1625
+ const revisionId = await ctx.getRevisionId();
1626
+ return unwrap(await tables({
1627
+ client: ctx.client,
1628
+ path: { revisionId },
1629
+ query: {
1630
+ first: options?.first ?? 100,
1631
+ after: options?.after
1632
+ }
1633
+ }));
1634
+ }
1635
+ async function getTable(ctx, tableId) {
1636
+ const revisionId = await ctx.getRevisionId();
1637
+ return unwrap(await table({
1638
+ client: ctx.client,
1639
+ path: {
1640
+ revisionId,
1641
+ tableId
1642
+ }
1643
+ }));
1644
+ }
1645
+ async function getTableSchema(ctx, tableId) {
1646
+ const revisionId = await ctx.getRevisionId();
1647
+ return unwrap(await tableSchema({
1648
+ client: ctx.client,
1649
+ path: {
1650
+ revisionId,
1651
+ tableId
1652
+ }
1653
+ }));
1654
+ }
1655
+ async function getRows(ctx, tableId, options) {
1656
+ const revisionId = await ctx.getRevisionId();
1657
+ return unwrap(await rows({
1658
+ client: ctx.client,
1659
+ path: {
1660
+ revisionId,
1661
+ tableId
1662
+ },
1663
+ body: options ?? { first: 100 }
1664
+ }));
1665
+ }
1666
+ async function getRow(ctx, tableId, rowId) {
1667
+ const revisionId = await ctx.getRevisionId();
1668
+ return unwrap(await row({
1669
+ client: ctx.client,
1670
+ path: {
1671
+ revisionId,
1672
+ tableId,
1673
+ rowId
1674
+ }
1675
+ }));
1676
+ }
1677
+ async function getChanges(ctx) {
1678
+ const revisionId = await ctx.getRevisionId();
1679
+ return unwrap(await revisionChanges({
1680
+ client: ctx.client,
1681
+ path: { revisionId }
1682
+ }));
1683
+ }
1684
+ async function createTable(ctx, tableId, schema) {
1685
+ assertDraft(ctx);
1686
+ const revisionId = await ctx.getRevisionId();
1687
+ return unwrap(await createTable$1({
1688
+ client: ctx.client,
1689
+ path: { revisionId },
1690
+ body: {
1691
+ tableId,
1692
+ schema
1693
+ }
1694
+ }));
1695
+ }
1696
+ async function updateTable(ctx, tableId, patches) {
1697
+ assertDraft(ctx);
1698
+ const revisionId = await ctx.getRevisionId();
1699
+ return unwrap(await updateTable$1({
1700
+ client: ctx.client,
1701
+ path: {
1702
+ revisionId,
1703
+ tableId
1704
+ },
1705
+ body: { patches }
1706
+ }));
1707
+ }
1708
+ async function deleteTable(ctx, tableId) {
1709
+ assertDraft(ctx);
1710
+ const revisionId = await ctx.getRevisionId();
1711
+ unwrap(await deleteTable$1({
1712
+ client: ctx.client,
1713
+ path: {
1714
+ revisionId,
1715
+ tableId
1716
+ }
1717
+ }));
1718
+ }
1719
+ async function renameTable(ctx, tableId, nextTableId) {
1720
+ assertDraft(ctx);
1721
+ const revisionId = await ctx.getRevisionId();
1722
+ return unwrap(await renameTable$1({
1723
+ client: ctx.client,
1724
+ path: {
1725
+ revisionId,
1726
+ tableId
1727
+ },
1728
+ body: { nextTableId }
1729
+ }));
1730
+ }
1731
+ async function createRow(ctx, tableId, rowId, data) {
1732
+ assertDraft(ctx);
1733
+ const revisionId = await ctx.getRevisionId();
1734
+ return unwrap(await createRow$1({
1735
+ client: ctx.client,
1736
+ path: {
1737
+ revisionId,
1738
+ tableId
1739
+ },
1740
+ body: {
1741
+ rowId,
1742
+ data
1743
+ }
1744
+ }));
1745
+ }
1746
+ async function createRows(ctx, tableId, rows) {
1747
+ assertDraft(ctx);
1748
+ const revisionId = await ctx.getRevisionId();
1749
+ return unwrap(await createRows$1({
1750
+ client: ctx.client,
1751
+ path: {
1752
+ revisionId,
1753
+ tableId
1754
+ },
1755
+ body: { rows: rows.map((r) => ({
1756
+ rowId: r.rowId,
1757
+ data: r.data
1758
+ })) }
1759
+ }));
1760
+ }
1761
+ async function updateRow(ctx, tableId, rowId, data) {
1762
+ assertDraft(ctx);
1763
+ const revisionId = await ctx.getRevisionId();
1764
+ return unwrap(await updateRow$1({
1765
+ client: ctx.client,
1766
+ path: {
1767
+ revisionId,
1768
+ tableId,
1769
+ rowId
1770
+ },
1771
+ body: { data }
1772
+ }));
1773
+ }
1774
+ async function updateRows(ctx, tableId, rows) {
1775
+ assertDraft(ctx);
1776
+ const revisionId = await ctx.getRevisionId();
1777
+ return unwrap(await updateRows$1({
1778
+ client: ctx.client,
1779
+ path: {
1780
+ revisionId,
1781
+ tableId
1782
+ },
1783
+ body: { rows: rows.map((r) => ({
1784
+ rowId: r.rowId,
1785
+ data: r.data
1786
+ })) }
1787
+ }));
1788
+ }
1789
+ async function patchRow(ctx, tableId, rowId, patches) {
1790
+ assertDraft(ctx);
1791
+ const revisionId = await ctx.getRevisionId();
1792
+ return unwrap(await patchRow$1({
1793
+ client: ctx.client,
1794
+ path: {
1795
+ revisionId,
1796
+ tableId,
1797
+ rowId
1798
+ },
1799
+ body: { patches }
1800
+ }));
1801
+ }
1802
+ async function deleteRow(ctx, tableId, rowId) {
1803
+ assertDraft(ctx);
1804
+ const revisionId = await ctx.getRevisionId();
1805
+ unwrap(await deleteRow$1({
1806
+ client: ctx.client,
1807
+ path: {
1808
+ revisionId,
1809
+ tableId,
1810
+ rowId
1811
+ }
1812
+ }));
1813
+ }
1814
+ async function deleteRows(ctx, tableId, rowIds) {
1815
+ assertDraft(ctx);
1816
+ const revisionId = await ctx.getRevisionId();
1817
+ unwrap(await deleteRows$1({
1818
+ client: ctx.client,
1819
+ path: {
1820
+ revisionId,
1821
+ tableId
1822
+ },
1823
+ body: { rowIds }
1824
+ }));
1825
+ }
1826
+ async function renameRow(ctx, tableId, rowId, nextRowId) {
1827
+ assertDraft(ctx);
1828
+ const revisionId = await ctx.getRevisionId();
1829
+ return unwrap(await renameRow$1({
1830
+ client: ctx.client,
1831
+ path: {
1832
+ revisionId,
1833
+ tableId,
1834
+ rowId
1835
+ },
1836
+ body: { nextRowId }
1837
+ }));
1838
+ }
1839
+ async function commit(ctx, comment) {
1840
+ assertDraft(ctx);
1841
+ return unwrap(await createRevision({
1842
+ client: ctx.client,
1843
+ path: {
1844
+ organizationId: ctx.branch.organizationId,
1845
+ projectName: ctx.branch.projectName,
1846
+ branchName: ctx.branch.branchName
1847
+ },
1848
+ body: { comment }
1849
+ }));
1850
+ }
1851
+ async function revertChanges(ctx) {
1852
+ assertDraft(ctx);
1853
+ unwrap(await revertChanges$1({
1854
+ client: ctx.client,
1855
+ path: {
1856
+ organizationId: ctx.branch.organizationId,
1857
+ projectName: ctx.branch.projectName,
1858
+ branchName: ctx.branch.branchName
1859
+ }
1860
+ }));
1861
+ }
1862
+ async function fetchDraftRevisionId(client, branch) {
1863
+ return unwrap(await draftRevision({
1864
+ client,
1865
+ path: {
1866
+ organizationId: branch.organizationId,
1867
+ projectName: branch.projectName,
1868
+ branchName: branch.branchName
1869
+ }
1870
+ })).id;
1871
+ }
1872
+ async function fetchHeadRevisionId(client, branch) {
1873
+ return unwrap(await headRevision({
1874
+ client,
1875
+ path: {
1876
+ organizationId: branch.organizationId,
1877
+ projectName: branch.projectName,
1878
+ branchName: branch.branchName
1879
+ }
1880
+ })).id;
1881
+ }
1882
+ async function validateRevisionId(client, revisionId) {
1883
+ unwrap(await revision({
1884
+ client,
1885
+ path: { revisionId }
1886
+ }));
1887
+ }
1888
+
1889
+ //#endregion
1890
+ //#region src/revisium-scope.ts
1891
+ var RevisiumScope = class {
1892
+ _client;
1893
+ _branch;
1894
+ _revisionMode;
1895
+ _owner;
1896
+ _revisionId;
1897
+ _isDraft;
1898
+ _stale = false;
1899
+ _disposed = false;
1900
+ _refreshPromise = null;
1901
+ constructor(init) {
1902
+ this._client = init.client;
1903
+ this._branch = init.branch;
1904
+ this._revisionId = init.revisionId;
1905
+ this._isDraft = init.isDraft;
1906
+ this._revisionMode = init.revisionMode;
1907
+ this._owner = init.owner;
1908
+ }
1909
+ get organizationId() {
1910
+ return this._branch.organizationId;
1911
+ }
1912
+ get projectName() {
1913
+ return this._branch.projectName;
1914
+ }
1915
+ get branchName() {
1916
+ return this._branch.branchName;
1917
+ }
1918
+ get revisionId() {
1919
+ return this._revisionId;
1920
+ }
1921
+ get isDraft() {
1922
+ return this._isDraft;
1923
+ }
1924
+ get isStale() {
1925
+ return this._stale;
1926
+ }
1927
+ get isDisposed() {
1928
+ return this._disposed;
1929
+ }
1930
+ get client() {
1931
+ return this._client;
1932
+ }
1933
+ markStale() {
1934
+ if (this._revisionMode === "explicit") return;
1935
+ this._stale = true;
1936
+ }
1937
+ async refresh() {
1938
+ this.assertNotDisposed();
1939
+ if (this._revisionMode === "explicit") return;
1940
+ this._revisionId = await this.fetchRevisionId();
1941
+ this._stale = false;
1942
+ }
1943
+ dispose() {
1944
+ if (this._disposed) return;
1945
+ this._disposed = true;
1946
+ this._owner.unregisterScope(this);
1947
+ }
1948
+ assertNotDisposed() {
1949
+ if (this._disposed) throw new Error("Scope has been disposed.");
1950
+ }
1951
+ get _branchKey() {
1952
+ return `${this._branch.organizationId}/${this._branch.projectName}/${this._branch.branchName}`;
1953
+ }
1954
+ async fetchRevisionId() {
1955
+ if (this._revisionMode === "draft") return fetchDraftRevisionId(this._client, this._branch);
1956
+ return fetchHeadRevisionId(this._client, this._branch);
1957
+ }
1958
+ async getRevisionId() {
1959
+ this.assertNotDisposed();
1960
+ if (this._stale) {
1961
+ this._refreshPromise ??= this.fetchRevisionId().then((id) => {
1962
+ this._revisionId = id;
1963
+ this._stale = false;
1964
+ this._refreshPromise = null;
1965
+ return id;
1966
+ }, (err) => {
1967
+ this._refreshPromise = null;
1968
+ throw err;
1969
+ });
1970
+ return this._refreshPromise;
1971
+ }
1972
+ return this._revisionId;
1973
+ }
1974
+ get _scopeContext() {
1975
+ return {
1976
+ client: this._client,
1977
+ branch: this._branch,
1978
+ isDraft: this._isDraft,
1979
+ getRevisionId: () => this.getRevisionId()
1980
+ };
1981
+ }
1982
+ async getTables(options) {
1983
+ return getTables(this._scopeContext, options);
1984
+ }
1985
+ async getTable(tableId) {
1986
+ return getTable(this._scopeContext, tableId);
1987
+ }
1988
+ async getTableSchema(tableId) {
1989
+ return getTableSchema(this._scopeContext, tableId);
1990
+ }
1991
+ async getRows(tableId, options) {
1992
+ return getRows(this._scopeContext, tableId, options);
1993
+ }
1994
+ async getRow(tableId, rowId) {
1995
+ return getRow(this._scopeContext, tableId, rowId);
1996
+ }
1997
+ async getChanges() {
1998
+ return getChanges(this._scopeContext);
1999
+ }
2000
+ async createTable(tableId, schema) {
2001
+ return createTable(this._scopeContext, tableId, schema);
2002
+ }
2003
+ async updateTable(tableId, patches) {
2004
+ return updateTable(this._scopeContext, tableId, patches);
2005
+ }
2006
+ async deleteTable(tableId) {
2007
+ return deleteTable(this._scopeContext, tableId);
2008
+ }
2009
+ async renameTable(tableId, nextTableId) {
2010
+ return renameTable(this._scopeContext, tableId, nextTableId);
2011
+ }
2012
+ async createRow(tableId, rowId, data) {
2013
+ return createRow(this._scopeContext, tableId, rowId, data);
2014
+ }
2015
+ async createRows(tableId, rows) {
2016
+ return createRows(this._scopeContext, tableId, rows);
2017
+ }
2018
+ async updateRow(tableId, rowId, data) {
2019
+ return updateRow(this._scopeContext, tableId, rowId, data);
2020
+ }
2021
+ async updateRows(tableId, rows) {
2022
+ return updateRows(this._scopeContext, tableId, rows);
2023
+ }
2024
+ async patchRow(tableId, rowId, patches) {
2025
+ return patchRow(this._scopeContext, tableId, rowId, patches);
2026
+ }
2027
+ async deleteRow(tableId, rowId) {
2028
+ return deleteRow(this._scopeContext, tableId, rowId);
2029
+ }
2030
+ async deleteRows(tableId, rowIds) {
2031
+ return deleteRows(this._scopeContext, tableId, rowIds);
2032
+ }
2033
+ async renameRow(tableId, rowId, nextRowId) {
2034
+ return renameRow(this._scopeContext, tableId, rowId, nextRowId);
2035
+ }
2036
+ async commit(comment) {
2037
+ this.assertNotDisposed();
2038
+ const data = await commit(this._scopeContext, comment);
2039
+ this._revisionId = await fetchDraftRevisionId(this._client, this._branch);
2040
+ this._stale = false;
2041
+ this._owner.notifyBranchChanged(this._branchKey, this);
2042
+ return data;
2043
+ }
2044
+ async revertChanges() {
2045
+ this.assertNotDisposed();
2046
+ await revertChanges(this._scopeContext);
2047
+ this._revisionId = await fetchDraftRevisionId(this._client, this._branch);
2048
+ this._stale = false;
2049
+ this._owner.notifyBranchChanged(this._branchKey, this);
2050
+ }
2051
+ };
2052
+
1608
2053
  //#endregion
1609
2054
  //#region src/revisium-client.ts
1610
2055
  var RevisiumClient = class {
@@ -1616,6 +2061,7 @@ var RevisiumClient = class {
1616
2061
  _revisionId = null;
1617
2062
  _isDraft = false;
1618
2063
  _isAuthenticated = false;
2064
+ _scopes = /* @__PURE__ */ new Map();
1619
2065
  constructor(options) {
1620
2066
  const url = options.baseUrl;
1621
2067
  this._baseUrl = url.endsWith("/") ? url.slice(0, -1) : url;
@@ -1653,7 +2099,7 @@ var RevisiumClient = class {
1653
2099
  password
1654
2100
  }
1655
2101
  });
1656
- const data = this.unwrap(result);
2102
+ const data = unwrap(result);
1657
2103
  this._client.setConfig({ auth: data.accessToken });
1658
2104
  this._isAuthenticated = true;
1659
2105
  }
@@ -1662,316 +2108,177 @@ var RevisiumClient = class {
1662
2108
  this._isAuthenticated = true;
1663
2109
  }
1664
2110
  async setContext(options) {
1665
- const { organizationId, projectName, branchName = "master", revision: revision$1 = "draft" } = options;
2111
+ const { organizationId, projectName, branchName = "master", revision = "draft" } = options;
1666
2112
  this._organizationId = organizationId;
1667
2113
  this._projectName = projectName;
1668
2114
  this._branchName = branchName;
1669
- const branchPath = {
2115
+ const branch = {
1670
2116
  organizationId,
1671
2117
  projectName,
1672
2118
  branchName
1673
2119
  };
1674
- if (revision$1 === "draft") {
1675
- const result = await draftRevision({
1676
- client: this._client,
1677
- path: branchPath
1678
- });
1679
- this._revisionId = this.unwrap(result).id;
2120
+ if (revision === "draft") {
2121
+ this._revisionId = await fetchDraftRevisionId(this._client, branch);
1680
2122
  this._isDraft = true;
1681
- } else if (revision$1 === "head") {
1682
- const result = await headRevision({
1683
- client: this._client,
1684
- path: branchPath
1685
- });
1686
- this._revisionId = this.unwrap(result).id;
2123
+ } else if (revision === "head") {
2124
+ this._revisionId = await fetchHeadRevisionId(this._client, branch);
1687
2125
  this._isDraft = false;
1688
2126
  } else {
1689
- const result = await revision({
1690
- client: this._client,
1691
- path: { revisionId: revision$1 }
1692
- });
1693
- this.unwrap(result);
1694
- this._revisionId = revision$1;
2127
+ await validateRevisionId(this._client, revision);
2128
+ this._revisionId = revision;
1695
2129
  this._isDraft = false;
1696
2130
  }
1697
2131
  }
1698
- async getTables(options) {
1699
- this.assertContext();
1700
- const result = await tables({
2132
+ async withContext(options) {
2133
+ const { organizationId, projectName, branchName = "master", revision = "draft" } = options;
2134
+ const branch = {
2135
+ organizationId,
2136
+ projectName,
2137
+ branchName
2138
+ };
2139
+ let revisionId;
2140
+ let isDraft;
2141
+ let revisionMode;
2142
+ if (revision === "draft") {
2143
+ revisionId = await fetchDraftRevisionId(this._client, branch);
2144
+ isDraft = true;
2145
+ revisionMode = "draft";
2146
+ } else if (revision === "head") {
2147
+ revisionId = await fetchHeadRevisionId(this._client, branch);
2148
+ isDraft = false;
2149
+ revisionMode = "head";
2150
+ } else {
2151
+ await validateRevisionId(this._client, revision);
2152
+ revisionId = revision;
2153
+ isDraft = false;
2154
+ revisionMode = "explicit";
2155
+ }
2156
+ const scope = new RevisiumScope({
1701
2157
  client: this._client,
1702
- path: { revisionId: this._revisionId },
1703
- query: {
1704
- first: options?.first ?? 100,
1705
- after: options?.after
1706
- }
2158
+ branch,
2159
+ revisionId,
2160
+ isDraft,
2161
+ revisionMode,
2162
+ owner: this
1707
2163
  });
1708
- return this.unwrap(result);
2164
+ const branchKey = `${organizationId}/${projectName}/${branchName}`;
2165
+ let scopeSet = this._scopes.get(branchKey);
2166
+ if (!scopeSet) {
2167
+ scopeSet = /* @__PURE__ */ new Set();
2168
+ this._scopes.set(branchKey, scopeSet);
2169
+ }
2170
+ scopeSet.add(scope);
2171
+ return scope;
2172
+ }
2173
+ notifyBranchChanged(branchKey, excludeScope) {
2174
+ const scopeSet = this._scopes.get(branchKey);
2175
+ if (!scopeSet) return;
2176
+ for (const scope of scopeSet) if (scope !== excludeScope) scope.markStale();
2177
+ }
2178
+ unregisterScope(scope) {
2179
+ for (const [key, scopeSet] of this._scopes) {
2180
+ scopeSet.delete(scope);
2181
+ if (scopeSet.size === 0) this._scopes.delete(key);
2182
+ }
1709
2183
  }
1710
- async getTable(tableId) {
1711
- this.assertContext();
1712
- const result = await table({
2184
+ get _scopeContext() {
2185
+ return {
1713
2186
  client: this._client,
1714
- path: {
1715
- revisionId: this._revisionId,
1716
- tableId
2187
+ branch: {
2188
+ organizationId: this._organizationId ?? "",
2189
+ projectName: this._projectName ?? "",
2190
+ branchName: this._branchName ?? ""
2191
+ },
2192
+ isDraft: this._isDraft,
2193
+ getRevisionId: () => {
2194
+ if (this._revisionId === null) return Promise.reject(/* @__PURE__ */ new Error("Context not set. Call setContext() first."));
2195
+ return Promise.resolve(this._revisionId);
1717
2196
  }
1718
- });
1719
- return this.unwrap(result);
2197
+ };
2198
+ }
2199
+ async getTables(options) {
2200
+ return getTables(this._scopeContext, options);
2201
+ }
2202
+ async getTable(tableId) {
2203
+ return getTable(this._scopeContext, tableId);
1720
2204
  }
1721
2205
  async getTableSchema(tableId) {
1722
- this.assertContext();
1723
- const result = await tableSchema({
1724
- client: this._client,
1725
- path: {
1726
- revisionId: this._revisionId,
1727
- tableId
1728
- }
1729
- });
1730
- return this.unwrap(result);
2206
+ return getTableSchema(this._scopeContext, tableId);
1731
2207
  }
1732
2208
  async getRows(tableId, options) {
1733
- this.assertContext();
1734
- const result = await rows({
1735
- client: this._client,
1736
- path: {
1737
- revisionId: this._revisionId,
1738
- tableId
1739
- },
1740
- body: options ?? { first: 100 }
1741
- });
1742
- return this.unwrap(result);
2209
+ return getRows(this._scopeContext, tableId, options);
1743
2210
  }
1744
2211
  async getRow(tableId, rowId) {
1745
- this.assertContext();
1746
- const result = await row({
1747
- client: this._client,
1748
- path: {
1749
- revisionId: this._revisionId,
1750
- tableId,
1751
- rowId
1752
- }
1753
- });
1754
- return this.unwrap(result);
2212
+ return getRow(this._scopeContext, tableId, rowId);
1755
2213
  }
1756
2214
  async getChanges() {
1757
- this.assertContext();
1758
- const result = await revisionChanges({
1759
- client: this._client,
1760
- path: { revisionId: this._revisionId }
1761
- });
1762
- return this.unwrap(result);
2215
+ return getChanges(this._scopeContext);
1763
2216
  }
1764
2217
  async createTable(tableId, schema) {
1765
- this.assertDraft();
1766
- const result = await createTable({
1767
- client: this._client,
1768
- path: { revisionId: this._revisionId },
1769
- body: {
1770
- tableId,
1771
- schema
1772
- }
1773
- });
1774
- return this.unwrap(result);
2218
+ return createTable(this._scopeContext, tableId, schema);
1775
2219
  }
1776
2220
  async updateTable(tableId, patches) {
1777
- this.assertDraft();
1778
- const result = await updateTable({
1779
- client: this._client,
1780
- path: {
1781
- revisionId: this._revisionId,
1782
- tableId
1783
- },
1784
- body: { patches }
1785
- });
1786
- return this.unwrap(result);
2221
+ return updateTable(this._scopeContext, tableId, patches);
1787
2222
  }
1788
2223
  async deleteTable(tableId) {
1789
- this.assertDraft();
1790
- const result = await deleteTable({
1791
- client: this._client,
1792
- path: {
1793
- revisionId: this._revisionId,
1794
- tableId
1795
- }
1796
- });
1797
- this.unwrap(result);
2224
+ return deleteTable(this._scopeContext, tableId);
1798
2225
  }
1799
2226
  async renameTable(tableId, nextTableId) {
1800
- this.assertDraft();
1801
- const result = await renameTable({
1802
- client: this._client,
1803
- path: {
1804
- revisionId: this._revisionId,
1805
- tableId
1806
- },
1807
- body: { nextTableId }
1808
- });
1809
- return this.unwrap(result);
2227
+ return renameTable(this._scopeContext, tableId, nextTableId);
1810
2228
  }
1811
2229
  async createRow(tableId, rowId, data) {
1812
- this.assertDraft();
1813
- const result = await createRow({
1814
- client: this._client,
1815
- path: {
1816
- revisionId: this._revisionId,
1817
- tableId
1818
- },
1819
- body: {
1820
- rowId,
1821
- data
1822
- }
1823
- });
1824
- return this.unwrap(result);
2230
+ return createRow(this._scopeContext, tableId, rowId, data);
1825
2231
  }
1826
2232
  async createRows(tableId, rows) {
1827
- this.assertDraft();
1828
- const result = await createRows({
1829
- client: this._client,
1830
- path: {
1831
- revisionId: this._revisionId,
1832
- tableId
1833
- },
1834
- body: { rows: rows.map((r) => ({
1835
- rowId: r.rowId,
1836
- data: r.data
1837
- })) }
1838
- });
1839
- return this.unwrap(result);
2233
+ return createRows(this._scopeContext, tableId, rows);
1840
2234
  }
1841
2235
  async updateRow(tableId, rowId, data) {
1842
- this.assertDraft();
1843
- const result = await updateRow({
1844
- client: this._client,
1845
- path: {
1846
- revisionId: this._revisionId,
1847
- tableId,
1848
- rowId
1849
- },
1850
- body: { data }
1851
- });
1852
- return this.unwrap(result);
2236
+ return updateRow(this._scopeContext, tableId, rowId, data);
1853
2237
  }
1854
2238
  async updateRows(tableId, rows) {
1855
- this.assertDraft();
1856
- const result = await updateRows({
1857
- client: this._client,
1858
- path: {
1859
- revisionId: this._revisionId,
1860
- tableId
1861
- },
1862
- body: { rows: rows.map((r) => ({
1863
- rowId: r.rowId,
1864
- data: r.data
1865
- })) }
1866
- });
1867
- return this.unwrap(result);
2239
+ return updateRows(this._scopeContext, tableId, rows);
1868
2240
  }
1869
2241
  async patchRow(tableId, rowId, patches) {
1870
- this.assertDraft();
1871
- const result = await patchRow({
1872
- client: this._client,
1873
- path: {
1874
- revisionId: this._revisionId,
1875
- tableId,
1876
- rowId
1877
- },
1878
- body: { patches }
1879
- });
1880
- return this.unwrap(result);
2242
+ return patchRow(this._scopeContext, tableId, rowId, patches);
1881
2243
  }
1882
2244
  async deleteRow(tableId, rowId) {
1883
- this.assertDraft();
1884
- const result = await deleteRow({
1885
- client: this._client,
1886
- path: {
1887
- revisionId: this._revisionId,
1888
- tableId,
1889
- rowId
1890
- }
1891
- });
1892
- this.unwrap(result);
2245
+ return deleteRow(this._scopeContext, tableId, rowId);
1893
2246
  }
1894
2247
  async deleteRows(tableId, rowIds) {
1895
- this.assertDraft();
1896
- const result = await deleteRows({
1897
- client: this._client,
1898
- path: {
1899
- revisionId: this._revisionId,
1900
- tableId
1901
- },
1902
- body: { rowIds }
1903
- });
1904
- this.unwrap(result);
2248
+ return deleteRows(this._scopeContext, tableId, rowIds);
1905
2249
  }
1906
2250
  async renameRow(tableId, rowId, nextRowId) {
1907
- this.assertDraft();
1908
- const result = await renameRow({
1909
- client: this._client,
1910
- path: {
1911
- revisionId: this._revisionId,
1912
- tableId,
1913
- rowId
1914
- },
1915
- body: { nextRowId }
1916
- });
1917
- return this.unwrap(result);
2251
+ return renameRow(this._scopeContext, tableId, rowId, nextRowId);
1918
2252
  }
1919
2253
  async commit(comment) {
1920
- this.assertDraft();
1921
- const result = await createRevision({
1922
- client: this._client,
1923
- path: {
1924
- organizationId: this._organizationId,
1925
- projectName: this._projectName,
1926
- branchName: this._branchName
1927
- },
1928
- body: { comment }
1929
- });
1930
- const data = this.unwrap(result);
2254
+ const data = await commit(this._scopeContext, comment);
1931
2255
  await this.refreshDraftRevisionId();
2256
+ this.notifyScopesOnCurrentBranch();
1932
2257
  return data;
1933
2258
  }
1934
2259
  async revertChanges() {
1935
- this.assertDraft();
1936
- await revertChanges({
1937
- client: this._client,
1938
- path: {
1939
- organizationId: this._organizationId,
1940
- projectName: this._projectName,
1941
- branchName: this._branchName
1942
- }
1943
- });
2260
+ await revertChanges(this._scopeContext);
1944
2261
  await this.refreshDraftRevisionId();
1945
- }
1946
- assertDraft() {
1947
- this.assertContext();
1948
- if (!this._isDraft) throw new Error("Mutations are only allowed in draft revision. Use setContext({ revision: \"draft\" }).");
1949
- }
1950
- assertContext() {
1951
- if (this._revisionId === null) throw new Error("Context not set. Call setContext() first.");
2262
+ this.notifyScopesOnCurrentBranch();
1952
2263
  }
1953
2264
  async refreshDraftRevisionId() {
1954
- const result = await draftRevision({
1955
- client: this._client,
1956
- path: {
1957
- organizationId: this._organizationId,
1958
- projectName: this._projectName,
1959
- branchName: this._branchName
1960
- }
2265
+ this._revisionId = await fetchDraftRevisionId(this._client, {
2266
+ organizationId: this._organizationId,
2267
+ projectName: this._projectName,
2268
+ branchName: this._branchName
1961
2269
  });
1962
- this._revisionId = this.unwrap(result).id;
1963
2270
  }
1964
- unwrap(result) {
1965
- if (result.error) {
1966
- const err = result.error;
1967
- throw new Error(err.message ?? `API error: ${JSON.stringify(result.error)}`);
2271
+ notifyScopesOnCurrentBranch() {
2272
+ if (this._organizationId && this._projectName && this._branchName) {
2273
+ const branchKey = `${this._organizationId}/${this._projectName}/${this._branchName}`;
2274
+ this.notifyBranchChanged(branchKey);
1968
2275
  }
1969
- return result.data;
1970
2276
  }
1971
2277
  };
1972
2278
 
1973
2279
  //#endregion
1974
2280
  exports.RevisiumClient = RevisiumClient;
2281
+ exports.RevisiumScope = RevisiumScope;
1975
2282
  exports.client = client;
1976
2283
  Object.defineProperty(exports, 'sdk', {
1977
2284
  enumerable: true,