@sanity-labs/client-fake-for-test 0.17.2 → 0.17.3

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
@@ -1056,7 +1056,11 @@ export declare interface TestClient {
1056
1056
  * handle is bound to a registered identity.
1057
1057
  */
1058
1058
  config: () => {
1059
- projectId: string;
1059
+ /**
1060
+ * Absent on a handle created global-hosted (`useProjectHostname: false`)
1061
+ * without a `projectId`, as in `@sanity/client`.
1062
+ */
1063
+ projectId?: string;
1060
1064
  dataset: string;
1061
1065
  /** Present iff the handle is `resource`-configured. */
1062
1066
  resource?: TestClientResource;
@@ -1170,6 +1174,15 @@ export declare interface TestClient {
1170
1174
  addIdentity: (token: string, identity: TestClientIdentity) => void;
1171
1175
  /** Snapshot of the registered token→identity entries. */
1172
1176
  identities: () => Record<string, TestClientIdentity>;
1177
+ /**
1178
+ * Revoke a registered token, as when it expires or is deleted: it stays
1179
+ * registered and still binds, but from the next request on every Content
1180
+ * Lake and platform request made with it (on any handle) rejects with the
1181
+ * platform's 401 `Session not found`, and nothing is written. Revocation
1182
+ * is permanent for the family; `addIdentity` does not restore it. Unknown
1183
+ * tokens throw.
1184
+ */
1185
+ revokeIdentity: (token: string) => void;
1173
1186
  /**
1174
1187
  * Every mutation committed through this client's registry, flattened
1175
1188
  * and in order — `create` / `createOrReplace` / `createIfNotExists` /
package/dist/index.d.ts CHANGED
@@ -1056,7 +1056,11 @@ export declare interface TestClient {
1056
1056
  * handle is bound to a registered identity.
1057
1057
  */
1058
1058
  config: () => {
1059
- projectId: string;
1059
+ /**
1060
+ * Absent on a handle created global-hosted (`useProjectHostname: false`)
1061
+ * without a `projectId`, as in `@sanity/client`.
1062
+ */
1063
+ projectId?: string;
1060
1064
  dataset: string;
1061
1065
  /** Present iff the handle is `resource`-configured. */
1062
1066
  resource?: TestClientResource;
@@ -1170,6 +1174,15 @@ export declare interface TestClient {
1170
1174
  addIdentity: (token: string, identity: TestClientIdentity) => void;
1171
1175
  /** Snapshot of the registered token→identity entries. */
1172
1176
  identities: () => Record<string, TestClientIdentity>;
1177
+ /**
1178
+ * Revoke a registered token, as when it expires or is deleted: it stays
1179
+ * registered and still binds, but from the next request on every Content
1180
+ * Lake and platform request made with it (on any handle) rejects with the
1181
+ * platform's 401 `Session not found`, and nothing is written. Revocation
1182
+ * is permanent for the family; `addIdentity` does not restore it. Unknown
1183
+ * tokens throw.
1184
+ */
1185
+ revokeIdentity: (token: string) => void;
1173
1186
  /**
1174
1187
  * Every mutation committed through this client's registry, flattened
1175
1188
  * and in order — `create` / `createOrReplace` / `createIfNotExists` /
package/dist/index.js CHANGED
@@ -22,8 +22,12 @@ function clientErrorMessage(args) {
22
22
  const { body } = args;
23
23
  if (isRecord(body)) {
24
24
  const { error, message } = body;
25
- if (isRecord(error) && typeof error.description == "string") return error.description;
26
- if (typeof message == "string") return message;
25
+ if (typeof error == "string" && typeof message == "string") return `${error} - ${message}`;
26
+ if (typeof error == "string") return error;
27
+ if (isRecord(error)) {
28
+ if (typeof error.description == "string") return error.description;
29
+ } else if (typeof message == "string")
30
+ return message;
27
31
  }
28
32
  const details = typeof body == "string" ? ` (${body.length > 100 ? `${body.slice(0, 100)}\u2026` : body})` : "";
29
33
  return `${args.method}-request to ${args.path} resulted in HTTP ${args.statusCode} ${args.statusMessage}${details}`;
@@ -44,7 +48,17 @@ function accessApiError(args) {
44
48
  body: { statusCode, error: statusMessage, message }
45
49
  });
46
50
  }
47
- const noPermission = (request) => accessApiError({
51
+ const sessionNotFound = (request) => new PlatformHttpError({
52
+ ...request,
53
+ statusCode: 401,
54
+ statusMessage: "Unauthorized",
55
+ body: {
56
+ error: "Unauthorized",
57
+ errorCode: "SIO-401-ANF",
58
+ message: "Session not found",
59
+ statusCode: 401
60
+ }
61
+ }), noPermission = (request) => accessApiError({
48
62
  request,
49
63
  statusCode: 403,
50
64
  statusMessage: "Forbidden",
@@ -1834,6 +1848,7 @@ function createTestClient(options = {}) {
1834
1848
  calls: [],
1835
1849
  concurrentEdits: /* @__PURE__ */ new Map(),
1836
1850
  identities: new Map(Object.entries(options.identities ?? {})),
1851
+ revoked: /* @__PURE__ */ new Set(),
1837
1852
  writeChain: Promise.resolve()
1838
1853
  }, registry = backend.registry;
1839
1854
  if (rejectAmbiguousIdentity("createTestClient()", options), options.token !== void 0 && requireIdentity(backend, options.token, "createTestClient()"), options.resource !== void 0 && validateResource(options.resource), options.documents) {
@@ -1858,7 +1873,8 @@ function createTestClient(options = {}) {
1858
1873
  requestTagPrefix: options.requestTagPrefix,
1859
1874
  mutationGuard: options.mutationGuard,
1860
1875
  token: options.token,
1861
- useProjectHostname: options.useProjectHostname ?? !0
1876
+ useProjectHostname: options.useProjectHostname ?? !0,
1877
+ hasProjectId: options.projectId !== void 0
1862
1878
  });
1863
1879
  }
1864
1880
  function dedupeIds(results) {
@@ -1921,7 +1937,7 @@ function makeHandle(handleArgs) {
1921
1937
  mutationGuard,
1922
1938
  token,
1923
1939
  useProjectHostname
1924
- } = handleArgs, { registry } = backend, { projectId, dataset } = resource?.type === "dataset" ? datasetParts(resource.id) : handleArgs, target = resource ?? datasetResource(projectId, dataset), activeAccess = () => {
1940
+ } = handleArgs, { registry } = backend, hasProjectId = handleArgs.hasProjectId || useProjectHostname && resource === void 0 || resource?.type === "dataset", { projectId, dataset } = resource?.type === "dataset" ? datasetParts(resource.id) : handleArgs, target = resource ?? datasetResource(projectId, dataset), activeAccess = () => {
1925
1941
  if (token === void 0) return accessControl;
1926
1942
  const identity = backend.identities.get(token);
1927
1943
  if (identity === void 0)
@@ -1938,7 +1954,10 @@ function makeHandle(handleArgs) {
1938
1954
  if (access === void 0)
1939
1955
  throw fakeError("this handle has no acting identity (no token or accessControl bound).");
1940
1956
  return access;
1941
- }, admitToLake = (request) => {
1957
+ }, refuseRevoked = (request) => {
1958
+ if (token !== void 0 && backend.revoked.has(token)) throw sessionNotFound(request);
1959
+ }, withSession = (request) => async (opts) => (refuseRevoked({ method: "GET", path: opts.uri ?? opts.url ?? "" }), request(opts)), admitToLake = (request) => {
1960
+ refuseRevoked(request);
1942
1961
  const access = activeAccess();
1943
1962
  if (access === void 0) return;
1944
1963
  const refusal = refusedToken(request, access.grants);
@@ -2124,7 +2143,7 @@ function makeHandle(handleArgs) {
2124
2143
  method: "POST",
2125
2144
  path: `${capabilities.dataBase(target.id)}/actions`
2126
2145
  };
2127
- if (capabilities.actions !== "served") throw capabilities.actions(request);
2146
+ if (refuseRevoked(request), capabilities.actions !== "served") throw capabilities.actions(request);
2128
2147
  return write(async () => {
2129
2148
  for (const action of Array.isArray(actions) ? actions : [actions]) {
2130
2149
  const refused = capabilities.refusedActions[action.actionType];
@@ -2328,36 +2347,38 @@ function makeHandle(handleArgs) {
2328
2347
  publishedId
2329
2348
  }),
2330
2349
  ...token !== void 0 || accessControl !== void 0 ? {
2331
- request: makeRequestImpl({
2332
- getAccess: requireActiveAccess,
2333
- // `@sanity/client` sends every request of a `resource`-configured
2334
- // client to the global API host.
2335
- useProjectHostname: useProjectHostname && resource === void 0,
2336
- organizationId: backend.organizationId,
2337
- listIdentities: members,
2338
- platform: {
2339
- projectId,
2350
+ request: withSession(
2351
+ makeRequestImpl({
2352
+ getAccess: requireActiveAccess,
2353
+ // `@sanity/client` sends every request of a `resource`-configured
2354
+ // client to the global API host.
2355
+ useProjectHostname: useProjectHostname && resource === void 0,
2340
2356
  organizationId: backend.organizationId,
2341
- projectFeatures: backend.projectFeatures,
2342
- own: (asked) => registryKey(asked) === registryKey(target) ? {
2343
- grants: requireActiveAccess().grants,
2344
- documents: () => store.all(),
2345
- history,
2346
- canRead,
2347
- admitToLake
2348
- } : void 0,
2349
- actorUser: () => {
2350
- const access = requireActiveAccess();
2351
- return access.globalUser ?? access.currentUser;
2352
- },
2353
- actorAccess: () => requireActiveAccess().access,
2354
- identities: members,
2355
- resources: () => [...registry.values()].map((entry) => entry.resource)
2356
- }
2357
- })
2357
+ listIdentities: members,
2358
+ platform: {
2359
+ projectId,
2360
+ organizationId: backend.organizationId,
2361
+ projectFeatures: backend.projectFeatures,
2362
+ own: (asked) => registryKey(asked) === registryKey(target) ? {
2363
+ grants: requireActiveAccess().grants,
2364
+ documents: () => store.all(),
2365
+ history,
2366
+ canRead,
2367
+ admitToLake
2368
+ } : void 0,
2369
+ actorUser: () => {
2370
+ const access = requireActiveAccess();
2371
+ return access.globalUser ?? access.currentUser;
2372
+ },
2373
+ actorAccess: () => requireActiveAccess().access,
2374
+ identities: members,
2375
+ resources: () => [...registry.values()].map((entry) => entry.resource)
2376
+ }
2377
+ })
2378
+ )
2358
2379
  } : {},
2359
2380
  config: () => ({
2360
- projectId,
2381
+ ...hasProjectId || resource !== void 0 ? { projectId } : {},
2361
2382
  dataset,
2362
2383
  ...resource !== void 0 ? { resource } : {},
2363
2384
  perspective,
@@ -2378,7 +2399,8 @@ function makeHandle(handleArgs) {
2378
2399
  requestTagPrefix: config.requestTagPrefix ?? requestTagPrefix,
2379
2400
  mutationGuard,
2380
2401
  token: rebinds ? config.token : token,
2381
- useProjectHostname: config.useProjectHostname ?? useProjectHostname
2402
+ useProjectHostname: config.useProjectHostname ?? useProjectHostname,
2403
+ hasProjectId: hasProjectId || config.projectId !== void 0
2382
2404
  });
2383
2405
  },
2384
2406
  seed: (documents) => store.seed(documents),
@@ -2400,6 +2422,9 @@ function makeHandle(handleArgs) {
2400
2422
  backend.identities.set(identityToken, identity);
2401
2423
  },
2402
2424
  identities: () => Object.fromEntries(backend.identities),
2425
+ revokeIdentity: (revokedToken) => {
2426
+ requireIdentity(backend, revokedToken, "revokeIdentity()"), backend.revoked.add(revokedToken);
2427
+ },
2403
2428
  mutations: () => backend.log.flatMap((entry) => entry.mutations),
2404
2429
  transactions: () => [...backend.log],
2405
2430
  clearLog: () => {