@sanity-labs/client-fake-for-test 0.22.0 → 0.23.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.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ClientError } from "@sanity/client";
2
+ import { ServerError } from "@sanity/client";
2
3
 
3
4
  export declare type Action = ReleaseAction | VersionAction | DocumentAction;
4
5
 
@@ -738,6 +739,9 @@ export declare type Perspective =
738
739
  | (string & {})
739
740
  | string[];
740
741
 
742
+ /** A platform component {@link TestClient.simulateOutage} can take down: `"project-api"` is populus. */
743
+ export declare type PlatformComponent = "project-api";
744
+
741
745
  /**
742
746
  * The platform's refusal, as the `ClientError` `@sanity/client` throws for
743
747
  * the same response: the fake hands `ClientError` the response the client
@@ -772,6 +776,15 @@ declare interface PlatformResponseArgs {
772
776
  body: unknown;
773
777
  }
774
778
 
779
+ /**
780
+ * The platform's 5xx answer, as the `ServerError` `@sanity/client` throws
781
+ * for a status of 500 or more (`src/http/request.ts`); see
782
+ * {@link PlatformHttpError} for how the fields are derived.
783
+ */
784
+ export declare class PlatformServerError extends ServerError {
785
+ constructor(args: PlatformResponseArgs);
786
+ }
787
+
775
788
  /** One row of the `/projects/<projectId>` members response. */
