@sanity-labs/client-fake-for-test 0.25.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/README.md +1 -1
- package/dist/index.cjs +30 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +30 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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\/([^/]+)(\/.*)$/, ACCESS_API_SINCE = "2025-07-11", 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\/[^/]+$/,
|
|
@@ -669,6 +669,13 @@ function serveAccess(args) {
|
|
|
669
669
|
if (record === void 0) throw notFound("User")(request);
|
|
670
670
|
return { value: record };
|
|
671
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
|
+
});
|
|
672
679
|
const role = rest.match(ACCESS_ROLE_PATH);
|
|
673
680
|
if (role !== null)
|
|
674
681
|
return gated(row.roles, row.rolesPermission, () => {
|
|
@@ -728,11 +735,26 @@ function membershipOn(identity, resource) {
|
|
|
728
735
|
roleNames: roles.map((role) => role.name).toSorted()
|
|
729
736
|
};
|
|
730
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
|
+
}
|
|
731
753
|
function usersOn(args) {
|
|
732
754
|
const { resource, registry, identities } = args, owned = ACCESS_CAPABILITIES[resource.type].owns?.(registry) ?? [], bySanityUserId = /* @__PURE__ */ new Map();
|
|
733
755
|
for (const identity of identities) {
|
|
734
756
|
const own = membershipOn(identity, resource);
|
|
735
|
-
if (own === void 0) continue;
|
|
757
|
+
if (own === void 0 || identity.robot) continue;
|
|
736
758
|
const user = identity.globalUser ?? identity.user, record = {
|
|
737
759
|
sanityUserId: user.id,
|
|
738
760
|
profile: {
|
|
@@ -923,8 +945,10 @@ function makeRequestImpl(ctx) {
|
|
|
923
945
|
const { getAccess, requestFor, useProjectHostname, listIdentities, organizationId, platform } = ctx;
|
|
924
946
|
return async (opts) => {
|
|
925
947
|
const accessControl = getAccess(), path = opts.uri ?? opts.url;
|
|
926
|
-
if (path === "/users/me")
|
|
927
|
-
|
|
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
|
+
}
|
|
928
952
|
if (path !== void 0 && path === accessControl.aclPath)
|
|
929
953
|
return refuseAclRequest(platform, requestFor(opts)), accessControl.grants;
|
|
930
954
|
const projectUsers = path?.match(PROJECT_USERS_PATH), [, pathProjectId, requestedIds] = projectUsers ?? [];
|
|
@@ -944,7 +968,7 @@ function makeRequestImpl(ctx) {
|
|
|
944
968
|
const served = path === void 0 ? void 0 : await servePlatformRequest(platform, path, requestFor(opts));
|
|
945
969
|
if (served !== void 0) return served.value;
|
|
946
970
|
throw fakeError(
|
|
947
|
-
`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.`
|
|
948
972
|
);
|
|
949
973
|
};
|
|
950
974
|
}
|
|
@@ -2301,6 +2325,7 @@ function makeHandle(handleArgs) {
|
|
|
2301
2325
|
// Per-project records apply only where the resource lives in a project.
|
|
2302
2326
|
currentUser: capabilitiesOf(target.type).identity === "project" ? projectUserOf(identity, projectId) : identity.user,
|
|
2303
2327
|
...identity.globalUser !== void 0 ? { globalUser: identity.globalUser } : {},
|
|
2328
|
+
...identity.robot ? { robot: !0 } : {},
|
|
2304
2329
|
grants: identity.grants,
|
|
2305
2330
|
...identity.access !== void 0 ? { access: identity.access } : {}
|
|
2306
2331
|
};
|