@sanity/sdk 2.11.0 → 2.11.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.
package/dist/index.js CHANGED
@@ -9,9 +9,9 @@ import { getVersionId, DocumentId, getPublishedId, getDraftId, isDraftId, isVers
9
9
  import { jsonMatch, stringifyPath, slicePath, getIndexForKey } from "@sanity/json-match";
10
10
  import { getIndexForKey as getIndexForKey2, getPathDepth, joinPaths, jsonMatch as jsonMatch2, slicePath as slicePath2, stringifyPath as stringifyPath2 } from "@sanity/json-match";
11
11
  import { evaluateSync, parse } from "groq-js";
12
- import { diffValue } from "@sanity/diff-patch";
13
12
  import { applyPatches, parsePatch } from "@sanity/diff-match-patch";
14
13
  import { isKeySegment, isKeyedObject } from "@sanity/types";
14
+ import { diffValue } from "@sanity/diff-patch";
15
15
  import { createDocumentLoaderFromClient } from "@sanity/mutate/_unstable_store";
16
16
  import { SDK_CHANNEL_NAME, SDK_NODE_NAME } from "@sanity/message-protocol";
17
17
  import { fromUrl } from "@sanity/bifur-client";
@@ -1290,6 +1290,366 @@ class ActionError extends Error {
1290
1290
  }
1291
1291
  class PermissionActionError extends ActionError {
1292
1292
  }
1293
+ function handleCreate(action, ctx) {
1294
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1295
+ let { base, working } = ctx;
1296
+ const documentId = getId(action.documentId);
1297
+ if (action.liveEdit) {
1298
+ if (working[documentId])
1299
+ throw new ActionError({
1300
+ documentId,
1301
+ transactionId,
1302
+ message: "This document already exists."
1303
+ });
1304
+ const newDocBase2 = { _type: action.documentType, _id: documentId, ...action.initialValue }, mutations2 = [{ create: {
1305
+ _type: action.documentType,
1306
+ _id: documentId,
1307
+ ...action.initialValue
1308
+ } }];
1309
+ if (base = processMutations({
1310
+ documents: base,
1311
+ transactionId,
1312
+ mutations: [{ create: newDocBase2 }],
1313
+ timestamp
1314
+ }), working = processMutations({
1315
+ documents: working,
1316
+ transactionId,
1317
+ mutations: mutations2,
1318
+ timestamp
1319
+ }), !checkGrant(grants.create, working[documentId]))
1320
+ throw new PermissionActionError({
1321
+ documentId,
1322
+ transactionId,
1323
+ message: `You do not have permission to create document "${documentId}".`
1324
+ });
1325
+ return outgoingMutations.push(...mutations2), { base, working };
1326
+ }
1327
+ const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : void 0, draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1328
+ if (versionId ? working[versionId] : working[draftId]) {
1329
+ const errorDocType = versionId ? "release version" : "draft";
1330
+ throw new ActionError({
1331
+ documentId,
1332
+ transactionId,
1333
+ message: `A ${errorDocType} of this document already exists. Please use or discard the existing ${errorDocType} before creating a new one.`
1334
+ });
1335
+ }
1336
+ const newDocBase = {
1337
+ ...base[draftId] ?? base[publishedId],
1338
+ _type: action.documentType,
1339
+ _id: versionId ?? draftId,
1340
+ ...action.initialValue
1341
+ }, newDocWorking = {
1342
+ ...working[draftId] ?? working[publishedId],
1343
+ _type: action.documentType,
1344
+ _id: versionId ?? draftId,
1345
+ ...action.initialValue
1346
+ }, mutations = [{ create: newDocWorking }];
1347
+ if (base = processMutations({
1348
+ documents: base,
1349
+ transactionId,
1350
+ mutations: [{ create: newDocBase }],
1351
+ timestamp
1352
+ }), working = processMutations({
1353
+ documents: working,
1354
+ transactionId,
1355
+ mutations,
1356
+ timestamp
1357
+ }), versionId && !checkGrant(grants.create, working[versionId]))
1358
+ throw new PermissionActionError({
1359
+ documentId,
1360
+ transactionId,
1361
+ message: `You do not have permission to create a release version for document "${documentId}".`
1362
+ });
1363
+ if (!versionId && !checkGrant(grants.create, working[draftId]))
1364
+ throw new PermissionActionError({
1365
+ documentId,
1366
+ transactionId,
1367
+ message: `You do not have permission to create a draft for document "${documentId}".`
1368
+ });
1369
+ return outgoingMutations.push(...mutations), outgoingActions.push({
1370
+ actionType: "sanity.action.document.version.create",
1371
+ publishedId,
1372
+ attributes: newDocWorking
1373
+ }), { base, working };
1374
+ }
1375
+ function handleDelete(action, ctx) {
1376
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1377
+ let { base, working } = ctx;
1378
+ const documentId = action.documentId;
1379
+ if (isReleasePerspective(action.perspective))
1380
+ throw new ActionError({
1381
+ documentId,
1382
+ transactionId,
1383
+ message: 'Cannot delete a version document. You may want to use the "unpublish" or "discard" actions instead.'
1384
+ });
1385
+ if (action.liveEdit) {
1386
+ if (!working[documentId])
1387
+ throw new ActionError({
1388
+ documentId,
1389
+ transactionId,
1390
+ message: "The document you are trying to delete does not exist."
1391
+ });
1392
+ if (!checkGrant(grants.update, working[documentId]))
1393
+ throw new PermissionActionError({
1394
+ documentId,
1395
+ transactionId,
1396
+ message: "You do not have permission to delete this document."
1397
+ });
1398
+ const mutations2 = [{ delete: { id: documentId } }];
1399
+ return base = processMutations({ documents: base, transactionId, mutations: mutations2, timestamp }), working = processMutations({ documents: working, transactionId, mutations: mutations2, timestamp }), outgoingMutations.push(...mutations2), { base, working };
1400
+ }
1401
+ const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1402
+ if (!working[publishedId])
1403
+ throw new ActionError({
1404
+ documentId,
1405
+ transactionId,
1406
+ message: working[draftId] ? "Cannot delete a document without a published version." : "The document you are trying to delete does not exist."
1407
+ });
1408
+ const cantDeleteDraft = working[draftId] && !checkGrant(grants.update, working[draftId]), cantDeletePublished = working[publishedId] && !checkGrant(grants.update, working[publishedId]);
1409
+ if (cantDeleteDraft || cantDeletePublished)
1410
+ throw new PermissionActionError({
1411
+ documentId,
1412
+ transactionId,
1413
+ message: "You do not have permission to delete this document."
1414
+ });
1415
+ const mutations = [{ delete: { id: publishedId } }, { delete: { id: draftId } }], includeDrafts = working[draftId] ? [draftId] : void 0;
1416
+ return base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1417
+ actionType: "sanity.action.document.delete",
1418
+ publishedId,
1419
+ ...includeDrafts ? { includeDrafts } : {}
1420
+ }), { base, working };
1421
+ }
1422
+ function handleDiscard(action, ctx) {
1423
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1424
+ let { base, working } = ctx;
1425
+ const documentId = getId(action.documentId);
1426
+ if (action.liveEdit)
1427
+ throw new ActionError({
1428
+ documentId,
1429
+ transactionId,
1430
+ message: `Cannot discard changes for liveEdit document "${documentId}". LiveEdit documents do not support drafts.`
1431
+ });
1432
+ const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : getDraftId(DocumentId(documentId)), mutations = [{ delete: { id: versionId } }];
1433
+ if (!working[versionId])
1434
+ throw new ActionError({
1435
+ documentId,
1436
+ transactionId,
1437
+ message: `There is no draft or version available to discard for document "${documentId}".`
1438
+ });
1439
+ if (!checkGrant(grants.update, working[versionId]))
1440
+ throw new PermissionActionError({
1441
+ documentId,
1442
+ transactionId,
1443
+ message: `You do not have permission to discard changes for document "${documentId}".`
1444
+ });
1445
+ return base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1446
+ actionType: "sanity.action.document.version.discard",
1447
+ versionId
1448
+ }), { base, working };
1449
+ }
1450
+ function handleEdit(action, ctx) {
1451
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1452
+ let { base, working } = ctx;
1453
+ const documentId = getId(action.documentId);
1454
+ if (action.liveEdit) {
1455
+ const userPatches2 = action.patches?.map((patch) => ({ patch: { id: documentId, ...patch } }));
1456
+ if (!userPatches2?.length) return { base, working };
1457
+ if (!working[documentId] || !base[documentId])
1458
+ throw new ActionError({
1459
+ documentId,
1460
+ transactionId,
1461
+ message: "Cannot edit document because it does not exist."
1462
+ });
1463
+ const baseBefore2 = base[documentId];
1464
+ userPatches2 && (base = processMutations({
1465
+ documents: base,
1466
+ transactionId,
1467
+ mutations: userPatches2,
1468
+ timestamp
1469
+ }));
1470
+ const baseAfter2 = base[documentId], patches2 = diffValue(baseBefore2, baseAfter2), workingBefore2 = working[documentId];
1471
+ if (!checkGrant(grants.update, workingBefore2))
1472
+ throw new PermissionActionError({
1473
+ documentId,
1474
+ transactionId,
1475
+ message: `You do not have permission to edit document "${documentId}".`
1476
+ });
1477
+ const workingMutations2 = patches2.map((patch) => ({ patch: { id: documentId, ...patch } }));
1478
+ return working = processMutations({
1479
+ documents: working,
1480
+ transactionId,
1481
+ mutations: workingMutations2,
1482
+ timestamp
1483
+ }), outgoingMutations.push(...workingMutations2), { base, working };
1484
+ }
1485
+ const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : void 0, draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId)), patchDocumentId = isReleasePerspective(action.perspective) ? versionId : draftId, userPatches = action.patches?.map((patch) => ({
1486
+ patch: { id: patchDocumentId, ...patch }
1487
+ }));
1488
+ if (!userPatches?.length) return { base, working };
1489
+ if (isReleasePerspective(action.perspective)) {
1490
+ if (!working[versionId] && !base[versionId])
1491
+ throw new ActionError({
1492
+ documentId,
1493
+ transactionId,
1494
+ message: "This document does not exist in the release. Please create it or add it to the release first."
1495
+ });
1496
+ } else if (!working[draftId] && !working[publishedId] || !base[draftId] && !base[publishedId])
1497
+ throw new ActionError({
1498
+ documentId,
1499
+ transactionId,
1500
+ message: "Cannot edit document because it does not exist in draft or published form."
1501
+ });
1502
+ const baseMutations = [];
1503
+ !isReleasePerspective(action.perspective) && !base[draftId] && base[publishedId] && baseMutations.push({ create: { ...base[publishedId], _id: draftId } });
1504
+ const baseBefore = base[patchDocumentId] ?? base[publishedId];
1505
+ userPatches && baseMutations.push(...userPatches), base = processMutations({
1506
+ documents: base,
1507
+ transactionId,
1508
+ mutations: baseMutations,
1509
+ timestamp
1510
+ });
1511
+ const baseAfter = base[patchDocumentId], patches = diffValue(baseBefore, baseAfter), workingMutations = [];
1512
+ if (!isReleasePerspective(action.perspective) && !working[draftId] && working[publishedId]) {
1513
+ const newDraftFromPublished = { ...working[publishedId], _id: draftId };
1514
+ if (!checkGrant(grants.create, newDraftFromPublished))
1515
+ throw new PermissionActionError({
1516
+ documentId,
1517
+ transactionId,
1518
+ message: "You do not have permission to create a draft for editing this document."
1519
+ });
1520
+ workingMutations.push({ create: newDraftFromPublished });
1521
+ }
1522
+ const workingBefore = working[patchDocumentId] ?? working[publishedId];
1523
+ if (!checkGrant(grants.update, workingBefore))
1524
+ throw new PermissionActionError({
1525
+ documentId,
1526
+ transactionId,
1527
+ message: `You do not have permission to edit document "${documentId}".`
1528
+ });
1529
+ return workingMutations.push(...patches.map((patch) => ({ patch: { id: patchDocumentId, ...patch } }))), working = processMutations({
1530
+ documents: working,
1531
+ transactionId,
1532
+ mutations: workingMutations,
1533
+ timestamp
1534
+ }), outgoingMutations.push(...workingMutations), outgoingActions.push(
1535
+ ...patches.map((patch) => ({
1536
+ actionType: "sanity.action.document.edit",
1537
+ draftId: patchDocumentId,
1538
+ publishedId,
1539
+ patch
1540
+ }))
1541
+ ), { base, working };
1542
+ }
1543
+ function handlePublish(action, ctx) {
1544
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1545
+ let { base, working } = ctx;
1546
+ const documentId = getId(action.documentId);
1547
+ if (action.liveEdit || isReleasePerspective(action.perspective))
1548
+ throw new ActionError({
1549
+ documentId,
1550
+ transactionId,
1551
+ message: "Cannot publish this document. Publishing is not supported for liveEdit or version (release) documents."
1552
+ });
1553
+ const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId)), workingDraft = working[draftId], baseDraft = base[draftId];
1554
+ if (!workingDraft || !baseDraft)
1555
+ throw new ActionError({
1556
+ documentId,
1557
+ transactionId,
1558
+ message: `Cannot publish because no draft version was found for document "${documentId}".`
1559
+ });
1560
+ if (!isDeepEqual(workingDraft, baseDraft))
1561
+ throw new ActionError({
1562
+ documentId,
1563
+ transactionId,
1564
+ message: "Publish aborted: The document has changed elsewhere. Please try again."
1565
+ });
1566
+ const newPublishedFromDraft = { ...strengthenOnPublish(workingDraft), _id: publishedId }, mutations = [{ delete: { id: draftId } }, { createOrReplace: newPublishedFromDraft }];
1567
+ if (working[draftId] && !checkGrant(grants.update, working[draftId]))
1568
+ throw new PermissionActionError({
1569
+ documentId,
1570
+ transactionId,
1571
+ message: `Publish failed: You do not have permission to update the draft for "${documentId}".`
1572
+ });
1573
+ if (working[publishedId] && !checkGrant(grants.update, newPublishedFromDraft))
1574
+ throw new PermissionActionError({
1575
+ documentId,
1576
+ transactionId,
1577
+ message: `Publish failed: You do not have permission to update the published version of "${documentId}".`
1578
+ });
1579
+ if (!working[publishedId] && !checkGrant(grants.create, newPublishedFromDraft))
1580
+ throw new PermissionActionError({
1581
+ documentId,
1582
+ transactionId,
1583
+ message: `Publish failed: You do not have permission to publish a new version of "${documentId}".`
1584
+ });
1585
+ return base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1586
+ actionType: "sanity.action.document.publish",
1587
+ draftId,
1588
+ publishedId
1589
+ }), { base, working };
1590
+ }
1591
+ function strengthenOnPublish(draft) {
1592
+ const isStrengthenReference = (value) => "_strengthenOnPublish" in value;
1593
+ function strengthen(value) {
1594
+ if (typeof value != "object" || !value) return value;
1595
+ if (isStrengthenReference(value)) {
1596
+ const { _strengthenOnPublish, _weak, ...rest } = value;
1597
+ return {
1598
+ ...rest,
1599
+ ..._strengthenOnPublish.weak && { _weak: !0 }
1600
+ };
1601
+ }
1602
+ return Array.isArray(value) ? value.map(strengthen) : Object.fromEntries(Object.entries(value).map(([k, v]) => [k, strengthen(v)]));
1603
+ }
1604
+ return strengthen(draft);
1605
+ }
1606
+ function handleUnpublish(action, ctx) {
1607
+ const { transactionId, timestamp, grants, outgoingActions, outgoingMutations } = ctx;
1608
+ let { base, working } = ctx;
1609
+ const documentId = getId(action.documentId);
1610
+ if (action.liveEdit || isReleasePerspective(action.perspective))
1611
+ throw new ActionError({
1612
+ documentId,
1613
+ transactionId,
1614
+ message: "Cannot unpublish this document. Unpublishing is not supported for liveEdit or version (release) documents."
1615
+ });
1616
+ const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1617
+ if (!working[publishedId] && !base[publishedId])
1618
+ throw new ActionError({
1619
+ documentId,
1620
+ transactionId,
1621
+ message: `Cannot unpublish because the document "${documentId}" is not currently published.`
1622
+ });
1623
+ const sourceDoc = working[publishedId] ?? base[publishedId], newDraftFromPublished = { ...sourceDoc, _id: draftId }, mutations = [
1624
+ { delete: { id: publishedId } },
1625
+ { createIfNotExists: newDraftFromPublished }
1626
+ ];
1627
+ if (!checkGrant(grants.update, sourceDoc))
1628
+ throw new PermissionActionError({
1629
+ documentId,
1630
+ transactionId,
1631
+ message: `You do not have permission to unpublish the document "${documentId}".`
1632
+ });
1633
+ if (!working[draftId] && !checkGrant(grants.create, newDraftFromPublished))
1634
+ throw new PermissionActionError({
1635
+ documentId,
1636
+ transactionId,
1637
+ message: `You do not have permission to create a draft from the published version of "${documentId}".`
1638
+ });
1639
+ return base = processMutations({
1640
+ documents: base,
1641
+ transactionId,
1642
+ mutations: [
1643
+ { delete: { id: publishedId } },
1644
+ { createIfNotExists: { ...base[publishedId] ?? sourceDoc, _id: draftId } }
1645
+ ],
1646
+ timestamp
1647
+ }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1648
+ actionType: "sanity.action.document.unpublish",
1649
+ draftId,
1650
+ publishedId
1651
+ }), { base, working };
1652
+ }
1293
1653
  function processActions({
1294
1654
  actions,
1295
1655
  transactionId,
@@ -1298,361 +1658,20 @@ function processActions({
1298
1658
  timestamp,
1299
1659
  grants
1300
1660
  }) {
1301
- let working = { ...initialWorking }, base = { ...initialBase };
1661
+ let base = { ...initialBase }, working = { ...initialWorking };
1302
1662
  const outgoingActions = [], outgoingMutations = [];
1303
- for (const action of actions)
1304
- switch (action.type) {
1305
- case "document.create": {
1306
- const documentId = getId(action.documentId);
1307
- if (action.liveEdit) {
1308
- if (working[documentId])
1309
- throw new ActionError({
1310
- documentId,
1311
- transactionId,
1312
- message: "This document already exists."
1313
- });
1314
- const newDocBase2 = { _type: action.documentType, _id: documentId, ...action.initialValue }, mutations2 = [{ create: {
1315
- _type: action.documentType,
1316
- _id: documentId,
1317
- ...action.initialValue
1318
- } }];
1319
- if (base = processMutations({
1320
- documents: base,
1321
- transactionId,
1322
- mutations: [{ create: newDocBase2 }],
1323
- timestamp
1324
- }), working = processMutations({
1325
- documents: working,
1326
- transactionId,
1327
- mutations: mutations2,
1328
- timestamp
1329
- }), !checkGrant(grants.create, working[documentId]))
1330
- throw new PermissionActionError({
1331
- documentId,
1332
- transactionId,
1333
- message: `You do not have permission to create document "${documentId}".`
1334
- });
1335
- outgoingMutations.push(...mutations2);
1336
- continue;
1337
- }
1338
- const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : void 0, draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1339
- if (versionId ? working[versionId] : working[draftId]) {
1340
- const errorDocType = versionId ? "release version" : "draft";
1341
- throw new ActionError({
1342
- documentId,
1343
- transactionId,
1344
- message: `A ${errorDocType} of this document already exists. Please use or discard the existing ${errorDocType} before creating a new one.`
1345
- });
1346
- }
1347
- const newDocBase = {
1348
- ...base[draftId] ?? base[publishedId],
1349
- _type: action.documentType,
1350
- _id: versionId ?? draftId,
1351
- ...action.initialValue
1352
- }, newDocWorking = {
1353
- ...working[draftId] ?? working[publishedId],
1354
- _type: action.documentType,
1355
- _id: versionId ?? draftId,
1356
- ...action.initialValue
1357
- }, mutations = [{ create: newDocWorking }];
1358
- if (base = processMutations({
1359
- documents: base,
1360
- transactionId,
1361
- mutations: [{ create: newDocBase }],
1362
- timestamp
1363
- }), working = processMutations({
1364
- documents: working,
1365
- transactionId,
1366
- mutations,
1367
- timestamp
1368
- }), versionId && !checkGrant(grants.create, working[versionId]))
1369
- throw new PermissionActionError({
1370
- documentId,
1371
- transactionId,
1372
- message: `You do not have permission to create a release version for document "${documentId}".`
1373
- });
1374
- if (!versionId && !checkGrant(grants.create, working[draftId]))
1375
- throw new PermissionActionError({
1376
- documentId,
1377
- transactionId,
1378
- message: `You do not have permission to create a draft for document "${documentId}".`
1379
- });
1380
- outgoingMutations.push(...mutations), outgoingActions.push({
1381
- actionType: "sanity.action.document.version.create",
1382
- publishedId,
1383
- attributes: newDocWorking
1384
- });
1385
- continue;
1386
- }
1387
- case "document.delete": {
1388
- const documentId = action.documentId;
1389
- if (isReleasePerspective(action.perspective))
1390
- throw new ActionError({
1391
- documentId,
1392
- transactionId,
1393
- message: 'Cannot delete a version document. You may want to use the "unpublish" or "discard" actions instead.'
1394
- });
1395
- if (action.liveEdit) {
1396
- if (!working[documentId])
1397
- throw new ActionError({
1398
- documentId,
1399
- transactionId,
1400
- message: "The document you are trying to delete does not exist."
1401
- });
1402
- if (!checkGrant(grants.update, working[documentId]))
1403
- throw new PermissionActionError({
1404
- documentId,
1405
- transactionId,
1406
- message: "You do not have permission to delete this document."
1407
- });
1408
- const mutations2 = [{ delete: { id: documentId } }];
1409
- base = processMutations({ documents: base, transactionId, mutations: mutations2, timestamp }), working = processMutations({ documents: working, transactionId, mutations: mutations2, timestamp }), outgoingMutations.push(...mutations2);
1410
- continue;
1411
- }
1412
- const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1413
- if (!working[publishedId])
1414
- throw new ActionError({
1415
- documentId,
1416
- transactionId,
1417
- message: working[draftId] ? "Cannot delete a document without a published version." : "The document you are trying to delete does not exist."
1418
- });
1419
- const cantDeleteDraft = working[draftId] && !checkGrant(grants.update, working[draftId]), cantDeletePublished = working[publishedId] && !checkGrant(grants.update, working[publishedId]);
1420
- if (cantDeleteDraft || cantDeletePublished)
1421
- throw new PermissionActionError({
1422
- documentId,
1423
- transactionId,
1424
- message: "You do not have permission to delete this document."
1425
- });
1426
- const mutations = [{ delete: { id: publishedId } }, { delete: { id: draftId } }], includeDrafts = working[draftId] ? [draftId] : void 0;
1427
- base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1428
- actionType: "sanity.action.document.delete",
1429
- publishedId,
1430
- ...includeDrafts ? { includeDrafts } : {}
1431
- });
1432
- continue;
1433
- }
1434
- case "document.discard": {
1435
- const documentId = getId(action.documentId);
1436
- if (action.liveEdit)
1437
- throw new ActionError({
1438
- documentId,
1439
- transactionId,
1440
- message: `Cannot discard changes for liveEdit document "${documentId}". LiveEdit documents do not support drafts.`
1441
- });
1442
- const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : getDraftId(DocumentId(documentId)), mutations = [{ delete: { id: versionId } }];
1443
- if (!working[versionId])
1444
- throw new ActionError({
1445
- documentId,
1446
- transactionId,
1447
- message: `There is no draft or version available to discard for document "${documentId}".`
1448
- });
1449
- if (!checkGrant(grants.update, working[versionId]))
1450
- throw new PermissionActionError({
1451
- documentId,
1452
- transactionId,
1453
- message: `You do not have permission to discard changes for document "${documentId}".`
1454
- });
1455
- base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1456
- actionType: "sanity.action.document.version.discard",
1457
- versionId
1458
- });
1459
- continue;
1460
- }
1461
- case "document.edit": {
1462
- const documentId = getId(action.documentId);
1463
- if (action.liveEdit) {
1464
- const userPatches2 = action.patches?.map((patch) => ({ patch: { id: documentId, ...patch } }));
1465
- if (!userPatches2?.length) continue;
1466
- if (!working[documentId] || !base[documentId])
1467
- throw new ActionError({
1468
- documentId,
1469
- transactionId,
1470
- message: "Cannot edit document because it does not exist."
1471
- });
1472
- const baseBefore2 = base[documentId];
1473
- userPatches2 && (base = processMutations({
1474
- documents: base,
1475
- transactionId,
1476
- mutations: userPatches2,
1477
- timestamp
1478
- }));
1479
- const baseAfter2 = base[documentId], patches2 = diffValue(baseBefore2, baseAfter2), workingBefore2 = working[documentId];
1480
- if (!checkGrant(grants.update, workingBefore2))
1481
- throw new PermissionActionError({
1482
- documentId,
1483
- transactionId,
1484
- message: `You do not have permission to edit document "${documentId}".`
1485
- });
1486
- const workingMutations2 = patches2.map((patch) => ({ patch: { id: documentId, ...patch } }));
1487
- working = processMutations({
1488
- documents: working,
1489
- transactionId,
1490
- mutations: workingMutations2,
1491
- timestamp
1492
- }), outgoingMutations.push(...workingMutations2);
1493
- continue;
1494
- }
1495
- const versionId = isReleasePerspective(action.perspective) ? getVersionId(DocumentId(documentId), action.perspective.releaseName) : void 0, draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId)), patchDocumentId = isReleasePerspective(action.perspective) ? versionId : draftId, userPatches = action.patches?.map((patch) => ({
1496
- patch: { id: patchDocumentId, ...patch }
1497
- }));
1498
- if (!userPatches?.length) continue;
1499
- if (isReleasePerspective(action.perspective)) {
1500
- if (!working[versionId] && !base[versionId])
1501
- throw new ActionError({
1502
- documentId,
1503
- transactionId,
1504
- message: "This document does not exist in the release. Please create it or add it to the release first."
1505
- });
1506
- } else if (!working[draftId] && !working[publishedId] || !base[draftId] && !base[publishedId])
1507
- throw new ActionError({
1508
- documentId,
1509
- transactionId,
1510
- message: "Cannot edit document because it does not exist in draft or published form."
1511
- });
1512
- const baseMutations = [];
1513
- !isReleasePerspective(action.perspective) && !base[draftId] && base[publishedId] && baseMutations.push({ create: { ...base[publishedId], _id: draftId } });
1514
- const baseBefore = base[patchDocumentId] ?? base[publishedId];
1515
- userPatches && baseMutations.push(...userPatches), base = processMutations({
1516
- documents: base,
1517
- transactionId,
1518
- mutations: baseMutations,
1519
- timestamp
1520
- });
1521
- const baseAfter = base[patchDocumentId], patches = diffValue(baseBefore, baseAfter), workingMutations = [];
1522
- if (!isReleasePerspective(action.perspective) && !working[draftId] && working[publishedId]) {
1523
- const newDraftFromPublished = { ...working[publishedId], _id: draftId };
1524
- if (!checkGrant(grants.create, newDraftFromPublished))
1525
- throw new PermissionActionError({
1526
- documentId,
1527
- transactionId,
1528
- message: "You do not have permission to create a draft for editing this document."
1529
- });
1530
- workingMutations.push({ create: newDraftFromPublished });
1531
- }
1532
- const workingBefore = working[patchDocumentId] ?? working[publishedId];
1533
- if (!checkGrant(grants.update, workingBefore))
1534
- throw new PermissionActionError({
1535
- documentId,
1536
- transactionId,
1537
- message: `You do not have permission to edit document "${documentId}".`
1538
- });
1539
- workingMutations.push(...patches.map((patch) => ({ patch: { id: patchDocumentId, ...patch } }))), working = processMutations({
1540
- documents: working,
1541
- transactionId,
1542
- mutations: workingMutations,
1543
- timestamp
1544
- }), outgoingMutations.push(...workingMutations), outgoingActions.push(
1545
- ...patches.map((patch) => ({
1546
- actionType: "sanity.action.document.edit",
1547
- draftId: patchDocumentId,
1548
- publishedId,
1549
- patch
1550
- }))
1551
- );
1552
- continue;
1553
- }
1554
- case "document.publish": {
1555
- const documentId = getId(action.documentId);
1556
- if (action.liveEdit || isReleasePerspective(action.perspective))
1557
- throw new ActionError({
1558
- documentId,
1559
- transactionId,
1560
- message: "Cannot publish this document. Publishing is not supported for liveEdit or version (release) documents."
1561
- });
1562
- const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId)), workingDraft = working[draftId], baseDraft = base[draftId];
1563
- if (!workingDraft || !baseDraft)
1564
- throw new ActionError({
1565
- documentId,
1566
- transactionId,
1567
- message: `Cannot publish because no draft version was found for document "${documentId}".`
1568
- });
1569
- if (!isDeepEqual(workingDraft, baseDraft))
1570
- throw new ActionError({
1571
- documentId,
1572
- transactionId,
1573
- message: "Publish aborted: The document has changed elsewhere. Please try again."
1574
- });
1575
- const newPublishedFromDraft = { ...strengthenOnPublish(workingDraft), _id: publishedId }, mutations = [
1576
- { delete: { id: draftId } },
1577
- { createOrReplace: newPublishedFromDraft }
1578
- ];
1579
- if (working[draftId] && !checkGrant(grants.update, working[draftId]))
1580
- throw new PermissionActionError({
1581
- documentId,
1582
- transactionId,
1583
- message: `Publish failed: You do not have permission to update the draft for "${documentId}".`
1584
- });
1585
- if (working[publishedId] && !checkGrant(grants.update, newPublishedFromDraft))
1586
- throw new PermissionActionError({
1587
- documentId,
1588
- transactionId,
1589
- message: `Publish failed: You do not have permission to update the published version of "${documentId}".`
1590
- });
1591
- if (!working[publishedId] && !checkGrant(grants.create, newPublishedFromDraft))
1592
- throw new PermissionActionError({
1593
- documentId,
1594
- transactionId,
1595
- message: `Publish failed: You do not have permission to publish a new version of "${documentId}".`
1596
- });
1597
- base = processMutations({ documents: base, transactionId, mutations, timestamp }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1598
- actionType: "sanity.action.document.publish",
1599
- draftId,
1600
- publishedId
1601
- });
1602
- continue;
1603
- }
1604
- case "document.unpublish": {
1605
- const documentId = getId(action.documentId);
1606
- if (action.liveEdit || isReleasePerspective(action.perspective))
1607
- throw new ActionError({
1608
- documentId,
1609
- transactionId,
1610
- message: "Cannot unpublish this document. Unpublishing is not supported for liveEdit or version (release) documents."
1611
- });
1612
- const draftId = getDraftId(DocumentId(documentId)), publishedId = getPublishedId(DocumentId(documentId));
1613
- if (!working[publishedId] && !base[publishedId])
1614
- throw new ActionError({
1615
- documentId,
1616
- transactionId,
1617
- message: `Cannot unpublish because the document "${documentId}" is not currently published.`
1618
- });
1619
- const sourceDoc = working[publishedId] ?? base[publishedId], newDraftFromPublished = { ...sourceDoc, _id: draftId }, mutations = [
1620
- { delete: { id: publishedId } },
1621
- { createIfNotExists: newDraftFromPublished }
1622
- ];
1623
- if (!checkGrant(grants.update, sourceDoc))
1624
- throw new PermissionActionError({
1625
- documentId,
1626
- transactionId,
1627
- message: `You do not have permission to unpublish the document "${documentId}".`
1628
- });
1629
- if (!working[draftId] && !checkGrant(grants.create, newDraftFromPublished))
1630
- throw new PermissionActionError({
1631
- documentId,
1632
- transactionId,
1633
- message: `You do not have permission to create a draft from the published version of "${documentId}".`
1634
- });
1635
- base = processMutations({
1636
- documents: base,
1637
- transactionId,
1638
- mutations: [
1639
- { delete: { id: publishedId } },
1640
- { createIfNotExists: { ...base[publishedId] ?? sourceDoc, _id: draftId } }
1641
- ],
1642
- timestamp
1643
- }), working = processMutations({ documents: working, transactionId, mutations, timestamp }), outgoingMutations.push(...mutations), outgoingActions.push({
1644
- actionType: "sanity.action.document.unpublish",
1645
- draftId,
1646
- publishedId
1647
- });
1648
- continue;
1649
- }
1650
- default:
1651
- throw new Error(
1652
- `Unknown action type: "${// @ts-expect-error invalid input
1653
- action.type}". Please contact support if this issue persists.`
1654
- );
1655
- }
1663
+ for (const action of actions) {
1664
+ const result = dispatch(action, {
1665
+ base,
1666
+ working,
1667
+ transactionId,
1668
+ timestamp,
1669
+ grants,
1670
+ outgoingActions,
1671
+ outgoingMutations
1672
+ });
1673
+ base = result.base, working = result.working;
1674
+ }
1656
1675
  const previousRevs = Object.fromEntries(
1657
1676
  Object.entries(initialWorking).map(([id, doc]) => [id, doc?._rev])
1658
1677
  );
