@sanity-labs/client-fake-for-test 0.26.0 → 0.27.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.js CHANGED
@@ -117,6 +117,25 @@ function invalidAccessType(request, type) {
117
117
  message: `Invalid resource type ${type}`
118
118
  });
119
119
  }
120
+ function robotsTypeRefused(request) {
121
+ return new PlatformHttpError({
122
+ ...request,
123
+ statusCode: 400,
124
+ statusMessage: "Bad Request",
125
+ body: {
126
+ statusCode: 400,
127
+ error: "Bad Request",
128
+ message: "Validation failed",
129
+ validation: {
130
+ params: {
131
+ source: "params",
132
+ keys: ["resourceType"],
133
+ message: '"resourceType" must be one of [project, organization]'
134
+ }
135
+ }
136
+ }
137
+ });
138
+ }
120
139
  function notFound(name) {
121
140
  return (request) => accessApiError({
122
141
  request,
@@ -482,7 +501,7 @@ function createDocumentHistory() {
482
501
  function historyIds(list) {
483
502
  return list.split(",").map((id) => decodeURIComponent(id));
484
503
  }
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 = [
504
+ 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
505
  /^\/users\/me$/,
487
506
  /^\/projects\/[^/]+$/,
488
507
  /^\/projects\/[^/]+\/users\/[^/]+$/,
@@ -670,12 +689,16 @@ function serveAccess(args) {
670
689
  return { value: record };
671
690
  });
672
691
  const robot = rest.match(ACCESS_ROBOT_PATH);
673
- if (robot !== null)
692
+ if (robot !== null) {
693
+ if (!ROBOT_RESOURCE_TYPES.has(resource.type)) throw robotsTypeRefused(request);
674
694
  return gated("served", `sanity.${resource.type}.tokens.read`, () => {
695
+ if (robot[1] === void 0)
696
+ return { value: robotsPage({ resource, registry, query, identities: ctx.identities() }) };
675
697
  const robotId = decodeURIComponent(robot[1]), record = robotOn({ resource, robotId, identities: ctx.identities() });
676
698
  if (record === void 0) throw notFound("Robot")(request);
677
699
  return { value: record };
678
700
  });
701
+ }
679
702
  const role = rest.match(ACCESS_ROLE_PATH);
680
703
  if (role !== null)
681
704
  return gated(row.roles, row.rolesPermission, () => {
@@ -742,11 +765,39 @@ function robotOn(args) {
742
765
  if (identity === void 0 || membership === void 0 || membership.roleNames.length === 0)
743
766
  return;
744
767
  const user = identity.globalUser ?? identity.user;
768
+ return robotRecord({ id: robotId, label: user.name, memberships: [membership] });
769
+ }
770
+ const ROBOT_RESOURCE_TYPES = /* @__PURE__ */ new Set(["project", "organization"]), ROBOTS_LIMIT = { default: 100, max: 500 };
771
+ function robotsPage(args) {
772
+ const { resource, registry, query, identities } = args, limit = robotsLimit(query), cursor = query.get("nextCursor"), children = query.get("includeChildren") === "true" ? ACCESS_CAPABILITIES[resource.type].owns?.(registry) ?? [] : [], byId = /* @__PURE__ */ new Map();
773
+ for (const identity of identities) {
774
+ if (!identity.robot) continue;
775
+ const user = identity.globalUser ?? identity.user, memberships = [resource, ...children].flatMap((target) => {
776
+ const membership = membershipOn(identity, target);
777
+ return membership === void 0 || membership.roleNames.length === 0 ? [] : [membership];
778
+ });
779
+ memberships.length !== 0 && byId.set(user.id, robotRecord({ id: user.id, label: user.name, memberships }));
780
+ }
781
+ const data = [...byId.values()].toSorted((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0).filter((robot) => cursor === null || robot.id > cursor).slice(0, limit);
782
+ return { data, nextCursor: data.length === limit ? data.at(-1)?.id ?? null : null };
783
+ }
784
+ function robotsLimit(query) {
785
+ const raw = query.get("limit");
786
+ if (raw === null) return ROBOTS_LIMIT.default;
787
+ const limit = Number(raw);
788
+ if (!Number.isInteger(limit) || limit < 1 || limit > ROBOTS_LIMIT.max)
789
+ throw fakeError(
790
+ `request(): robots got limit=${JSON.stringify(raw)}; the fake serves integer limits from 1 to ${ROBOTS_LIMIT.max} only (the platform's 400 for others is not simulated).`
791
+ );
792
+ return limit;
793
+ }
794
+ function robotRecord(args) {
795
+ const { id, label, memberships } = args;
745
796
  return {
746
- id: robotId,
747
- label: user.name ?? robotId,
797
+ id,
798
+ label: label ?? id,
748
799
  createdAt: (/* @__PURE__ */ new Date(0)).toISOString(),
749
- memberships: [membership],
800
+ memberships,
750
801
  networkPolicy: null
751
802
  };
752
803
  }
@@ -968,7 +1019,7 @@ function makeRequestImpl(ctx) {
968
1019
  const served = path === void 0 ? void 0 : await servePlatformRequest(platform, path, requestFor(opts));
969
1020
  if (served !== void 0) return served.value;
970
1021
  throw fakeError(
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.`
1022
+ `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.`
972
1023
  );
973
1024
  };
974
1025
  }