@sanity-labs/client-fake-for-test 0.24.0 → 0.26.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.d.cts CHANGED
@@ -1340,6 +1340,8 @@ export declare interface TestClientAccessControl {
1340
1340
  * `"project:<projectId>"`, `"media-library:<id>"`, `"canvas:<id>"`, `"dashboard:<id>"`. See {@link ResourceAccess}.
1341
1341
  */
1342
1342
  access?: Record<string, ResourceAccess>;
1343
+ /** The acting identity is a robot; see {@link TestClientIdentity.robot}. */
1344
+ robot?: true;
1343
1345
  }
1344
1346
 
1345
1347
  /**
@@ -1393,6 +1395,12 @@ export declare interface TestClientIdentity {
1393
1395
  * every host.
1394
1396
  */
1395
1397
  projectId?: string;
1398
+ /**
1399
+ * A robot (an API token's principal), as populus flags its user. `/users/me`
1400
+ * reports it with `provider: "sanity-token"`, the Access API lists it under
1401
+ * `…/robots/<id>` instead of `…/users`, and it is never a user row.
1402
+ */
1403
+ robot?: true;
1396
1404
  }
1397
1405
 
1398
1406
  /**
package/dist/index.d.ts CHANGED
@@ -1340,6 +1340,8 @@ export declare interface TestClientAccessControl {
1340
1340
  * `"project:<projectId>"`, `"media-library:<id>"`, `"canvas:<id>"`, `"dashboard:<id>"`. See {@link ResourceAccess}.
1341
1341
  */
1342
1342
  access?: Record<string, ResourceAccess>;
1343
+ /** The acting identity is a robot; see {@link TestClientIdentity.robot}. */
1344
+ robot?: true;
1343
1345
  }
1344
1346
 
1345
1347
  /**
@@ -1393,6 +1395,12 @@ export declare interface TestClientIdentity {
1393
1395
  * every host.
1394
1396
  */
1395
1397
  projectId?: string;
1398
+ /**
1399
+ * A robot (an API token's principal), as populus flags its user. `/users/me`
1400
+ * reports it with `provider: "sanity-token"`, the Access API lists it under
1401
+ * `…/robots/<id>` instead of `…/users`, and it is never a user row.
1402
+ */
1403
+ robot?: true;
1396
1404
  }
1397
1405
 
