@sanity-labs/client-fake-for-test 0.21.0 → 0.22.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/README.md CHANGED
@@ -362,6 +362,23 @@ expect(client.calls("fetch")[0]?.args).toEqual([`*[_type=="article"]`, {}]);
362
362
  expect(client.calls("getDocument")[0]?.result).toMatchObject({ _id: "a" });
363
363
  ```
364
364
 
365
+ Each record also carries the `token` (absent when the handle has none) and `apiVersion` of the handle that made the call, after `withConfig` overrides. That answers "which token and API version did this read use" without intercepting HTTP:
366
+
367
+ ```ts
368
+ const client = createTestClient({
369
+ token: "caller-token",
370
+ identities: { "caller-token": caller, "service-token": service },
371
+ apiVersion: "2025-02-19",
372
+ });
373
+ const serviceClient = client.withConfig({ token: "service-token" });
374
+ await loadAs(client, serviceClient); // code under test reads with one or the other
375
+
376
+ expect(client.calls("getDocument").map(({ token, apiVersion }) => ({ token, apiVersion }))).toEqual([
377
+ { token: "caller-token", apiVersion: "2025-02-19" },
378
+ { token: "service-token", apiVersion: "2025-02-19" },
379
+ ]);
380
+ ```
381
+
365
382
  > [!NOTE]
366
383
  > Builder-returning methods (`patch`, `transaction`) record the call at builder-creation time (`patch("a")` → `args: ["a"]`); the chained ops/commit show up in the mutation log, not as extra calls. Test-only helpers (`snapshot`, `mutations`, `calls`, …) are never recorded.
367
384
 
package/dist/index.cjs CHANGED
@@ -1685,7 +1685,7 @@ function createTransaction(executor) {
1685
1685
  }
1686
1686
  const DEFAULT_API_HOST = "https://api.sanity.io", DEFAULT_API_VERSION = "1", DEFAULT_CDN_HOST = "apicdn.sanity.io", QUERY_SIZE_LIMIT = 11264;
1687
1687
  function normalizeTagPrefix(prefix) {
1688
- return prefix?.replace(/\.+$/, "") || void 0;
1688
+ return prefix ? prefix.replace(/\.+$/, "") : void 0;
1689
1689
  }
1690
1690
  function normalizeApiVersion(apiVersion) {
1691
1691
  const version = `${apiVersion}`.replace(/^v/, "");
@@ -2206,7 +2206,7 @@ function makeHandle(handleArgs) {
2206
2206
  mutations,
2207
2207
  ...effectiveTag !== void 0 ? { tag: effectiveTag } : {}
2208
2208
  });
2209
- }, effectiveRequestTag = (tag) => requestTagPrefix === void 0 ? tag : tag ? `${requestTagPrefix}.${tag}` : requestTagPrefix, writeResult = (documents, results, transactionId, opts) => formatMutationResult(
2209
+ }, effectiveRequestTag = (tag) => requestTagPrefix ? tag ? `${requestTagPrefix}.${tag}` : requestTagPrefix : tag, writeResult = (documents, results, transactionId, opts) => formatMutationResult(
2210
2210
  documents,
2211
2211
  results,
2212
2212
  transactionId,
@@ -2634,8 +2634,12 @@ function makeHandle(handleArgs) {
2634
2634
  resource: nextResource,
2635
2635
  accessControl: rebinds ? config.accessControl : accessControl,
2636
2636
  perspective: config.perspective ?? perspective,
2637
- // `requestTagPrefix: undefined` unsets the prefix, as in `@sanity/client`.
2638
- requestTagPrefix: "requestTagPrefix" in config ? normalizeTagPrefix(config.requestTagPrefix) : requestTagPrefix,
2637
+ // `requestTagPrefix: undefined` unsets the prefix, and an inherited
2638
+ // one is normalized again, as `@sanity/client` re-runs `initConfig`
2639
+ // on the merged config (`SanityClient.ts:1417`, `config.ts:239-244`).
2640
+ requestTagPrefix: normalizeTagPrefix(
2641
+ "requestTagPrefix" in config ? config.requestTagPrefix : requestTagPrefix
2642
+ ),
2639
2643
  mutationGuard,
2640
2644
  token: rebinds ? config.token : token,
2641
2645
  useProjectHostname: config.useProjectHostname ?? useProjectHostname,
@@ -2687,7 +2691,9 @@ function makeHandle(handleArgs) {
2687
2691
  args,
2688
2692
  projectId,
2689
2693
  dataset,
2690
- ...resource !== void 0 ? { resource } : {}
2694
+ ...resource !== void 0 ? { resource } : {},
2695
+ ...token !== void 0 ? { token } : {},
2696
+ apiVersion
2691
2697
  }, tag = OPTIONS_ARG_POSITION[method] === void 0 ? void 0 : effectiveRequestTag(tagFromArgs(method, args));
2692
2698
  return tag !== void 0 && (rec.tag = tag), backend.calls.push(rec), (outcome) => {
2693
2699
  "error" in outcome ? rec.error = outcome.error : rec.result = outcome.result;