@sanity/client 7.1.0 → 7.2.1

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.
@@ -1495,6 +1495,24 @@
1495
1495
  });
1496
1496
  }
1497
1497
 
1498
+ function firstValueFrom(source, config) {
1499
+ return new Promise(function (resolve, reject) {
1500
+ var subscriber = new SafeSubscriber({
1501
+ next: function (value) {
1502
+ resolve(value);
1503
+ subscriber.unsubscribe();
1504
+ },
1505
+ error: reject,
1506
+ complete: function () {
1507
+ {
1508
+ reject(new EmptyError());
1509
+ }
1510
+ },
1511
+ });
1512
+ source.subscribe(subscriber);
1513
+ });
1514
+ }
1515
+
1498
1516
  function isValidDate$1(value) {
1499
1517
  return value instanceof Date && !isNaN(value);
1500
1518
  }
@@ -1912,6 +1930,53 @@
1912
1930
  return O(result);
1913
1931
  }
1914
1932
 
1933
+ const DRAFTS_FOLDER$1 = "drafts", VERSION_FOLDER$1 = "versions", PATH_SEPARATOR$1 = ".", DRAFTS_PREFIX$1 = `${DRAFTS_FOLDER$1}${PATH_SEPARATOR$1}`, VERSION_PREFIX$1 = `${VERSION_FOLDER$1}${PATH_SEPARATOR$1}`;
1934
+ function isDraftId$1(id) {
1935
+ return id.startsWith(DRAFTS_PREFIX$1);
1936
+ }
1937
+ function isVersionId$1(id) {
1938
+ return id.startsWith(VERSION_PREFIX$1);
1939
+ }
1940
+ function getDraftId(id) {
1941
+ if (isVersionId$1(id)) {
1942
+ const publishedId = getPublishedId$1(id);
1943
+ return DRAFTS_PREFIX$1 + publishedId;
1944
+ }
1945
+ return isDraftId$1(id) ? id : DRAFTS_PREFIX$1 + id;
1946
+ }
1947
+ function getVersionId(id, version) {
1948
+ if (version === "drafts" || version === "published")
1949
+ throw new Error('Version can not be "published" or "drafts"');
1950
+ return `${VERSION_PREFIX$1}${version}${PATH_SEPARATOR$1}${getPublishedId$1(id)}`;
1951
+ }
1952
+ function getVersionFromId$1(id) {
1953
+ if (!isVersionId$1(id)) return;
1954
+ const [_versionPrefix, versionId, ..._publishedId] = id.split(PATH_SEPARATOR$1);
1955
+ return versionId;
1956
+ }
1957
+ function getPublishedId$1(id) {
1958
+ return isVersionId$1(id) ? id.split(PATH_SEPARATOR$1).slice(2).join(PATH_SEPARATOR$1) : isDraftId$1(id) ? id.slice(DRAFTS_PREFIX$1.length) : id;
1959
+ }
1960
+
1961
+ let random = bytes => crypto.getRandomValues(new Uint8Array(bytes));
1962
+ let customRandom = (alphabet, defaultSize, getRandom) => {
1963
+ let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1;
1964
+ let step = -~((1.6 * mask * defaultSize) / alphabet.length);
1965
+ return (size = defaultSize) => {
1966
+ let id = '';
1967
+ while (true) {
1968
+ let bytes = getRandom(step);
1969
+ let j = step | 0;
1970
+ while (j--) {
1971
+ id += alphabet[bytes[j] & mask] || '';
1972
+ if (id.length === size) return id
1973
+ }
1974
+ }
1975
+ }
1976
+ };
1977
+ let customAlphabet = (alphabet, size = 21) =>
1978
+ customRandom(alphabet, size, random);
1979
+
1915
1980
  class ClientError extends Error {
1916
1981
  response;
1917
1982
  statusCode = 400;
@@ -2043,6 +2108,18 @@
2043
2108
  if (!doc._id)
2044
2109
  throw new Error(`${op}() requires that the document contains an ID ("_id" property)`);
2045
2110
  validateDocumentId(op, doc._id);
2111
+ }, validateDocumentType = (op, type) => {
2112
+ if (typeof type != "string")
2113
+ throw new Error(`\`${op}()\`: \`${type}\` is not a valid document type`);
2114
+ }, requireDocumentType = (op, doc) => {
2115
+ if (!doc._type)
2116
+ throw new Error(`\`${op}()\` requires that the document contains a type (\`_type\` property)`);
2117
+ validateDocumentType(op, doc._type);
2118
+ }, validateVersionIdMatch = (builtVersionId, document) => {
2119
+ if (document._id && document._id !== builtVersionId)
2120
+ throw new Error(
2121
+ `The provided document ID (\`${document._id}\`) does not match the generated version ID (\`${builtVersionId}\`)`
2122
+ );
2046
2123
  }, validateInsert = (at, selector, items) => {
2047
2124
  const signature = "insert(at, selector, items)";
2048
2125
  if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
@@ -2684,8 +2761,24 @@ ${selectionOpts}`);
2684
2761
  ) : $request.pipe(map(mapResponse));
2685
2762
  }
2686
2763
  function _getDocument(client, httpRequest, id, opts = {}) {
2687
- const options = {
2688
- uri: _getDataUrl(client, "doc", id),
2764
+ const docId = (() => {
2765
+ if (!opts.releaseId)
2766
+ return id;
2767
+ const versionId = getVersionFromId$1(id);
2768
+ if (!versionId) {
2769
+ if (isDraftId$1(id))
2770
+ throw new Error(
2771
+ `The document ID (\`${id}\`) is a draft, but \`options.releaseId\` is set as \`${opts.releaseId}\``
2772
+ );
2773
+ return getVersionId(id, opts.releaseId);
2774
+ }
2775
+ if (versionId !== opts.releaseId)
2776
+ throw new Error(
2777
+ `The document ID (\`${id}\`) is already a version of \`${versionId}\` release, but this does not match the provided \`options.releaseId\` (\`${opts.releaseId}\`)`
2778
+ );
2779
+ return id;
2780
+ })(), options = {
2781
+ uri: _getDataUrl(client, "doc", docId),
2689
2782
  json: true,
2690
2783
  tag: opts.tag,
2691
2784
  signal: opts.signal
@@ -2710,12 +2803,33 @@ ${selectionOpts}`);
2710
2803
  })
2711
2804
  );
2712
2805
  }
2806
+ function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
2807
+ return _dataRequest(
2808
+ client,
2809
+ httpRequest,
2810
+ "query",
2811
+ {
2812
+ query: "*[sanity::partOfRelease($releaseId)]",
2813
+ params: {
2814
+ releaseId
2815
+ }
2816
+ },
2817
+ opts
2818
+ );
2819
+ }
2713
2820
  function _createIfNotExists(client, httpRequest, doc, options) {
2714
2821
  return requireDocumentId("createIfNotExists", doc), _create(client, httpRequest, doc, "createIfNotExists", options);
2715
2822
  }
2716
2823
  function _createOrReplace(client, httpRequest, doc, options) {
2717
2824
  return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
2718
2825
  }
2826
+ function _createVersion(client, httpRequest, doc, publishedId, options) {
2827
+ return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
2828
+ actionType: "sanity.action.document.version.create",
2829
+ publishedId,
2830
+ document: doc
2831
+ }, options);
2832
+ }
2719
2833
  function _delete(client, httpRequest, selection, options) {
2720
2834
  return _dataRequest(
2721
2835
  client,
@@ -2725,6 +2839,26 @@ ${selectionOpts}`);
2725
2839
  options
