@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 +41 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -6
- package/dist/index.d.ts +43 -6
- package/dist/index.js +41 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export declare type Action = ReleaseAction | VersionAction;
|
|
2
2
|
|
|
3
|
+
/** Options for `action()`. Only `tag` is meaningful to the fake. */
|
|
4
|
+
export declare interface ActionOptions {
|
|
5
|
+
/** Request tag, recorded on the {@link CallRecord} (`null` = explicit suppression). */
|
|
6
|
+
tag?: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
3
9
|
export declare interface ActionResult {
|
|
4
10
|
transactionId: string;
|
|
5
11
|
}
|
|
@@ -51,6 +57,8 @@ export declare interface CallRecord {
|
|
|
51
57
|
result?: unknown;
|
|
52
58
|
/** Thrown or rejected value, set if the call failed. */
|
|
53
59
|
error?: unknown;
|
|
60
|
+
/** Request tag from the call's options, when one was passed (`null` = explicit suppression). */
|
|
61
|
+
tag?: string | null;
|
|
54
62
|
projectId: string;
|
|
55
63
|
dataset: string;
|
|
56
64
|
}
|
|
@@ -321,8 +329,12 @@ export declare interface FetchOptions {
|
|
|
321
329
|
* raw response. Ignored when `filterResponse` is not `false`.
|
|
322
330
|
*/
|
|
323
331
|
returnQuery?: boolean;
|
|
324
|
-
/**
|
|
325
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Request tag, recorded on the {@link CallRecord} for assertions.
|
|
334
|
+
* `null` mirrors `@sanity/client`'s explicit-suppression form and is
|
|
335
|
+
* recorded as-is. No other behavior is simulated.
|
|
336
|
+
*/
|
|
337
|
+
tag?: string | null;
|
|
326
338
|
/** Accepted for signature parity; not simulated. */
|
|
327
339
|
signal?: AbortSignal;
|
|
328
340
|
}
|
|
@@ -429,6 +441,8 @@ export declare interface MutationLogEntry {
|
|
|
429
441
|
projectId: string;
|
|
430
442
|
dataset: string;
|
|
431
443
|
mutations: TxnMutation[];
|
|
444
|
+
/** Request tag the commit carried, when one was passed (`null` = explicit suppression). */
|
|
445
|
+
tag?: string | null;
|
|
432
446
|
}
|
|
433
447
|
|
|
434
448
|
/** The operation recorded per affected document, matching `@sanity/client`. */
|
|
@@ -456,8 +470,21 @@ export declare interface MutationOptions {
|
|
|
456
470
|
visibility?: "sync" | "async" | "deferred";
|
|
457
471
|
/** Accepted for signature parity; not simulated. */
|
|
458
472
|
autoGenerateArrayKeys?: boolean;
|
|
473
|
+
/**
|
|
474
|
+
* Request tag, recorded on the {@link CallRecord} and the
|
|
475
|
+
* {@link MutationLogEntry} for assertions. `null` mirrors
|
|
476
|
+
* `@sanity/client`'s explicit-suppression form and is recorded as-is.
|
|
477
|
+
* No other behavior is simulated.
|
|
478
|
+
*/
|
|
479
|
+
tag?: string | null;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Options for `PatchHandle.commit()`. Only `tag` is meaningful to the fake. */
|
|
483
|
+
export declare interface PatchCommitOptions {
|
|
484
|
+
/** Request tag, recorded on the mutation log entry (`null` = explicit suppression). */
|
|
485
|
+
tag?: string | null;
|
|
459
486
|
/** Accepted for signature parity; not simulated. */
|
|
460
|
-
|
|
487
|
+
visibility?: "sync" | "async" | "deferred";
|
|
461
488
|
}
|
|
462
489
|
|
|
463
490
|
export declare interface PatchExecutor {
|
|
@@ -465,6 +492,7 @@ export declare interface PatchExecutor {
|
|
|
465
492
|
documentId: string,
|
|
466
493
|
ops: PatchOp[],
|
|
467
494
|
ifRev: string | undefined,
|
|
495
|
+
options: PatchCommitOptions | undefined,
|
|
468
496
|
) => Promise<SanityDocument>;
|
|
469
497
|
}
|
|
470
498
|
|
|
@@ -480,7 +508,7 @@ export declare interface PatchHandle {
|
|
|
480
508
|
items: unknown[],
|
|
481
509
|
) => PatchHandle;
|
|
482
510
|
ifRevisionId: (rev: string) => PatchHandle;
|
|
483
|
-
commit: () => Promise<SanityDocument>;
|
|
511
|
+
commit: (options?: PatchCommitOptions) => Promise<SanityDocument>;
|
|
484
512
|
/** Test-only introspection — not on real Patch */
|
|
485
513
|
toJSON: () => {
|
|
486
514
|
id: string;
|
|
@@ -727,7 +755,7 @@ export declare interface TestClient {
|
|
|
727
755
|
url?: string;
|
|
728
756
|
uri?: string;
|
|
729
757
|
signal?: AbortSignal;
|
|
730
|
-
tag?: string;
|
|
758
|
+
tag?: string | null;
|
|
731
759
|
}) => Promise<T>;
|
|
732
760
|
create: <T extends SanityDocumentLike>(
|
|
733
761
|
doc: T,
|
|
@@ -765,7 +793,10 @@ export declare interface TestClient {
|
|
|
765
793
|
* Returns a synthetic `transactionId`. Multi-action arrays are applied
|
|
766
794
|
* atomically — any failure rolls back the batch.
|
|
767
795
|
*/
|
|
768
|
-
action: (
|
|
796
|
+
action: (
|
|
797
|
+
action: Action | Action[],
|
|
798
|
+
options?: ActionOptions,
|
|
799
|
+
) => Promise<ActionResult>;
|
|
769
800
|
/** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */
|
|
770
801
|
releases: ReleasesHandle;
|
|
771
802
|
/** Helper for `sanity.action.document.version.create`. */
|
|
@@ -987,6 +1018,12 @@ export declare interface TxnCommitOptions {
|
|
|
987
1018
|
returnFirst?: boolean;
|
|
988
1019
|
/** Override the synthetic transaction id (mirrors `client.transaction(undefined, { transactionId })`). */
|
|
989
1020
|
transactionId?: string;
|
|
1021
|
+
/**
|
|
1022
|
+
* Request tag, recorded on the mutation log entry for assertions.
|
|
1023
|
+
* `null` mirrors `@sanity/client`'s explicit-suppression form and is
|
|
1024
|
+
* recorded as-is. No other behavior is simulated.
|
|
1025
|
+
*/
|
|
1026
|
+
tag?: string | null;
|
|
990
1027
|
}
|
|
991
1028
|
|
|
992
1029
|
export declare type TxnCommitResult =
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export declare type Action = ReleaseAction | VersionAction;
|
|
2
2
|
|
|
3
|
+
/** Options for `action()`. Only `tag` is meaningful to the fake. */
|
|
4
|
+
export declare interface ActionOptions {
|
|
5
|
+
/** Request tag, recorded on the {@link CallRecord} (`null` = explicit suppression). */
|
|
6
|
+
tag?: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
3
9
|
export declare interface ActionResult {
|
|
4
10
|
transactionId: string;
|
|
5
11
|
}
|
|
@@ -51,6 +57,8 @@ export declare interface CallRecord {
|
|
|
51
57
|
result?: unknown;
|
|
52
58
|
/** Thrown or rejected value, set if the call failed. */
|
|
53
59
|
error?: unknown;
|
|
60
|
+
/** Request tag from the call's options, when one was passed (`null` = explicit suppression). */
|
|
61
|
+
tag?: string | null;
|
|
54
62
|
projectId: string;
|
|
55
63
|
dataset: string;
|
|
56
64
|
}
|
|
@@ -321,8 +329,12 @@ export declare interface FetchOptions {
|
|
|
321
329
|
* raw response. Ignored when `filterResponse` is not `false`.
|
|
322
330
|
*/
|
|
323
331
|
returnQuery?: boolean;
|
|
324
|
-
/**
|
|
325
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Request tag, recorded on the {@link CallRecord} for assertions.
|
|
334
|
+
* `null` mirrors `@sanity/client`'s explicit-suppression form and is
|
|
335
|
+
* recorded as-is. No other behavior is simulated.
|
|
336
|
+
*/
|
|
337
|
+
tag?: string | null;
|
|
326
338
|
/** Accepted for signature parity; not simulated. */
|
|
327
339
|
signal?: AbortSignal;
|
|
328
340
|
}
|
|
@@ -429,6 +441,8 @@ export declare interface MutationLogEntry {
|
|
|
429
441
|
projectId: string;
|
|
430
442
|
dataset: string;
|
|
431
443
|
mutations: TxnMutation[];
|
|
444
|
+
/** Request tag the commit carried, when one was passed (`null` = explicit suppression). */
|
|
445
|
+
tag?: string | null;
|
|
432
446
|
}
|
|
433
447
|
|
|
434
448
|
/** The operation recorded per affected document, matching `@sanity/client`. */
|
|
@@ -456,8 +470,21 @@ export declare interface MutationOptions {
|
|
|
456
470
|
visibility?: "sync" | "async" | "deferred";
|
|
457
471
|
/** Accepted for signature parity; not simulated. */
|
|
458
472
|
autoGenerateArrayKeys?: boolean;
|
|
473
|
+
/**
|
|
474
|
+
* Request tag, recorded on the {@link CallRecord} and the
|
|
475
|
+
* {@link MutationLogEntry} for assertions. `null` mirrors
|
|
476
|
+
* `@sanity/client`'s explicit-suppression form and is recorded as-is.
|
|
477
|
+
* No other behavior is simulated.
|
|
478
|
+
*/
|
|
479
|
+
tag?: string | null;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Options for `PatchHandle.commit()`. Only `tag` is meaningful to the fake. */
|
|
483
|
+
export declare interface PatchCommitOptions {
|
|
484
|
+
/** Request tag, recorded on the mutation log entry (`null` = explicit suppression). */
|
|
485
|
+
tag?: string | null;
|
|
459
486
|
/** Accepted for signature parity; not simulated. */
|
|
460
|
-
|
|
487
|
+
visibility?: "sync" | "async" | "deferred";
|
|
461
488
|
}
|
|
462
489
|
|
|
463
490
|
export declare interface PatchExecutor {
|
|
@@ -465,6 +492,7 @@ export declare interface PatchExecutor {
|
|
|
465
492
|
documentId: string,
|
|
466
493
|
ops: PatchOp[],
|
|
467
494
|
ifRev: string | undefined,
|
|
495
|
+
options: PatchCommitOptions | undefined,
|
|
468
496
|
) => Promise<SanityDocument>;
|
|
469
497
|
}
|
|
470
498
|
|
|
@@ -480,7 +508,7 @@ export declare interface PatchHandle {
|
|
|
480
508
|
items: unknown[],
|
|
481
509
|
) => PatchHandle;
|
|
482
510
|
ifRevisionId: (rev: string) => PatchHandle;
|
|
483
|
-
commit: () => Promise<SanityDocument>;
|
|
511
|
+
commit: (options?: PatchCommitOptions) => Promise<SanityDocument>;
|
|
484
512
|
/** Test-only introspection — not on real Patch */
|
|
485
513
|
toJSON: () => {
|
|
486
514
|
id: string;
|
|
@@ -727,7 +755,7 @@ export declare interface TestClient {
|
|
|
727
755
|
url?: string;
|
|
728
756
|
uri?: string;
|
|
729
757
|
signal?: AbortSignal;
|
|
730
|
-
tag?: string;
|
|
758
|
+
tag?: string | null;
|
|
731
759
|
}) => Promise<T>;
|
|
732
760
|
create: <T extends SanityDocumentLike>(
|
|
733
761
|
doc: T,
|
|
@@ -765,7 +793,10 @@ export declare interface TestClient {
|
|
|
765
793
|
* Returns a synthetic `transactionId`. Multi-action arrays are applied
|
|
766
794
|
* atomically — any failure rolls back the batch.
|
|
767
795
|
*/
|
|
768
|
-
action: (
|
|
796
|
+
action: (
|
|
797
|
+
action: Action | Action[],
|
|
798
|
+
options?: ActionOptions,
|
|
799
|
+
) => Promise<ActionResult>;
|
|
769
800
|
/** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */
|
|
770
801
|
releases: ReleasesHandle;
|
|
771
802
|
/** Helper for `sanity.action.document.version.create`. */
|
|
@@ -987,6 +1018,12 @@ export declare interface TxnCommitOptions {
|
|
|
987
1018
|
returnFirst?: boolean;
|
|
988
1019
|
/** Override the synthetic transaction id (mirrors `client.transaction(undefined, { transactionId })`). */
|
|
989
1020
|
transactionId?: string;
|
|
1021
|
+
/**
|
|
1022
|
+
* Request tag, recorded on the mutation log entry for assertions.
|
|
1023
|
+
* `null` mirrors `@sanity/client`'s explicit-suppression form and is
|
|
1024
|
+
* recorded as-is. No other behavior is simulated.
|
|
1025
|
+
*/
|
|
1026
|
+
tag?: string | null;
|
|
990
1027
|
}
|
|
991
1028
|
|
|
992
1029
|
export declare type TxnCommitResult =
|
package/dist/index.js
CHANGED
|
@@ -136,7 +136,7 @@ function createPatch(documentId, executor) {
|
|
|
136
136
|
dec: (props) => (ops.push({ kind: "dec", props }), handle),
|
|
137
137
|
insert: (position, anchor, items) => (ops.push({ kind: "insert", position, anchor, items }), handle),
|
|
138
138
|
ifRevisionId: (rev) => (ifRev = rev, handle),
|
|
139
|
-
commit: () => executor.apply(documentId, ops, ifRev),
|
|
139
|
+
commit: (options) => executor.apply(documentId, ops, ifRev, options),
|
|
140
140
|
toJSON: () => ({
|
|
141
141
|
id: documentId,
|
|
142
142
|
ops: [...ops],
|
|
@@ -837,6 +837,25 @@ function wrapWithGuards(client, config) {
|
|
|
837
837
|
}
|
|
838
838
|
});
|
|
839
839
|
}
|
|
840
|
+
const OPTIONS_ARG_POSITION = {
|
|
841
|
+
fetch: 2,
|
|
842
|
+
getDocument: 1,
|
|
843
|
+
getDocuments: 1,
|
|
844
|
+
create: 1,
|
|
845
|
+
createOrReplace: 1,
|
|
846
|
+
createIfNotExists: 1,
|
|
847
|
+
delete: 1,
|
|
848
|
+
mutate: 1,
|
|
849
|
+
action: 1,
|
|
850
|
+
request: 0
|
|
851
|
+
};
|
|
852
|
+
function tagFromArgs(method, args) {
|
|
853
|
+
const position = OPTIONS_ARG_POSITION[method];
|
|
854
|
+
if (position === void 0) return;
|
|
855
|
+
const options = args[position];
|
|
856
|
+
if (!(typeof options != "object" || options === null || !("tag" in options)))
|
|
857
|
+
return options.tag;
|
|
858
|
+
}
|
|
840
859
|
const RECORDED_METHODS = /* @__PURE__ */ new Set([
|
|
841
860
|
"fetch",
|
|
842
861
|
"getDocument",
|
|
@@ -967,8 +986,14 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
967
986
|
const key = registryKey(projectId, dataset);
|
|
968
987
|
let s = registry.get(key);
|
|
969
988
|
return s === void 0 && (s = createTestStore(), registry.set(key, s)), s;
|
|
970
|
-
})(), concurrentEditKey = (id) => registryKey(projectId, dataset) + " " + id, record = (transactionId, mutations) => {
|
|
971
|
-
backend.log.push({
|
|
989
|
+
})(), concurrentEditKey = (id) => registryKey(projectId, dataset) + " " + id, record = (transactionId, mutations, tag) => {
|
|
990
|
+
backend.log.push({
|
|
991
|
+
transactionId,
|
|
992
|
+
projectId,
|
|
993
|
+
dataset,
|
|
994
|
+
mutations,
|
|
995
|
+
...tag !== void 0 ? { tag } : {}
|
|
996
|
+
});
|
|
972
997
|
}, writeResult = (documents, results, transactionId, opts) => formatMutationResult(
|
|
973
998
|
documents,
|
|
974
999
|
results,
|
|
@@ -1092,7 +1117,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1092
1117
|
doc === void 0 ? store.remove(id) : store.put(doc);
|
|
1093
1118
|
throw error;
|
|
1094
1119
|
}
|
|
1095
|
-
record(transactionId, mutations);
|
|
1120
|
+
record(transactionId, mutations, txnOptions?.tag);
|
|
1096
1121
|
const documents = touchedIds.map((id) => store.get(id)).filter((d) => d !== void 0);
|
|
1097
1122
|
return formatMutationResult(
|
|
1098
1123
|
documents,
|
|
@@ -1153,7 +1178,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1153
1178
|
const promoted = promote2(doc);
|
|
1154
1179
|
await gate(promoted, "create"), await guardGate("create", null, promoted);
|
|
1155
1180
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
1156
|
-
return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }])), writeResult(
|
|
1181
|
+
return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }], opts?.tag)), writeResult(
|
|
1157
1182
|
[promoted],
|
|
1158
1183
|
[{ id: promoted._id, operation: "create" }],
|
|
1159
1184
|
transactionId,
|
|
@@ -1165,7 +1190,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1165
1190
|
const promoted = promote2(doc), existing = store.get(doc._id);
|
|
1166
1191
|
await gate(existing ?? promoted, existing !== void 0 ? "update" : "create"), await guardGate(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
|
|
1167
1192
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
1168
|
-
opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }]));
|
|
1193
|
+
opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }], opts?.tag));
|
|
1169
1194
|
const operation = existing !== void 0 ? "update" : "create";
|
|
1170
1195
|
return writeResult(
|
|
1171
1196
|
[promoted],
|
|
@@ -1185,7 +1210,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1185
1210
|
opts
|
|
1186
1211
|
);
|
|
1187
1212
|
const promoted = promote2(doc);
|
|
1188
|
-
return await gate(promoted, "create"), await guardGate("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }])), writeResult(
|
|
1213
|
+
return await gate(promoted, "create"), await guardGate("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }], opts?.tag)), writeResult(
|
|
1189
1214
|
[promoted],
|
|
1190
1215
|
[{ id: promoted._id, operation: "create" }],
|
|
1191
1216
|
transactionId,
|
|
@@ -1198,7 +1223,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1198
1223
|
existing !== void 0 && await gate(existing, "update"), existing !== void 0 && await guardGate("delete", existing, null);
|
|
1199
1224
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
1200
1225
|
let removed = existing;
|
|
1201
|
-
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(
|
|
1226
|
+
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(
|
|
1202
1227
|
removed !== void 0 ? [removed] : [],
|
|
1203
1228
|
removed !== void 0 ? [{ id, operation: "delete" }] : [],
|
|
1204
1229
|
transactionId,
|
|
@@ -1207,11 +1232,13 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1207
1232
|
);
|
|
1208
1233
|
},
|
|
1209
1234
|
patch: (documentId) => createPatch(documentId, {
|
|
1210
|
-
apply: async (docId, ops, ifRev) => {
|
|
1235
|
+
apply: async (docId, ops, ifRev, options) => {
|
|
1211
1236
|
const doc = await applyPatch(docId, ops, ifRev);
|
|
1212
|
-
return record(
|
|
1213
|
-
|
|
1214
|
-
|
|
1237
|
+
return record(
|
|
1238
|
+
cryptoRandomKey(),
|
|
1239
|
+
[{ kind: "patch", documentId: docId, ops, ...ifRev !== void 0 ? { ifRev } : {} }],
|
|
1240
|
+
options?.tag
|
|
1241
|
+
), doc;
|
|
1215
1242
|
}
|
|
1216
1243
|
}),
|
|
1217
1244
|
transaction: () => createTransaction({ apply: applyTransaction }),
|
|
@@ -1305,8 +1332,8 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
|
|
|
1305
1332
|
return wrapWithGuards(client, {
|
|
1306
1333
|
getStub: (name) => backend.stubs.get(name),
|
|
1307
1334
|
onCall: (method, args) => {
|
|
1308
|
-
const rec = { method, args, projectId, dataset };
|
|
1309
|
-
return backend.calls.push(rec), (outcome) => {
|
|
1335
|
+
const rec = { method, args, projectId, dataset }, tag = tagFromArgs(method, args);
|
|
1336
|
+
return tag !== void 0 && (rec.tag = tag), backend.calls.push(rec), (outcome) => {
|
|
1310
1337
|
"error" in outcome ? rec.error = outcome.error : rec.result = outcome.result;
|
|
1311
1338
|
};
|
|
1312
1339
|
},
|