@sanity-labs/client-fake-for-test 0.31.0 → 0.32.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 +151 -141
- package/dist/index.cjs +134 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +134 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -557,6 +557,18 @@ export declare interface GuardDeniedResponse {
|
|
|
557
557
|
statusMessage: "Forbidden";
|
|
558
558
|
}
|
|
559
559
|
|
|
560
|
+
/** What `@sanity/client` hands a `requestHandler`, as far as the fake reads it. */
|
|
561
|
+
declare interface HandledRequestOptions {
|
|
562
|
+
url?: string | undefined;
|
|
563
|
+
method?: string | undefined;
|
|
564
|
+
headers?: [string, string][] | Record<string, string> | Headers | undefined;
|
|
565
|
+
body?: unknown;
|
|
566
|
+
query?:
|
|
567
|
+
| URLSearchParams
|
|
568
|
+
| Record<string, string | number | boolean | undefined>
|
|
569
|
+
| undefined;
|
|
570
|
+
}
|
|
571
|
+
|
|
560
572
|
/** An event as `GET <data base>/history/events/documents/<ids>` serves it. */
|
|
561
573
|
export declare interface HistoryEvent {
|
|
562
574
|
type: "createLiveDocument" | "updateLiveDocument";
|
|
@@ -702,6 +714,14 @@ export declare interface MutationOptions {
|
|
|
702
714
|
tag?: string | null;
|
|
703
715
|
}
|
|
704
716
|
|
|
717
|
+
/**
|
|
718
|
+
* A store id in populus's format, `os` and ten characters
|
|
719
|
+
* (`organizationStoreService.ts:47-51`), without a segment populus disallows
|
|
720
|
+
* (`src/util/createUniqueId.js:39-41`). Derived from the organization so a
|
|
721
|
+
* test can name it before the lookup.
|
|
722
|
+
*/
|
|
723
|
+
export declare function organizationStoreId(organizationId: string): string;
|
|
724
|
+
|
|
705
725
|
/** The statuses populus serves a store in. */
|
|
706
726
|
declare type OrganizationStoreStatus = "provisioning" | "active";
|
|
707
727
|
|
|
@@ -1343,6 +1363,17 @@ export declare interface TestClient {
|
|
|
1343
1363
|
* change the store's status; the routes serve it once it is `active`.
|
|
1344
1364
|
*/
|
|
1345
1365
|
organizationStore: (organizationId: string) => TestStore;
|
|
1366
|
+
/**
|
|
1367
|
+
* A `requestHandler` for a real `@sanity/client`, so code that builds its
|
|
1368
|
+
* own client runs against this fake: `createClient({ ...config,
|
|
1369
|
+
* requestHandler: fake.requestHandler() })`. Each request is answered as
|
|
1370
|
+
* {@link TestClient.request} answers it on a sibling bound to the
|
|
1371
|
+
* request's bearer token, API host and API version, and resolves to the
|
|
1372
|
+
* response body. Requests the fake's `request()` does not serve throw its
|
|
1373
|
+
* fake error, as `request()` does; route data API calls through the fake's
|
|
1374
|
+
* own methods instead.
|
|
1375
|
+
*/
|
|
1376
|
+
requestHandler: () => (options: HandledRequestOptions) => Promise<unknown>;
|
|
1346
1377
|
/**
|
|
1347
1378
|
* Deploy a function, or replace the one with its id, for every handle of
|
|
1348
1379
|
* the family. Throws for an id shorter than 10 characters, an unknown
|
|
@@ -1500,6 +1531,14 @@ export declare interface TestClientIdentity {
|
|
|
1500
1531
|
* `…/robots/<id>` instead of `…/users`, and it is never a user row.
|
|
1501
1532
|
*/
|
|
1502
1533
|
robot?: true;
|
|
1534
|
+
/**
|
|
1535
|
+
* The organization an organization-scoped token is limited to. The
|
|
1536
|
+
* Content Lake refuses it on another organization's store with 403
|
|
1537
|
+
* `permissionDeniedError`, `Unauthorized organization access` (gradient
|
|
1538
|
+
* `pkg/system/session.go:71-94`). Omit for a token with no organization
|
|
1539
|
+
* scope.
|
|
1540
|
+
*/
|
|
1541
|
+
organizationScope?: string;
|
|
1503
1542
|
}
|
|
1504
1543
|
|
|
1505
1544
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -557,6 +557,18 @@ export declare interface GuardDeniedResponse {
|
|
|
557
557
|
statusMessage: "Forbidden";
|
|
558
558
|
}
|
|
559
559
|
|
|
560
|
+
/** What `@sanity/client` hands a `requestHandler`, as far as the fake reads it. */
|
|
561
|
+
declare interface HandledRequestOptions {
|
|
562
|
+
url?: string | undefined;
|
|
563
|
+
method?: string | undefined;
|
|
564
|
+
headers?: [string, string][] | Record<string, string> | Headers | undefined;
|
|
565
|
+
body?: unknown;
|
|
566
|
+
query?:
|
|
567
|
+
| URLSearchParams
|
|
568
|
+
| Record<string, string | number | boolean | undefined>
|
|
569
|
+
| undefined;
|
|
570
|
+
}
|
|
571
|
+
|
|
560
572
|
/** An event as `GET <data base>/history/events/documents/<ids>` serves it. */
|
|
561
573
|
export declare interface HistoryEvent {
|
|
562
574
|
type: "createLiveDocument" | "updateLiveDocument";
|
|
@@ -702,6 +714,14 @@ export declare interface MutationOptions {
|
|
|
702
714
|
tag?: string | null;
|
|
703
715
|
}
|
|
704
716
|
|
|
717
|
+
/**
|
|
718
|
+
* A store id in populus's format, `os` and ten characters
|
|
719
|
+
* (`organizationStoreService.ts:47-51`), without a segment populus disallows
|
|
720
|
+
* (`src/util/createUniqueId.js:39-41`). Derived from the organization so a
|
|
721
|
+
* test can name it before the lookup.
|
|
722
|
+
*/
|
|
723
|
+
export declare function organizationStoreId(organizationId: string): string;
|
|
724
|
+
|
|
705
725
|
/** The statuses populus serves a store in. */
|
|
706
726
|
declare type OrganizationStoreStatus = "provisioning" | "active";
|
|
707
727
|
|
|
@@ -1343,6 +1363,17 @@ export declare interface TestClient {
|
|
|
1343
1363
|
* change the store's status; the routes serve it once it is `active`.
|
|
1344
1364
|
*/
|
|
1345
1365
|
organizationStore: (organizationId: string) => TestStore;
|
|
1366
|
+
/**
|
|
1367
|
+
* A `requestHandler` for a real `@sanity/client`, so code that builds its
|
|
1368
|
+
* own client runs against this fake: `createClient({ ...config,
|
|
1369
|
+
* requestHandler: fake.requestHandler() })`. Each request is answered as
|
|
1370
|
+
* {@link TestClient.request} answers it on a sibling bound to the
|
|
1371
|
+
* request's bearer token, API host and API version, and resolves to the
|
|
1372
|
+
* response body. Requests the fake's `request()` does not serve throw its
|
|
1373
|
+
* fake error, as `request()` does; route data API calls through the fake's
|
|
1374
|
+
* own methods instead.
|
|
1375
|
+
*/
|
|
1376
|
+
requestHandler: () => (options: HandledRequestOptions) => Promise<unknown>;
|
|
1346
1377
|
/**
|
|
1347
1378
|
* Deploy a function, or replace the one with its id, for every handle of
|
|
1348
1379
|
* the family. Throws for an id shorter than 10 characters, an unknown
|
|
@@ -1500,6 +1531,14 @@ export declare interface TestClientIdentity {
|
|
|
1500
1531
|
* `…/robots/<id>` instead of `…/users`, and it is never a user row.
|
|
1501
1532
|
*/
|
|
1502
1533
|
robot?: true;
|
|
1534
|
+
/**
|
|
1535
|
+
* The organization an organization-scoped token is limited to. The
|
|
1536
|
+
* Content Lake refuses it on another organization's store with 403
|
|
1537
|
+
* `permissionDeniedError`, `Unauthorized organization access` (gradient
|
|
1538
|
+
* `pkg/system/session.go:71-94`). Omit for a token with no organization
|
|
1539
|
+
* scope.
|
|
1540
|
+
*/
|
|
1541
|
+
organizationScope?: string;
|
|
1503
1542
|
}
|
|
1504
1543
|
|
|
1505
1544
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1076,6 +1076,21 @@ async function readHistory(args) {
|
|
|
1076
1076
|
}
|
|
1077
1077
|
};
|
|
1078
1078
|
}
|
|
1079
|
+
function permissionRecords(args) {
|
|
1080
|
+
const { ctx, resource } = args, entries = Object.entries(ctx.actorAccess() ?? {}), own = accessKey(resource.type, resource.id);
|
|
1081
|
+
return (resource.type === "organization" && resource.id === ctx.organizationId ? entries : entries.filter(([key]) => key === own)).flatMap(([key, entry]) => {
|
|
1082
|
+
const separator = key.indexOf(":"), resourceType = key.slice(0, separator), resourceId = key.slice(separator + 1);
|
|
1083
|
+
return (entry.permissions ?? []).map((permission) => {
|
|
1084
|
+
const dot = permission.lastIndexOf(".");
|
|
1085
|
+
return {
|
|
1086
|
+
type: permission.slice(0, dot),
|
|
1087
|
+
resourceType,
|
|
1088
|
+
resourceId,
|
|
1089
|
+
action: permission.slice(dot + 1)
|
|
1090
|
+
};
|
|
1091
|
+
});
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1079
1094
|
function serveGated(args) {
|
|
1080
1095
|
const { capability, permission, held, request, serve } = args;
|
|
1081
1096
|
return serveCapability(capability, request, () => {
|
|
@@ -1155,19 +1170,7 @@ function serveAccess(args) {
|
|
|
1155
1170
|
value: usersPage(usersOn({ resource, registry, identities: ctx.identities() }))
|
|
1156
1171
|
}));
|
|
1157
1172
|
case "/user-permissions/me":
|
|
1158
|
-
return {
|
|
1159
|
-
value: page(
|
|
1160
|
-
permissions.map((permission) => {
|
|
1161
|
-
const dot = permission.lastIndexOf(".");
|
|
1162
|
-
return {
|
|
1163
|
-
type: permission.slice(0, dot),
|
|
1164
|
-
resourceType: resource.type,
|
|
1165
|
-
resourceId: id,
|
|
1166
|
-
action: permission.slice(dot + 1)
|
|
1167
|
-
};
|
|
1168
|
-
})
|
|
1169
|
-
)
|
|
1170
|
-
};
|
|
1173
|
+
return { value: page(permissionRecords({ ctx, resource })) };
|
|
1171
1174
|
default:
|
|
1172
1175
|
return;
|
|
1173
1176
|
}
|
|
@@ -2290,6 +2293,25 @@ function encodeGroqQuery(query, params = {}) {
|
|
|
2290
2293
|
value !== void 0 && search.append(`$${key}`, JSON.stringify(value));
|
|
2291
2294
|
return `?${search}`;
|
|
2292
2295
|
}
|
|
2296
|
+
function handledRequest(options) {
|
|
2297
|
+
const full = new URL(options.url ?? ""), [, version, ...path] = full.pathname.split("/");
|
|
2298
|
+
if (version === void 0 || !version.startsWith("v"))
|
|
2299
|
+
throw fakeError(`requestHandler(): "${options.url}" has no /v<apiVersion> segment.`);
|
|
2300
|
+
const entries = options.query instanceof URLSearchParams ? [...options.query] : Object.entries(options.query ?? {}), search = new URLSearchParams(full.search);
|
|
2301
|
+
let tag;
|
|
2302
|
+
for (const [key, value] of entries)
|
|
2303
|
+
key === "tag" && typeof value == "string" ? tag = value : value !== void 0 && search.append(key, String(value));
|
|
2304
|
+
const query = search.toString(), bearer = new Headers(options.headers).get("authorization") ?? void 0;
|
|
2305
|
+
return {
|
|
2306
|
+
apiHost: `${full.protocol}//${full.host}`,
|
|
2307
|
+
apiVersion: version.slice(1),
|
|
2308
|
+
url: `/${path.join("/")}${query === "" ? "" : `?${query}`}`,
|
|
2309
|
+
method: options.method?.toUpperCase() === "POST" ? "POST" : "GET",
|
|
2310
|
+
token: bearer?.replace(/^Bearer /, ""),
|
|
2311
|
+
body: options.body,
|
|
2312
|
+
tag
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2293
2315
|
const SIGNING_METHODS = /* @__PURE__ */ new Set([
|
|
2294
2316
|
...["HS", "RS", "PS", "ES"].flatMap((family) => ["256", "384", "512"].map((n) => family + n)),
|
|
2295
2317
|
"EdDSA",
|
|
@@ -2780,6 +2802,76 @@ function storeNotFound(request, identifier) {
|
|
|
2780
2802
|
}
|
|
2781
2803
|
});
|
|
2782
2804
|
}
|
|
2805
|
+
function unauthorizedOrganization(request) {
|
|
2806
|
+
return new PlatformHttpError({
|
|
2807
|
+
...request,
|
|
2808
|
+
statusCode: 403,
|
|
2809
|
+
statusMessage: "Forbidden",
|
|
2810
|
+
body: {
|
|
2811
|
+
error: { type: "permissionDeniedError", description: "Unauthorized organization access" }
|
|
2812
|
+
}
|
|
2813
|
+
});
|
|
2814
|
+
}
|
|
2815
|
+
function failingMutation(args) {
|
|
2816
|
+
const held = new Set(args.revisions.keys()), written = /* @__PURE__ */ new Set();
|
|
2817
|
+
for (const mutation of args.mutations) {
|
|
2818
|
+
const refusal2 = refusalOf({ ...args, held, written, mutation });
|
|
2819
|
+
if (refusal2 !== void 0) return refusal2;
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
function refusalOf(args) {
|
|
2823
|
+
const { request, revisions, held, written, mutation } = args, created = mutation.create ?? mutation.createOrReplace ?? mutation.createIfNotExists;
|
|
2824
|
+
if (created !== void 0) {
|
|
2825
|
+
const id2 = String(created._id);
|
|
2826
|
+
if (mutation.create !== void 0 && held.has(id2))
|
|
2827
|
+
return mutationError(request, 409, {
|
|
2828
|
+
type: "documentAlreadyExistsError",
|
|
2829
|
+
description: `Document by ID ${goQuoted(id2)} already exists`,
|
|
2830
|
+
id: id2
|
|
2831
|
+
});
|
|
2832
|
+
held.add(id2), written.add(id2);
|
|
2833
|
+
return;
|
|
2834
|
+
}
|
|
2835
|
+
if (mutation.delete !== void 0) {
|
|
2836
|
+
held.delete(String(mutation.delete.id));
|
|
2837
|
+
return;
|
|
2838
|
+
}
|
|
2839
|
+
if (mutation.patch === void 0) return;
|
|
2840
|
+
const id = String(mutation.patch.id);
|
|
2841
|
+
if (!held.has(id))
|
|
2842
|
+
return mutationError(request, 404, {
|
|
2843
|
+
type: "documentNotFoundError",
|
|
2844
|
+
description: `The document with the ID ${goQuoted(id)} was not found`,
|
|
2845
|
+
id
|
|
2846
|
+
});
|
|
2847
|
+
const expected = mutation.patch.ifRevisionID;
|
|
2848
|
+
if (typeof expected == "string" && expected !== "") {
|
|
2849
|
+
if (written.has(id))
|
|
2850
|
+
throw fakeError(
|
|
2851
|
+
`request(): a mutate body patches "${id}" with ifRevisionID after writing it; the revision gradient gives a document mid-transaction is not modelled.`
|
|
2852
|
+
);
|
|
2853
|
+
const current = revisions.get(id) ?? "";
|
|
2854
|
+
if (expected !== current)
|
|
2855
|
+
return mutationError(request, 409, {
|
|
2856
|
+
type: "documentRevisionIDDoesNotMatchError",
|
|
2857
|
+
description: `Document ${goQuoted(id)} has unexpected revision ID (${goQuoted(current)}), expected ${goQuoted(expected)}`,
|
|
2858
|
+
expectedRevisionID: expected,
|
|
2859
|
+
currentRevisionID: current
|
|
2860
|
+
});
|
|
2861
|
+
}
|
|
2862
|
+
written.add(id);
|
|
2863
|
+
}
|
|
2864
|
+
function mutationError(request, statusCode, error) {
|
|
2865
|
+
return new PlatformHttpError({
|
|
2866
|
+
...request,
|
|
2867
|
+
statusCode,
|
|
2868
|
+
statusMessage: statusCode === 404 ? "Not Found" : "Conflict",
|
|
2869
|
+
body: { error }
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
function goQuoted(text) {
|
|
2873
|
+
return JSON.stringify(text);
|
|
2874
|
+
}
|
|
2783
2875
|
const OPTIONS_ARG_POSITION = {
|
|
2784
2876
|
fetch: 2,
|
|
2785
2877
|
getDocument: 1,
|
|
@@ -2888,6 +2980,14 @@ async function serveStoreAction(args) {
|
|
|
2888
2980
|
const body = opts.body;
|
|
2889
2981
|
if (!Array.isArray(body?.mutations))
|
|
2890
2982
|
throw fakeError(`request(): POST "${sent.path}" needs a body \`{ mutations: [...] }\`.`);
|
|
2983
|
+
const refusal2 = failingMutation({
|
|
2984
|
+
request: sent,
|
|
2985
|
+
revisions: new Map(
|
|
2986
|
+
client.store().all().map((doc) => [doc._id, doc._rev])
|
|
2987
|
+
),
|
|
2988
|
+
mutations: body.mutations
|
|
2989
|
+
});
|
|
2990
|
+
if (refusal2 !== void 0) throw refusal2;
|
|
2891
2991
|
const committed = await client.mutate(body.mutations, {
|
|
2892
2992
|
returnDocuments: !1,
|
|
2893
2993
|
returnFirst: !1,
|
|
@@ -3170,6 +3270,8 @@ function makeHandle(handleArgs) {
|
|
|
3170
3270
|
throw fakeError(
|
|
3171
3271
|
`request(): "${sent.path}" is ${routed.action} on the organization store; the fake serves ${servedActions.join(", ")} there.`
|
|
3172
3272
|
);
|
|
3273
|
+
const scope = token === void 0 ? void 0 : backend.identities.get(token)?.organizationScope;
|
|
3274
|
+
if (scope !== void 0 && scope !== store2.organizationId) throw unauthorizedOrganization(sent);
|
|
3173
3275
|
const onStore = makeHandle({
|
|
3174
3276
|
...handleArgs,
|
|
3175
3277
|
resource: void 0,
|
|
@@ -3813,6 +3915,24 @@ function makeHandle(handleArgs) {
|
|
|
3813
3915
|
);
|
|
3814
3916
|
return ensureEntry(registry, { type: "organization-store", id: organizationStore.id }).store;
|
|
3815
3917
|
},
|
|
3918
|
+
requestHandler: () => (options) => {
|
|
3919
|
+
const sent = handledRequest(options), sibling = client.withConfig({
|
|
3920
|
+
...sent.token !== void 0 ? { token: sent.token } : {},
|
|
3921
|
+
apiHost: sent.apiHost,
|
|
3922
|
+
apiVersion: sent.apiVersion,
|
|
3923
|
+
useProjectHostname: !1
|
|
3924
|
+
});
|
|
3925
|
+
if (sibling.request === void 0)
|
|
3926
|
+
throw fakeError(
|
|
3927
|
+
"requestHandler(): the request carries no token the fake knows, so it has no `request()` to answer it."
|
|
3928
|
+
);
|
|
3929
|
+
return sibling.request({
|
|
3930
|
+
url: sent.url,
|
|
3931
|
+
method: sent.method,
|
|
3932
|
+
...sent.body !== void 0 ? { body: sent.body } : {},
|
|
3933
|
+
...sent.tag !== void 0 ? { tag: sent.tag } : {}
|
|
3934
|
+
});
|
|
3935
|
+
},
|
|
3816
3936
|
addFunction: (fn) => registerFunction(backend.functions, { fn, site: "addFunction()" }),
|
|
3817
3937
|
removeFunction: (id) => unregisterFunction(backend.functions, id),
|
|
3818
3938
|
functionInvocations: () => structuredClone(backend.functions.invocations),
|
|
@@ -3867,6 +3987,7 @@ export {
|
|
|
3867
3987
|
isReleaseDocId,
|
|
3868
3988
|
isSystemId,
|
|
3869
3989
|
isVersionId,
|
|
3990
|
+
organizationStoreId,
|
|
3870
3991
|
publishedIdOf,
|
|
3871
3992
|
releaseNameOf,
|
|
3872
3993
|
wrapWithGuards
|