@@ -1664,20 +1683,26 @@ function processActions({
1664
1683
  previousRevs
1665
1684
  };
1666
1685
  }
1667
- function strengthenOnPublish(draft) {
1668
- const isStrengthenReference = (value) => "_strengthenOnPublish" in value;
1669
- function strengthen(value) {
1670
- if (typeof value != "object" || !value) return value;
1671
- if (isStrengthenReference(value)) {
1672
- const { _strengthenOnPublish, _weak, ...rest } = value;
1673
- return {
1674
- ...rest,
1675
- ..._strengthenOnPublish.weak && { _weak: !0 }
1676
- };
1677
- }
1678
- return Array.isArray(value) ? value.map(strengthen) : Object.fromEntries(Object.entries(value).map(([k, v]) => [k, strengthen(v)]));
1686
+ function dispatch(action, ctx) {
1687
+ switch (action.type) {
1688
+ case "document.create":
1689
+ return handleCreate(action, ctx);
1690
+ case "document.delete":
1691
+ return handleDelete(action, ctx);
1692
+ case "document.discard":
1693
+ return handleDiscard(action, ctx);
1694
+ case "document.edit":
1695
+ return handleEdit(action, ctx);
1696
+ case "document.publish":
1697
+ return handlePublish(action, ctx);
1698
+ case "document.unpublish":
1699
+ return handleUnpublish(action, ctx);
1700
+ default:
1701
+ throw new Error(
1702
+ `Unknown action type: "${// @ts-expect-error invalid input
1703
+ action.type}". Please contact support if this issue persists.`
1704
+ );
1679
1705
  }
1680
- return strengthen(draft);
1681
1706
  }
1682
1707
  const EMPTY_REVISIONS = {};
1683
1708
  function queueTransaction(prev, transaction) {
@@ -2725,7 +2750,7 @@ const handleIncomingMessage = (event) => {
2725
2750
  }) : getClient(instance, {
2726
2751
  apiVersion: PRESENCE_API_VERSION,
2727
2752
  resource
2728
- }), token$ = getTokenState(instance).observable.pipe(distinctUntilChanged()), [incomingEvents$, dispatch] = createBifurTransport({
2753
+ }), token$ = getTokenState(instance).observable.pipe(distinctUntilChanged()), [incomingEvents$, dispatch2] = createBifurTransport({
2729
2754
  client,
2730
2755
  token$,
2731
2756
  sessionId
@@ -2746,7 +2771,7 @@ const handleIncomingMessage = (event) => {
2746
2771
  return newLocations.delete(event.sessionId), { ...prevState, locations: newLocations };
2747
2772
  }));
2748
2773
  })
2749
- ), dispatch({ type: "rollCall" }).subscribe(), isCanvasResource(resource)) {
2774
+ ), dispatch2({ type: "rollCall" }).subscribe(), isCanvasResource(resource)) {
2750
2775
  const globalClient = getClient(instance, { apiVersion: PRESENCE_API_VERSION });
2751
2776
  subscription.add(
2752
2777
  globalClient.observable.request({
@@ -2758,7 +2783,7 @@ const handleIncomingMessage = (event) => {
2758
2783
  );
2759
2784
  }
2760
2785
  return () => {
2761
- dispatch({ type: "disconnect" }).subscribe(), subscription.unsubscribe();
2786
+ dispatch2({ type: "disconnect" }).subscribe(), subscription.unsubscribe();
2762
2787
  };
2763
2788
  }
2764
2789
  }), selectLocations = (state) => state.locations, selectUsers = (state) => state.users, selectPresence = createSelector(