@sanity/client 7.1.0 → 7.2.1-agent-actions.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.
@@ -19,7 +19,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
19
19
  mod
20
20
  ));
21
21
  Object.defineProperty(exports, "__esModule", { value: !0 });
22
- var getIt = require("get-it"), middleware = require("get-it/middleware"), rxjs = require("rxjs"), stegaClean = require("./_chunks-cjs/stegaClean.cjs"), operators = require("rxjs/operators");
22
+ var getIt = require("get-it"), middleware = require("get-it/middleware"), rxjs = require("rxjs"), stegaClean = require("./_chunks-cjs/stegaClean.cjs"), operators = require("rxjs/operators"), csm = require("@sanity/client/csm"), nanoid = require("nanoid");
23
23
  class ClientError extends Error {
24
24
  response;
25
25
  statusCode = 400;
@@ -151,6 +151,18 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
151
151
  if (!doc._id)
152
152
  throw new Error(`${op}() requires that the document contains an ID ("_id" property)`);
153
153
  validateDocumentId(op, doc._id);
154
+ }, validateDocumentType = (op, type) => {
155
+ if (typeof type != "string")
156
+ throw new Error(`\`${op}()\`: \`${type}\` is not a valid document type`);
157
+ }, requireDocumentType = (op, doc) => {
158
+ if (!doc._type)
159
+ throw new Error(`\`${op}()\` requires that the document contains a type (\`_type\` property)`);
160
+ validateDocumentType(op, doc._type);
161
+ }, validateVersionIdMatch = (builtVersionId, document) => {
162
+ if (document._id && document._id !== builtVersionId)
163
+ throw new Error(
164
+ `The provided document ID (\`${document._id}\`) does not match the generated version ID (\`${builtVersionId}\`)`
165
+ );
154
166
  }, validateInsert = (at, selector, items) => {
155
167
  const signature = "insert(at, selector, items)";
156
168
  if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
@@ -794,8 +806,24 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
794
806
  ) : $request.pipe(operators.map(mapResponse));
795
807
  }
796
808
  function _getDocument(client, httpRequest, id, opts = {}) {
797
- const options = {
798
- uri: _getDataUrl(client, "doc", id),
809
+ const docId = (() => {
810
+ if (!opts.releaseId)
811
+ return id;
812
+ const versionId = csm.getVersionFromId(id);
813
+ if (!versionId) {
814
+ if (csm.isDraftId(id))
815
+ throw new Error(
816
+ `The document ID (\`${id}\`) is a draft, but \`options.releaseId\` is set as \`${opts.releaseId}\``
817
+ );
818
+ return csm.getVersionId(id, opts.releaseId);
819
+ }
820
+ if (versionId !== opts.releaseId)
821
+ throw new Error(
822
+ `The document ID (\`${id}\`) is already a version of \`${versionId}\` release, but this does not match the provided \`options.releaseId\` (\`${opts.releaseId}\`)`
823
+ );
824
+ return id;
825
+ })(), options = {
826
+ uri: _getDataUrl(client, "doc", docId),
799
827
  json: !0,
800
828
  tag: opts.tag,
801
829
  signal: opts.signal
@@ -820,12 +848,33 @@ function _getDocuments(client, httpRequest, ids, opts = {}) {
820
848
  })
821
849
  );
822
850
  }
851
+ function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
852
+ return _dataRequest(
853
+ client,
854
+ httpRequest,
855
+ "query",
856
+ {
857
+ query: "*[sanity::partOfRelease($releaseId)]",
858
+ params: {
859
+ releaseId
860
+ }
861
+ },
862
+ opts
863
+ );
864
+ }
823
865
  function _createIfNotExists(client, httpRequest, doc, options) {
824
866
  return requireDocumentId("createIfNotExists", doc), _create(client, httpRequest, doc, "createIfNotExists", options);
825
867
  }
826
868
  function _createOrReplace(client, httpRequest, doc, options) {
827
869
  return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
828
870
  }
