@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.
@@ -1,9 +1,11 @@
1
1
  import { getIt } from "get-it";
2
2
  import { adapter, environment } from "get-it";
3
3
  import { retry, jsonRequest, jsonResponse, progress, observable } from "get-it/middleware";
4
- import { Observable, defer, of, isObservable, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, throwError, timer, tap, finalize, share, merge, EMPTY } from "rxjs";
4
+ import { Observable, defer, of, isObservable, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, throwError, timer, tap, finalize, share, merge, EMPTY, map as map$1, firstValueFrom } from "rxjs";
5
5
  import { stegaClean } from "./_chunks-es/stegaClean.js";
6
6
  import { combineLatestWith, map, filter, finalize as finalize$1 } from "rxjs/operators";
7
+ import { getVersionFromId, isDraftId, getVersionId, getDraftId, isVersionId, getPublishedId } from "@sanity/client/csm";
8
+ import { customAlphabet } from "nanoid";
7
9
  class ClientError extends Error {
8
10
  response;
9
11
  statusCode = 400;
@@ -135,6 +137,18 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
135
137
  if (!doc._id)
136
138
  throw new Error(`${op}() requires that the document contains an ID ("_id" property)`);
137
139
  validateDocumentId(op, doc._id);
140
+ }, validateDocumentType = (op, type) => {
141
+ if (typeof type != "string")
142
+ throw new Error(`\`${op}()\`: \`${type}\` is not a valid document type`);
143
+ }, requireDocumentType = (op, doc) => {
144
+ if (!doc._type)
145
+ throw new Error(`\`${op}()\` requires that the document contains a type (\`_type\` property)`);
146
+ validateDocumentType(op, doc._type);
147
+ }, validateVersionIdMatch = (builtVersionId, document) => {
148
+ if (document._id && document._id !== builtVersionId)
149
+ throw new Error(
150
+ `The provided document ID (\`${document._id}\`) does not match the generated version ID (\`${builtVersionId}\`)`
151
+ );
138
152
  }, validateInsert = (at, selector, items) => {
139
153
  const signature = "insert(at, selector, items)";
140
154
  if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
@@ -776,8 +790,24 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
776
790
  ) : $request.pipe(map(mapResponse));
777
791
  }
778
792
  function _getDocument(client, httpRequest, id, opts = {}) {
779
- const options = {
780
- uri: _getDataUrl(client, "doc", id),
793
+ const docId = (() => {
794
+ if (!opts.releaseId)
795
+ return id;
796
+ const versionId = getVersionFromId(id);
797
+ if (!versionId) {
798
+ if (isDraftId(id))
799
+ throw new Error(
800
+ `The document ID (\`${id}\`) is a draft, but \`options.releaseId\` is set as \`${opts.releaseId}\``
801
+ );
802
+ return getVersionId(id, opts.releaseId);
803
+ }
804
+ if (versionId !== opts.releaseId)
805
+ throw new Error(
806
+ `The document ID (\`${id}\`) is already a version of \`${versionId}\` release, but this does not match the provided \`options.releaseId\` (\`${opts.releaseId}\`)`
807
+ );
808
+ return id;
809
+ })(), options = {
810
+ uri: _getDataUrl(client, "doc", docId),
781
811
  json: !0,
782
812
  tag: opts.tag,
783
813
  signal: opts.signal
@@ -802,12 +832,33 @@ function _getDocuments(client, httpRequest, ids, opts = {}) {
802
832
  })
803
833
  );
804
834
  }
835
+ function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
836
+ return _dataRequest(
837
+ client,
838
+ httpRequest,
839
+ "query",
840
+ {
841
+ query: "*[sanity::partOfRelease($releaseId)]",
842
+ params: {
843
+ releaseId
844
+ }
845
+ },
846
+ opts
847
+ );
848
+ }
805
849
  function _createIfNotExists(client, httpRequest, doc, options) {
806
850
  return requireDocumentId("createIfNotExists", doc), _create(client, httpRequest, doc, "createIfNotExists", options);
807
851
  }
808
852
  function _createOrReplace(client, httpRequest, doc, options) {
809
853
  return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
810
854
  }
