@sanity/client 7.7.0 → 7.8.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,4 +1,4 @@
1
- import {getVersionFromId, getVersionId, isDraftId} from '@sanity/client/csm'
1
+ import {getDraftId, getVersionFromId, getVersionId, isDraftId} from '@sanity/client/csm'
2
2
  import {from, type MonoTypeOperatorFunction, Observable} from 'rxjs'
3
3
  import {combineLatestWith, filter, map} from 'rxjs/operators'
4
4
 
@@ -39,7 +39,11 @@ import type {
39
39
  import {getSelection} from '../util/getSelection'
40
40
  import * as validate from '../validators'
41
41
  import * as validators from '../validators'
42
- import {printCdnPreviewDraftsWarning, printPreviewDraftsDeprecationWarning} from '../warnings'
42
+ import {
43
+ printCdnPreviewDraftsWarning,
44
+ printCreateVersionWithBaseIdWarning,
45
+ printPreviewDraftsDeprecationWarning,
46
+ } from '../warnings'
43
47
  import {encodeQueryString} from './encodeQueryString'
44
48
  import {ObservablePatch, Patch} from './patch'
45
49
  import {ObservableTransaction, Transaction} from './transaction'
@@ -266,6 +270,7 @@ export function _createVersion<R extends Record<string, Any>>(
266
270
  ): Observable<SingleActionResult> {
267
271
  validators.requireDocumentId('createVersion', doc)
268
272
  validators.requireDocumentType('createVersion', doc)
273
+ printCreateVersionWithBaseIdWarning()
269
274
 
270
275
  const createVersionAction: CreateVersionAction = {
271
276
  actionType: 'sanity.action.document.version.create',
@@ -276,6 +281,38 @@ export function _createVersion<R extends Record<string, Any>>(
276
281
  return _action(client, httpRequest, createVersionAction, options)
277
282
  }
278
283
 
284
+ /** @internal */
285
+ export function _createVersionFromBase(
286
+ client: ObservableSanityClient | SanityClient,
287
+ httpRequest: HttpRequest,
288
+ publishedId?: string,
289
+ baseId?: string,
290
+ releaseId?: string,
291
+ ifBaseRevisionId?: string,
292
+ options?: BaseActionOptions,
293
+ ): Observable<SingleActionResult> {
294
+ if (!baseId) {
295
+ throw new Error('`createVersion()` requires `baseId` when no `document` is provided')
296
+ }
297
+
298
+ if (!publishedId) {
299
+ throw new Error('`createVersion()` requires `publishedId` when `baseId` is provided')
300
+ }
301
+
302
+ validators.validateDocumentId('createVersion', baseId)
303
+ validators.validateDocumentId('createVersion', publishedId)
304
+
305
+ const createVersionAction: CreateVersionAction = {
306
+ actionType: 'sanity.action.document.version.create',
307
+ publishedId,
308
+ baseId,
309
+ versionId: releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId),
310
+ ifBaseRevisionId,
311
+ }
312
+
313
+ return _action(client, httpRequest, createVersionAction, options)
314
+ }
315
+
279
316
  /** @internal */
280
317
  export function _delete<R extends Record<string, Any>>(
281
318
  client: Client,
package/src/types.ts CHANGED
@@ -725,16 +725,29 @@ export interface DeleteReleaseAction {
725
725
  }
726
726
 
727
727
  /**
728
- * Creates a new version of an existing document, attached to the release as given
729
- * by `document._id`
728
+ * Creates a new version of an existing document.
729
+ *
730
+ * If the `document` is provided, the version is created from the document
731
+ * attached to the release as given by `document._id`
732
+ *
733
+ * If the `baseId` and `versionId` are provided, the version is created from the base document
734
+ * and the version is attached to the release as given by `publishedId` and `versionId`
730
735
  *
731
736
  * @public
732
737
  */
733
- export interface CreateVersionAction {
738
+ export type CreateVersionAction = {
734
739
  actionType: 'sanity.action.document.version.create'
735
740
  publishedId: string
736
- document: IdentifiedSanityDocumentStub
737
- }
741
+ } & (
742
+ | {
743
+ document: IdentifiedSanityDocumentStub
744
+ }
745
+ | {
746
+ baseId: string
747
+ versionId: string
748
+ ifBaseRevisionId?: string
749
+ }
750
+ )
738
751
 
739
752
  /**
740
753
  * Delete a version of a document.
package/src/warnings.ts CHANGED
@@ -46,3 +46,7 @@ export const printNoApiVersionSpecifiedWarning = createWarningPrinter([
46
46
  export const printNoDefaultExport = createWarningPrinter([
47
47
  'The default export of @sanity/client has been deprecated. Use the named export `createClient` instead.',
48
48
  ])
49
+
50
+ export const printCreateVersionWithBaseIdWarning = createWarningPrinter([
51
+ 'You have called `createVersion()` with a defined `document`. The recommended approach is to provide a `baseId` and `releaseId` instead.',
52
+ ])
@@ -2293,6 +2293,8 @@ ${codeFrame(query, { start, end }, description)}${withTag}`;
2293
2293
  `See ${generateHelpUrl("js-client-api-version")}`
2294
2294
  ]), printNoDefaultExport = createWarningPrinter([
2295
2295
  "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
2296
+ ]), printCreateVersionWithBaseIdWarning = createWarningPrinter([
2297
+ "You have called `createVersion()` with a defined `document`. The recommended approach is to provide a `baseId` and `releaseId` instead."
2296
2298
  ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
2297
2299
  apiHost: "https://api.sanity.io",
2298
2300
  apiVersion: "1",
@@ -2928,12 +2930,27 @@ ${selectionOpts}`);
2928
2930
  return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
2929
2931
  }
2930
2932
  function _createVersion(client, httpRequest, doc, publishedId, options) {
2931
- return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
2933
+ return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), printCreateVersionWithBaseIdWarning(), _action(client, httpRequest, {
2932
2934
  actionType: "sanity.action.document.version.create",
2933
2935
  publishedId,
2934
2936
  document: doc
2935
2937
  }, options);
2936
2938
  }
2939
+ function _createVersionFromBase(client, httpRequest, publishedId, baseId, releaseId, ifBaseRevisionId, options) {
2940
+ if (!baseId)
2941
+ throw new Error("`createVersion()` requires `baseId` when no `document` is provided");
2942
+ if (!publishedId)
2943
+ throw new Error("`createVersion()` requires `publishedId` when `baseId` is provided");
2944
+ validateDocumentId("createVersion", baseId), validateDocumentId("createVersion", publishedId);
2945
+ const createVersionAction = {
2946
+ actionType: "sanity.action.document.version.create",
2947
+ publishedId,
2948
+ baseId,
2949
+ versionId: releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId),
2950
+ ifBaseRevisionId
2951
+ };
2952
+ return _action(client, httpRequest, createVersionAction, options);
2953
+ }
2937
2954
  function _delete(client, httpRequest, selection, options) {
2938
2955
  return _dataRequest(
2939
2956
  client,
@@ -4219,8 +4236,20 @@ ${selectionOpts}`);
4219
4236
  createVersion({
4220
4237
  document,
4221
4238
  publishedId,
4222
- releaseId
4239
+ releaseId,
4240
+ baseId,
4241
+ ifBaseRevisionId
4223
4242
  }, options) {
4243
+ if (!document)
4244
+ return _createVersionFromBase(
4245
+ this,
4246
+ this.#httpRequest,
4247
+ publishedId,
4248
+ baseId,
4249
+ releaseId,
4250
+ ifBaseRevisionId,
4251
+ options
4252
+ );
4224
4253
  const documentVersionId = deriveDocumentVersionId("createVersion", {
4225
4254
  document,
4226
4255
  publishedId,
@@ -4471,8 +4500,22 @@ ${selectionOpts}`);
4471
4500
  createVersion({
4472
4501
  document,
4473
4502
  publishedId,
4474
- releaseId
4503
+ releaseId,
4504
+ baseId,
4505
+ ifBaseRevisionId
4475
4506
  }, options) {
4507
+ if (!document)
4508
+ return firstValueFrom(
4509
+ _createVersionFromBase(
4510
+ this,
4511
+ this.#httpRequest,
4512
+ publishedId,
4513
+ baseId,
4514
+ releaseId,
4515
+ ifBaseRevisionId,
4516
+ options
4517
+ )
4518
+ );
4476
4519
  const documentVersionId = deriveDocumentVersionId("createVersion", {
4477
4520
  document,
4478
4521
  publishedId,