@sanity-labs/client-fake-for-test 0.14.0 → 0.16.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 +8 -3
- package/dist/index.cjs +159 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -6
- package/dist/index.d.ts +56 -6
- package/dist/index.js +159 -28
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -841,6 +841,12 @@ export declare type ReleaseType = "asap" | "scheduled" | "undecided";
|
|
|
841
841
|
export declare interface ResourceAccess {
|
|
842
842
|
roles?: NonNullable<CurrentUser["roles"]>;
|
|
843
843
|
permissions?: string[];
|
|
844
|
+
/**
|
|
845
|
+
* The user's attributes in the organization, as an administrator or SAML
|
|
846
|
+
* sync set them; read on `"organization:<id>"` entries only. Served by
|
|
847
|
+
* `GET /organizations/<id>/users/me/attributes`.
|
|
848
|
+
*/
|
|
849
|
+
attributes?: UserAttribute[];
|
|
844
850
|
}
|
|
845
851
|
|
|
846
852
|
/** Resource types `@sanity/client`'s `resource` config can name (the ones staging probed). */
|
|
@@ -998,12 +1004,25 @@ export declare interface TestClient {
|
|
|
998
1004
|
) => Promise<ActionResult>;
|
|
999
1005
|
/** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */
|
|
1000
1006
|
releases: ReleasesHandle;
|
|
1001
|
-
/**
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
+
/**
|
|
1008
|
+
* Helper for `sanity.action.document.version.create`: writes `document`, or,
|
|
1009
|
+
* given `baseId` (as `@sanity/client` takes it), copies that document into
|
|
1010
|
+
* `versions.<releaseId>.<publishedId>`.
|
|
1011
|
+
*/
|
|
1012
|
+
createVersion: (
|
|
1013
|
+
params:
|
|
1014
|
+
| {
|
|
1015
|
+
releaseId: string;
|
|
1016
|
+
publishedId: string;
|
|
1017
|
+
document: DocumentBody;
|
|
1018
|
+
}
|
|
1019
|
+
| {
|
|
1020
|
+
releaseId: string;
|
|
1021
|
+
publishedId: string;
|
|
1022
|
+
baseId: string;
|
|
1023
|
+
ifBaseRevisionId?: string;
|
|
1024
|
+
},
|
|
1025
|
+
) => Promise<ActionResult>;
|
|
1007
1026
|
/** Helper for `sanity.action.document.version.unpublish`. */
|
|
1008
1027
|
unpublishVersion: (params: {
|
|
1009
1028
|
releaseId: string;
|
|
@@ -1365,6 +1384,29 @@ declare interface UnsupportedEntry {
|
|
|
1365
1384
|
workaround: string;
|
|
1366
1385
|
}
|
|
1367
1386
|
|
|
1387
|
+
/**
|
|
1388
|
+
* One user attribute: its key, its type, and its value from each source.
|
|
1389
|
+
* The served row adds `activeSource` and `activeValue`: the Sanity value
|
|
1390
|
+
* when there is one, else the SAML value.
|
|
1391
|
+
*/
|
|
1392
|
+
export declare interface UserAttribute {
|
|
1393
|
+
key: string;
|
|
1394
|
+
type: string;
|
|
1395
|
+
values: {
|
|
1396
|
+
saml?: UserAttributeValue;
|
|
1397
|
+
sanity?: UserAttributeValue;
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/** A value a user attribute can hold. */
|
|
1402
|
+
export declare type UserAttributeValue =
|
|
1403
|
+
| string
|
|
1404
|
+
| number
|
|
1405
|
+
| boolean
|
|
1406
|
+
| string[]
|
|
1407
|
+
| number[]
|
|
1408
|
+
| boolean[];
|
|
1409
|
+
|
|
1368
1410
|
export declare type VersionAction =
|
|
1369
1411
|
| {
|
|
1370
1412
|
actionType: "sanity.action.document.version.create";
|
|
@@ -1372,6 +1414,14 @@ export declare type VersionAction =
|
|
|
1372
1414
|
document: DocumentBody;
|
|
1373
1415
|
releaseId?: string;
|
|
1374
1416
|
}
|
|
1417
|
+
| {
|
|
1418
|
+
/** Copy the document at `baseId` into `versionId`, as `@sanity/client` sends `createVersion({ baseId })`. */
|
|
1419
|
+
actionType: "sanity.action.document.version.create";
|
|
1420
|
+
publishedId: string;
|
|
1421
|
+
baseId: string;
|
|
1422
|
+
versionId: string;
|
|
1423
|
+
ifBaseRevisionId?: string;
|
|
1424
|
+
}
|
|
1375
1425
|
| {
|
|
1376
1426
|
actionType: "sanity.action.document.version.replace";
|
|
1377
1427
|
document: SanityDocumentLike;
|
package/dist/index.d.ts
CHANGED
|
@@ -841,6 +841,12 @@ export declare type ReleaseType = "asap" | "scheduled" | "undecided";
|
|
|
841
841
|
export declare interface ResourceAccess {
|
|
842
842
|
roles?: NonNullable<CurrentUser["roles"]>;
|
|
843
843
|
permissions?: string[];
|
|
844
|
+
/**
|
|
845
|
+
* The user's attributes in the organization, as an administrator or SAML
|
|
846
|
+
* sync set them; read on `"organization:<id>"` entries only. Served by
|
|
847
|
+
* `GET /organizations/<id>/users/me/attributes`.
|
|
848
|
+
*/
|
|
849
|
+
attributes?: UserAttribute[];
|
|
844
850
|
}
|
|
845
851
|
|
|
846
852
|
/** Resource types `@sanity/client`'s `resource` config can name (the ones staging probed). */
|
|
@@ -998,12 +1004,25 @@ export declare interface TestClient {
|
|
|
998
1004
|
) => Promise<ActionResult>;
|
|
999
1005
|
/** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */
|
|
1000
1006
|
releases: ReleasesHandle;
|
|
1001
|
-
/**
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
+
/**
|
|
1008
|
+
* Helper for `sanity.action.document.version.create`: writes `document`, or,
|
|
1009
|
+
* given `baseId` (as `@sanity/client` takes it), copies that document into
|
|
1010
|
+
* `versions.<releaseId>.<publishedId>`.
|
|
1011
|
+
*/
|
|
1012
|
+
createVersion: (
|
|
1013
|
+
params:
|
|
1014
|
+
| {
|
|
1015
|
+
releaseId: string;
|
|
1016
|
+
publishedId: string;
|
|
1017
|
+
document: DocumentBody;
|
|
1018
|
+
}
|
|
1019
|
+
| {
|
|
1020
|
+
releaseId: string;
|
|
1021
|
+
publishedId: string;
|
|
1022
|
+
baseId: string;
|
|
1023
|
+
ifBaseRevisionId?: string;
|
|
1024
|
+
},
|
|
1025
|
+
) => Promise<ActionResult>;
|
|
1007
1026
|
/** Helper for `sanity.action.document.version.unpublish`. */
|
|
1008
1027
|
unpublishVersion: (params: {
|
|
1009
1028
|
releaseId: string;
|
|
@@ -1365,6 +1384,29 @@ declare interface UnsupportedEntry {
|
|
|
1365
1384
|
workaround: string;
|
|
1366
1385
|
}
|
|
1367
1386
|
|
|
1387
|
+
/**
|
|
1388
|
+
* One user attribute: its key, its type, and its value from each source.
|
|
1389
|
+
* The served row adds `activeSource` and `activeValue`: the Sanity value
|
|
1390
|
+
* when there is one, else the SAML value.
|
|
1391
|
+
*/
|
|
1392
|
+
export declare interface UserAttribute {
|
|
1393
|
+
key: string;
|
|
1394
|
+
type: string;
|
|
1395
|
+
values: {
|
|
1396
|
+
saml?: UserAttributeValue;
|
|
1397
|
+
sanity?: UserAttributeValue;
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/** A value a user attribute can hold. */
|
|
1402
|
+
export declare type UserAttributeValue =
|
|
1403
|
+
| string
|
|
1404
|
+
| number
|
|
1405
|
+
| boolean
|
|
1406
|
+
| string[]
|
|
1407
|
+
| number[]
|
|
1408
|
+
| boolean[];
|
|
1409
|
+
|
|
1368
1410
|
export declare type VersionAction =
|
|
1369
1411
|
| {
|
|
1370
1412
|
actionType: "sanity.action.document.version.create";
|
|
@@ -1372,6 +1414,14 @@ export declare type VersionAction =
|
|
|
1372
1414
|
document: DocumentBody;
|
|
1373
1415
|
releaseId?: string;
|
|
1374
1416
|
}
|
|
1417
|
+
| {
|
|
1418
|
+
/** Copy the document at `baseId` into `versionId`, as `@sanity/client` sends `createVersion({ baseId })`. */
|
|
1419
|
+
actionType: "sanity.action.document.version.create";
|
|
1420
|
+
publishedId: string;
|
|
1421
|
+
baseId: string;
|
|
1422
|
+
versionId: string;
|
|
1423
|
+
ifBaseRevisionId?: string;
|
|
1424
|
+
}
|
|
1375
1425
|
| {
|
|
1376
1426
|
actionType: "sanity.action.document.version.replace";
|
|
1377
1427
|
document: SanityDocumentLike;
|
package/dist/index.js
CHANGED
|
@@ -282,6 +282,8 @@ const ACCESS_CAPABILITIES = {
|
|
|
282
282
|
},
|
|
283
283
|
project: {
|
|
284
284
|
roles: "served",
|
|
285
|
+
// Also gates `GET /projects/<id>/roles`; the stock user roles all hold it on staging.
|
|
286
|
+
rolesPermission: "sanity.project.roles.read",
|
|
285
287
|
users: "served",
|
|
286
288
|
// Single-user reads are served only where the fake checks members read (the
|
|
287
289
|
// organization); elsewhere they refuse rather than answer unchecked.
|
|
@@ -290,8 +292,9 @@ const ACCESS_CAPABILITIES = {
|
|
|
290
292
|
memberRoles: ({ identity, id }) => (identity.usersByProject?.[id] ?? identity.user).roles ?? []
|
|
291
293
|
},
|
|
292
294
|
"media-library": {
|
|
293
|
-
//
|
|
294
|
-
roles:
|
|
295
|
+
// Staging refuses it only because no probe token held roles read.
|
|
296
|
+
roles: "served",
|
|
297
|
+
rolesPermission: "sanity.media-library.roles.read",
|
|
295
298
|
users: "served",
|
|
296
299
|
// Unproven on staging (only the listing was probed); refused as on `project`.
|
|
297
300
|
user: noPermission,
|
|
@@ -330,7 +333,7 @@ function dataUrl(args) {
|
|
|
330
333
|
const base = `/${operation}/${dataset}`;
|
|
331
334
|
return `/data${path !== void 0 ? `${base}/${path}` : base}`.replace(/\/($|\?)/, "$1");
|
|
332
335
|
}
|
|
333
|
-
const ACCESS_PATH = /^\/access\/([^/]+)\/([^/]+)(\/.*)?$/, ACCESS_USER_PATH = /^\/users\/([^/]+)$/, DATA_HISTORY_PATH = /^\/data\/history\/([^/]+)(\/.*)$/;
|
|
336
|
+
const ACCESS_PATH = /^\/access\/([^/]+)\/([^/]+)(\/.*)?$/, ACCESS_USER_PATH = /^\/users\/([^/]+)$/, ACCESS_ROLE_PATH = /^\/roles\/([^/]+)$/, PROJECT_ROLES_PATH = /^\/projects\/([^/]+)\/roles$/, MY_ATTRIBUTES_PATH = /^\/organizations\/([^/]+)\/users\/me\/attributes$/, DATA_HISTORY_PATH = /^\/data\/history\/([^/]+)(\/.*)$/;
|
|
334
337
|
function page(data) {
|
|
335
338
|
return { data, nextCursor: null };
|
|
336
339
|
}
|
|
@@ -339,7 +342,17 @@ function usersPage(data) {
|
|
|
339
342
|
}
|
|
340
343
|
const UNMODELLED_PROFILE = { provider: "sanity", createdAt: (/* @__PURE__ */ new Date(0)).toISOString() };
|
|
341
344
|
async function servePlatformRequest(ctx, url) {
|
|
342
|
-
const queryStart = url.indexOf("?"), path = queryStart === -1 ? url : url.slice(0, queryStart), query = new URLSearchParams(queryStart === -1 ? "" : url.slice(queryStart + 1)), request = { method: "GET", path },
|
|
345
|
+
const queryStart = url.indexOf("?"), path = queryStart === -1 ? url : url.slice(0, queryStart), query = new URLSearchParams(queryStart === -1 ? "" : url.slice(queryStart + 1)), request = { method: "GET", path }, myAttributes = path.match(MY_ATTRIBUTES_PATH);
|
|
346
|
+
if (myAttributes !== null) {
|
|
347
|
+
const organizationId = decodeURIComponent(myAttributes[1]);
|
|
348
|
+
return serveMyAttributes({ ctx, request, organizationId, query });
|
|
349
|
+
}
|
|
350
|
+
const projectRoles = path.match(PROJECT_ROLES_PATH);
|
|
351
|
+
if (projectRoles !== null) {
|
|
352
|
+
const projectId = decodeURIComponent(projectRoles[1]);
|
|
353
|
+
return serveProjectRoles({ ctx, request, projectId });
|
|
354
|
+
}
|
|
355
|
+
const access = path.match(ACCESS_PATH);
|
|
343
356
|
if (access !== null) {
|
|
344
357
|
const [, type, id, rest2] = access;
|
|
345
358
|
return serveAccess({ ctx, request, type, id: decodeURIComponent(id), rest: rest2 ?? "", query });
|
|
@@ -406,6 +419,35 @@ async function readHistory(args) {
|
|
|
406
419
|
}
|
|
407
420
|
};
|
|
408
421
|
}
|
|
422
|
+
function serveGated(args) {
|
|
423
|
+
const { capability, permission, held, request, serve } = args;
|
|
424
|
+
return serveCapability(capability, request, () => {
|
|
425
|
+
if (permission !== void 0 && !held.includes(permission)) throw noPermission(request);
|
|
426
|
+
return serve();
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
function serveProjectRoles(args) {
|
|
430
|
+
const { ctx, request, projectId } = args, row = ACCESS_CAPABILITIES.project;
|
|
431
|
+
return serveGated({
|
|
432
|
+
capability: row.roles,
|
|
433
|
+
permission: row.rolesPermission,
|
|
434
|
+
held: ctx.actorAccess()?.[accessKey("project", projectId)]?.permissions ?? [],
|
|
435
|
+
request,
|
|
436
|
+
serve: () => ({
|
|
437
|
+
value: rolesOn({
|
|
438
|
+
resource: { type: "project", id: projectId },
|
|
439
|
+
identities: ctx.identities()
|
|
440
|
+
}).map((role) => ({
|
|
441
|
+
name: role.name,
|
|
442
|
+
title: role.title,
|
|
443
|
+
...role.description !== void 0 ? { description: role.description } : {},
|
|
444
|
+
projectId,
|
|
445
|
+
appliesToUsers: role.appliesToUsers,
|
|
446
|
+
appliesToRobots: role.appliesToRobots
|
|
447
|
+
}))
|
|
448
|
+
})
|
|
449
|
+
});
|
|
450
|
+
}
|
|
409
451
|
function serveAccess(args) {
|
|
410
452
|
const { ctx, request, type, id, rest, query } = args, row = accessCapabilitiesOf(type);
|
|
411
453
|
if (row === void 0) throw invalidAccessType(request, type);
|
|
@@ -421,11 +463,7 @@ function serveAccess(args) {
|
|
|
421
463
|
}
|
|
422
464
|
if (row.unknown !== void 0 && !row.unknown.holds(registry, id))
|
|
423
465
|
throw row.unknown.refusal(request);
|
|
424
|
-
const
|
|
425
|
-
if (row.usersPermission !== void 0 && !permissions.includes(row.usersPermission))
|
|
426
|
-
throw noPermission(request);
|
|
427
|
-
return serve();
|
|
428
|
-
}), user = rest.match(ACCESS_USER_PATH);
|
|
466
|
+
const gated = (capability, permission, serve) => serveGated({ capability, permission, held: permissions, request, serve }), users = (capability, serve) => gated(capability, row.usersPermission, serve), roles = () => rolesOn({ resource, identities: ctx.identities() }), user = rest.match(ACCESS_USER_PATH);
|
|
429
467
|
if (user !== null)
|
|
430
468
|
return users(row.user, () => {
|
|
431
469
|
const sanityUserId = decodeURIComponent(user[1]), record = usersOn({ resource, registry, identities: ctx.identities() }).find(
|
|
@@ -434,11 +472,16 @@ function serveAccess(args) {
|
|
|
434
472
|
if (record === void 0) throw notFound("User")(request);
|
|
435
473
|
return { value: record };
|
|
436
474
|
});
|
|
475
|
+
const role = rest.match(ACCESS_ROLE_PATH);
|
|
476
|
+
if (role !== null)
|
|
477
|
+
return gated(row.roles, row.rolesPermission, () => {
|
|
478
|
+
const name = decodeURIComponent(role[1]), record = roles().find((candidate) => candidate.name === name);
|
|
479
|
+
if (record === void 0) throw notFound("Role")(request);
|
|
480
|
+
return { value: record };
|
|
481
|
+
});
|
|
437
482
|
switch (rest) {
|
|
438
483
|
case "/roles":
|
|
439
|
-
return
|
|
440
|
-
value: page(rolesOn({ resource, identities: ctx.identities() }))
|
|
441
|
-
}));
|
|
484
|
+
return gated(row.roles, row.rolesPermission, () => ({ value: page(roles()) }));
|
|
442
485
|
case "/users":
|
|
443
486
|
return users(row.users, () => ({
|
|
444
487
|
value: usersPage(usersOn({ resource, registry, identities: ctx.identities() }))
|
|
@@ -461,6 +504,7 @@ function serveAccess(args) {
|
|
|
461
504
|
return;
|
|
462
505
|
}
|
|
463
506
|
}
|
|
507
|
+
const UNMODELLED_ROLE = { appliesToUsers: !0, appliesToRobots: !0 };
|
|
464
508
|
function rolesOn(args) {
|
|
465
509
|
const { resource } = args, byName = /* @__PURE__ */ new Map();
|
|
466
510
|
for (const identity of args.identities) {
|
|
@@ -468,8 +512,12 @@ function rolesOn(args) {
|
|
|
468
512
|
for (const role of roles ?? [])
|
|
469
513
|
byName.set(role.name, {
|
|
470
514
|
name: role.name,
|
|
471
|
-
|
|
472
|
-
...role.description !== void 0 ? { description: role.description } : {}
|
|
515
|
+
title: role.title ?? role.name,
|
|
516
|
+
...role.description !== void 0 ? { description: role.description } : {},
|
|
517
|
+
resourceType: resource.type,
|
|
518
|
+
resourceId: resource.id,
|
|
519
|
+
...UNMODELLED_ROLE,
|
|
520
|
+
permissions: []
|
|
473
521
|
});
|
|
474
522
|
}
|
|
475
523
|
return [...byName.values()].toSorted((a, b) => a.name.localeCompare(b.name));
|
|
@@ -507,12 +555,40 @@ function usersOn(args) {
|
|
|
507
555
|
}, existing = bySanityUserId.get(record.sanityUserId);
|
|
508
556
|
if (existing !== void 0 && JSON.stringify(existing) !== JSON.stringify(record))
|
|
509
557
|
throw fakeError(
|
|
510
|
-
`request(): two registered
|
|
558
|
+
`request(): two identities (registered, or the handle's inline accessControl) resolve to user "${record.sanityUserId}" on ${resource.type} "${resource.id}" with different records. Fix the identities so duplicate ids carry identical records.`
|
|
511
559
|
);
|
|
512
560
|
bySanityUserId.set(record.sanityUserId, record);
|
|
513
561
|
}
|
|
514
562
|
return [...bySanityUserId.values()];
|
|
515
563
|
}
|
|
564
|
+
const DEFAULT_ATTRIBUTES_LIMIT = 50;
|
|
565
|
+
function serveMyAttributes(args) {
|
|
566
|
+
const { ctx, request, organizationId, query } = args, organization = ACCESS_CAPABILITIES.organization, registry = { organizationId: ctx.organizationId, resources: ctx.resources };
|
|
567
|
+
if (organization.unknown !== void 0 && !organization.unknown.holds(registry, organizationId))
|
|
568
|
+
throw organization.unknown.refusal(request);
|
|
569
|
+
const entry = ctx.actorAccess()?.[accessKey("organization", organizationId)];
|
|
570
|
+
if (entry?.roles === void 0) throw notFound("User")(request);
|
|
571
|
+
const limit = attributesLimit(query), cursor = query.get("cursor"), attributes = (entry.attributes ?? []).map(attributeRecord).toSorted((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0).filter((attribute) => cursor === null || attribute.key > cursor).slice(0, limit);
|
|
572
|
+
return { value: { sanityUserId: ctx.actorUser().id, organizationId, attributes } };
|
|
573
|
+
}
|
|
574
|
+
function attributesLimit(query) {
|
|
575
|
+
const raw = query.get("limit");
|
|
576
|
+
if (raw === null) return DEFAULT_ATTRIBUTES_LIMIT;
|
|
577
|
+
const limit = Number(raw);
|
|
578
|
+
if (!Number.isInteger(limit) || limit < 1)
|
|
579
|
+
throw fakeError(
|
|
580
|
+
`request(): users/me/attributes got limit=${JSON.stringify(raw)}; the fake serves positive integer limits only (the platform's answer to others is not simulated).`
|
|
581
|
+
);
|
|
582
|
+
return limit;
|
|
583
|
+
}
|
|
584
|
+
function attributeRecord(attribute) {
|
|
585
|
+
const { key, type, values } = attribute, activeSource = values.sanity !== void 0 ? "sanity" : "saml", activeValue = values[activeSource];
|
|
586
|
+
if (activeValue === void 0)
|
|
587
|
+
throw fakeError(
|
|
588
|
+
`request(): user attribute "${key}" is seeded with no value; give it a \`saml\` or \`sanity\` value.`
|
|
589
|
+
);
|
|
590
|
+
return { type, key, values: { ...values }, activeSource, activeValue };
|
|
591
|
+
}
|
|
516
592
|
class WriteAccessDeniedError extends Error {
|
|
517
593
|
documentId;
|
|
518
594
|
permission;
|
|
@@ -584,7 +660,7 @@ function consistentProjectRecords(args) {
|
|
|
584
660
|
const existing = byId.get(record.id);
|
|
585
661
|
if (existing !== void 0 && JSON.stringify(existing) !== JSON.stringify(record))
|
|
586
662
|
throw fakeError(
|
|
587
|
-
`request(): two registered
|
|
663
|
+
`request(): two identities (registered, or the handle's inline accessControl) resolve to project user "${record.id}" in project "${args.projectId}" with different records. The project records cannot be served unambiguously \u2014 fix the identities so duplicate ids carry identical records.`
|
|
588
664
|
);
|
|
589
665
|
byId.set(record.id, record);
|
|
590
666
|
}
|
|
@@ -669,7 +745,7 @@ function makeRequestImpl(ctx) {
|
|
|
669
745
|
const served = path === void 0 ? void 0 : await servePlatformRequest(platform, path);
|
|
670
746
|
if (served !== void 0) return served.value;
|
|
671
747
|
throw fakeError(
|
|
672
|
-
`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>", the History API (client.getDataUrl("history", \u2026)), resource lookup ("/media-libraries/<id>", "/canvases/<id>", "/dashboards/<id>") and the Access API ("/access/<type>/<id>/roles | users[/<id>] | user-permissions/me[/check]"). For any other endpoint, register a stub (client.stub("request", fn)) or intercept the HTTP call with a tool like MSW.`
|
|
748
|
+
`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", 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.`
|
|
673
749
|
);
|
|
674
750
|
};
|
|
675
751
|
}
|
|
@@ -1215,6 +1291,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1215
1291
|
return store.remove(release._id), { transactionId: cryptoRandomKey() };
|
|
1216
1292
|
}
|
|
1217
1293
|
case "sanity.action.document.version.create": {
|
|
1294
|
+
if ("baseId" in action) return createVersionFromBase(store, action);
|
|
1218
1295
|
const releaseId = action.releaseId ?? (action.document._id !== void 0 ? parseVersionId(action.document._id).releaseId : void 0);
|
|
1219
1296
|
if (releaseId === void 0)
|
|
1220
1297
|
throw fakeError(
|
|
@@ -1286,10 +1363,33 @@ function requireDraftOf(action) {
|
|
|
1286
1363
|
`${action.actionType}: draftId "${action.draftId}" is not the draft of publishedId "${action.publishedId}" \u2014 expected draftId "drafts.${action.publishedId}".`
|
|
1287
1364
|
);
|
|
1288
1365
|
}
|
|
1366
|
+
function createVersionFromBase(store, action) {
|
|
1367
|
+
const { releaseId, publishedId } = parseVersionId(action.versionId);
|
|
1368
|
+
if (publishedId !== action.publishedId)
|
|
1369
|
+
throw fakeError(
|
|
1370
|
+
`version.create: versionId "${action.versionId}" is not a version of publishedId "${action.publishedId}" (expected "${versionDocId(releaseId, action.publishedId)}").`
|
|
1371
|
+
);
|
|
1372
|
+
requireActive(getRelease(store, releaseId));
|
|
1373
|
+
const base = store.get(action.baseId);
|
|
1374
|
+
if (base === void 0)
|
|
1375
|
+
throw fakeError(
|
|
1376
|
+
`Cannot create version "${action.versionId}": base document "${action.baseId}" not found. version.create with \`baseId\` copies an existing document; pass \`document\` instead when there is nothing to copy.`
|
|
1377
|
+
);
|
|
1378
|
+
requireRevisionMatch(action.baseId, base._rev, action.ifBaseRevisionId);
|
|
1379
|
+
const existing = store.get(action.versionId), promoted = promote({
|
|
1380
|
+
...base,
|
|
1381
|
+
_id: action.versionId,
|
|
1382
|
+
_createdAt: existing?._createdAt ?? nowIso()
|
|
1383
|
+
});
|
|
1384
|
+
return store.put(promoted), { transactionId: cryptoRandomKey() };
|
|
1385
|
+
}
|
|
1289
1386
|
function requireRevisionMatch(documentId, actualRev, expectedRev) {
|
|
1290
1387
|
if (!(expectedRev === void 0 || actualRev === expectedRev))
|
|
1291
|
-
throw
|
|
1292
|
-
|
|
1388
|
+
throw Object.assign(
|
|
1389
|
+
fakeError(
|
|
1390
|
+
`Revision precondition failed for "${documentId}": expected _rev "${expectedRev}" but the document is at ${actualRev === void 0 ? "no revision (missing)" : `"${actualRev}"`}.`
|
|
1391
|
+
),
|
|
1392
|
+
{ statusCode: 409 }
|
|
1293
1393
|
);
|
|
1294
1394
|
}
|
|
1295
1395
|
async function applyActions(store, actions, hooks) {
|
|
@@ -1317,6 +1417,10 @@ async function applyActions(store, actions, hooks) {
|
|
|
1317
1417
|
break;
|
|
1318
1418
|
}
|
|
1319
1419
|
case "sanity.action.document.version.create": {
|
|
1420
|
+
if ("baseId" in a) {
|
|
1421
|
+
remember(a.versionId);
|
|
1422
|
+
break;
|
|
1423
|
+
}
|
|
1320
1424
|
const releaseId = a.releaseId ?? (typeof a.document._id == "string" ? parseVersionId(a.document._id).releaseId : void 0);
|
|
1321
1425
|
releaseId !== void 0 && remember(versionDocId(releaseId, a.publishedId));
|
|
1322
1426
|
break;
|
|
@@ -1732,7 +1836,17 @@ function makeHandle(handleArgs) {
|
|
|
1732
1836
|
if (access === void 0)
|
|
1733
1837
|
throw fakeError("this handle has no acting identity (no token or accessControl bound).");
|
|
1734
1838
|
return access;
|
|
1735
|
-
},
|
|
1839
|
+
}, members = () => [
|
|
1840
|
+
...backend.identities.values(),
|
|
1841
|
+
...token === void 0 && accessControl !== void 0 ? [
|
|
1842
|
+
{
|
|
1843
|
+
user: accessControl.currentUser,
|
|
1844
|
+
...accessControl.globalUser !== void 0 ? { globalUser: accessControl.globalUser } : {},
|
|
1845
|
+
grants: accessControl.grants,
|
|
1846
|
+
...accessControl.access !== void 0 ? { access: accessControl.access } : {}
|
|
1847
|
+
}
|
|
1848
|
+
] : []
|
|
1849
|
+
], capabilities = capabilitiesOf(target.type), targetLabel = capabilities.label(target.id), { store, history } = ensureEntry(registry, target), concurrentEditKey = (id) => registryKey(target) + " " + id, actorId = () => {
|
|
1736
1850
|
const access = activeAccess();
|
|
1737
1851
|
if (access !== void 0)
|
|
1738
1852
|
return capabilities.identity === "global" ? (access.globalUser ?? access.currentUser).id : access.currentUser.id;
|
|
@@ -2070,12 +2184,25 @@ function makeHandle(handleArgs) {
|
|
|
2070
2184
|
unarchive: ({ releaseId }) => runAction({ actionType: "sanity.action.release.unarchive", releaseId }),
|
|
2071
2185
|
delete: ({ releaseId }) => runAction({ actionType: "sanity.action.release.delete", releaseId })
|
|
2072
2186
|
},
|
|
2073
|
-
createVersion: (
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2187
|
+
createVersion: (params) => {
|
|
2188
|
+
const { releaseId, publishedId, document, baseId, ifBaseRevisionId } = params;
|
|
2189
|
+
if (document)
|
|
2190
|
+
return runAction({
|
|
2191
|
+
actionType: "sanity.action.document.version.create",
|
|
2192
|
+
releaseId,
|
|
2193
|
+
publishedId,
|
|
2194
|
+
document
|
|
2195
|
+
});
|
|
2196
|
+
if (baseId === void 0)
|
|
2197
|
+
throw fakeError("createVersion() requires `baseId` when no `document` is provided.");
|
|
2198
|
+
return runAction({
|
|
2199
|
+
actionType: "sanity.action.document.version.create",
|
|
2200
|
+
publishedId,
|
|
2201
|
+
baseId,
|
|
2202
|
+
versionId: `versions.${releaseId}.${publishedId}`,
|
|
2203
|
+
...ifBaseRevisionId !== void 0 ? { ifBaseRevisionId } : {}
|
|
2204
|
+
});
|
|
2205
|
+
},
|
|
2079
2206
|
unpublishVersion: ({ releaseId, publishedId }) => runAction({
|
|
2080
2207
|
actionType: "sanity.action.document.version.unpublish",
|
|
2081
2208
|
versionId: `versions.${releaseId}.${publishedId}`,
|
|
@@ -2088,7 +2215,7 @@ function makeHandle(handleArgs) {
|
|
|
2088
2215
|
// client to the global API host.
|
|
2089
2216
|
useProjectHostname: useProjectHostname && resource === void 0,
|
|
2090
2217
|
organizationId: backend.organizationId,
|
|
2091
|
-
listIdentities:
|
|
2218
|
+
listIdentities: members,
|
|
2092
2219
|
platform: {
|
|
2093
2220
|
projectId,
|
|
2094
2221
|
organizationId: backend.organizationId,
|
|
@@ -2098,8 +2225,12 @@ function makeHandle(handleArgs) {
|
|
|
2098
2225
|
history,
|
|
2099
2226
|
canRead
|
|
2100
2227
|
} : void 0,
|
|
2228
|
+
actorUser: () => {
|
|
2229
|
+
const access = requireActiveAccess();
|
|
2230
|
+
return access.globalUser ?? access.currentUser;
|
|
2231
|
+
},
|
|
2101
2232
|
actorAccess: () => requireActiveAccess().access,
|
|
2102
|
-
identities:
|
|
2233
|
+
identities: members,
|
|
2103
2234
|
resources: () => [...registry.values()].map((entry) => entry.resource)
|
|
2104
2235
|
}
|
|
2105
2236
|
})
|