855
+ function _createVersion(client, httpRequest, doc, publishedId, options) {
856
+ return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
857
+ actionType: "sanity.action.document.version.create",
858
+ publishedId,
859
+ document: doc
860
+ }, options);
861
+ }
811
862
  function _delete(client, httpRequest, selection, options) {
812
863
  return _dataRequest(
813
864
  client,
@@ -817,6 +868,26 @@ function _delete(client, httpRequest, selection, options) {
817
868
  options
818
869
  );
819
870
  }
871
+ function _discardVersion(client, httpRequest, versionId, purge = !1, options) {
872
+ return _action(client, httpRequest, {
873
+ actionType: "sanity.action.document.version.discard",
874
+ versionId,
875
+ purge
876
+ }, options);
877
+ }
878
+ function _replaceVersion(client, httpRequest, doc, options) {
879
+ return requireDocumentId("replaceVersion", doc), requireDocumentType("replaceVersion", doc), _action(client, httpRequest, {
880
+ actionType: "sanity.action.document.version.replace",
881
+ document: doc
882
+ }, options);
883
+ }
884
+ function _unpublishVersion(client, httpRequest, versionId, publishedId, options) {
885
+ return _action(client, httpRequest, {
886
+ actionType: "sanity.action.document.version.unpublish",
887
+ versionId,
888
+ publishedId
889
+ }, options);
890
+ }
820
891
  function _mutate(client, httpRequest, mutations, options) {
821
892
  let mut;
822
893
  mutations instanceof Patch || mutations instanceof ObservablePatch ? mut = { patch: mutations.serialize() } : mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mut = mutations.serialize() : mut = mutations;
@@ -971,6 +1042,14 @@ function _generate(client, httpRequest, request) {
971
1042
  body: request
972
1043
  });
973
1044
  }
