@sanity-labs/client-fake-for-test 0.6.0 → 0.7.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.
package/dist/index.cjs CHANGED
@@ -138,7 +138,7 @@ function createPatch(documentId, executor) {
138
138
  dec: (props) => (ops.push({ kind: "dec", props }), handle),
139
139
  insert: (position, anchor, items) => (ops.push({ kind: "insert", position, anchor, items }), handle),
140
140
  ifRevisionId: (rev) => (ifRev = rev, handle),
141
- commit: () => executor.apply(documentId, ops, ifRev),
141
+ commit: (options) => executor.apply(documentId, ops, ifRev, options),
142
142
  toJSON: () => ({
143
143
  id: documentId,
144
144
  ops: [...ops],
@@ -839,6 +839,25 @@ function wrapWithGuards(client, config) {
839
839
  }
840
840
  });
841
841
  }
842
+ const OPTIONS_ARG_POSITION = {
843
+ fetch: 2,
844
+ getDocument: 1,
845
+ getDocuments: 1,
846
+ create: 1,
847
+ createOrReplace: 1,
848
+ createIfNotExists: 1,
849
+ delete: 1,
850
+ mutate: 1,
851
+ action: 1,
852
+ request: 0
853
+ };
854
+ function tagFromArgs(method, args) {
855
+ const position = OPTIONS_ARG_POSITION[method];
856
+ if (position === void 0) return;
857
+ const options = args[position];
858
+ if (!(typeof options != "object" || options === null || !("tag" in options)))
859
+ return options.tag;
860
+ }
842
861
  const RECORDED_METHODS = /* @__PURE__ */ new Set([
843
862
  "fetch",
844
863
  "getDocument",
@@ -969,8 +988,14 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
969
988
  const key = registryKey(projectId, dataset);
970
989
  let s = registry.get(key);
971
990
  return s === void 0 && (s = createTestStore(), registry.set(key, s)), s;
972
- })(), concurrentEditKey = (id) => registryKey(projectId, dataset) + " " + id, record = (transactionId, mutations) => {
973
- backend.log.push({ transactionId, projectId, dataset, mutations });
991
+ })(), concurrentEditKey = (id) => registryKey(projectId, dataset) + " " + id, record = (transactionId, mutations, tag) => {
992
+ backend.log.push({
993
+ transactionId,
994
+ projectId,
995
+ dataset,
996
+ mutations,
997
+ ...tag !== void 0 ? { tag } : {}
998
+ });
974
999
  }, writeResult = (documents, results, transactionId, opts) => formatMutationResult(
975
1000
  documents,
976
1001
  results,
@@ -1094,7 +1119,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1094
1119
  doc === void 0 ? store.remove(id) : store.put(doc);
1095
1120
  throw error;
1096
1121
  }
1097
- record(transactionId, mutations);
1122
+ record(transactionId, mutations, txnOptions?.tag);
1098
1123
  const documents = touchedIds.map((id) => store.get(id)).filter((d) => d !== void 0);
1099
1124
  return formatMutationResult(
1100
1125
  documents,
@@ -1155,7 +1180,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1155
1180
  const promoted = promote2(doc);
1156
1181
  await gate(promoted, "create"), await guardGate("create", null, promoted);
1157
1182
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
1158
- return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }])), writeResult(
1183
+ return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }], opts?.tag)), writeResult(
1159
1184
  [promoted],
1160
1185
  [{ id: promoted._id, operation: "create" }],
1161
1186
  transactionId,
@@ -1167,7 +1192,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1167
1192
  const promoted = promote2(doc), existing = store.get(doc._id);
1168
1193
  await gate(existing ?? promoted, existing !== void 0 ? "update" : "create"), await guardGate(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
1169
1194
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
1170
- opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }]));
1195
+ opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }], opts?.tag));
1171
1196
  const operation = existing !== void 0 ? "update" : "create";
1172
1197
  return writeResult(
1173
1198
  [promoted],
@@ -1187,7 +1212,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1187
1212
  opts
1188
1213
  );
1189
1214
  const promoted = promote2(doc);
1190
- return await gate(promoted, "create"), await guardGate("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }])), writeResult(
1215
+ return await gate(promoted, "create"), await guardGate("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }], opts?.tag)), writeResult(
1191
1216
  [promoted],
1192
1217
  [{ id: promoted._id, operation: "create" }],
1193
1218
  transactionId,
@@ -1200,7 +1225,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1200
1225
  existing !== void 0 && await gate(existing, "update"), existing !== void 0 && await guardGate("delete", existing, null);
1201
1226
  const transactionId = opts?.transactionId ?? cryptoRandomKey();
1202
1227
  let removed = existing;
1203
- return opts?.dryRun !== !0 && (removed = store.remove(id), removed !== void 0 && record(transactionId, [{ kind: "delete", id }])), opts?.returnDocuments === void 0 && opts?.returnFirst === void 0 ? removed !== void 0 ? { _id: id } : null : formatMutationResult(
1228
+ 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(
1204
1229
  removed !== void 0 ? [removed] : [],
1205
1230
  removed !== void 0 ? [{ id, operation: "delete" }] : [],
1206
1231
  transactionId,
@@ -1209,11 +1234,13 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1209
1234
  );
1210
1235
  },
1211
1236
  patch: (documentId) => createPatch(documentId, {
1212
- apply: async (docId, ops, ifRev) => {
1237
+ apply: async (docId, ops, ifRev, options) => {
1213
1238
  const doc = await applyPatch(docId, ops, ifRev);
1214
- return record(cryptoRandomKey(), [
1215
- { kind: "patch", documentId: docId, ops, ...ifRev !== void 0 ? { ifRev } : {} }
1216
- ]), doc;
1239
+ return record(
1240
+ cryptoRandomKey(),
1241
+ [{ kind: "patch", documentId: docId, ops, ...ifRev !== void 0 ? { ifRev } : {} }],
1242
+ options?.tag
1243
+ ), doc;
1217
1244
  }
1218
1245
  }),
1219
1246
  transaction: () => createTransaction({ apply: applyTransaction }),
@@ -1307,8 +1334,8 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1307
1334
  return wrapWithGuards(client, {
1308
1335
  getStub: (name) => backend.stubs.get(name),
1309
1336
  onCall: (method, args) => {
1310
- const rec = { method, args, projectId, dataset };
1311
- return backend.calls.push(rec), (outcome) => {
1337
+ const rec = { method, args, projectId, dataset }, tag = tagFromArgs(method, args);
1338
+ return tag !== void 0 && (rec.tag = tag), backend.calls.push(rec), (outcome) => {
1312
1339
  "error" in outcome ? rec.error = outcome.error : rec.result = outcome.result;
1313
1340
  };
1314
1341
  },