871
+ function _createVersion(client, httpRequest, doc, publishedId, options) {
872
+ return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
873
+ actionType: "sanity.action.document.version.create",
874
+ publishedId,
875
+ document: doc
876
+ }, options);
877
+ }
829
878
  function _delete(client, httpRequest, selection, options) {
830
879
  return _dataRequest(
831
880
  client,
@@ -835,6 +884,26 @@ function _delete(client, httpRequest, selection, options) {
835
884
  options
836
885
  );
837
886
  }
887
+ function _discardVersion(client, httpRequest, versionId, purge = !1, options) {
888
+ return _action(client, httpRequest, {
889
+ actionType: "sanity.action.document.version.discard",
890
+ versionId,
891
+ purge
892
+ }, options);
893
+ }
894
+ function _replaceVersion(client, httpRequest, doc, options) {
895
+ return requireDocumentId("replaceVersion", doc), requireDocumentType("replaceVersion", doc), _action(client, httpRequest, {
896
+ actionType: "sanity.action.document.version.replace",
897
+ document: doc
898
+ }, options);
899
+ }
900
+ function _unpublishVersion(client, httpRequest, versionId, publishedId, options) {
901
+ return _action(client, httpRequest, {
902
+ actionType: "sanity.action.document.version.unpublish",
903
+ versionId,
904
+ publishedId
905
+ }, options);
906
+ }
838
907
  function _mutate(client, httpRequest, mutations, options) {
839
908
  let mut;
840
909
  mutations instanceof Patch || mutations instanceof ObservablePatch ? mut = { patch: mutations.serialize() } : mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mut = mutations.serialize() : mut = mutations;
@@ -989,6 +1058,14 @@ function _generate(client, httpRequest, request) {
989
1058
  body: request
990
1059
  });
991
1060
  }
