@sanity-labs/client-fake-for-test 0.17.0 → 0.17.2

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
@@ -1277,7 +1277,7 @@ function requireActive(release) {
1277
1277
  `Release "${release.name}" is "${release.state}", but only "active" releases accept version writes (create / replace / discard / unpublish). Unschedule or unarchive it back to "active" first, or target a different release.`
1278
1278
  );
1279
1279
  }
1280
- async function applyAction(store, action, hooks) {
1280
+ async function applyAction(store, action) {
1281
1281
  switch (action.actionType) {
1282
1282
  case "sanity.action.release.create": {
1283
1283
  if (maybeRelease(store, action.releaseId) !== void 0)
@@ -1430,7 +1430,7 @@ async function applyAction(store, action, hooks) {
1430
1430
  const published = store.get(action.publishedId);
1431
1431
  requireRevisionMatch(action.draftId, draft._rev, action.ifDraftRevisionId), requireRevisionMatch(action.publishedId, published?._rev, action.ifPublishedRevisionId);
1432
1432
  const promoted = promote({ ...draft, _id: action.publishedId });
1433
- return published !== void 0 && (promoted._createdAt = published._createdAt), await hooks?.gate?.(promoted, published === void 0 ? "create" : "update"), store.put(promoted), store.remove(action.draftId), { transactionId: cryptoRandomKey() };
1433
+ return published !== void 0 && (promoted._createdAt = published._createdAt), store.put(promoted), store.remove(action.draftId), { transactionId: cryptoRandomKey() };
1434
1434
  }
1435
1435
  case "sanity.action.document.unpublish": {
1436
1436
  requireDraftOf(action);
@@ -1439,7 +1439,7 @@ async function applyAction(store, action, hooks) {
1439
1439
  throw fakeError(
1440
1440
  `Cannot unpublish "${action.publishedId}": no published document with that _id exists. document.unpublish retracts a live published document into "${action.draftId}".`
1441
1441
  );
1442
- if (await hooks?.gate?.(published, "update"), store.get(action.draftId) === void 0) {
1442
+ if (store.get(action.draftId) === void 0) {
1443
1443
  const draft = promote({ ...published, _id: action.draftId });
1444
1444
  draft._createdAt = published._createdAt, store.put(draft);
1445
1445
  }
@@ -1453,7 +1453,7 @@ function requireDraftOf(action) {
1453
1453
  `${action.actionType}: draftId "${action.draftId}" is not the draft of publishedId "${action.publishedId}" \u2014 expected draftId "drafts.${action.publishedId}".`
1454
1454
  );
1455
1455
  }
1456
- function createVersionFromBase(store, action) {
1456
+ async function createVersionFromBase(store, action) {
1457
1457
  const { releaseId, publishedId } = parseVersionId(action.versionId);
1458
1458
  if (publishedId !== action.publishedId)
1459
1459
  throw fakeError(
@@ -1466,11 +1466,12 @@ function createVersionFromBase(store, action) {
1466
1466
  `Cannot create version "${action.versionId}": base document "${action.baseId}" not found. version.create with \`baseId\` copies an existing document; pass \`document\` instead when there is nothing to copy.`
1467
1467
  );
1468
1468
  requireRevisionMatch(action.baseId, base._rev, action.ifBaseRevisionId);
1469
- const existing = store.get(action.versionId), promoted = promote({
1469
+ const existing = store.get(action.versionId), copy = {
1470
1470
  ...base,
1471
1471
  _id: action.versionId,
1472
- _createdAt: existing?._createdAt ?? nowIso()
1473
- });
1472
+ _createdAt: existing?._createdAt ?? nowIso(),
1473
+ _system: { base: { id: base._id, rev: base._rev } }
1474
+ }, promoted = promote(copy);
1474
1475
  return store.put(promoted), { transactionId: cryptoRandomKey() };
1475
1476
  }
1476
1477
  function requireRevisionMatch(documentId, actualRev, expectedRev) {
@@ -1483,7 +1484,7 @@ function requireRevisionMatch(documentId, actualRev, expectedRev) {
1483
1484
  );
1484
1485
  }
1485
1486
  async function applyActions(store, actions, hooks) {
1486
- const list = Array.isArray(actions) ? actions : [actions], snapshot = /* @__PURE__ */ new Map(), guardedIds = /* @__PURE__ */ new Set(), remember = (id) => {
1487
+ const list = Array.isArray(actions) ? actions : [actions], snapshot = /* @__PURE__ */ new Map(), guardedIds = /* @__PURE__ */ new Set(), gatedIds = /* @__PURE__ */ new Set(), remember = (id) => {
1487
1488
  snapshot.has(id) || snapshot.set(id, store.get(id));
1488
1489
  };
1489
1490
  try {
@@ -1496,9 +1497,15 @@ async function applyActions(store, actions, hooks) {
1496
1497
  case "sanity.action.release.unschedule":
1497
1498
  case "sanity.action.release.archive":
1498
1499
  case "sanity.action.release.unarchive":
1499
- case "sanity.action.release.delete":
1500
1500
  remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
1501
1501
  break;
1502
+ case "sanity.action.release.delete": {
1503
+ remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
1504
+ const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;
1505
+ for (const d of store.all())
1506
+ d._id.startsWith(prefix) && remember(d._id);
1507
+ break;
1508
+ }
1502
1509
  case "sanity.action.release.publish": {
1503
1510
  remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
1504
1511
  const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;
@@ -1508,11 +1515,11 @@ async function applyActions(store, actions, hooks) {
1508
1515
  }
1509
1516
  case "sanity.action.document.version.create": {
1510
1517
  if ("baseId" in a) {
1511
- remember(a.versionId);
1518
+ remember(a.versionId), gatedIds.add(a.versionId);
1512
1519
  break;
1513
1520
  }
1514
1521
  const releaseId = a.releaseId ?? (typeof a.document._id == "string" ? parseVersionId(a.document._id).releaseId : void 0);
1515
- releaseId !== void 0 && remember(versionDocId(releaseId, a.publishedId));
1522
+ releaseId !== void 0 && (remember(versionDocId(releaseId, a.publishedId)), gatedIds.add(versionDocId(releaseId, a.publishedId)));
1516
1523
  break;
1517
1524
  }
1518
1525
  case "sanity.action.document.version.replace":
@@ -1523,13 +1530,17 @@ async function applyActions(store, actions, hooks) {
1523
1530
  remember(a.versionId);
1524
1531
  break;
1525
1532
  case "sanity.action.document.publish":
1526
- remember(a.draftId), remember(a.publishedId), guardedIds.add(a.publishedId), guardedIds.add(a.draftId);
1533
+ remember(a.draftId), remember(a.publishedId), guardedIds.add(a.publishedId), guardedIds.add(a.draftId), gatedIds.add(a.publishedId), gatedIds.add(a.draftId);
1527
1534
  break;
1528
1535
  case "sanity.action.document.unpublish":
1529
- remember(a.draftId), remember(a.publishedId), guardedIds.add(a.draftId), guardedIds.add(a.publishedId);
1536
+ remember(a.draftId), remember(a.publishedId), guardedIds.add(a.draftId), guardedIds.add(a.publishedId), gatedIds.add(a.publishedId), gatedIds.add(a.draftId);
1530
1537
  break;
1531
1538
  }
1532
- last = await applyAction(store, a, hooks);
1539
+ last = await applyAction(store, a);
1540
+ }
1541
+ for (const id of gatedIds) {
1542
+ const before = snapshot.get(id), after = store.get(id);
1543
+ before !== after && await hooks?.gate?.(before, after);
1533
1544
  }
1534
1545
  const contexts = mutationContexts(
1535
1546
  [...guardedIds].map((id) => ({ before: snapshot.get(id), after: store.get(id) }))
@@ -2007,14 +2018,21 @@ function makeHandle(handleArgs) {
2007
2018
  throw fakeError(
2008
2019
  `ifRevisionId check failed: document "${id}" is at _rev="${existing._rev}" but the mutation required _rev="${ifRevisionId}". The document changed since you read it \u2014 re-read it and retry. (This mirrors Sanity's optimistic-locking 409.)`
2009
2020
  );
2010
- }, gate = async (document, permission) => {
2021
+ }, gate = async (prev, next) => {
2011
2022
  const access = activeAccess();
2012
- access !== void 0 && await enforceWriteAccess({
2013
- document,
2014
- grants: access.grants,
2015
- permission: document._id !== void 0 && isSystemId(document._id) ? "manage" : permission,
2016
- userId: actorId()
2017
- });
2023
+ if (access === void 0) return;
2024
+ const id = (prev ?? next)?._id;
2025
+ if (id === void 0) return;
2026
+ const required = isSystemId(id) ? ["manage"] : [];
2027
+ required.push(prev === void 0 ? "create" : "update");
2028
+ for (const permission of required)
2029
+ for (const document of [prev, next])
2030
+ document !== void 0 && await enforceWriteAccess({
2031
+ document,
2032
+ grants: access.grants,
2033
+ permission,
2034
+ userId: actorId()
2035
+ });
2018
2036
  }, guardGate = async (contexts) => {
2019
2037
  const identity = actorId();
2020
2038
  await enforceMutationGuards(
@@ -2037,13 +2055,13 @@ function makeHandle(handleArgs) {
2037
2055
  throw fakeError(
2038
2056
  `Cannot patch document "${documentId}": not found \u2014 no document with that _id exists in ${targetLabel}. patch() requires the target to already exist; use createOrReplace() / createIfNotExists() to upsert, or seed the document first.`
2039
2057
  );
2040
- checkRevision(documentId, ifRev), await gate(existing, "update");
2058
+ checkRevision(documentId, ifRev);
2041
2059
  const patched = applyPatchOps(existing, ops), now = (/* @__PURE__ */ new Date()).toISOString(), next = {
2042
2060
  ...patched,
2043
2061
  _rev: cryptoRandomKey(),
2044
2062
  _updatedAt: now
2045
2063
  };
2046
- return options.evaluateGuard !== !1 && await guardOne("update", existing, next), store.put(next), next;
2064
+ return options.inTransaction !== !0 && (await gate(existing, next), await guardOne("update", existing, next)), store.put(next), next;
2047
2065
  }, applyTransaction = async (mutations, txnOptions) => {
2048
2066
  const transactionId = txnOptions?.transactionId ?? cryptoRandomKey(), snapshot = /* @__PURE__ */ new Map(), touchedIds = [], operations = [], remember = (id) => {
2049
2067
  snapshot.has(id) || snapshot.set(id, store.get(id));
@@ -2058,39 +2076,35 @@ function makeHandle(handleArgs) {
2058
2076
  throw fakeError(
2059
2077
  `create() failed: a document with _id "${promoted._id}" already exists in ${targetLabel}. Use createOrReplace() to overwrite it, or createIfNotExists() to leave the existing one untouched.`
2060
2078
  );
2061
- await gate(promoted, "create"), store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
2079
+ store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
2062
2080
  break;
2063
2081
  }
2064
2082
  case "createOrReplace": {
2065
2083
  remember(mut.doc._id);
2066
- const promoted = promote2(mut.doc), existing = store.get(promoted._id);
2067
- await gate(existing ?? promoted, existing !== void 0 ? "update" : "create");
2068
- const wasPresent = existing !== void 0;
2084
+ const promoted = promote2(mut.doc), wasPresent = store.get(promoted._id) !== void 0;
2069
2085
  store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: wasPresent ? "update" : "create" });
2070
2086
  break;
2071
2087
  }
2072
2088
  case "createIfNotExists": {
2073
2089
  if (remember(mut.doc._id), store.get(mut.doc._id) === void 0) {
2074
2090
  const promoted = promote2(mut.doc);
2075
- await gate(promoted, "create"), store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
2091
+ store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
2076
2092
  }
2077
2093
  break;
2078
2094
  }
2079
2095
  case "delete": {
2080
- remember(mut.id);
2081
- const existing = store.get(mut.id);
2082
- existing !== void 0 && await gate(existing, "update"), store.remove(mut.id) && (touchedIds.push(mut.id), operations.push({ id: mut.id, operation: "delete" }));
2096
+ remember(mut.id), store.remove(mut.id) && (touchedIds.push(mut.id), operations.push({ id: mut.id, operation: "delete" }));
2083
2097
  break;
2084
2098
  }
2085
2099
  case "patch": {
2086
- remember(mut.documentId), await applyPatch(mut.documentId, mut.ops, mut.ifRev, { evaluateGuard: !1 }), touchedIds.push(mut.documentId), operations.push({ id: mut.documentId, operation: "update" });
2100
+ remember(mut.documentId), await applyPatch(mut.documentId, mut.ops, mut.ifRev, { inTransaction: !0 }), touchedIds.push(mut.documentId), operations.push({ id: mut.documentId, operation: "update" });
2087
2101
  break;
2088
2102
  }
2089
2103
  }
2090
- const contexts = mutationContexts(
2091
- [...snapshot].map(([id, before]) => ({ before, after: store.get(id) }))
2092
- );
2093
- await guardGate(contexts);
2104
+ const images = [...snapshot].map(([id, before]) => ({ before, after: store.get(id) }));
2105
+ for (const { before, after } of images)
2106
+ before !== after && await gate(before, after);
2107
+ await guardGate(mutationContexts(images));
2094
2108
  } catch (error) {
2095
2109
  for (const [id, doc] of snapshot)
2096
2110
  doc === void 0 ? store.remove(id) : store.put(doc);
@@ -2173,7 +2187,7 @@ function makeHandle(handleArgs) {
2173
2187
  `create() failed: a document with _id "${doc._id}" already exists in ${targetLabel}. Use createOrReplace() to overwrite it, or createIfNotExists() to leave the existing one untouched.`
2174
2188
  );
2175
2189
  const promoted = promote2(doc);
2176
- await gate(promoted, "create"), await guardOne("create", null, promoted);
2190
+ await gate(void 0, promoted), await guardOne("create", null, promoted);
2177
2191
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
2178
2192
  return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }], opts?.tag)), writeResult(
2179
2193
  [promoted],
@@ -2185,7 +2199,7 @@ function makeHandle(handleArgs) {
2185
2199
  createOrReplace: (doc, opts) => write(async () => {
2186
2200
  checkRevision(doc._id, opts?.ifRevisionId);
2187
2201
  const promoted = promote2(doc), existing = store.get(doc._id);
2188
- await gate(existing ?? promoted, existing !== void 0 ? "update" : "create"), await guardOne(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
2202
+ await gate(existing, promoted), await guardOne(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
2189
2203
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
2190
2204
  opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }], opts?.tag));
2191
2205
  const operation = existing !== void 0 ? "update" : "create";
@@ -2207,7 +2221,7 @@ function makeHandle(handleArgs) {
2207
2221
  opts
2208
2222
  );
2209
2223
  const promoted = promote2(doc);
2210
- return await gate(promoted, "create"), await guardOne("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }], opts?.tag)), writeResult(
2224
+ return await gate(void 0, promoted), await guardOne("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }], opts?.tag)), writeResult(
2211
2225
  [promoted],
2212
2226
  [{ id: promoted._id, operation: "create" }],
2213
2227
  transactionId,
@@ -2217,7 +2231,7 @@ function makeHandle(handleArgs) {
2217
2231
  delete: (id, opts) => write(async () => {
2218
2232
  checkRevision(id, opts?.ifRevisionId);
2219
2233
  const existing = store.get(id);
2220
- existing !== void 0 && await gate(existing, "update"), existing !== void 0 && await guardOne("delete", existing, null);
2234
+ existing !== void 0 && await gate(existing, void 0), existing !== void 0 && await guardOne("delete", existing, null);
2221
2235
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
2222
2236
  let removed = existing;
2223
2237
  return opts?.dryRun !== !0 && (removed = store.remove(id), removed !== void 0 && record(transactionId, [{ kind: "delete", id }], opts?.tag)), opts?.returnDocuments === void 0 && opts?.returnFirst === void 0 ? removed !== void 0 ? { _id: id } : null : formatMutationResult(