@sanity-labs/client-fake-for-test 0.17.1 → 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/README.md +5 -1
- package/dist/index.cjs +53 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +53 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
|
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)
|
|
@@ -1381,7 +1381,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1381
1381
|
return store.remove(release._id), { transactionId: cryptoRandomKey() };
|
|
1382
1382
|
}
|
|
1383
1383
|
case "sanity.action.document.version.create": {
|
|
1384
|
-
if ("baseId" in action) return createVersionFromBase(store, action
|
|
1384
|
+
if ("baseId" in action) return createVersionFromBase(store, action);
|
|
1385
1385
|
const releaseId = action.releaseId ?? (action.document._id !== void 0 ? parseVersionId(action.document._id).releaseId : void 0);
|
|
1386
1386
|
if (releaseId === void 0)
|
|
1387
1387
|
throw fakeError(
|
|
@@ -1393,7 +1393,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1393
1393
|
...action.document,
|
|
1394
1394
|
_id: vid
|
|
1395
1395
|
}, existing = store.get(vid), promoted = promote(doc);
|
|
1396
|
-
return existing !== void 0 && (promoted._createdAt = existing._createdAt),
|
|
1396
|
+
return existing !== void 0 && (promoted._createdAt = existing._createdAt), store.put(promoted), { transactionId: cryptoRandomKey() };
|
|
1397
1397
|
}
|
|
1398
1398
|
case "sanity.action.document.version.replace": {
|
|
1399
1399
|
const id = action.document._id;
|
|
@@ -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),
|
|
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 (
|
|
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
|
-
async 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(
|
|
@@ -1472,7 +1472,7 @@ async function createVersionFromBase(store, action, hooks) {
|
|
|
1472
1472
|
_createdAt: existing?._createdAt ?? nowIso(),
|
|
1473
1473
|
_system: { base: { id: base._id, rev: base._rev } }
|
|
1474
1474
|
}, promoted = promote(copy);
|
|
1475
|
-
return
|
|
1475
|
+
return store.put(promoted), { transactionId: cryptoRandomKey() };
|
|
1476
1476
|
}
|
|
1477
1477
|
function requireRevisionMatch(documentId, actualRev, expectedRev) {
|
|
1478
1478
|
if (!(expectedRev === void 0 || actualRev === expectedRev))
|
|
@@ -1484,7 +1484,7 @@ function requireRevisionMatch(documentId, actualRev, expectedRev) {
|
|
|
1484
1484
|
);
|
|
1485
1485
|
}
|
|
1486
1486
|
async function applyActions(store, actions, hooks) {
|
|
1487
|
-
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) => {
|
|
1488
1488
|
snapshot.has(id) || snapshot.set(id, store.get(id));
|
|
1489
1489
|
};
|
|
1490
1490
|
try {
|
|
@@ -1497,9 +1497,15 @@ async function applyActions(store, actions, hooks) {
|
|
|
1497
1497
|
case "sanity.action.release.unschedule":
|
|
1498
1498
|
case "sanity.action.release.archive":
|
|
1499
1499
|
case "sanity.action.release.unarchive":
|
|
1500
|
-
case "sanity.action.release.delete":
|
|
1501
1500
|
remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
|
|
1502
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
|
+
}
|
|
1503
1509
|
case "sanity.action.release.publish": {
|
|
1504
1510
|
remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
|
|
1505
1511
|
const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;
|
|
@@ -1509,11 +1515,11 @@ async function applyActions(store, actions, hooks) {
|
|
|
1509
1515
|
}
|
|
1510
1516
|
case "sanity.action.document.version.create": {
|
|
1511
1517
|
if ("baseId" in a) {
|
|
1512
|
-
remember(a.versionId);
|
|
1518
|
+
remember(a.versionId), gatedIds.add(a.versionId);
|
|
1513
1519
|
break;
|
|
1514
1520
|
}
|
|
1515
1521
|
const releaseId = a.releaseId ?? (typeof a.document._id == "string" ? parseVersionId(a.document._id).releaseId : void 0);
|
|
1516
|
-
releaseId !== void 0 && remember(versionDocId(releaseId, a.publishedId));
|
|
1522
|
+
releaseId !== void 0 && (remember(versionDocId(releaseId, a.publishedId)), gatedIds.add(versionDocId(releaseId, a.publishedId)));
|
|
1517
1523
|
break;
|
|
1518
1524
|
}
|
|
1519
1525
|
case "sanity.action.document.version.replace":
|
|
@@ -1524,13 +1530,17 @@ async function applyActions(store, actions, hooks) {
|
|
|
1524
1530
|
remember(a.versionId);
|
|
1525
1531
|
break;
|
|
1526
1532
|
case "sanity.action.document.publish":
|
|
1527
|
-
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);
|
|
1528
1534
|
break;
|
|
1529
1535
|
case "sanity.action.document.unpublish":
|
|
1530
|
-
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);
|
|
1531
1537
|
break;
|
|
1532
1538
|
}
|
|
1533
|
-
last = await applyAction(store, a
|
|
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);
|
|
1534
1544
|
}
|
|
1535
1545
|
const contexts = mutationContexts(
|
|
1536
1546
|
[...guardedIds].map((id) => ({ before: snapshot.get(id), after: store.get(id) }))
|
|
@@ -2008,14 +2018,21 @@ function makeHandle(handleArgs) {
|
|
|
2008
2018
|
throw fakeError(
|
|
2009
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.)`
|
|
2010
2020
|
);
|
|
2011
|
-
}, gate = async (
|
|
2021
|
+
}, gate = async (prev, next) => {
|
|
2012
2022
|
const access = activeAccess();
|
|
2013
|
-
access
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
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
|
+
});
|
|
2019
2036
|
}, guardGate = async (contexts) => {
|
|
2020
2037
|
const identity = actorId();
|
|
2021
2038
|
await enforceMutationGuards(
|
|
@@ -2038,13 +2055,13 @@ function makeHandle(handleArgs) {
|
|
|
2038
2055
|
throw fakeError(
|
|
2039
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.`
|
|
2040
2057
|
);
|
|
2041
|
-
checkRevision(documentId, ifRev)
|
|
2058
|
+
checkRevision(documentId, ifRev);
|
|
2042
2059
|
const patched = applyPatchOps(existing, ops), now = (/* @__PURE__ */ new Date()).toISOString(), next = {
|
|
2043
2060
|
...patched,
|
|
2044
2061
|
_rev: cryptoRandomKey(),
|
|
2045
2062
|
_updatedAt: now
|
|
2046
2063
|
};
|
|
2047
|
-
return options.
|
|
2064
|
+
return options.inTransaction !== !0 && (await gate(existing, next), await guardOne("update", existing, next)), store.put(next), next;
|
|
2048
2065
|
}, applyTransaction = async (mutations, txnOptions) => {
|
|
2049
2066
|
const transactionId = txnOptions?.transactionId ?? cryptoRandomKey(), snapshot = /* @__PURE__ */ new Map(), touchedIds = [], operations = [], remember = (id) => {
|
|
2050
2067
|
snapshot.has(id) || snapshot.set(id, store.get(id));
|
|
@@ -2059,39 +2076,35 @@ function makeHandle(handleArgs) {
|
|
|
2059
2076
|
throw fakeError(
|
|
2060
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.`
|
|
2061
2078
|
);
|
|
2062
|
-
|
|
2079
|
+
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
|
|
2063
2080
|
break;
|
|
2064
2081
|
}
|
|
2065
2082
|
case "createOrReplace": {
|
|
2066
2083
|
remember(mut.doc._id);
|
|
2067
|
-
const promoted = promote2(mut.doc),
|
|
2068
|
-
await gate(existing ?? promoted, existing !== void 0 ? "update" : "create");
|
|
2069
|
-
const wasPresent = existing !== void 0;
|
|
2084
|
+
const promoted = promote2(mut.doc), wasPresent = store.get(promoted._id) !== void 0;
|
|
2070
2085
|
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: wasPresent ? "update" : "create" });
|
|
2071
2086
|
break;
|
|
2072
2087
|
}
|
|
2073
2088
|
case "createIfNotExists": {
|
|
2074
2089
|
if (remember(mut.doc._id), store.get(mut.doc._id) === void 0) {
|
|
2075
2090
|
const promoted = promote2(mut.doc);
|
|
2076
|
-
|
|
2091
|
+
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
|
|
2077
2092
|
}
|
|
2078
2093
|
break;
|
|
2079
2094
|
}
|
|
2080
2095
|
case "delete": {
|
|
2081
|
-
remember(mut.id);
|
|
2082
|
-
const existing = store.get(mut.id);
|
|
2083
|
-
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" }));
|
|
2084
2097
|
break;
|
|
2085
2098
|
}
|
|
2086
2099
|
case "patch": {
|
|
2087
|
-
remember(mut.documentId), await applyPatch(mut.documentId, mut.ops, mut.ifRev, {
|
|
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" });
|
|
2088
2101
|
break;
|
|
2089
2102
|
}
|
|
2090
2103
|
}
|
|
2091
|
-
const
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
await guardGate(
|
|
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));
|
|
2095
2108
|
} catch (error) {
|
|
2096
2109
|
for (const [id, doc] of snapshot)
|
|
2097
2110
|
doc === void 0 ? store.remove(id) : store.put(doc);
|
|
@@ -2174,7 +2187,7 @@ function makeHandle(handleArgs) {
|
|
|
2174
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.`
|
|
2175
2188
|
);
|
|
2176
2189
|
const promoted = promote2(doc);
|
|
2177
|
-
await gate(
|
|
2190
|
+
await gate(void 0, promoted), await guardOne("create", null, promoted);
|
|
2178
2191
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2179
2192
|
return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }], opts?.tag)), writeResult(
|
|
2180
2193
|
[promoted],
|
|
@@ -2186,7 +2199,7 @@ function makeHandle(handleArgs) {
|
|
|
2186
2199
|
createOrReplace: (doc, opts) => write(async () => {
|
|
2187
2200
|
checkRevision(doc._id, opts?.ifRevisionId);
|
|
2188
2201
|
const promoted = promote2(doc), existing = store.get(doc._id);
|
|
2189
|
-
await gate(existing
|
|
2202
|
+
await gate(existing, promoted), await guardOne(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
|
|
2190
2203
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2191
2204
|
opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }], opts?.tag));
|
|
2192
2205
|
const operation = existing !== void 0 ? "update" : "create";
|
|
@@ -2208,7 +2221,7 @@ function makeHandle(handleArgs) {
|
|
|
2208
2221
|
opts
|
|
2209
2222
|
);
|
|
2210
2223
|
const promoted = promote2(doc);
|
|
2211
|
-
return await gate(
|
|
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(
|
|
2212
2225
|
[promoted],
|
|
2213
2226
|
[{ id: promoted._id, operation: "create" }],
|
|
2214
2227
|
transactionId,
|
|
@@ -2218,7 +2231,7 @@ function makeHandle(handleArgs) {
|
|
|
2218
2231
|
delete: (id, opts) => write(async () => {
|
|
2219
2232
|
checkRevision(id, opts?.ifRevisionId);
|
|
2220
2233
|
const existing = store.get(id);
|
|
2221
|
-
existing !== void 0 && await gate(existing,
|
|
2234
|
+
existing !== void 0 && await gate(existing, void 0), existing !== void 0 && await guardOne("delete", existing, null);
|
|
2222
2235
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2223
2236
|
let removed = existing;
|
|
2224
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(
|