@sanity/client 7.7.0 → 7.8.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.
@@ -1005,16 +1005,29 @@ export declare interface CreateReleaseAction {
1005
1005
  }
1006
1006
 
1007
1007
  /**
1008
- * Creates a new version of an existing document, attached to the release as given
1009
- * by `document._id`
1008
+ * Creates a new version of an existing document.
1009
+ *
1010
+ * If the `document` is provided, the version is created from the document
1011
+ * attached to the release as given by `document._id`
1012
+ *
1013
+ * If the `baseId` and `versionId` are provided, the version is created from the base document
1014
+ * and the version is attached to the release as given by `publishedId` and `versionId`
1010
1015
  *
1011
1016
  * @public
1012
1017
  */
1013
- export declare interface CreateVersionAction {
1018
+ export declare type CreateVersionAction = {
1014
1019
  actionType: 'sanity.action.document.version.create'
1015
1020
  publishedId: string
1016
- document: IdentifiedSanityDocumentStub
1017
- }
1021
+ } & (
1022
+ | {
1023
+ document: IdentifiedSanityDocumentStub
1024
+ }
1025
+ | {
1026
+ baseId: string
1027
+ versionId: string
1028
+ ifBaseRevisionId?: string
1029
+ }
1030
+ )
1018
1031
 
1019
1032
  /** @public */
1020
1033
  export declare interface CurrentSanityUser {
@@ -1023,6 +1036,7 @@ export declare interface CurrentSanityUser {
1023
1036
  email: string
1024
1037
  profileImage: string | null
1025
1038
  role: string
1039
+ provider: string
1026
1040
  }
1027
1041
 
1028
1042
  /** @public */
@@ -3119,6 +3133,15 @@ export declare class ObservableSanityClient {
3119
3133
  },
3120
3134
  options?: BaseActionOptions,
3121
3135
  ): Observable<SingleActionResult | MultipleActionResult>