1061
+ function _prompt(client, httpRequest, request) {
1062
+ const dataset2 = hasDataset(client.config());
1063
+ return _request(client, httpRequest, {
1064
+ method: "POST",
1065
+ uri: `/agent/action/prompt/${dataset2}`,
1066
+ body: request
1067
+ });
1068
+ }
992
1069
  function _transform(client, httpRequest, request) {
993
1070
  const dataset2 = hasDataset(client.config());
994
1071
  return _request(client, httpRequest, {
@@ -1060,6 +1137,13 @@ class AgentActionsClient {
1060
1137
  translate(request) {
1061
1138
  return rxjs.lastValueFrom(_translate(this.#client, this.#httpRequest, request));
1062
1139
  }
1140
+ /**
1141
+ * Run a raw instruction and return the result either as text or json
1142
+ * @param request - prompt request
1143
+ */
1144
+ prompt(request) {
1145
+ return rxjs.lastValueFrom(_prompt(this.#client, this.#httpRequest, request));
1146
+ }
1063
1147
  }
1064
1148
  class ObservableAssetsClient {
1065
1149
  #client;
@@ -1437,6 +1521,498 @@ class ProjectsClient {
1437
1521
  );
1438
1522
  }
1439
1523
  }
1524
+ const generateReleaseId = nanoid.customAlphabet(
1525
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
1526
+ 8
1527
+ ), getDocumentVersionId = (publishedId, releaseId) => releaseId ? csm.getVersionId(publishedId, releaseId) : csm.getDraftId(publishedId);
1528
+ function deriveDocumentVersionId(op, {
1529
+ releaseId,
1530
+ publishedId,
1531
+ document
1532
+ }) {
1533
+ if (publishedId && document._id) {
1534
+ const versionId = getDocumentVersionId(publishedId, releaseId);
1535
+ return validateVersionIdMatch(versionId, document), versionId;
1536
+ }
1537
+ if (document._id) {
1538
+ const isDraft = csm.isDraftId(document._id), isVersion = csm.isVersionId(document._id);
1539
+ if (!isDraft && !isVersion)
1540
+ throw new Error(
1541
+ `\`${op}()\` requires a document with an \`_id\` that is a version or draft ID`
1542
+ );
1543
+ if (releaseId) {
1544
+ if (isDraft)
1545
+ throw new Error(
1546
+ `\`${op}()\` was called with a document ID (\`${document._id}\`) that is a draft ID, but a release ID (\`${releaseId}\`) was also provided.`
1547
+ );
1548
+ const builtVersionId = csm.getVersionFromId(document._id);
1549
+ if (builtVersionId !== releaseId)
1550
+ throw new Error(
1551
+ `\`${op}()\` was called with a document ID (\`${document._id}\`) that is a version ID, but the release ID (\`${releaseId}\`) does not match the document's version ID (\`${builtVersionId}\`).`
1552
+ );
1553
+ }
1554
+ return document._id;
1555
+ }
1556
+ if (publishedId)
1557
+ return getDocumentVersionId(publishedId, releaseId);
1558
+ throw new Error(`\`${op}()\` requires either a publishedId or a document with an \`_id\``);
1559
+ }
1560
+ const getArgs = (releaseOrOptions, maybeOptions) => {
1561
+ if (typeof releaseOrOptions == "object" && releaseOrOptions !== null && ("releaseId" in releaseOrOptions || "metadata" in releaseOrOptions)) {
1562
+ const { releaseId = generateReleaseId(), metadata = {} } = releaseOrOptions;
1563
+ return [releaseId, metadata, maybeOptions];
1564
+ }
1565
+ return [generateReleaseId(), {}, releaseOrOptions];
1566
+ }, createRelease = (releaseOrOptions, maybeOptions) => {
1567
+ const [releaseId, metadata, options] = getArgs(releaseOrOptions, maybeOptions), finalMetadata = {
1568
+ ...metadata,
1569
+ releaseType: metadata.releaseType || "undecided"
1570
+ };
1571
+ return { action: {
1572
+ actionType: "sanity.action.release.create",
1573
+ releaseId,
1574
+ metadata: finalMetadata
1575
+ }, options };
1576
+ };
1577
+ class ObservableReleasesClient {
1578
+ #client;
1579
+ #httpRequest;
1580
+ constructor(client, httpRequest) {
1581
+ this.#client = client, this.#httpRequest = httpRequest;
1582
+ }
1583
+ /**
1584
+ * @public
1585
+ *
1586
+ * Retrieve a release by id.
1587
+ *
1588
+ * @category Releases
1589
+ *
1590
+ * @param params - Release action parameters:
1591
+ * - `releaseId` - The id of the release to retrieve.
1592
+ * @param options - Additional query options including abort signal and query tag.
1593
+ * @returns An observable that resolves to the release document {@link ReleaseDocument}.
1594
+ *
1595
+ * @example Retrieving a release by id
1596
+ * ```ts
1597
+ * client.observable.releases.get({releaseId: 'my-release'}).pipe(
1598
+ * tap((release) => console.log(release)),
1599
+ * // {
1600
+ * // _id: '_.releases.my-release',
1601
+ * // name: 'my-release'
1602
+ * // _type: 'system.release',
1603
+ * // metadata: {releaseType: 'asap'},
1604
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
1605
+ * // ...
1606
+ * // }
1607
+ * ).subscribe()
1608
+ * ```
1609
+ */
1610
+ get({ releaseId }, options) {
1611
+ return _getDocument(
1612
+ this.#client,
1613
+ this.#httpRequest,
1614
+ `_.releases.${releaseId}`,
1615
+ options
1616
+ );
1617
+ }
1618
+ create(releaseOrOptions, maybeOptions) {
1619
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
1620
+ return _action(this.#client, this.#httpRequest, action, options).pipe(
1621
+ rxjs.map((actionResult) => ({
1622
+ ...actionResult,
1623
+ releaseId,
1624
+ metadata
1625
+ }))
1626
+ );
1627
+ }
1628
+ /**
1629
+ * @public
1630
+ *
1631
+ * Edits an existing release, updating the metadata.
1632
+ *
1633
+ * @category Releases
1634
+ *
1635
+ * @param params - Release action parameters:
1636
+ * - `releaseId` - The id of the release to edit.
1637
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
1638
+ * @param options - Additional action options.
1639
+ * @returns An observable that resolves to the `transactionId`.
1640
+ */
1641
+ edit({ releaseId, patch }, options) {
1642
+ const editAction = {
1643
+ actionType: "sanity.action.release.edit",
1644
+ releaseId,
1645
+ patch
1646
+ };
1647
+ return _action(this.#client, this.#httpRequest, editAction, options);
1648
+ }
1649
+ /**
1650
+ * @public
1651
+ *
1652
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
1653
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
1654
+ * documents and creation of the corresponding published documents with the new content may
1655
+ * take some time.
1656
+ *
1657
+ * During this period both the source and target documents are locked and cannot be
1658
+ * modified through any other means.
1659
+ *
1660
+ * @category Releases
1661
+ *
1662
+ * @param params - Release action parameters:
1663
+ * - `releaseId` - The id of the release to publish.
1664
+ * @param options - Additional action options.
1665
+ * @returns An observable that resolves to the `transactionId`.
1666
+ */
1667
+ publish({ releaseId }, options) {
1668
+ const publishAction = {
1669
+ actionType: "sanity.action.release.publish",
1670
+ releaseId
1671
+ };
1672
+ return _action(this.#client, this.#httpRequest, publishAction, options);
1673
+ }
1674
+ /**
1675
+ * @public
1676
+ *
1677
+ * An archive action removes an active release. The documents that comprise the release
1678
+ * are deleted and therefore no longer queryable.
1679
+ *
1680
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
1681
+ *
1682
+ * @category Releases
1683
+ *
1684
+ * @param params - Release action parameters:
1685
+ * - `releaseId` - The id of the release to archive.
1686
+ * @param options - Additional action options.
1687
+ * @returns An observable that resolves to the `transactionId`.
1688
+ */
1689
+ archive({ releaseId }, options) {
1690
+ const archiveAction = {
1691
+ actionType: "sanity.action.release.archive",
1692
+ releaseId
1693
+ };
1694
+ return _action(this.#client, this.#httpRequest, archiveAction, options);
1695
+ }
1696
+ /**
1697
+ * @public
1698
+ *
1699
+ * An unarchive action restores an archived release and all documents
1700
+ * with the content they had just prior to archiving.
1701
+ *
1702
+ * @category Releases
1703
+ *
1704
+ * @param params - Release action parameters:
1705
+ * - `releaseId` - The id of the release to unarchive.
1706
+ * @param options - Additional action options.
1707
+ * @returns An observable that resolves to the `transactionId`.
1708
+ */
1709
+ unarchive({ releaseId }, options) {
1710
+ const unarchiveAction = {
1711
+ actionType: "sanity.action.release.unarchive",
1712
+ releaseId
1713
+ };
1714
+ return _action(this.#client, this.#httpRequest, unarchiveAction, options);
1715
+ }
1716
+ /**
1717
+ * @public
1718
+ *
1719
+ * A schedule action queues a release for publishing at the given future time.
1720
+ * The release is locked such that no documents in the release can be modified and
1721
+ * no documents that it references can be deleted as this would make the publish fail.
1722
+ * At the given time, the same logic as for the publish action is triggered.
1723
+ *
1724
+ * @category Releases
1725
+ *
1726
+ * @param params - Release action parameters:
1727
+ * - `releaseId` - The id of the release to schedule.
1728
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
1729
+ * @param options - Additional action options.
1730
+ * @returns An observable that resolves to the `transactionId`.
1731
+ */
1732
+ schedule({ releaseId, publishAt }, options) {
1733
+ const scheduleAction = {
1734
+ actionType: "sanity.action.release.schedule",
1735
+ releaseId,
1736
+ publishAt
1737
+ };
1738
+ return _action(this.#client, this.#httpRequest, scheduleAction, options);
1739
+ }
1740
+ /**
1741
+ * @public
1742
+ *
1743
+ * An unschedule action stops a release from being published.
1744
+ * The documents in the release are considered unlocked and can be edited again.
1745
+ * This may fail if another release is scheduled to be published after this one and
1746
+ * has a reference to a document created by this one.
1747
+ *
1748
+ * @category Releases
1749
+ *
1750
+ * @param params - Release action parameters:
1751
+ * - `releaseId` - The id of the release to unschedule.
1752
+ * @param options - Additional action options.
1753
+ * @returns An observable that resolves to the `transactionId`.
1754
+ */
1755
+ unschedule({ releaseId }, options) {
1756
+ const unscheduleAction = {
1757
+ actionType: "sanity.action.release.unschedule",
1758
+ releaseId
1759
+ };
1760
+ return _action(this.#client, this.#httpRequest, unscheduleAction, options);
1761
+ }
1762
+ /**
1763
+ * @public
1764
+ *
1765
+ * A delete action removes a published or archived release.
1766
+ * The backing system document will be removed from the dataset.
1767
+ *
1768
+ * @category Releases
1769
+ *
1770
+ * @param params - Release action parameters:
1771
+ * - `releaseId` - The id of the release to delete.
1772
+ * @param options - Additional action options.
1773
+ * @returns An observable that resolves to the `transactionId`.
1774
+ */
1775
+ delete({ releaseId }, options) {
1776
+ const deleteAction = {
1777
+ actionType: "sanity.action.release.delete",
1778
+ releaseId
1779
+ };
1780
+ return _action(this.#client, this.#httpRequest, deleteAction, options);
1781
+ }
1782
+ /**
1783
+ * @public
1784
+ *
1785
+ * Fetch the documents in a release by release id.
1786
+ *
1787
+ * @category Releases
1788
+ *
1789
+ * @param params - Release action parameters:
1790
+ * - `releaseId` - The id of the release to fetch documents for.
1791
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
1792
+ * @returns An observable that resolves to the documents in the release.
1793
+ */
1794
+ fetchDocuments({ releaseId }, options) {
1795
+ return _getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options);
1796
+ }
1797
+ }
1798
+ class ReleasesClient {
1799
+ #client;
1800
+ #httpRequest;
1801
+ constructor(client, httpRequest) {
1802
+ this.#client = client, this.#httpRequest = httpRequest;
1803
+ }
1804
+ /**
1805
+ * @public
1806
+ *
1807
+ * Retrieve a release by id.
1808
+ *
1809
+ * @category Releases
1810
+ *
1811
+ * @param params - Release action parameters:
1812
+ * - `releaseId` - The id of the release to retrieve.
1813
+ * @param options - Additional query options including abort signal and query tag.
1814
+ * @returns A promise that resolves to the release document {@link ReleaseDocument}.
1815
+ *
1816
+ * @example Retrieving a release by id
1817
+ * ```ts
1818
+ * const release = await client.releases.get({releaseId: 'my-release'})
1819
+ * console.log(release)
1820
+ * // {
1821
+ * // _id: '_.releases.my-release',
1822
+ * // name: 'my-release'
1823
+ * // _type: 'system.release',
1824
+ * // metadata: {releaseType: 'asap'},
1825
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
1826
+ * // ...
1827
+ * // }
1828
+ * ```
1829
+ */
1830
+ get({ releaseId }, options) {
1831
+ return rxjs.lastValueFrom(
1832
+ _getDocument(
1833
+ this.#client,
1834
+ this.#httpRequest,
1835
+ `_.releases.${releaseId}`,
1836
+ options
1837
+ )
1838
+ );
1839
+ }
1840
+ async create(releaseOrOptions, maybeOptions) {
1841
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
1842
+ return { ...await rxjs.lastValueFrom(
1843
+ _action(this.#client, this.#httpRequest, action, options)
1844
+ ), releaseId, metadata };
1845
+ }
1846
+ /**
1847
+ * @public
1848
+ *
1849
+ * Edits an existing release, updating the metadata.
1850
+ *
1851
+ * @category Releases
1852
+ *
1853
+ * @param params - Release action parameters:
1854
+ * - `releaseId` - The id of the release to edit.
1855
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
1856
+ * @param options - Additional action options.
1857
+ * @returns A promise that resolves to the `transactionId`.
1858
+ */
1859
+ edit({ releaseId, patch }, options) {
1860
+ const editAction = {
1861
+ actionType: "sanity.action.release.edit",
1862
+ releaseId,
1863
+ patch
1864
+ };
1865
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, editAction, options));
1866
+ }
1867
+ /**
1868
+ * @public
1869
+ *
1870
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
1871
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
1872
+ * documents and creation of the corresponding published documents with the new content may
1873
+ * take some time.
1874
+ *
1875
+ * During this period both the source and target documents are locked and cannot be
1876
+ * modified through any other means.
1877
+ *
1878
+ * @category Releases
1879
+ *
1880
+ * @param params - Release action parameters:
1881
+ * - `releaseId` - The id of the release to publish.
1882
+ * @param options - Additional action options.
1883
+ * @returns A promise that resolves to the `transactionId`.
1884
+ */
1885
+ publish({ releaseId }, options) {
1886
+ const publishAction = {
1887
+ actionType: "sanity.action.release.publish",
1888
+ releaseId
1889
+ };
1890
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, publishAction, options));
1891
+ }
1892
+ /**
1893
+ * @public
1894
+ *
1895
+ * An archive action removes an active release. The documents that comprise the release
1896
+ * are deleted and therefore no longer queryable.
1897
+ *
1898
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
1899
+ *
1900
+ * @category Releases
1901
+ *
1902
+ * @param params - Release action parameters:
1903
+ * - `releaseId` - The id of the release to archive.
1904
+ * @param options - Additional action options.
1905
+ * @returns A promise that resolves to the `transactionId`.
1906
+ */
1907
+ archive({ releaseId }, options) {
1908
+ const archiveAction = {
1909
+ actionType: "sanity.action.release.archive",
1910
+ releaseId
1911
+ };
1912
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, archiveAction, options));
1913
+ }
1914
+ /**
1915
+ * @public
1916
+ *
1917
+ * An unarchive action restores an archived release and all documents
1918
+ * with the content they had just prior to archiving.
1919
+ *
1920
+ * @category Releases
1921
+ *
1922
+ * @param params - Release action parameters:
1923
+ * - `releaseId` - The id of the release to unarchive.
1924
+ * @param options - Additional action options.
1925
+ * @returns A promise that resolves to the `transactionId`.
1926
+ */
1927
+ unarchive({ releaseId }, options) {
1928
+ const unarchiveAction = {
1929
+ actionType: "sanity.action.release.unarchive",
1930
+ releaseId
1931
+ };
1932
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, unarchiveAction, options));
1933
+ }
1934
+ /**
1935
+ * @public
1936
+ *
1937
+ * A schedule action queues a release for publishing at the given future time.
1938
+ * The release is locked such that no documents in the release can be modified and
1939
+ * no documents that it references can be deleted as this would make the publish fail.
1940
+ * At the given time, the same logic as for the publish action is triggered.
1941
+ *
1942
+ * @category Releases
1943
+ *
1944
+ * @param params - Release action parameters:
1945
+ * - `releaseId` - The id of the release to schedule.
1946
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
1947
+ * @param options - Additional action options.
1948
+ * @returns A promise that resolves to the `transactionId`.
1949
+ */
1950
+ schedule({ releaseId, publishAt }, options) {
1951
+ const scheduleAction = {
1952
+ actionType: "sanity.action.release.schedule",
1953
+ releaseId,
1954
+ publishAt
1955
+ };
1956
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, scheduleAction, options));
1957
+ }
1958
+ /**
1959
+ * @public
1960
+ *
1961
+ * An unschedule action stops a release from being published.
1962
+ * The documents in the release are considered unlocked and can be edited again.
1963
+ * This may fail if another release is scheduled to be published after this one and
1964
+ * has a reference to a document created by this one.
1965
+ *
1966
+ * @category Releases
1967
+ *
1968
+ * @param params - Release action parameters:
1969
+ * - `releaseId` - The id of the release to unschedule.
1970
+ * @param options - Additional action options.
1971
+ * @returns A promise that resolves to the `transactionId`.
1972
+ */
1973
+ unschedule({ releaseId }, options) {
1974
+ const unscheduleAction = {
1975
+ actionType: "sanity.action.release.unschedule",
1976
+ releaseId
1977
+ };
1978
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, unscheduleAction, options));
1979
+ }
1980
+ /**
1981
+ * @public
1982
+ *
1983
+ * A delete action removes a published or archived release.
1984
+ * The backing system document will be removed from the dataset.
1985
+ *
1986
+ * @category Releases
1987
+ *
1988
+ * @param params - Release action parameters:
1989
+ * - `releaseId` - The id of the release to delete.
1990
+ * @param options - Additional action options.
1991
+ * @returns A promise that resolves to the `transactionId`.
1992
+ */
1993
+ delete({ releaseId }, options) {
1994
+ const deleteAction = {
1995
+ actionType: "sanity.action.release.delete",
1996
+ releaseId
1997
+ };
1998
+ return rxjs.lastValueFrom(_action(this.#client, this.#httpRequest, deleteAction, options));
1999
+ }
2000
+ /**
2001
+ * @public
2002
+ *
2003
+ * Fetch the documents in a release by release id.
2004
+ *
2005
+ * @category Releases
2006
+ *
2007
+ * @param params - Release action parameters:
2008
+ * - `releaseId` - The id of the release to fetch documents for.
2009
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
2010
+ * @returns A promise that resolves to the documents in the release.
2011
+ */
2012
+ fetchDocuments({ releaseId }, options) {
2013
+ return rxjs.lastValueFrom(_getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options));
2014
+ }
2015
+ }
1440
2016
  class ObservableUsersClient {
1441
2017
  #client;
1442
2018
  #httpRequest;
@@ -1482,6 +2058,7 @@ class ObservableSanityClient {
1482
2058
  projects;
1483
2059
  users;
1484
2060
  agent;
2061
+ releases;
1485
2062
  /**
1486
2063
  * Private properties
1487
2064
  */
@@ -1494,7 +2071,7 @@ class ObservableSanityClient {
1494
2071
  constructor(httpRequest, config = defaultConfig) {
1495
2072
  this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.agent = {
1496
2073
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
1497
- };
2074
+ }, this.releases = new ObservableReleasesClient(this, this.#httpRequest);
1498
2075
  }
1499
2076
  /**
1500
2077
  * Clone the client - returns a new instance
@@ -1567,9 +2144,96 @@ class ObservableSanityClient {
1567
2144
  createOrReplace(document, options) {
1568
2145
  return _createOrReplace(this, this.#httpRequest, document, options);
1569
2146
  }
2147
+ createVersion({
2148
+ document,
2149
+ publishedId,
2150
+ releaseId
2151
+ }, options) {
2152
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
2153
+ document,
2154
+ publishedId,
2155
+ releaseId
2156
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || csm.getPublishedId(document._id);
2157
+ return _createVersion(
2158
+ this,
2159
+ this.#httpRequest,
2160
+ documentVersion,
2161
+ versionPublishedId,
2162
+ options
2163
+ );
2164
+ }
1570
2165
  delete(selection, options) {
1571
2166
  return _delete(this, this.#httpRequest, selection, options);
1572
2167
  }
2168
+ /**
2169
+ * @public
2170
+ *
2171
+ * Deletes the draft or release version of a document.
2172
+ *
2173
+ * @remarks
2174
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
2175
+ * * If the draft or release version does not exist, any error will throw.
2176
+ *
2177
+ * @param params - Version action parameters:
2178
+ * - `releaseId` - The ID of the release to discard the document from.
2179
+ * - `publishedId` - The published ID of the document to discard.
2180
+ * @param purge - if `true` the document history is also discarded.
2181
+ * @param options - Additional action options.
2182
+ * @returns an observable that resolves to the `transactionId`.
2183
+ *
2184
+ * @example Discarding a release version of a document
2185
+ * ```ts
2186
+ * client.observable.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2187
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
2188
+ * ```
2189
+ *
2190
+ * @example Discarding a draft version of a document
2191
+ * ```ts
2192
+ * client.observable.discardVersion({publishedId: 'myDocument'})
2193
+ * // The document with the ID `drafts.myDocument` will be discarded.
2194
+ * ```
2195
+ */
2196
+ discardVersion({ releaseId, publishedId }, purge, options) {
2197
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
2198
+ return _discardVersion(this, this.#httpRequest, documentVersionId, purge, options);
2199
+ }
2200
+ replaceVersion({
2201
+ document,
2202
+ publishedId,
2203
+ releaseId
2204
+ }, options) {
2205
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
2206
+ document,
2207
+ publishedId,
2208
+ releaseId
2209
+ }), documentVersion = { ...document, _id: documentVersionId };
2210
+ return _replaceVersion(this, this.#httpRequest, documentVersion, options);
2211
+ }
2212
+ /**
2213
+ * @public
2214
+ *
2215
+ * Used to indicate when a document within a release should be unpublished when
2216
+ * the release is run.
2217
+ *
2218
+ * @remarks
2219
+ * * If the published document does not exist, an error will be thrown.
2220
+ *
2221
+ * @param params - Version action parameters:
2222
+ * - `releaseId` - The ID of the release to unpublish the document from.
2223
+ * - `publishedId` - The published ID of the document to unpublish.
2224
+ * @param options - Additional action options.
2225
+ * @returns an observable that resolves to the `transactionId`.
2226
+ *
2227
+ * @example Unpublishing a release version of a published document
2228
+ * ```ts
2229
+ * client.observable.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2230
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
2231
+ * ```
2232
+ */
2233
+ unpublishVersion({ releaseId, publishedId }, options) {
2234
+ const versionId = csm.getVersionId(publishedId, releaseId);
2235
+ return _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options);
2236
+ }
1573
2237
  mutate(operations, options) {
1574
2238
  return _mutate(this, this.#httpRequest, operations, options);
1575
2239
  }
@@ -1634,6 +2298,7 @@ class SanityClient {
1634
2298
  projects;
1635
2299
  users;
1636
2300
  agent;
2301
+ releases;
1637
2302
  /**
1638
2303
  * Observable version of the Sanity client, with the same configuration as the promise-based one
1639
2304
  */
@@ -1650,7 +2315,7 @@ class SanityClient {
1650
2315
  constructor(httpRequest, config = defaultConfig) {
1651
2316
  this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.agent = {
1652
2317
  action: new AgentActionsClient(this, this.#httpRequest)
1653
- }, this.observable = new ObservableSanityClient(httpRequest, config);
2318
+ }, this.releases = new ReleasesClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1654
2319
  }
1655
2320
  /**
1656
2321
  * Clone the client - returns a new instance
@@ -1731,9 +2396,104 @@ class SanityClient {
1731
2396
  _createOrReplace(this, this.#httpRequest, document, options)
1732
2397
  );
1733
2398
  }
2399
+ createVersion({
2400
+ document,
2401
+ publishedId,
2402
+ releaseId
2403
+ }, options) {
2404
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
2405
+ document,
2406
+ publishedId,
2407
+ releaseId
2408
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || csm.getPublishedId(document._id);
2409
+ return rxjs.firstValueFrom(
2410
+ _createVersion(
2411
+ this,
2412
+ this.#httpRequest,
2413
+ documentVersion,
2414
+ versionPublishedId,
2415
+ options
2416
+ )
2417
+ );
2418
+ }
1734
2419
  delete(selection, options) {
1735
2420
  return rxjs.lastValueFrom(_delete(this, this.#httpRequest, selection, options));
1736
2421
  }
2422
+ /**
2423
+ * @public
2424
+ *
2425
+ * Deletes the draft or release version of a document.
2426
+ *
2427
+ * @remarks
2428
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
2429
+ * * If the draft or release version does not exist, any error will throw.
2430
+ *
2431
+ * @param params - Version action parameters:
2432
+ * - `releaseId` - The ID of the release to discard the document from.
2433
+ * - `publishedId` - The published ID of the document to discard.
2434
+ * @param purge - if `true` the document history is also discarded.
2435
+ * @param options - Additional action options.
2436
+ * @returns a promise that resolves to the `transactionId`.
2437
+ *
2438
+ * @example Discarding a release version of a document
2439
+ * ```ts
2440
+ * client.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2441
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
2442
+ * ```
2443
+ *
2444
+ * @example Discarding a draft version of a document
2445
+ * ```ts
2446
+ * client.discardVersion({publishedId: 'myDocument'})
2447
+ * // The document with the ID `drafts.myDocument` will be discarded.
2448
+ * ```
2449
+ */
2450
+ discardVersion({ releaseId, publishedId }, purge, options) {
2451
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
2452
+ return rxjs.lastValueFrom(
2453
+ _discardVersion(this, this.#httpRequest, documentVersionId, purge, options)
2454
+ );
2455
+ }
2456
+ replaceVersion({
2457
+ document,
2458
+ publishedId,
2459
+ releaseId
2460
+ }, options) {
2461
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
2462
+ document,
2463
+ publishedId,
2464
+ releaseId
2465
+ }), documentVersion = { ...document, _id: documentVersionId };
2466
+ return rxjs.firstValueFrom(
2467
+ _replaceVersion(this, this.#httpRequest, documentVersion, options)
2468
+ );
2469
+ }
2470
+ /**
2471
+ * @public
2472
+ *
2473
+ * Used to indicate when a document within a release should be unpublished when
2474
+ * the release is run.
2475
+ *
2476
+ * @remarks
2477
+ * * If the published document does not exist, an error will be thrown.
2478
+ *
2479
+ * @param params - Version action parameters:
2480
+ * - `releaseId` - The ID of the release to unpublish the document from.
2481
+ * - `publishedId` - The published ID of the document to unpublish.
2482
+ * @param options - Additional action options.
2483
+ * @returns a promise that resolves to the `transactionId`.
2484
+ *
2485
+ * @example Unpublishing a release version of a published document
2486
+ * ```ts
2487
+ * await client.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2488
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
2489
+ * ```
2490
+ */
2491
+ unpublishVersion({ releaseId, publishedId }, options) {
2492
+ const versionId = csm.getVersionId(publishedId, releaseId);
2493
+ return rxjs.lastValueFrom(
2494
+ _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options)
2495
+ );
2496
+ }
1737
2497
  mutate(operations, options) {
1738
2498
  return rxjs.lastValueFrom(_mutate(this, this.#httpRequest, operations, options));
1739
2499
  }