1045
+ function _prompt(client, httpRequest, request) {
1046
+ const dataset2 = hasDataset(client.config());
1047
+ return _request(client, httpRequest, {
1048
+ method: "POST",
1049
+ uri: `/agent/action/prompt/${dataset2}`,
1050
+ body: request
1051
+ });
1052
+ }
974
1053
  function _transform(client, httpRequest, request) {
975
1054
  const dataset2 = hasDataset(client.config());
976
1055
  return _request(client, httpRequest, {
@@ -1042,6 +1121,13 @@ class AgentActionsClient {
1042
1121
  translate(request) {
1043
1122
  return lastValueFrom(_translate(this.#client, this.#httpRequest, request));
1044
1123
  }
1124
+ /**
1125
+ * Run a raw instruction and return the result either as text or json
1126
+ * @param request - prompt request
1127
+ */
1128
+ prompt(request) {
1129
+ return lastValueFrom(_prompt(this.#client, this.#httpRequest, request));
1130
+ }
1045
1131
  }
1046
1132
  class ObservableAssetsClient {
1047
1133
  #client;
@@ -1419,6 +1505,498 @@ class ProjectsClient {
1419
1505
  );
1420
1506
  }
1421
1507
  }
1508
+ const generateReleaseId = customAlphabet(
1509
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
1510
+ 8
1511
+ ), getDocumentVersionId = (publishedId, releaseId) => releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId);
1512
+ function deriveDocumentVersionId(op, {
1513
+ releaseId,
1514
+ publishedId,
1515
+ document
1516
+ }) {
1517
+ if (publishedId && document._id) {
1518
+ const versionId = getDocumentVersionId(publishedId, releaseId);
1519
+ return validateVersionIdMatch(versionId, document), versionId;
1520
+ }
1521
+ if (document._id) {
1522
+ const isDraft = isDraftId(document._id), isVersion = isVersionId(document._id);
1523
+ if (!isDraft && !isVersion)
1524
+ throw new Error(
1525
+ `\`${op}()\` requires a document with an \`_id\` that is a version or draft ID`
1526
+ );
1527
+ if (releaseId) {
1528
+ if (isDraft)
1529
+ throw new Error(
1530
+ `\`${op}()\` was called with a document ID (\`${document._id}\`) that is a draft ID, but a release ID (\`${releaseId}\`) was also provided.`
1531
+ );
1532
+ const builtVersionId = getVersionFromId(document._id);
1533
+ if (builtVersionId !== releaseId)
1534
+ throw new Error(
1535
+ `\`${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}\`).`
1536
+ );
1537
+ }
1538
+ return document._id;
1539
+ }
1540
+ if (publishedId)
1541
+ return getDocumentVersionId(publishedId, releaseId);
1542
+ throw new Error(`\`${op}()\` requires either a publishedId or a document with an \`_id\``);
1543
+ }
1544
+ const getArgs = (releaseOrOptions, maybeOptions) => {
1545
+ if (typeof releaseOrOptions == "object" && releaseOrOptions !== null && ("releaseId" in releaseOrOptions || "metadata" in releaseOrOptions)) {
1546
+ const { releaseId = generateReleaseId(), metadata = {} } = releaseOrOptions;
1547
+ return [releaseId, metadata, maybeOptions];
1548
+ }
1549
+ return [generateReleaseId(), {}, releaseOrOptions];
1550
+ }, createRelease = (releaseOrOptions, maybeOptions) => {
1551
+ const [releaseId, metadata, options] = getArgs(releaseOrOptions, maybeOptions), finalMetadata = {
1552
+ ...metadata,
1553
+ releaseType: metadata.releaseType || "undecided"
1554
+ };
1555
+ return { action: {
1556
+ actionType: "sanity.action.release.create",
1557
+ releaseId,
1558
+ metadata: finalMetadata
1559
+ }, options };
1560
+ };
1561
+ class ObservableReleasesClient {
1562
+ #client;
1563
+ #httpRequest;
1564
+ constructor(client, httpRequest) {
1565
+ this.#client = client, this.#httpRequest = httpRequest;
1566
+ }
1567
+ /**
1568
+ * @public
1569
+ *
1570
+ * Retrieve a release by id.
1571
+ *
1572
+ * @category Releases
1573
+ *
1574
+ * @param params - Release action parameters:
1575
+ * - `releaseId` - The id of the release to retrieve.
1576
+ * @param options - Additional query options including abort signal and query tag.
1577
+ * @returns An observable that resolves to the release document {@link ReleaseDocument}.
1578
+ *
1579
+ * @example Retrieving a release by id
1580
+ * ```ts
1581
+ * client.observable.releases.get({releaseId: 'my-release'}).pipe(
1582
+ * tap((release) => console.log(release)),
1583
+ * // {
1584
+ * // _id: '_.releases.my-release',
1585
+ * // name: 'my-release'
1586
+ * // _type: 'system.release',
1587
+ * // metadata: {releaseType: 'asap'},
1588
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
1589
+ * // ...
1590
+ * // }
1591
+ * ).subscribe()
1592
+ * ```
1593
+ */
1594
+ get({ releaseId }, options) {
1595
+ return _getDocument(
1596
+ this.#client,
1597
+ this.#httpRequest,
1598
+ `_.releases.${releaseId}`,
1599
+ options
1600
+ );
1601
+ }
1602
+ create(releaseOrOptions, maybeOptions) {
1603
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
1604
+ return _action(this.#client, this.#httpRequest, action, options).pipe(
1605
+ map$1((actionResult) => ({
1606
+ ...actionResult,
1607
+ releaseId,
1608
+ metadata
1609
+ }))
1610
+ );
1611
+ }
1612
+ /**
1613
+ * @public
1614
+ *
1615
+ * Edits an existing release, updating the metadata.
1616
+ *
1617
+ * @category Releases
1618
+ *
1619
+ * @param params - Release action parameters:
1620
+ * - `releaseId` - The id of the release to edit.
1621
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
1622
+ * @param options - Additional action options.
1623
+ * @returns An observable that resolves to the `transactionId`.
1624
+ */
1625
+ edit({ releaseId, patch }, options) {
1626
+ const editAction = {
1627
+ actionType: "sanity.action.release.edit",
1628
+ releaseId,
1629
+ patch
1630
+ };
1631
+ return _action(this.#client, this.#httpRequest, editAction, options);
1632
+ }
1633
+ /**
1634
+ * @public
1635
+ *
1636
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
1637
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
1638
+ * documents and creation of the corresponding published documents with the new content may
1639
+ * take some time.
1640
+ *
1641
+ * During this period both the source and target documents are locked and cannot be
1642
+ * modified through any other means.
1643
+ *
1644
+ * @category Releases
1645
+ *
1646
+ * @param params - Release action parameters:
1647
+ * - `releaseId` - The id of the release to publish.
1648
+ * @param options - Additional action options.
1649
+ * @returns An observable that resolves to the `transactionId`.
1650
+ */
1651
+ publish({ releaseId }, options) {
1652
+ const publishAction = {
1653
+ actionType: "sanity.action.release.publish",
1654
+ releaseId
1655
+ };
1656
+ return _action(this.#client, this.#httpRequest, publishAction, options);
1657
+ }
1658
+ /**
1659
+ * @public
1660
+ *
1661
+ * An archive action removes an active release. The documents that comprise the release
1662
+ * are deleted and therefore no longer queryable.
1663
+ *
1664
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
1665
+ *
1666
+ * @category Releases
1667
+ *
1668
+ * @param params - Release action parameters:
1669
+ * - `releaseId` - The id of the release to archive.
1670
+ * @param options - Additional action options.
1671
+ * @returns An observable that resolves to the `transactionId`.
1672
+ */
1673
+ archive({ releaseId }, options) {
1674
+ const archiveAction = {
1675
+ actionType: "sanity.action.release.archive",
1676
+ releaseId
1677
+ };
1678
+ return _action(this.#client, this.#httpRequest, archiveAction, options);
1679
+ }
1680
+ /**
1681
+ * @public
1682
+ *
1683
+ * An unarchive action restores an archived release and all documents
1684
+ * with the content they had just prior to archiving.
1685
+ *
1686
+ * @category Releases
1687
+ *
1688
+ * @param params - Release action parameters:
1689
+ * - `releaseId` - The id of the release to unarchive.
1690
+ * @param options - Additional action options.
1691
+ * @returns An observable that resolves to the `transactionId`.
1692
+ */
1693
+ unarchive({ releaseId }, options) {
1694
+ const unarchiveAction = {
1695
+ actionType: "sanity.action.release.unarchive",
1696
+ releaseId
1697
+ };
1698
+ return _action(this.#client, this.#httpRequest, unarchiveAction, options);
1699
+ }
1700
+ /**
1701
+ * @public
1702
+ *
1703
+ * A schedule action queues a release for publishing at the given future time.
1704
+ * The release is locked such that no documents in the release can be modified and
1705
+ * no documents that it references can be deleted as this would make the publish fail.
1706
+ * At the given time, the same logic as for the publish action is triggered.
1707
+ *
1708
+ * @category Releases
1709
+ *
1710
+ * @param params - Release action parameters:
1711
+ * - `releaseId` - The id of the release to schedule.
1712
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
1713
+ * @param options - Additional action options.
1714
+ * @returns An observable that resolves to the `transactionId`.
1715
+ */
1716
+ schedule({ releaseId, publishAt }, options) {
1717
+ const scheduleAction = {
1718
+ actionType: "sanity.action.release.schedule",
1719
+ releaseId,
1720
+ publishAt
1721
+ };
1722
+ return _action(this.#client, this.#httpRequest, scheduleAction, options);
1723
+ }
1724
+ /**
1725
+ * @public
1726
+ *
1727
+ * An unschedule action stops a release from being published.
1728
+ * The documents in the release are considered unlocked and can be edited again.
1729
+ * This may fail if another release is scheduled to be published after this one and
1730
+ * has a reference to a document created by this one.
1731
+ *
1732
+ * @category Releases
1733
+ *
1734
+ * @param params - Release action parameters:
1735
+ * - `releaseId` - The id of the release to unschedule.
1736
+ * @param options - Additional action options.
1737
+ * @returns An observable that resolves to the `transactionId`.
1738
+ */
1739
+ unschedule({ releaseId }, options) {
1740
+ const unscheduleAction = {
1741
+ actionType: "sanity.action.release.unschedule",
1742
+ releaseId
1743
+ };
1744
+ return _action(this.#client, this.#httpRequest, unscheduleAction, options);
1745
+ }
1746
+ /**
1747
+ * @public
1748
+ *
1749
+ * A delete action removes a published or archived release.
1750
+ * The backing system document will be removed from the dataset.
1751
+ *
1752
+ * @category Releases
1753
+ *
1754
+ * @param params - Release action parameters:
1755
+ * - `releaseId` - The id of the release to delete.
1756
+ * @param options - Additional action options.
1757
+ * @returns An observable that resolves to the `transactionId`.
1758
+ */
1759
+ delete({ releaseId }, options) {
1760
+ const deleteAction = {
1761
+ actionType: "sanity.action.release.delete",
1762
+ releaseId
1763
+ };
1764
+ return _action(this.#client, this.#httpRequest, deleteAction, options);
1765
+ }
1766
+ /**
1767
+ * @public
1768
+ *
1769
+ * Fetch the documents in a release by release id.
1770
+ *
1771
+ * @category Releases
1772
+ *
1773
+ * @param params - Release action parameters:
1774
+ * - `releaseId` - The id of the release to fetch documents for.
1775
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
1776
+ * @returns An observable that resolves to the documents in the release.
1777
+ */
1778
+ fetchDocuments({ releaseId }, options) {
1779
+ return _getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options);
1780
+ }
1781
+ }
1782
+ class ReleasesClient {
1783
+ #client;
1784
+ #httpRequest;
1785
+ constructor(client, httpRequest) {
1786
+ this.#client = client, this.#httpRequest = httpRequest;
1787
+ }
1788
+ /**
1789
+ * @public
1790
+ *
1791
+ * Retrieve a release by id.
1792
+ *
1793
+ * @category Releases
1794
+ *
1795
+ * @param params - Release action parameters:
1796
+ * - `releaseId` - The id of the release to retrieve.
1797
+ * @param options - Additional query options including abort signal and query tag.
1798
+ * @returns A promise that resolves to the release document {@link ReleaseDocument}.
1799
+ *
1800
+ * @example Retrieving a release by id
1801
+ * ```ts
1802
+ * const release = await client.releases.get({releaseId: 'my-release'})
1803
+ * console.log(release)
1804
+ * // {
1805
+ * // _id: '_.releases.my-release',
1806
+ * // name: 'my-release'
1807
+ * // _type: 'system.release',
1808
+ * // metadata: {releaseType: 'asap'},
1809
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
1810
+ * // ...
1811
+ * // }
1812
+ * ```
1813
+ */
1814
+ get({ releaseId }, options) {
1815
+ return lastValueFrom(
1816
+ _getDocument(
1817
+ this.#client,
1818
+ this.#httpRequest,
1819
+ `_.releases.${releaseId}`,
1820
+ options
1821
+ )
1822
+ );
1823
+ }
1824
+ async create(releaseOrOptions, maybeOptions) {
1825
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
1826
+ return { ...await lastValueFrom(
1827
+ _action(this.#client, this.#httpRequest, action, options)
1828
+ ), releaseId, metadata };
1829
+ }
1830
+ /**
1831
+ * @public
1832
+ *
1833
+ * Edits an existing release, updating the metadata.
1834
+ *
1835
+ * @category Releases
1836
+ *
1837
+ * @param params - Release action parameters:
1838
+ * - `releaseId` - The id of the release to edit.
1839
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
1840
+ * @param options - Additional action options.
1841
+ * @returns A promise that resolves to the `transactionId`.
1842
+ */
1843
+ edit({ releaseId, patch }, options) {
1844
+ const editAction = {
1845
+ actionType: "sanity.action.release.edit",
1846
+ releaseId,
1847
+ patch
1848
+ };
1849
+ return lastValueFrom(_action(this.#client, this.#httpRequest, editAction, options));
1850
+ }
1851
+ /**
1852
+ * @public
1853
+ *
1854
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
1855
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
1856
+ * documents and creation of the corresponding published documents with the new content may
1857
+ * take some time.
1858
+ *
1859
+ * During this period both the source and target documents are locked and cannot be
1860
+ * modified through any other means.
1861
+ *
1862
+ * @category Releases
1863
+ *
1864
+ * @param params - Release action parameters:
1865
+ * - `releaseId` - The id of the release to publish.
1866
+ * @param options - Additional action options.
1867
+ * @returns A promise that resolves to the `transactionId`.
1868
+ */
1869
+ publish({ releaseId }, options) {
1870
+ const publishAction = {
1871
+ actionType: "sanity.action.release.publish",
1872
+ releaseId
1873
+ };
1874
+ return lastValueFrom(_action(this.#client, this.#httpRequest, publishAction, options));
1875
+ }
1876
+ /**
1877
+ * @public
1878
+ *
1879
+ * An archive action removes an active release. The documents that comprise the release
1880
+ * are deleted and therefore no longer queryable.
1881
+ *
1882
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
1883
+ *
1884
+ * @category Releases
1885
+ *
1886
+ * @param params - Release action parameters:
1887
+ * - `releaseId` - The id of the release to archive.
1888
+ * @param options - Additional action options.
1889
+ * @returns A promise that resolves to the `transactionId`.
1890
+ */
1891
+ archive({ releaseId }, options) {
1892
+ const archiveAction = {
1893
+ actionType: "sanity.action.release.archive",
1894
+ releaseId
1895
+ };
1896
+ return lastValueFrom(_action(this.#client, this.#httpRequest, archiveAction, options));
1897
+ }
1898
+ /**
1899
+ * @public
1900
+ *
1901
+ * An unarchive action restores an archived release and all documents
1902
+ * with the content they had just prior to archiving.
1903
+ *
1904
+ * @category Releases
1905
+ *
1906
+ * @param params - Release action parameters:
1907
+ * - `releaseId` - The id of the release to unarchive.
1908
+ * @param options - Additional action options.
1909
+ * @returns A promise that resolves to the `transactionId`.
1910
+ */
1911
+ unarchive({ releaseId }, options) {
1912
+ const unarchiveAction = {
1913
+ actionType: "sanity.action.release.unarchive",
1914
+ releaseId
1915
+ };
1916
+ return lastValueFrom(_action(this.#client, this.#httpRequest, unarchiveAction, options));
1917
+ }
1918
+ /**
1919
+ * @public
1920
+ *
1921
+ * A schedule action queues a release for publishing at the given future time.
1922
+ * The release is locked such that no documents in the release can be modified and
1923
+ * no documents that it references can be deleted as this would make the publish fail.
1924
+ * At the given time, the same logic as for the publish action is triggered.
1925
+ *
1926
+ * @category Releases
1927
+ *
1928
+ * @param params - Release action parameters:
1929
+ * - `releaseId` - The id of the release to schedule.
1930
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
1931
+ * @param options - Additional action options.
1932
+ * @returns A promise that resolves to the `transactionId`.
1933
+ */
1934
+ schedule({ releaseId, publishAt }, options) {
1935
+ const scheduleAction = {
1936
+ actionType: "sanity.action.release.schedule",
1937
+ releaseId,
1938
+ publishAt
1939
+ };
1940
+ return lastValueFrom(_action(this.#client, this.#httpRequest, scheduleAction, options));
1941
+ }
1942
+ /**
1943
+ * @public
1944
+ *
1945
+ * An unschedule action stops a release from being published.
1946
+ * The documents in the release are considered unlocked and can be edited again.
1947
+ * This may fail if another release is scheduled to be published after this one and
1948
+ * has a reference to a document created by this one.
1949
+ *
1950
+ * @category Releases
1951
+ *
1952
+ * @param params - Release action parameters:
1953
+ * - `releaseId` - The id of the release to unschedule.
1954
+ * @param options - Additional action options.
1955
+ * @returns A promise that resolves to the `transactionId`.
1956
+ */
1957
+ unschedule({ releaseId }, options) {
1958
+ const unscheduleAction = {
1959
+ actionType: "sanity.action.release.unschedule",
1960
+ releaseId
1961
+ };
1962
+ return lastValueFrom(_action(this.#client, this.#httpRequest, unscheduleAction, options));
1963
+ }
1964
+ /**
1965
+ * @public
1966
+ *
1967
+ * A delete action removes a published or archived release.
1968
+ * The backing system document will be removed from the dataset.
1969
+ *
1970
+ * @category Releases
1971
+ *
1972
+ * @param params - Release action parameters:
1973
+ * - `releaseId` - The id of the release to delete.
1974
+ * @param options - Additional action options.
1975
+ * @returns A promise that resolves to the `transactionId`.
1976
+ */
1977
+ delete({ releaseId }, options) {
1978
+ const deleteAction = {
1979
+ actionType: "sanity.action.release.delete",
1980
+ releaseId
1981
+ };
1982
+ return lastValueFrom(_action(this.#client, this.#httpRequest, deleteAction, options));
1983
+ }
1984
+ /**
1985
+ * @public
1986
+ *
1987
+ * Fetch the documents in a release by release id.
1988
+ *
1989
+ * @category Releases
1990
+ *
1991
+ * @param params - Release action parameters:
1992
+ * - `releaseId` - The id of the release to fetch documents for.
1993
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
1994
+ * @returns A promise that resolves to the documents in the release.
1995
+ */
1996
+ fetchDocuments({ releaseId }, options) {
1997
+ return lastValueFrom(_getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options));
1998
+ }
1999
+ }
1422
2000
  class ObservableUsersClient {
1423
2001
  #client;
1424
2002
  #httpRequest;
@@ -1464,6 +2042,7 @@ class ObservableSanityClient {
1464
2042
  projects;
1465
2043
  users;
1466
2044
  agent;
2045
+ releases;
1467
2046
  /**
1468
2047
  * Private properties
1469
2048
  */
@@ -1476,7 +2055,7 @@ class ObservableSanityClient {
1476
2055
  constructor(httpRequest, config = defaultConfig) {
1477
2056
  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 = {
1478
2057
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
1479
- };
2058
+ }, this.releases = new ObservableReleasesClient(this, this.#httpRequest);
1480
2059
  }
1481
2060
  /**
1482
2061
  * Clone the client - returns a new instance
@@ -1549,9 +2128,96 @@ class ObservableSanityClient {
1549
2128
  createOrReplace(document, options) {
1550
2129
  return _createOrReplace(this, this.#httpRequest, document, options);
1551
2130
  }
2131
+ createVersion({
2132
+ document,
2133
+ publishedId,
2134
+ releaseId
2135
+ }, options) {
2136
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
2137
+ document,
2138
+ publishedId,
2139
+ releaseId
2140
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || getPublishedId(document._id);
2141
+ return _createVersion(
2142
+ this,
2143
+ this.#httpRequest,
2144
+ documentVersion,
2145
+ versionPublishedId,
2146
+ options
2147
+ );
2148
+ }
1552
2149
  delete(selection, options) {
1553
2150
  return _delete(this, this.#httpRequest, selection, options);
1554
2151
  }
2152
+ /**
2153
+ * @public
2154
+ *
2155
+ * Deletes the draft or release version of a document.
2156
+ *
2157
+ * @remarks
2158
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
2159
+ * * If the draft or release version does not exist, any error will throw.
2160
+ *
2161
+ * @param params - Version action parameters:
2162
+ * - `releaseId` - The ID of the release to discard the document from.
2163
+ * - `publishedId` - The published ID of the document to discard.
2164
+ * @param purge - if `true` the document history is also discarded.
2165
+ * @param options - Additional action options.
2166
+ * @returns an observable that resolves to the `transactionId`.
2167
+ *
2168
+ * @example Discarding a release version of a document
2169
+ * ```ts
2170
+ * client.observable.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2171
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
2172
+ * ```
2173
+ *
2174
+ * @example Discarding a draft version of a document
2175
+ * ```ts
2176
+ * client.observable.discardVersion({publishedId: 'myDocument'})
2177
+ * // The document with the ID `drafts.myDocument` will be discarded.
2178
+ * ```
2179
+ */
2180
+ discardVersion({ releaseId, publishedId }, purge, options) {
2181
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
2182
+ return _discardVersion(this, this.#httpRequest, documentVersionId, purge, options);
2183
+ }
2184
+ replaceVersion({
2185
+ document,
2186
+ publishedId,
2187
+ releaseId
2188
+ }, options) {
2189
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
2190
+ document,
2191
+ publishedId,
2192
+ releaseId
2193
+ }), documentVersion = { ...document, _id: documentVersionId };
2194
+ return _replaceVersion(this, this.#httpRequest, documentVersion, options);
2195
+ }
2196
+ /**
2197
+ * @public
2198
+ *
2199
+ * Used to indicate when a document within a release should be unpublished when
2200
+ * the release is run.
2201
+ *
2202
+ * @remarks
2203
+ * * If the published document does not exist, an error will be thrown.
2204
+ *
2205
+ * @param params - Version action parameters:
2206
+ * - `releaseId` - The ID of the release to unpublish the document from.
2207
+ * - `publishedId` - The published ID of the document to unpublish.
2208
+ * @param options - Additional action options.
2209
+ * @returns an observable that resolves to the `transactionId`.
2210
+ *
2211
+ * @example Unpublishing a release version of a published document
2212
+ * ```ts
2213
+ * client.observable.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2214
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
2215
+ * ```
2216
+ */
2217
+ unpublishVersion({ releaseId, publishedId }, options) {
2218
+ const versionId = getVersionId(publishedId, releaseId);
2219
+ return _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options);
2220
+ }
1555
2221
  mutate(operations, options) {
1556
2222
  return _mutate(this, this.#httpRequest, operations, options);
1557
2223
  }
@@ -1616,6 +2282,7 @@ class SanityClient {
1616
2282
  projects;
1617
2283
  users;
1618
2284
  agent;
2285
+ releases;
1619
2286
  /**
1620
2287
  * Observable version of the Sanity client, with the same configuration as the promise-based one
1621
2288
  */
@@ -1632,7 +2299,7 @@ class SanityClient {
1632
2299
  constructor(httpRequest, config = defaultConfig) {
1633
2300
  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 = {
1634
2301
  action: new AgentActionsClient(this, this.#httpRequest)
1635
- }, this.observable = new ObservableSanityClient(httpRequest, config);
2302
+ }, this.releases = new ReleasesClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1636
2303
  }
1637
2304
  /**
1638
2305
  * Clone the client - returns a new instance
@@ -1713,9 +2380,104 @@ class SanityClient {
1713
2380
  _createOrReplace(this, this.#httpRequest, document, options)
1714
2381
  );
1715
2382
  }
2383
+ createVersion({
2384
+ document,
2385
+ publishedId,
2386
+ releaseId
2387
+ }, options) {
2388
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
2389
+ document,
2390
+ publishedId,
2391
+ releaseId
2392
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || getPublishedId(document._id);
2393
+ return firstValueFrom(
2394
+ _createVersion(
2395
+ this,
2396
+ this.#httpRequest,
2397
+ documentVersion,
2398
+ versionPublishedId,
2399
+ options
2400
+ )
2401
+ );
2402
+ }
1716
2403
  delete(selection, options) {
1717
2404
  return lastValueFrom(_delete(this, this.#httpRequest, selection, options));
1718
2405
  }
2406
+ /**
2407
+ * @public
2408
+ *
2409
+ * Deletes the draft or release version of a document.
2410
+ *
2411
+ * @remarks
2412
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
2413
+ * * If the draft or release version does not exist, any error will throw.
2414
+ *
2415
+ * @param params - Version action parameters:
2416
+ * - `releaseId` - The ID of the release to discard the document from.
2417
+ * - `publishedId` - The published ID of the document to discard.
2418
+ * @param purge - if `true` the document history is also discarded.
2419
+ * @param options - Additional action options.
2420
+ * @returns a promise that resolves to the `transactionId`.
2421
+ *
2422
+ * @example Discarding a release version of a document
2423
+ * ```ts
2424
+ * client.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2425
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
2426
+ * ```
2427
+ *
2428
+ * @example Discarding a draft version of a document
2429
+ * ```ts
2430
+ * client.discardVersion({publishedId: 'myDocument'})
2431
+ * // The document with the ID `drafts.myDocument` will be discarded.
2432
+ * ```
2433
+ */
2434
+ discardVersion({ releaseId, publishedId }, purge, options) {
2435
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
2436
+ return lastValueFrom(
2437
+ _discardVersion(this, this.#httpRequest, documentVersionId, purge, options)
2438
+ );
2439
+ }
2440
+ replaceVersion({
2441
+ document,
2442
+ publishedId,
2443
+ releaseId
2444
+ }, options) {
2445
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
2446
+ document,
2447
+ publishedId,
2448
+ releaseId
2449
+ }), documentVersion = { ...document, _id: documentVersionId };
2450
+ return firstValueFrom(
2451
+ _replaceVersion(this, this.#httpRequest, documentVersion, options)
2452
+ );
2453
+ }
2454
+ /**
2455
+ * @public
2456
+ *
2457
+ * Used to indicate when a document within a release should be unpublished when
2458
+ * the release is run.
2459
+ *
2460
+ * @remarks
2461
+ * * If the published document does not exist, an error will be thrown.
2462
+ *
2463
+ * @param params - Version action parameters:
2464
+ * - `releaseId` - The ID of the release to unpublish the document from.
2465
+ * - `publishedId` - The published ID of the document to unpublish.
2466
+ * @param options - Additional action options.
2467
+ * @returns a promise that resolves to the `transactionId`.
2468
+ *
2469
+ * @example Unpublishing a release version of a published document
2470
+ * ```ts
2471
+ * await client.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
2472
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
2473
+ * ```
2474
+ */
2475
+ unpublishVersion({ releaseId, publishedId }, options) {
2476
+ const versionId = getVersionId(publishedId, releaseId);
2477
+ return lastValueFrom(
2478
+ _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options)
2479
+ );
2480
+ }
1719
2481
  mutate(operations, options) {
1720
2482
  return lastValueFrom(_mutate(this, this.#httpRequest, operations, options));
1721
2483
  }