3136
+ createVersion(
3137
+ args: {
3138
+ baseId: string
3139
+ releaseId: string
3140
+ publishedId: string
3141
+ ifBaseRevisionId?: string
3142
+ },
3143
+ options?: BaseActionOptions,
3144
+ ): Observable<SingleActionResult | MultipleActionResult>
3122
3145
  /**
3123
3146
  * Deletes a document with the given document ID.
3124
3147
  * Returns an observable that resolves to the deleted document.
@@ -4834,7 +4857,8 @@ export declare class SanityClient {
4834
4857
  * Creates a new version of a published document.
4835
4858
  *
4836
4859
  * @remarks
4837
- * * Requires a document with a `_type` property.
4860
+ * * The preferred approach is to use `baseId` to refer to the existing published document, but it is also possible to provide a complete `document` instead.
4861
+ * * If `document` is provided, it must have a `_type` property.
4838
4862
  * * Creating a version with no `releaseId` will create a new draft version of the published document.
4839
4863
  * * If the `document._id` is defined, it should be a draft or release version ID that matches the version ID generated from `publishedId` and `releaseId`.
4840
4864
  * * If the `document._id` is not defined, it will be generated from `publishedId` and `releaseId`.
@@ -4843,17 +4867,18 @@ export declare class SanityClient {
4843
4867
  * @category Versions
4844
4868
  *
4845
4869
  * @param params - Version action parameters:
4870
+ * - `baseId` - The ID of the published document from which to create a new version from.
4871
+ * - `ifBaseRevisionId` - If `baseId` is provided, this ensures the `baseId`'s revision Id is as expected before creating the new version from it.
4846
4872
  * - `document` - The document to create as a new version (must include `_type`).
4847
4873
  * - `publishedId` - The ID of the published document being versioned.
4848
4874
  * - `releaseId` - The ID of the release to create the version for.
4849
4875
  * @param options - Additional action options.
4850
4876
  * @returns A promise that resolves to the `transactionId`.
4851
4877
  *
4852
- * @example Creating a new version of a published document with a generated version ID
4878
+ * @example Creating a new version of a published document
4853
4879
  * ```ts
4854
4880
  * const transactionId = await client.createVersion({
4855
- * // The document does not need to include an `_id` property since it will be generated from `publishedId` and `releaseId`
4856
- * document: {_type: 'myDocument', title: 'My Document'},
4881
+ * baseId: 'myDocument',
4857
4882
  * publishedId: 'myDocument',
4858
4883
  * releaseId: 'myRelease',
4859
4884
  * })
@@ -4866,25 +4891,11 @@ export declare class SanityClient {
4866
4891
  * // }
4867
4892
  * ```
4868
4893
  *
4869
- * @example Creating a new version of a published document with a specified version ID
4870
- * ```ts
4871
- * const transactionId = await client.createVersion({
4872
- * document: {_type: 'myDocument', _id: 'versions.myRelease.myDocument', title: 'My Document'},
4873
- * // `publishedId` and `releaseId` are not required since `document._id` has been specified
4874
- * })
4875
- *
4876
- * // The following document will be created:
4877
- * // {
4878
- * // _id: 'versions.myRelease.myDocument',
4879
- * // _type: 'myDocument',
4880
- * // title: 'My Document',
4881
- * // }
4882
- * ```
4883
4894
  *
4884
4895
  * @example Creating a new draft version of a published document
4885
4896
  * ```ts
4886
4897
  * const transactionId = await client.createVersion({
4887
- * document: {_type: 'myDocument', title: 'My Document'},
4898
+ * baseId: 'myDocument',
4888
4899
  * publishedId: 'myDocument',
4889
4900
  * })
4890
4901
  *
@@ -4912,6 +4923,15 @@ export declare class SanityClient {
4912
4923
  },
4913
4924
  options?: BaseActionOptions,
4914
4925
  ): Promise<SingleActionResult | MultipleActionResult>
4926
+ createVersion(
4927
+ args: {
4928
+ publishedId: string
4929
+ baseId: string
4930
+ releaseId: string
4931
+ ifBaseRevisionId?: string
4932
+ },
4933
+ options?: BaseActionOptions,
4934
+ ): Promise<SingleActionResult | MultipleActionResult>
4915
4935
  /**
4916
4936
  * Deletes a document with the given document ID.
4917
4937
  * Returns a promise that resolves to the deleted document.
@@ -1005,16 +1005,29 @@ export declare interface CreateReleaseAction {
1005
1005
  }
1006
1006
 
1007
1007
  /**
1008
- * Creates a new version of an existing document, attached to the release as given
1009
- * by `document._id`
1008
+ * Creates a new version of an existing document.
1009
+ *
1010
+ * If the `document` is provided, the version is created from the document
1011
+ * attached to the release as given by `document._id`
1012
+ *
1013
+ * If the `baseId` and `versionId` are provided, the version is created from the base document
1014
+ * and the version is attached to the release as given by `publishedId` and `versionId`
1010
1015
  *
1011
1016
  * @public
1012
1017
  */
1013
- export declare interface CreateVersionAction {
1018
+ export declare type CreateVersionAction = {
1014
1019
  actionType: 'sanity.action.document.version.create'
1015
1020
  publishedId: string
1016
- document: IdentifiedSanityDocumentStub
1017
- }
1021
+ } & (
1022
+ | {
1023
+ document: IdentifiedSanityDocumentStub
1024
+ }
1025
+ | {
1026
+ baseId: string
1027
+ versionId: string
1028
+ ifBaseRevisionId?: string
1029
+ }
1030
+ )
1018
1031
 
1019
1032
  /** @public */
1020
1033
  export declare interface CurrentSanityUser {
@@ -1023,6 +1036,7 @@ export declare interface CurrentSanityUser {
1023
1036
  email: string
1024
1037
  profileImage: string | null
1025
1038
  role: string
1039
+ provider: string
1026
1040
  }
1027
1041
 
1028
1042
  /** @public */
@@ -3119,6 +3133,15 @@ export declare class ObservableSanityClient {
3119
3133
  },
3120
3134
  options?: BaseActionOptions,
3121
3135
  ): Observable<SingleActionResult | MultipleActionResult>