2726
2840
  );
2727
2841
  }
2842
+ function _discardVersion(client, httpRequest, versionId, purge = false, options) {
2843
+ return _action(client, httpRequest, {
2844
+ actionType: "sanity.action.document.version.discard",
2845
+ versionId,
2846
+ purge
2847
+ }, options);
2848
+ }
2849
+ function _replaceVersion(client, httpRequest, doc, options) {
2850
+ return requireDocumentId("replaceVersion", doc), requireDocumentType("replaceVersion", doc), _action(client, httpRequest, {
2851
+ actionType: "sanity.action.document.version.replace",
2852
+ document: doc
2853
+ }, options);
2854
+ }
2855
+ function _unpublishVersion(client, httpRequest, versionId, publishedId, options) {
2856
+ return _action(client, httpRequest, {
2857
+ actionType: "sanity.action.document.version.unpublish",
2858
+ versionId,
2859
+ publishedId
2860
+ }, options);
2861
+ }
2728
2862
  function _mutate(client, httpRequest, mutations, options) {
2729
2863
  let mut;
2730
2864
  mutations instanceof Patch || mutations instanceof ObservablePatch ? mut = { patch: mutations.serialize() } : mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mut = mutations.serialize() : mut = mutations;
@@ -3327,6 +3461,498 @@ ${selectionOpts}`);
3327
3461
  );
3328
3462
  }
3329
3463
  }
3464
+ const generateReleaseId = customAlphabet(
3465
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
3466
+ 8
3467
+ ), getDocumentVersionId = (publishedId, releaseId) => releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId);
3468
+ function deriveDocumentVersionId(op, {
3469
+ releaseId,
3470
+ publishedId,
3471
+ document
3472
+ }) {
3473
+ if (publishedId && document._id) {
3474
+ const versionId = getDocumentVersionId(publishedId, releaseId);
3475
+ return validateVersionIdMatch(versionId, document), versionId;
3476
+ }
3477
+ if (document._id) {
3478
+ const isDraft = isDraftId$1(document._id), isVersion = isVersionId$1(document._id);
3479
+ if (!isDraft && !isVersion)
3480
+ throw new Error(
3481
+ `\`${op}()\` requires a document with an \`_id\` that is a version or draft ID`
3482
+ );
3483
+ if (releaseId) {
3484
+ if (isDraft)
3485
+ throw new Error(
3486
+ `\`${op}()\` was called with a document ID (\`${document._id}\`) that is a draft ID, but a release ID (\`${releaseId}\`) was also provided.`
3487
+ );
3488
+ const builtVersionId = getVersionFromId$1(document._id);
3489
+ if (builtVersionId !== releaseId)
3490
+ throw new Error(
3491
+ `\`${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}\`).`
3492
+ );
3493
+ }
3494
+ return document._id;
3495
+ }
3496
+ if (publishedId)
3497
+ return getDocumentVersionId(publishedId, releaseId);
3498
+ throw new Error(`\`${op}()\` requires either a publishedId or a document with an \`_id\``);
3499
+ }
3500
+ const getArgs = (releaseOrOptions, maybeOptions) => {
3501
+ if (typeof releaseOrOptions == "object" && releaseOrOptions !== null && ("releaseId" in releaseOrOptions || "metadata" in releaseOrOptions)) {
3502
+ const { releaseId = generateReleaseId(), metadata = {} } = releaseOrOptions;
3503
+ return [releaseId, metadata, maybeOptions];
3504
+ }
3505
+ return [generateReleaseId(), {}, releaseOrOptions];
3506
+ }, createRelease = (releaseOrOptions, maybeOptions) => {
3507
+ const [releaseId, metadata, options] = getArgs(releaseOrOptions, maybeOptions), finalMetadata = {
3508
+ ...metadata,
3509
+ releaseType: metadata.releaseType || "undecided"
3510
+ };
3511
+ return { action: {
3512
+ actionType: "sanity.action.release.create",
3513
+ releaseId,
3514
+ metadata: finalMetadata
3515
+ }, options };
3516
+ };
3517
+ class ObservableReleasesClient {
3518
+ #client;
3519
+ #httpRequest;
3520
+ constructor(client, httpRequest) {
3521
+ this.#client = client, this.#httpRequest = httpRequest;
3522
+ }
3523
+ /**
3524
+ * @public
3525
+ *
3526
+ * Retrieve a release by id.
3527
+ *
3528
+ * @category Releases
3529
+ *
3530
+ * @param params - Release action parameters:
3531
+ * - `releaseId` - The id of the release to retrieve.
3532
+ * @param options - Additional query options including abort signal and query tag.
3533
+ * @returns An observable that resolves to the release document {@link ReleaseDocument}.
3534
+ *
3535
+ * @example Retrieving a release by id
3536
+ * ```ts
3537
+ * client.observable.releases.get({releaseId: 'my-release'}).pipe(
3538
+ * tap((release) => console.log(release)),
3539
+ * // {
3540
+ * // _id: '_.releases.my-release',
3541
+ * // name: 'my-release'
3542
+ * // _type: 'system.release',
3543
+ * // metadata: {releaseType: 'asap'},
3544
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
3545
+ * // ...
3546
+ * // }
3547
+ * ).subscribe()
3548
+ * ```
3549
+ */
3550
+ get({ releaseId }, options) {
3551
+ return _getDocument(
3552
+ this.#client,
3553
+ this.#httpRequest,
3554
+ `_.releases.${releaseId}`,
3555
+ options
3556
+ );
3557
+ }
3558
+ create(releaseOrOptions, maybeOptions) {
3559
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
3560
+ return _action(this.#client, this.#httpRequest, action, options).pipe(
3561
+ map((actionResult) => ({
3562
+ ...actionResult,
3563
+ releaseId,
3564
+ metadata
3565
+ }))
3566
+ );
3567
+ }
3568
+ /**
3569
+ * @public
3570
+ *
3571
+ * Edits an existing release, updating the metadata.
3572
+ *
3573
+ * @category Releases
3574
+ *
3575
+ * @param params - Release action parameters:
3576
+ * - `releaseId` - The id of the release to edit.
3577
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
3578
+ * @param options - Additional action options.
3579
+ * @returns An observable that resolves to the `transactionId`.
3580
+ */
3581
+ edit({ releaseId, patch }, options) {
3582
+ const editAction = {
3583
+ actionType: "sanity.action.release.edit",
3584
+ releaseId,
3585
+ patch
3586
+ };
3587
+ return _action(this.#client, this.#httpRequest, editAction, options);
3588
+ }
3589
+ /**
3590
+ * @public
3591
+ *
3592
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
3593
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
3594
+ * documents and creation of the corresponding published documents with the new content may
3595
+ * take some time.
3596
+ *
3597
+ * During this period both the source and target documents are locked and cannot be
3598
+ * modified through any other means.
3599
+ *
3600
+ * @category Releases
3601
+ *
3602
+ * @param params - Release action parameters:
3603
+ * - `releaseId` - The id of the release to publish.
3604
+ * @param options - Additional action options.
3605
+ * @returns An observable that resolves to the `transactionId`.
3606
+ */
3607
+ publish({ releaseId }, options) {
3608
+ const publishAction = {
3609
+ actionType: "sanity.action.release.publish",
3610
+ releaseId
3611
+ };
3612
+ return _action(this.#client, this.#httpRequest, publishAction, options);
3613
+ }
3614
+ /**
3615
+ * @public
3616
+ *
3617
+ * An archive action removes an active release. The documents that comprise the release
3618
+ * are deleted and therefore no longer queryable.
3619
+ *
3620
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
3621
+ *
3622
+ * @category Releases
3623
+ *
3624
+ * @param params - Release action parameters:
3625
+ * - `releaseId` - The id of the release to archive.
3626
+ * @param options - Additional action options.
3627
+ * @returns An observable that resolves to the `transactionId`.
3628
+ */
3629
+ archive({ releaseId }, options) {
3630
+ const archiveAction = {
3631
+ actionType: "sanity.action.release.archive",
3632
+ releaseId
3633
+ };
3634
+ return _action(this.#client, this.#httpRequest, archiveAction, options);
3635
+ }
3636
+ /**
3637
+ * @public
3638
+ *
3639
+ * An unarchive action restores an archived release and all documents
3640
+ * with the content they had just prior to archiving.
3641
+ *
3642
+ * @category Releases
3643
+ *
3644
+ * @param params - Release action parameters:
3645
+ * - `releaseId` - The id of the release to unarchive.
3646
+ * @param options - Additional action options.
3647
+ * @returns An observable that resolves to the `transactionId`.
3648
+ */
3649
+ unarchive({ releaseId }, options) {
3650
+ const unarchiveAction = {
3651
+ actionType: "sanity.action.release.unarchive",
3652
+ releaseId
3653
+ };
3654
+ return _action(this.#client, this.#httpRequest, unarchiveAction, options);
3655
+ }
3656
+ /**
3657
+ * @public
3658
+ *
3659
+ * A schedule action queues a release for publishing at the given future time.
3660
+ * The release is locked such that no documents in the release can be modified and
3661
+ * no documents that it references can be deleted as this would make the publish fail.
3662
+ * At the given time, the same logic as for the publish action is triggered.
3663
+ *
3664
+ * @category Releases
3665
+ *
3666
+ * @param params - Release action parameters:
3667
+ * - `releaseId` - The id of the release to schedule.
3668
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
3669
+ * @param options - Additional action options.
3670
+ * @returns An observable that resolves to the `transactionId`.
3671
+ */
3672
+ schedule({ releaseId, publishAt }, options) {
3673
+ const scheduleAction = {
3674
+ actionType: "sanity.action.release.schedule",
3675
+ releaseId,
3676
+ publishAt
3677
+ };
3678
+ return _action(this.#client, this.#httpRequest, scheduleAction, options);
3679
+ }
3680
+ /**
3681
+ * @public
3682
+ *
3683
+ * An unschedule action stops a release from being published.
3684
+ * The documents in the release are considered unlocked and can be edited again.
3685
+ * This may fail if another release is scheduled to be published after this one and
3686
+ * has a reference to a document created by this one.
3687
+ *
3688
+ * @category Releases
3689
+ *
3690
+ * @param params - Release action parameters:
3691
+ * - `releaseId` - The id of the release to unschedule.
3692
+ * @param options - Additional action options.
3693
+ * @returns An observable that resolves to the `transactionId`.
3694
+ */
3695
+ unschedule({ releaseId }, options) {
3696
+ const unscheduleAction = {
3697
+ actionType: "sanity.action.release.unschedule",
3698
+ releaseId
3699
+ };
3700
+ return _action(this.#client, this.#httpRequest, unscheduleAction, options);
3701
+ }
3702
+ /**
3703
+ * @public
3704
+ *
3705
+ * A delete action removes a published or archived release.
3706
+ * The backing system document will be removed from the dataset.
3707
+ *
3708
+ * @category Releases
3709
+ *
3710
+ * @param params - Release action parameters:
3711
+ * - `releaseId` - The id of the release to delete.
3712
+ * @param options - Additional action options.
3713
+ * @returns An observable that resolves to the `transactionId`.
3714
+ */
3715
+ delete({ releaseId }, options) {
3716
+ const deleteAction = {
3717
+ actionType: "sanity.action.release.delete",
3718
+ releaseId
3719
+ };
3720
+ return _action(this.#client, this.#httpRequest, deleteAction, options);
3721
+ }
3722
+ /**
3723
+ * @public
3724
+ *
3725
+ * Fetch the documents in a release by release id.
3726
+ *
3727
+ * @category Releases
3728
+ *
3729
+ * @param params - Release action parameters:
3730
+ * - `releaseId` - The id of the release to fetch documents for.
3731
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
3732
+ * @returns An observable that resolves to the documents in the release.
3733
+ */
3734
+ fetchDocuments({ releaseId }, options) {
3735
+ return _getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options);
3736
+ }
3737
+ }
3738
+ class ReleasesClient {
3739
+ #client;
3740
+ #httpRequest;
3741
+ constructor(client, httpRequest) {
3742
+ this.#client = client, this.#httpRequest = httpRequest;
3743
+ }
3744
+ /**
3745
+ * @public
3746
+ *
3747
+ * Retrieve a release by id.
3748
+ *
3749
+ * @category Releases
3750
+ *
3751
+ * @param params - Release action parameters:
3752
+ * - `releaseId` - The id of the release to retrieve.
3753
+ * @param options - Additional query options including abort signal and query tag.
3754
+ * @returns A promise that resolves to the release document {@link ReleaseDocument}.
3755
+ *
3756
+ * @example Retrieving a release by id
3757
+ * ```ts
3758
+ * const release = await client.releases.get({releaseId: 'my-release'})
3759
+ * console.log(release)
3760
+ * // {
3761
+ * // _id: '_.releases.my-release',
3762
+ * // name: 'my-release'
3763
+ * // _type: 'system.release',
3764
+ * // metadata: {releaseType: 'asap'},
3765
+ * // _createdAt: '2021-01-01T00:00:00.000Z',
3766
+ * // ...
3767
+ * // }
3768
+ * ```
3769
+ */
3770
+ get({ releaseId }, options) {
3771
+ return lastValueFrom(
3772
+ _getDocument(
3773
+ this.#client,
3774
+ this.#httpRequest,
3775
+ `_.releases.${releaseId}`,
3776
+ options
3777
+ )
3778
+ );
3779
+ }
3780
+ async create(releaseOrOptions, maybeOptions) {
3781
+ const { action, options } = createRelease(releaseOrOptions, maybeOptions), { releaseId, metadata } = action;
3782
+ return { ...await lastValueFrom(
3783
+ _action(this.#client, this.#httpRequest, action, options)
3784
+ ), releaseId, metadata };
3785
+ }
3786
+ /**
3787
+ * @public
3788
+ *
3789
+ * Edits an existing release, updating the metadata.
3790
+ *
3791
+ * @category Releases
3792
+ *
3793
+ * @param params - Release action parameters:
3794
+ * - `releaseId` - The id of the release to edit.
3795
+ * - `patch` - The patch operation to apply on the release metadata {@link PatchMutationOperation}.
3796
+ * @param options - Additional action options.
3797
+ * @returns A promise that resolves to the `transactionId`.
3798
+ */
3799
+ edit({ releaseId, patch }, options) {
3800
+ const editAction = {
3801
+ actionType: "sanity.action.release.edit",
3802
+ releaseId,
3803
+ patch
3804
+ };
3805
+ return lastValueFrom(_action(this.#client, this.#httpRequest, editAction, options));
3806
+ }
3807
+ /**
3808
+ * @public
3809
+ *
3810
+ * Publishes all documents in a release at once. For larger releases the effect of the publish
3811
+ * will be visible immediately when querying but the removal of the `versions.<releasesId>.*`
3812
+ * documents and creation of the corresponding published documents with the new content may
3813
+ * take some time.
3814
+ *
3815
+ * During this period both the source and target documents are locked and cannot be
3816
+ * modified through any other means.
3817
+ *
3818
+ * @category Releases
3819
+ *
3820
+ * @param params - Release action parameters:
3821
+ * - `releaseId` - The id of the release to publish.
3822
+ * @param options - Additional action options.
3823
+ * @returns A promise that resolves to the `transactionId`.
3824
+ */
3825
+ publish({ releaseId }, options) {
3826
+ const publishAction = {
3827
+ actionType: "sanity.action.release.publish",
3828
+ releaseId
3829
+ };
3830
+ return lastValueFrom(_action(this.#client, this.#httpRequest, publishAction, options));
3831
+ }
3832
+ /**
3833
+ * @public
3834
+ *
3835
+ * An archive action removes an active release. The documents that comprise the release
3836
+ * are deleted and therefore no longer queryable.
3837
+ *
3838
+ * While the documents remain in retention the last version can still be accessed using document history endpoint.
3839
+ *
3840
+ * @category Releases
3841
+ *
3842
+ * @param params - Release action parameters:
3843
+ * - `releaseId` - The id of the release to archive.
3844
+ * @param options - Additional action options.
3845
+ * @returns A promise that resolves to the `transactionId`.
3846
+ */
3847
+ archive({ releaseId }, options) {
3848
+ const archiveAction = {
3849
+ actionType: "sanity.action.release.archive",
3850
+ releaseId
3851
+ };
3852
+ return lastValueFrom(_action(this.#client, this.#httpRequest, archiveAction, options));
3853
+ }
3854
+ /**
3855
+ * @public
3856
+ *
3857
+ * An unarchive action restores an archived release and all documents
3858
+ * with the content they had just prior to archiving.
3859
+ *
3860
+ * @category Releases
3861
+ *
3862
+ * @param params - Release action parameters:
3863
+ * - `releaseId` - The id of the release to unarchive.
3864
+ * @param options - Additional action options.
3865
+ * @returns A promise that resolves to the `transactionId`.
3866
+ */
3867
+ unarchive({ releaseId }, options) {
3868
+ const unarchiveAction = {
3869
+ actionType: "sanity.action.release.unarchive",
3870
+ releaseId
3871
+ };
3872
+ return lastValueFrom(_action(this.#client, this.#httpRequest, unarchiveAction, options));
3873
+ }
3874
+ /**
3875
+ * @public
3876
+ *
3877
+ * A schedule action queues a release for publishing at the given future time.
3878
+ * The release is locked such that no documents in the release can be modified and
3879
+ * no documents that it references can be deleted as this would make the publish fail.
3880
+ * At the given time, the same logic as for the publish action is triggered.
3881
+ *
3882
+ * @category Releases
3883
+ *
3884
+ * @param params - Release action parameters:
3885
+ * - `releaseId` - The id of the release to schedule.
3886
+ * - `publishAt` - The serialised date and time to publish the release. If the `publishAt` is in the past, the release will be published immediately.
3887
+ * @param options - Additional action options.
3888
+ * @returns A promise that resolves to the `transactionId`.
3889
+ */
3890
+ schedule({ releaseId, publishAt }, options) {
3891
+ const scheduleAction = {
3892
+ actionType: "sanity.action.release.schedule",
3893
+ releaseId,
3894
+ publishAt
3895
+ };
3896
+ return lastValueFrom(_action(this.#client, this.#httpRequest, scheduleAction, options));
3897
+ }
3898
+ /**
3899
+ * @public
3900
+ *
3901
+ * An unschedule action stops a release from being published.
3902
+ * The documents in the release are considered unlocked and can be edited again.
3903
+ * This may fail if another release is scheduled to be published after this one and
3904
+ * has a reference to a document created by this one.
3905
+ *
3906
+ * @category Releases
3907
+ *
3908
+ * @param params - Release action parameters:
3909
+ * - `releaseId` - The id of the release to unschedule.
3910
+ * @param options - Additional action options.
3911
+ * @returns A promise that resolves to the `transactionId`.
3912
+ */
3913
+ unschedule({ releaseId }, options) {
3914
+ const unscheduleAction = {
3915
+ actionType: "sanity.action.release.unschedule",
3916
+ releaseId
3917
+ };
3918
+ return lastValueFrom(_action(this.#client, this.#httpRequest, unscheduleAction, options));
3919
+ }
3920
+ /**
3921
+ * @public
3922
+ *
3923
+ * A delete action removes a published or archived release.
3924
+ * The backing system document will be removed from the dataset.
3925
+ *
3926
+ * @category Releases
3927
+ *
3928
+ * @param params - Release action parameters:
3929
+ * - `releaseId` - The id of the release to delete.
3930
+ * @param options - Additional action options.
3931
+ * @returns A promise that resolves to the `transactionId`.
3932
+ */
3933
+ delete({ releaseId }, options) {
3934
+ const deleteAction = {
3935
+ actionType: "sanity.action.release.delete",
3936
+ releaseId
3937
+ };
3938
+ return lastValueFrom(_action(this.#client, this.#httpRequest, deleteAction, options));
3939
+ }
3940
+ /**
3941
+ * @public
3942
+ *
3943
+ * Fetch the documents in a release by release id.
3944
+ *
3945
+ * @category Releases
3946
+ *
3947
+ * @param params - Release action parameters:
3948
+ * - `releaseId` - The id of the release to fetch documents for.
3949
+ * @param options - Additional mutation options {@link BaseMutationOptions}.
3950
+ * @returns A promise that resolves to the documents in the release.
3951
+ */
3952
+ fetchDocuments({ releaseId }, options) {
3953
+ return lastValueFrom(_getReleaseDocuments(this.#client, this.#httpRequest, releaseId, options));
3954
+ }
3955
+ }
3330
3956
  class ObservableUsersClient {
3331
3957
  #client;
3332
3958
  #httpRequest;
@@ -3372,6 +3998,7 @@ ${selectionOpts}`);
3372
3998
  projects;
3373
3999
  users;
3374
4000
  agent;
4001
+ releases;
3375
4002
  /**
3376
4003
  * Private properties
3377
4004
  */
@@ -3384,7 +4011,7 @@ ${selectionOpts}`);
3384
4011
  constructor(httpRequest, config = defaultConfig) {
3385
4012
  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 = {
3386
4013
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
3387
- };
4014
+ }, this.releases = new ObservableReleasesClient(this, this.#httpRequest);
3388
4015
  }
3389
4016
  /**
3390
4017
  * Clone the client - returns a new instance
@@ -3457,9 +4084,96 @@ ${selectionOpts}`);
3457
4084
  createOrReplace(document, options) {
3458
4085
  return _createOrReplace(this, this.#httpRequest, document, options);
3459
4086
  }
4087
+ createVersion({
4088
+ document,
4089
+ publishedId,
4090
+ releaseId
4091
+ }, options) {
4092
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
4093
+ document,
4094
+ publishedId,
4095
+ releaseId
4096
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || getPublishedId$1(document._id);
4097
+ return _createVersion(
4098
+ this,
4099
+ this.#httpRequest,
4100
+ documentVersion,
4101
+ versionPublishedId,
4102
+ options
4103
+ );
4104
+ }
3460
4105
  delete(selection, options) {
3461
4106
  return _delete(this, this.#httpRequest, selection, options);
3462
4107
  }
4108
+ /**
4109
+ * @public
4110
+ *
4111
+ * Deletes the draft or release version of a document.
4112
+ *
4113
+ * @remarks
4114
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
4115
+ * * If the draft or release version does not exist, any error will throw.
4116
+ *
4117
+ * @param params - Version action parameters:
4118
+ * - `releaseId` - The ID of the release to discard the document from.
4119
+ * - `publishedId` - The published ID of the document to discard.
4120
+ * @param purge - if `true` the document history is also discarded.
4121
+ * @param options - Additional action options.
4122
+ * @returns an observable that resolves to the `transactionId`.
4123
+ *
4124
+ * @example Discarding a release version of a document
4125
+ * ```ts
4126
+ * client.observable.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
4127
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
4128
+ * ```
4129
+ *
4130
+ * @example Discarding a draft version of a document
4131
+ * ```ts
4132
+ * client.observable.discardVersion({publishedId: 'myDocument'})
4133
+ * // The document with the ID `drafts.myDocument` will be discarded.
4134
+ * ```
4135
+ */
4136
+ discardVersion({ releaseId, publishedId }, purge, options) {
4137
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
4138
+ return _discardVersion(this, this.#httpRequest, documentVersionId, purge, options);
4139
+ }
4140
+ replaceVersion({
4141
+ document,
4142
+ publishedId,
4143
+ releaseId
4144
+ }, options) {
4145
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
4146
+ document,
4147
+ publishedId,
4148
+ releaseId
4149
+ }), documentVersion = { ...document, _id: documentVersionId };
4150
+ return _replaceVersion(this, this.#httpRequest, documentVersion, options);
4151
+ }
4152
+ /**
4153
+ * @public
4154
+ *
4155
+ * Used to indicate when a document within a release should be unpublished when
4156
+ * the release is run.
4157
+ *
4158
+ * @remarks
4159
+ * * If the published document does not exist, an error will be thrown.
4160
+ *
4161
+ * @param params - Version action parameters:
4162
+ * - `releaseId` - The ID of the release to unpublish the document from.
4163
+ * - `publishedId` - The published ID of the document to unpublish.
4164
+ * @param options - Additional action options.
4165
+ * @returns an observable that resolves to the `transactionId`.
4166
+ *
4167
+ * @example Unpublishing a release version of a published document
4168
+ * ```ts
4169
+ * client.observable.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
4170
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
4171
+ * ```
4172
+ */
4173
+ unpublishVersion({ releaseId, publishedId }, options) {
4174
+ const versionId = getVersionId(publishedId, releaseId);
4175
+ return _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options);
4176
+ }
3463
4177
  mutate(operations, options) {
3464
4178
  return _mutate(this, this.#httpRequest, operations, options);
3465
4179
  }
@@ -3524,6 +4238,7 @@ ${selectionOpts}`);
3524
4238
  projects;
3525
4239
  users;
3526
4240
  agent;
4241
+ releases;
3527
4242
  /**
3528
4243
  * Observable version of the Sanity client, with the same configuration as the promise-based one
3529
4244
  */
@@ -3540,7 +4255,7 @@ ${selectionOpts}`);
3540
4255
  constructor(httpRequest, config = defaultConfig) {
3541
4256
  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 = {
3542
4257
  action: new AgentActionsClient(this, this.#httpRequest)
3543
- }, this.observable = new ObservableSanityClient(httpRequest, config);
4258
+ }, this.releases = new ReleasesClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
3544
4259
  }
3545
4260
  /**
3546
4261
  * Clone the client - returns a new instance
@@ -3621,9 +4336,104 @@ ${selectionOpts}`);
3621
4336
  _createOrReplace(this, this.#httpRequest, document, options)
3622
4337
  );
3623
4338
  }
4339
+ createVersion({
4340
+ document,
4341
+ publishedId,
4342
+ releaseId
4343
+ }, options) {
4344
+ const documentVersionId = deriveDocumentVersionId("createVersion", {
4345
+ document,
4346
+ publishedId,
4347
+ releaseId
4348
+ }), documentVersion = { ...document, _id: documentVersionId }, versionPublishedId = publishedId || getPublishedId$1(document._id);
4349
+ return firstValueFrom(
4350
+ _createVersion(
4351
+ this,
4352
+ this.#httpRequest,
4353
+ documentVersion,
4354
+ versionPublishedId,
4355
+ options
4356
+ )
4357
+ );
4358
+ }
3624
4359
  delete(selection, options) {
3625
4360
  return lastValueFrom(_delete(this, this.#httpRequest, selection, options));
3626
4361
  }
4362
+ /**
4363
+ * @public
4364
+ *
4365
+ * Deletes the draft or release version of a document.
4366
+ *
4367
+ * @remarks
4368
+ * * Discarding a version with no `releaseId` will discard the draft version of the published document.
4369
+ * * If the draft or release version does not exist, any error will throw.
4370
+ *
4371
+ * @param params - Version action parameters:
4372
+ * - `releaseId` - The ID of the release to discard the document from.
4373
+ * - `publishedId` - The published ID of the document to discard.
4374
+ * @param purge - if `true` the document history is also discarded.
4375
+ * @param options - Additional action options.
4376
+ * @returns a promise that resolves to the `transactionId`.
4377
+ *
4378
+ * @example Discarding a release version of a document
4379
+ * ```ts
4380
+ * client.discardVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
4381
+ * // The document with the ID `versions.myRelease.myDocument` will be discarded.
4382
+ * ```
4383
+ *
4384
+ * @example Discarding a draft version of a document
4385
+ * ```ts
4386
+ * client.discardVersion({publishedId: 'myDocument'})
4387
+ * // The document with the ID `drafts.myDocument` will be discarded.
4388
+ * ```
4389
+ */
4390
+ discardVersion({ releaseId, publishedId }, purge, options) {
4391
+ const documentVersionId = getDocumentVersionId(publishedId, releaseId);
4392
+ return lastValueFrom(
4393
+ _discardVersion(this, this.#httpRequest, documentVersionId, purge, options)
4394
+ );
4395
+ }
4396
+ replaceVersion({
4397
+ document,
4398
+ publishedId,
4399
+ releaseId
4400
+ }, options) {
4401
+ const documentVersionId = deriveDocumentVersionId("replaceVersion", {
4402
+ document,
4403
+ publishedId,
4404
+ releaseId
4405
+ }), documentVersion = { ...document, _id: documentVersionId };
4406
+ return firstValueFrom(
4407
+ _replaceVersion(this, this.#httpRequest, documentVersion, options)
4408
+ );
4409
+ }
4410
+ /**
4411
+ * @public
4412
+ *
4413
+ * Used to indicate when a document within a release should be unpublished when
4414
+ * the release is run.
4415
+ *
4416
+ * @remarks
4417
+ * * If the published document does not exist, an error will be thrown.
4418
+ *
4419
+ * @param params - Version action parameters:
4420
+ * - `releaseId` - The ID of the release to unpublish the document from.
4421
+ * - `publishedId` - The published ID of the document to unpublish.
4422
+ * @param options - Additional action options.
4423
+ * @returns a promise that resolves to the `transactionId`.
4424
+ *
4425
+ * @example Unpublishing a release version of a published document
4426
+ * ```ts
4427
+ * await client.unpublishVersion({publishedId: 'myDocument', releaseId: 'myRelease'})
4428
+ * // The document with the ID `versions.myRelease.myDocument` will be unpublished. when `myRelease` is run.
4429
+ * ```
4430
+ */
4431
+ unpublishVersion({ releaseId, publishedId }, options) {
4432
+ const versionId = getVersionId(publishedId, releaseId);
4433
+ return lastValueFrom(
4434
+ _unpublishVersion(this, this.#httpRequest, versionId, publishedId, options)
4435
+ );
4436
+ }
3627
4437
  mutate(operations, options) {
3628
4438
  return lastValueFrom(_mutate(this, this.#httpRequest, operations, options));
3629
4439
  }