776
789
  export declare interface ProjectMemberRecord {
777
790
  id: string;
@@ -1245,6 +1258,16 @@ export declare interface TestClient {
1245
1258
  * tokens throw.
1246
1259
  */
1247
1260
  revokeIdentity: (token: string) => void;
1261
+ /**
1262
+ * Take a platform component down, as in an outage: from the next request
1263
+ * on, every request it serves, on every handle of the family (siblings
1264
+ * made before and after the call), throws its 5xx answer. `"project-api"`
1265
+ * is populus: `request()` of `/users/me`, the project, organization and
1266
+ * Access APIs, resource lookup and ACLs throws {@link PlatformServerError}
1267
+ * 500. The Content Lake (queries, documents, mutations, actions, history)
1268
+ * keeps answering. The outage lasts for the rest of the family's life.
1269
+ */
1270
+ simulateOutage: (component: PlatformComponent) => void;
1248
1271
  /**
1249
1272
  * Every mutation committed through this client's registry, flattened
1250
1273
  * and in order — `create` / `createOrReplace` / `createIfNotExists` /
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ClientError } from "@sanity/client";
2
+ import { ServerError } from "@sanity/client";
2
3
 
3
4
  export declare type Action = ReleaseAction | VersionAction | DocumentAction;
4
5
 
@@ -738,6 +739,9 @@ export declare type Perspective =
738
739
  | (string & {})
739
740
  | string[];
740
741
 
742
+ /** A platform component {@link TestClient.simulateOutage} can take down: `"project-api"` is populus. */
743
+ export declare type PlatformComponent = "project-api";
744
+
741
745
  /**
742
746
  * The platform's refusal, as the `ClientError` `@sanity/client` throws for
743
747
  * the same response: the fake hands `ClientError` the response the client
@@ -772,6 +776,15 @@ declare interface PlatformResponseArgs {
772
776
  body: unknown;
773
777
  }
774
778
 
779
+ /**
780
+ * The platform's 5xx answer, as the `ServerError` `@sanity/client` throws
781
+ * for a status of 500 or more (`src/http/request.ts`); see
782
+ * {@link PlatformHttpError} for how the fields are derived.
783
+ */
784
+ export declare class PlatformServerError extends ServerError {
785
+ constructor(args: PlatformResponseArgs);
786
+ }
787
+
775
788
  /** One row of the `/projects/<projectId>` members response. */
776
789
  export declare interface ProjectMemberRecord {
777
790
  id: string;
@@ -1245,6 +1258,16 @@ export declare interface TestClient {
1245
1258
  * tokens throw.
1246
1259
  */
1247
1260
  revokeIdentity: (token: string) => void;
1261
+ /**
1262
+ * Take a platform component down, as in an outage: from the next request
1263
+ * on, every request it serves, on every handle of the family (siblings
1264
+ * made before and after the call), throws its 5xx answer. `"project-api"`
1265
+ * is populus: `request()` of `/users/me`, the project, organization and
1266
+ * Access APIs, resource lookup and ACLs throws {@link PlatformServerError}
1267
+ * 500. The Content Lake (queries, documents, mutations, actions, history)
1268
+ * keeps answering. The outage lasts for the rest of the family's life.
1269
+ */
1270
+ simulateOutage: (component: PlatformComponent) => void;
1248
1271
  /**
1249
1272
  * Every mutation committed through this client's registry, flattened
1250
1273
  * and in order — `create` / `createOrReplace` / `createIfNotExists` /
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { parse, evaluate } from "groq-js";
2
- import { ClientError } from "@sanity/client";
2
+ import { ClientError, ServerError } from "@sanity/client";
3
3
  const ERR_PREFIX = "[client-fake-for-test]";
4
4
  function fakeError(message) {
5
5
  return new Error(`${ERR_PREFIX} ${message}`);
@@ -9,6 +9,11 @@ class PlatformHttpError extends ClientError {
9
9
  super(platformResponse(args)), this.name = "PlatformHttpError";
10
10
  }
11
11
  }
12
+ class PlatformServerError extends ServerError {
13
+ constructor(args) {
14
+ super(platformResponse(args)), this.name = "PlatformServerError";
15
+ }
16
+ }
12
17
  function platformResponse(args) {
13
18
  const { method, url, statusCode, statusMessage, body } = args;
14
19
  return {
@@ -79,7 +84,20 @@ const sessionNotFound = (request) => new PlatformHttpError({
79
84
  statusCode: 401,
80
85
  statusMessage: "Unauthorized",
81
86
  message: "User must be a member of this project to perform this operation"
82
- }), mediaLibraryReadRequired = (request) => accessApiError({
87
+ });
88
+ function projectApiDown(request) {
89
+ return new PlatformServerError({
90
+ ...request,
91
+ statusCode: 500,
92
+ statusMessage: "Internal Server Error",
93
+ body: {
94
+ statusCode: 500,
95
+ error: "Internal Server Error",
96
+ message: "An internal server error occurred"
97
+ }
98
+ });
99
+ }
100
+ const mediaLibraryReadRequired = (request) => accessApiError({
83
101
  request,
84
102
  statusCode: 401,
85
103
  statusMessage: "Unauthorized",
@@ -461,7 +479,22 @@ function createDocumentHistory() {
461
479
  function historyIds(list) {
462
480
  return list.split(",").map((id) => decodeURIComponent(id));
463
481
  }
464
- 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\/([^/]+)(\/.*)$/;
482
+ 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\/([^/]+)(\/.*)$/, PROJECT_API_PATHS = [
483
+ /^\/users\/me$/,
484
+ /^\/projects\/[^/]+$/,
485
+ /^\/projects\/[^/]+\/users\/[^/]+$/,
486
+ PROJECT_ROLES_PATH,
487
+ PROJECT_FEATURES_PATH,
488
+ MY_ATTRIBUTES_PATH,
489
+ ACCESS_PATH
490
+ ];
491
+ function servedByProjectApi(path) {
492
+ if (PROJECT_API_PATHS.some((pattern) => pattern.test(path))) return !0;
493
+ const matched = matchResourcePath(path);
494
+ if (matched === void 0) return !1;
495
+ const row = capabilitiesOf(matched.resource.type);
496
+ return matched.rest === "" ? row.lookupAtBase : matched.rest === "/acl" && row.acl === "served";
497
+ }
465
498
  function page(data) {
466
499
  return { data, nextCursor: null };
467
500
  }
@@ -1986,6 +2019,7 @@ function createTestClient(options = {}) {
1986
2019
  concurrentEdits: /* @__PURE__ */ new Map(),
1987
2020
  identities: new Map(Object.entries(options.identities ?? {})),
1988
2021
  revoked: /* @__PURE__ */ new Set(),
2022
+ outages: /* @__PURE__ */ new Set(),
1989
2023
  writeChain: Promise.resolve()
1990
2024
  }, registry = backend.registry;
1991
2025
  if (rejectAmbiguousIdentity("createTestClient()", options), options.token !== void 0 && requireIdentity(backend, options.token, "createTestClient()"), options.resource !== void 0 && validateResource(options.resource), options.documents) {
@@ -2105,7 +2139,12 @@ function makeHandle(handleArgs) {
2105
2139
  const tokenProjectId = backend.identities.get(token)?.projectId;
2106
2140
  if (projectHosted && tokenProjectId !== void 0 && tokenProjectId !== projectId)
2107
2141
  throw sessionHostMismatch(request);
2108
- }, withSession = (request) => async (opts) => (refuseSession(rawRequest(opts)), request(opts)), admitToLake = (request) => {
2142
+ }, withSession = (request) => async (opts) => {
2143
+ const sent = rawRequest(opts);
2144
+ if (refuseSession(sent), backend.outages.has("project-api") && servedByProjectApi(sent.path))
2145
+ throw projectApiDown(sent);
2146
+ return request(opts);
2147
+ }, admitToLake = (request) => {
2109
2148
  refuseSession(request);
2110
2149
  const access = activeAccess();
2111
2150
  if (access === void 0) return;
@@ -2671,6 +2710,9 @@ function makeHandle(handleArgs) {
2671
2710
  revokeIdentity: (revokedToken) => {
2672
2711
  requireIdentity(backend, revokedToken, "revokeIdentity()"), backend.revoked.add(revokedToken);
2673
2712
  },
2713
+ simulateOutage: (component) => {
2714
+ backend.outages.add(component);
2715
+ },
2674
2716
  mutations: () => backend.log.flatMap((entry) => entry.mutations),
2675
2717
  transactions: () => [...backend.log],
2676
2718
  clearLog: () => {
@@ -2705,6 +2747,7 @@ export {
2705
2747
  ERR_PREFIX,
2706
2748
  GuardDeniedError,
2707
2749
  PlatformHttpError,
2750
+ PlatformServerError,
2708
2751
  SUPPORTED_SURFACE,
2709
2752
  UNSUPPORTED_MEMBERS,
2710
2753
  WriteAccessDeniedError,