3136
+ createVersion(
3137
+ args: {
3138
+ baseId: string
3139
+ releaseId: string
3140
+ publishedId: string
3141
+ ifBaseRevisionId?: string
3142
+ },
3143
+ options?: BaseActionOptions,
3144
+ ): Observable<SingleActionResult | MultipleActionResult>
3122
3145
  /**
3123
3146
  * Deletes a document with the given document ID.
3124
3147
  * Returns an observable that resolves to the deleted document.
@@ -4834,7 +4857,8 @@ export declare class SanityClient {
4834
4857
  * Creates a new version of a published document.
4835
4858
  *
4836
4859
  * @remarks
4837
- * * Requires a document with a `_type` property.
4860
+ * * The preferred approach is to use `baseId` to refer to the existing published document, but it is also possible to provide a complete `document` instead.
4861
+ * * If `document` is provided, it must have a `_type` property.
4838
4862
  * * Creating a version with no `releaseId` will create a new draft version of the published document.
4839
4863
  * * If the `document._id` is defined, it should be a draft or release version ID that matches the version ID generated from `publishedId` and `releaseId`.
4840
4864
  * * If the `document._id` is not defined, it will be generated from `publishedId` and `releaseId`.
@@ -4843,17 +4867,18 @@ export declare class SanityClient {
4843
4867
  * @category Versions
4844
4868
  *
4845
4869
  * @param params - Version action parameters:
4870
+ * - `baseId` - The ID of the published document from which to create a new version from.
4871
+ * - `ifBaseRevisionId` - If `baseId` is provided, this ensures the `baseId`'s revision Id is as expected before creating the new version from it.
4846
4872
  * - `document` - The document to create as a new version (must include `_type`).
4847
4873
  * - `publishedId` - The ID of the published document being versioned.
4848
4874
  * - `releaseId` - The ID of the release to create the version for.
4849
4875
  * @param options - Additional action options.
4850
4876
  * @returns A promise that resolves to the `transactionId`.
4851
4877
  *
4852
- * @example Creating a new version of a published document with a generated version ID
4878
+ * @example Creating a new version of a published document
4853
4879
  * ```ts
4854
4880
  * const transactionId = await client.createVersion({
4855
- * // The document does not need to include an `_id` property since it will be generated from `publishedId` and `releaseId`
4856
- * document: {_type: 'myDocument', title: 'My Document'},
4881
+ * baseId: 'myDocument',
4857
4882
  * publishedId: 'myDocument',
4858
4883
  * releaseId: 'myRelease',
4859
4884
  * })
@@ -4866,25 +4891,11 @@ export declare class SanityClient {
4866
4891
  * // }
4867
4892
  * ```
4868
4893
  *
4869
- * @example Creating a new version of a published document with a specified version ID
4870
- * ```ts
4871
- * const transactionId = await client.createVersion({
4872
- * document: {_type: 'myDocument', _id: 'versions.myRelease.myDocument', title: 'My Document'},
4873
- * // `publishedId` and `releaseId` are not required since `document._id` has been specified
4874
- * })
4875
- *
4876
- * // The following document will be created:
4877
- * // {
4878
- * // _id: 'versions.myRelease.myDocument',
4879
- * // _type: 'myDocument',
4880
- * // title: 'My Document',
4881
- * // }
4882
- * ```
4883
4894
  *
4884
4895
  * @example Creating a new draft version of a published document
4885
4896
  * ```ts
4886
4897
  * const transactionId = await client.createVersion({
4887
- * document: {_type: 'myDocument', title: 'My Document'},
4898
+ * baseId: 'myDocument',
4888
4899
  * publishedId: 'myDocument',
4889
4900
  * })
4890
4901
  *
@@ -4912,6 +4923,15 @@ export declare class SanityClient {
4912
4923
  },
4913
4924
  options?: BaseActionOptions,
4914
4925
  ): Promise<SingleActionResult | MultipleActionResult>
4926
+ createVersion(
4927
+ args: {
4928
+ publishedId: string
4929
+ baseId: string
4930
+ releaseId: string
4931
+ ifBaseRevisionId?: string
4932
+ },
4933
+ options?: BaseActionOptions,
4934
+ ): Promise<SingleActionResult | MultipleActionResult>
4915
4935
  /**
4916
4936
  * Deletes a document with the given document ID.
4917
4937
  * Returns a promise that resolves to the deleted document.
@@ -319,6 +319,8 @@ const createWarningPrinter = (message) => (
319
319
  `See ${generateHelpUrl("js-client-api-version")}`
320
320
  ]), printNoDefaultExport = createWarningPrinter([
321
321
  "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
322
+ ]), printCreateVersionWithBaseIdWarning = createWarningPrinter([
323
+ "You have called `createVersion()` with a defined `document`. The recommended approach is to provide a `baseId` and `releaseId` instead."
322
324
  ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
323
325
  apiHost: "https://api.sanity.io",
324
326
  apiVersion: "1",
@@ -954,12 +956,27 @@ function _createOrReplace(client, httpRequest, doc, options) {
954
956
  return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
955
957
  }
956
958
  function _createVersion(client, httpRequest, doc, publishedId, options) {
957
- return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
959
+ return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), printCreateVersionWithBaseIdWarning(), _action(client, httpRequest, {
958
960
  actionType: "sanity.action.document.version.create",
959
961
  publishedId,
960
962
  document: doc
961
963
  }, options);
962
964
  }
965
+ function _createVersionFromBase(client, httpRequest, publishedId, baseId, releaseId, ifBaseRevisionId, options) {
966
+ if (!baseId)
967
+ throw new Error("`createVersion()` requires `baseId` when no `document` is provided");
968
+ if (!publishedId)
969
+ throw new Error("`createVersion()` requires `publishedId` when `baseId` is provided");
970
+ validateDocumentId("createVersion", baseId), validateDocumentId("createVersion", publishedId);
971
+ const createVersionAction = {
972
+ actionType: "sanity.action.document.version.create",
973
+ publishedId,
974
+ baseId,
975
+ versionId: releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId),
976
+ ifBaseRevisionId
977
+ };
978
+ return _action(client, httpRequest, createVersionAction, options);
979
+ }
963
980
  function _delete(client, httpRequest, selection, options) {
964
981
  return _dataRequest(
965
982
  client,
@@ -2245,8 +2262,20 @@ class ObservableSanityClient {
2245
2262
  createVersion({
2246
2263
  document,
2247
2264
  publishedId,
2248
- releaseId
2265
+ releaseId,
2266
+ baseId,
2267
+ ifBaseRevisionId
2249
2268
  }, options) {
2269
+ if (!document)
2270
+ return _createVersionFromBase(
2271
+ this,
2272
+ this.#httpRequest,
2273
+ publishedId,
2274
+ baseId,
2275
+ releaseId,
2276
+ ifBaseRevisionId,
2277
+ options
2278
+ );
2250
2279
  const documentVersionId = deriveDocumentVersionId("createVersion", {
2251
2280
  document,
2252
2281
  publishedId,
@@ -2497,8 +2526,22 @@ class SanityClient {
2497
2526
  createVersion({
2498
2527
  document,
2499
2528
  publishedId,
2500
- releaseId
2529
+ releaseId,
2530
+ baseId,
2531
+ ifBaseRevisionId
2501
2532
  }, options) {
2533
+ if (!document)
2534
+ return firstValueFrom(
2535
+ _createVersionFromBase(
2536
+ this,
2537
+ this.#httpRequest,
2538
+ publishedId,
2539
+ baseId,
2540
+ releaseId,
2541
+ ifBaseRevisionId,
2542
+ options
2543
+ )
2544
+ );
2502
2545
  const documentVersionId = deriveDocumentVersionId("createVersion", {
2503
2546
  document,
2504
2547
  publishedId,