@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.
@@ -543,18 +543,43 @@ export class ObservableSanityClient {
543
543
  },
544
544
  options?: BaseActionOptions,
545
545
  ): Observable<SingleActionResult | MultipleActionResult>
546
+ createVersion(
547
+ args: {
548
+ baseId: string
549
+ releaseId: string
550
+ publishedId: string
551
+ ifBaseRevisionId?: string
552
+ },
553
+ options?: BaseActionOptions,
554
+ ): Observable<SingleActionResult | MultipleActionResult>
546
555
  createVersion<R extends Record<string, Any>>(
547
556
  {
548
557
  document,
549
558
  publishedId,
550
559
  releaseId,
560
+ baseId,
561
+ ifBaseRevisionId,
551
562
  }: {
552
- document: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
563
+ document?: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
553
564
  publishedId?: string
554
565
  releaseId?: string
566
+ baseId?: string
567
+ ifBaseRevisionId?: string
555
568
  },
556
569
  options?: BaseActionOptions,
557
570
  ): Observable<SingleActionResult | MultipleActionResult> {
571
+ if (!document) {
572
+ return dataMethods._createVersionFromBase(
573
+ this,
574
+ this.#httpRequest,
575
+ publishedId,
576
+ baseId,
577
+ releaseId,
578
+ ifBaseRevisionId,
579
+ options,
580
+ )
581
+ }
582
+
558
583
  const documentVersionId = deriveDocumentVersionId('createVersion', {
559
584
  document,
560
585
  publishedId,
@@ -1446,7 +1471,8 @@ export class SanityClient {
1446
1471
  * Creates a new version of a published document.
1447
1472
  *
1448
1473
  * @remarks
1449
- * * Requires a document with a `_type` property.
1474
+ * * 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.
1475
+ * * If `document` is provided, it must have a `_type` property.
1450
1476
  * * Creating a version with no `releaseId` will create a new draft version of the published document.
1451
1477
  * * 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`.
1452
1478
  * * If the `document._id` is not defined, it will be generated from `publishedId` and `releaseId`.
@@ -1455,17 +1481,18 @@ export class SanityClient {
1455
1481
  * @category Versions
1456
1482
  *
1457
1483
  * @param params - Version action parameters:
1484
+ * - `baseId` - The ID of the published document from which to create a new version from.
1485
+ * - `ifBaseRevisionId` - If `baseId` is provided, this ensures the `baseId`'s revision Id is as expected before creating the new version from it.
1458
1486
  * - `document` - The document to create as a new version (must include `_type`).
1459
1487
  * - `publishedId` - The ID of the published document being versioned.
1460
1488
  * - `releaseId` - The ID of the release to create the version for.
1461
1489
  * @param options - Additional action options.
1462
1490
  * @returns A promise that resolves to the `transactionId`.
1463
1491
  *
1464
- * @example Creating a new version of a published document with a generated version ID
1492
+ * @example Creating a new version of a published document
1465
1493
  * ```ts
1466
1494
  * const transactionId = await client.createVersion({
1467
- * // The document does not need to include an `_id` property since it will be generated from `publishedId` and `releaseId`
1468
- * document: {_type: 'myDocument', title: 'My Document'},
1495
+ * baseId: 'myDocument',
1469
1496
  * publishedId: 'myDocument',
1470
1497
  * releaseId: 'myRelease',
1471
1498
  * })
@@ -1478,25 +1505,11 @@ export class SanityClient {
1478
1505
  * // }
1479
1506
  * ```
1480
1507
  *
1481
- * @example Creating a new version of a published document with a specified version ID
1482
- * ```ts
1483
- * const transactionId = await client.createVersion({
1484
- * document: {_type: 'myDocument', _id: 'versions.myRelease.myDocument', title: 'My Document'},
1485
- * // `publishedId` and `releaseId` are not required since `document._id` has been specified
1486
- * })
1487
- *
1488
- * // The following document will be created:
1489
- * // {
1490
- * // _id: 'versions.myRelease.myDocument',
1491
- * // _type: 'myDocument',
1492
- * // title: 'My Document',
1493
- * // }
1494
- * ```
1495
1508
  *
1496
1509
  * @example Creating a new draft version of a published document
1497
1510
  * ```ts
1498
1511
  * const transactionId = await client.createVersion({
1499
- * document: {_type: 'myDocument', title: 'My Document'},
1512
+ * baseId: 'myDocument',
1500
1513
  * publishedId: 'myDocument',
1501
1514
  * })
1502
1515
  *
@@ -1524,18 +1537,45 @@ export class SanityClient {
1524
1537
  },
1525
1538
  options?: BaseActionOptions,
1526
1539
  ): Promise<SingleActionResult | MultipleActionResult>
1540
+ createVersion(
1541
+ args: {
1542
+ publishedId: string
1543
+ baseId: string
1544
+ releaseId: string
1545
+ ifBaseRevisionId?: string
1546
+ },
1547
+ options?: BaseActionOptions,
1548
+ ): Promise<SingleActionResult | MultipleActionResult>
1527
1549
  createVersion<R extends Record<string, Any>>(
1528
1550
  {
1529
1551
  document,
1530
1552
  publishedId,
1531
1553
  releaseId,
1554
+ baseId,
1555
+ ifBaseRevisionId,
1532
1556
  }: {
1533
- document: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
1557
+ document?: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
1534
1558
  publishedId?: string
1535
1559
  releaseId?: string
1560
+ baseId?: string
1561
+ ifBaseRevisionId?: string
1536
1562
  },
1537
1563
  options?: BaseActionOptions,
1538
1564
  ): Promise<SingleActionResult | MultipleActionResult> {
1565
+ if (!document) {
1566
+ return firstValueFrom(
1567
+ dataMethods._createVersionFromBase(
1568
+ this,
1569
+ this.#httpRequest,
1570
+ publishedId,
1571
+ baseId,
1572
+ releaseId,
1573
+ ifBaseRevisionId,
1574
+ options,
1575
+ ),
1576
+ )
1577
+ }
1578
+
1539
1579
  const documentVersionId = deriveDocumentVersionId('createVersion', {
1540
1580
  document,
1541
1581
  publishedId,
@@ -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
@@ -508,6 +508,7 @@ export interface CurrentSanityUser {
508
508
  email: string
509
509
  profileImage: string | null
510
510
  role: string
511
+ provider: string
511
512
  }
512
513
 
513
514
  /** @public */
@@ -725,16 +726,29 @@ export interface DeleteReleaseAction {
725
726
  }
726
727
 
727
728
  /**
728
- * Creates a new version of an existing document, attached to the release as given
729
- * by `document._id`
729
+ * Creates a new version of an existing document.
730
+ *
731
+ * If the `document` is provided, the version is created from the document
732
+ * attached to the release as given by `document._id`
733
+ *
734
+ * If the `baseId` and `versionId` are provided, the version is created from the base document
735
+ * and the version is attached to the release as given by `publishedId` and `versionId`
730
736
  *
731
737
  * @public
732
738
  */
733
- export interface CreateVersionAction {
739
+ export type CreateVersionAction = {
734
740
  actionType: 'sanity.action.document.version.create'
735
741
  publishedId: string
736
- document: IdentifiedSanityDocumentStub
737
- }
742
+ } & (
743
+ | {
744
+ document: IdentifiedSanityDocumentStub
745
+ }
746
+ | {
747
+ baseId: string
748
+ versionId: string
749
+ ifBaseRevisionId?: string
750
+ }
751
+ )
738
752
 
739
753
  /**
740
754
  * 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,