1398
1406
  /**
package/dist/index.js CHANGED
@@ -482,7 +482,7 @@ function createDocumentHistory() {
482
482
  function historyIds(list) {
483
483
  return list.split(",").map((id) => decodeURIComponent(id));
484
484
  }
485
- const ACCESS_PATH = /^\/access\/([^/]+)\/([^/]+)(\/.*)?$/, ACCESS_USER_PATH = /^\/users\/([^/]+)$/, ACCESS_ROLE_PATH = /^\/roles\/([^/]+)$/, PROJECT_ROLES_PATH = /^\/projects\/([^/]+)\/roles$/, PROJECT_FEATURES_PATH = /^\/projects\/([^/]+)\/features$/, MY_ATTRIBUTES_PATH = /^\/organizations\/([^/]+)\/users\/me\/attributes$/, DATA_HISTORY_PATH = /^\/data\/history\/([^/]+)(\/.*)$/, PROJECT_API_PATHS = [
485
+ const ACCESS_PATH = /^\/access\/([^/]+)\/([^/]+)(\/.*)?$/, ACCESS_USER_PATH = /^\/users\/([^/]+)$/, ACCESS_ROLE_PATH = /^\/roles\/([^/]+)$/, ACCESS_ROBOT_PATH = /^\/robots\/([^/]+)$/, PROJECT_ROLES_PATH = /^\/projects\/([^/]+)\/roles$/, PROJECT_FEATURES_PATH = /^\/projects\/([^/]+)\/features$/, MY_ATTRIBUTES_PATH = /^\/organizations\/([^/]+)\/users\/me\/attributes$/, DATA_HISTORY_PATH = /^\/data\/history\/([^/]+)(\/.*)$/, ACCESS_API_SINCE = "2025-07-11", PROJECT_API_PATHS = [
486
486
  /^\/users\/me$/,
487
487
  /^\/projects\/[^/]+$/,
488
488
  /^\/projects\/[^/]+\/users\/[^/]+$/,
@@ -525,6 +525,7 @@ async function servePlatformRequest(ctx, uri, request) {
525
525
  }
526
526
  const access = path.match(ACCESS_PATH);
527
527
  if (access !== null) {
528
+ if (ctx.apiVersion < ACCESS_API_SINCE) throw notMounted(request);
528
529
  const [, type, id, rest2] = access;
529
530
  return serveAccess({ ctx, request, type, id: decodeURIComponent(id), rest: rest2 ?? "", query });
530
531
  }
@@ -668,6 +669,13 @@ function serveAccess(args) {
668
669
  if (record === void 0) throw notFound("User")(request);
669
670
  return { value: record };
670
671
  });
672
+ const robot = rest.match(ACCESS_ROBOT_PATH);
673
+ if (robot !== null)
674
+ return gated("served", `sanity.${resource.type}.tokens.read`, () => {
675
+ const robotId = decodeURIComponent(robot[1]), record = robotOn({ resource, robotId, identities: ctx.identities() });
676
+ if (record === void 0) throw notFound("Robot")(request);
677
+ return { value: record };
678
+ });
671
679
  const role = rest.match(ACCESS_ROLE_PATH);
672
680
  if (role !== null)
673
681
  return gated(row.roles, row.rolesPermission, () => {
@@ -727,11 +735,26 @@ function membershipOn(identity, resource) {
727
735
  roleNames: roles.map((role) => role.name).toSorted()
728
736
  };
729
737
  }
738
+ function robotOn(args) {
739
+ const { resource, robotId, identities } = args, identity = identities.find(
740
+ (candidate) => candidate.robot && (candidate.globalUser ?? candidate.user).id === robotId
741
+ ), membership = identity === void 0 ? void 0 : membershipOn(identity, resource);
742
+ if (identity === void 0 || membership === void 0 || membership.roleNames.length === 0)
743
+ return;
744
+ const user = identity.globalUser ?? identity.user;
745
+ return {
746
+ id: robotId,
747
+ label: user.name ?? robotId,
748
+ createdAt: (/* @__PURE__ */ new Date(0)).toISOString(),
749
+ memberships: [membership],
750
+ networkPolicy: null
751
+ };
752
+ }
730
753
  function usersOn(args) {
731
754
  const { resource, registry, identities } = args, owned = ACCESS_CAPABILITIES[resource.type].owns?.(registry) ?? [], bySanityUserId = /* @__PURE__ */ new Map();
732
755
  for (const identity of identities) {
733
756
  const own = membershipOn(identity, resource);
734
- if (own === void 0) continue;
757
+ if (own === void 0 || identity.robot) continue;
735
758
  const user = identity.globalUser ?? identity.user, record = {
736
759
  sanityUserId: user.id,
737
760
  profile: {
@@ -922,8 +945,10 @@ function makeRequestImpl(ctx) {
922
945
  const { getAccess, requestFor, useProjectHostname, listIdentities, organizationId, platform } = ctx;
923
946
  return async (opts) => {
924
947
  const accessControl = getAccess(), path = opts.uri ?? opts.url;
925
- if (path === "/users/me")
926
- return useProjectHostname ? accessControl.currentUser : accessControl.globalUser ?? accessControl.currentUser;
948
+ if (path === "/users/me") {
949
+ const user = useProjectHostname ? accessControl.currentUser : accessControl.globalUser ?? accessControl.currentUser;
950
+ return accessControl.robot ? { ...user, provider: "sanity-token" } : user;
951
+ }
927
952
  if (path !== void 0 && path === accessControl.aclPath)
928
953
  return refuseAclRequest(platform, requestFor(opts)), accessControl.grants;
929
954
  const projectUsers = path?.match(PROJECT_USERS_PATH), [, pathProjectId, requestedIds] = projectUsers ?? [];
@@ -943,7 +968,7 @@ function makeRequestImpl(ctx) {
943
968
  const served = path === void 0 ? void 0 : await servePlatformRequest(platform, path, requestFor(opts));
944
969
  if (served !== void 0) return served.value;
945
970
  throw fakeError(
946
- `request(): no handler for path "${path}". With access control configured, the fake serves "/users/me", the handle's ACL ("<data base>/acl"), "/projects/<projectId>" (members), "/projects/<projectId>/users/<ids>", "/projects/<projectId>/roles", "/projects/<projectId>/features", the History API (client.getDataUrl("history", \u2026)), resource lookup ("/media-libraries/<id>", "/canvases/<id>", "/dashboards/<id>") and the Access API ("/access/<type>/<id>/roles[/<name>] | users[/<id>] | user-permissions/me[/check]") and the caller's attributes ("/organizations/<id>/users/me/attributes"). For any other endpoint, register a stub (client.stub("request", fn)) or intercept the HTTP call with a tool like MSW.`
971
+ `request(): no handler for path "${path}". With access control configured, the fake serves "/users/me", the handle's ACL ("<data base>/acl"), "/projects/<projectId>" (members), "/projects/<projectId>/users/<ids>", "/projects/<projectId>/roles", "/projects/<projectId>/features", the History API (client.getDataUrl("history", \u2026)), resource lookup ("/media-libraries/<id>", "/canvases/<id>", "/dashboards/<id>") and the Access API ("/access/<type>/<id>/roles[/<name>] | users[/<id>] | robots/<id> | user-permissions/me[/check]") and the caller's attributes ("/organizations/<id>/users/me/attributes"). For any other endpoint, register a stub (client.stub("request", fn)) or intercept the HTTP call with a tool like MSW.`
947
972
  );
948
973
  };
949
974
  }
@@ -2300,6 +2325,7 @@ function makeHandle(handleArgs) {
2300
2325
  // Per-project records apply only where the resource lives in a project.
2301
2326
  currentUser: capabilitiesOf(target.type).identity === "project" ? projectUserOf(identity, projectId) : identity.user,
2302
2327
  ...identity.globalUser !== void 0 ? { globalUser: identity.globalUser } : {},
2328
+ ...identity.robot ? { robot: !0 } : {},
2303
2329
  grants: identity.grants,
2304
2330
  ...identity.access !== void 0 ? { access: identity.access } : {}
2305
2331
  };
@@ -2811,6 +2837,7 @@ function makeHandle(handleArgs) {
2811
2837
  listIdentities: members,
2812
2838
  platform: {
2813
2839
  projectId,
2840
+ apiVersion,
2814
2841
  organizationId: backend.organizationId,
2815
2842
  projectFeatures: backend.projectFeatures,
2816
2843
  own: (asked) => registryKey(asked) === registryKey(target) ? {