@sanity-labs/client-fake-for-test 0.27.0 → 0.28.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 CHANGED
@@ -222,6 +222,24 @@ A JWT that does not verify still binds, as production clients accept any string;
222
222
 
223
223
  That is the body populus sends for an unexpected error: its error handler sends Boom's payload, and Boom hides the error's own message behind a fixed one. The Content Lake keeps answering (queries, documents, mutations, actions, the History API), as does a route a resource does not mount (Express's 404). A token without a session is still refused with its 401 first, as the gateway checks the session before it routes. The call is recorded in `client.calls()` with its error. `"project-api"` is the only component so far, and an outage lasts for the rest of the family's life; create a new client for a healthy platform.
224
224
 
225
+ #### Missing datasets
226
+
227
+ `createTestClient({ missingDatasets: ["p1:staged"] })` names datasets the platform does not have, as `"<projectId>:<dataset>"` keys. Every Content Lake request on one (queries, documents, mutations, actions, the History API) throws the `PlatformHttpError` the Content Lake answers for a missing dataset, on every handle of the family:
228
+
229
+ ```ts
230
+ {
231
+ statusCode: 404,
232
+ message: 'Dataset not found - Dataset "staged" not found for project ID "p1"',
233
+ response: {
234
+ statusCode: 404,
235
+ body: { error: "Dataset not found", message: 'Dataset "staged" not found for project ID "p1"', statusCode: 404 },
236
+ // url, method, headers, statusMessage
237
+ },
238
+ }
239
+ ```
240
+
241
+ A token without a session is refused with its 401 first, as the gateway checks the session before it routes. The order is the session first, then the missing dataset: a listed dataset answers its 404 before an internal token's or a malformed grant's refusal. A dataset that is not listed answers as before: one the fake has never seen is an empty dataset. A key may not also be seeded, at construction or later through `client.seed()` / `client.add()` / `client.store()`'s `seed` / `add` / `put`.
242
+
225
243
  #### Project-scoped tokens: membership and refusals
226
244
 
227
245
  A robot token belongs to the project that issued it. Model one with `projectId` on the identity:
package/dist/index.cjs CHANGED
@@ -98,6 +98,18 @@ function projectApiDown(request) {
98
98
  }
99
99
  });
100
100
  }
101
+ function datasetNotFound(request, projectId, dataset) {
102
+ return new PlatformHttpError({
103
+ ...request,
104
+ statusCode: 404,
105
+ statusMessage: "Not Found",
106
+ body: {
107
+ error: "Dataset not found",
108
+ message: `Dataset "${dataset}" not found for project ID "${projectId}"`,
109
+ statusCode: 404
110
+ }
111
+ });
112
+ }
101
113
  const mediaLibraryReadRequired = (request) => accessApiError({
102
114
  request,
103
115
  statusCode: 401,
@@ -2251,6 +2263,11 @@ function ensureEntry(registry, resource) {
2251
2263
  exists: !1
2252
2264
  }, registry.set(key, entry)), entry;
2253
2265
  }
2266
+ function seededMissingDataset(caller, key) {
2267
+ return fakeError(
2268
+ `${caller}: dataset "${key}" is both seeded and in \`missingDatasets\`; a dataset the platform does not have holds no documents.`
2269
+ );
2270
+ }
2254
2271
  function seedEntry(registry, resource, documents) {
2255
2272
  const entry = ensureEntry(registry, resource);
2256
2273
  entry.store.seed(documents), entry.exists = !0;
@@ -2269,6 +2286,7 @@ function createTestClient(options = {}) {
2269
2286
  internalTokenKeys: parseInternalTokenKeys(options.internalTokenKeys ?? []),
2270
2287
  internalTokens: /* @__PURE__ */ new Map(),
2271
2288
  outages: /* @__PURE__ */ new Set(),
2289
+ missingDatasets: new Set(options.missingDatasets ?? []),
2272
2290
  writeChain: Promise.resolve()
2273
2291
  }, registry = backend.registry;
2274
2292
  if (rejectAmbiguousIdentity("createTestClient()", options), options.token !== void 0 && requireIdentity(backend, options.token, "createTestClient()"), options.resource !== void 0 && validateResource(options.resource), options.documents) {
@@ -2283,6 +2301,11 @@ function createTestClient(options = {}) {
2283
2301
  if (options.resources)
2284
2302
  for (const [key, docs] of Object.entries(options.resources))
2285
2303
  seedEntry(registry, parseResourceKey(key), docs);
2304
+ for (const key of backend.missingDatasets) {
2305
+ const { projectId, dataset } = parseDatasetKey(key);
2306
+ if (registry.get(registryKey(datasetResource(projectId, dataset)))?.exists)
2307
+ throw seededMissingDataset("createTestClient()", key);
2308
+ }
2286
2309
  return makeHandle({
2287
2310
  backend,
2288
2311
  projectId: defaultProjectId,
@@ -2406,7 +2429,11 @@ function makeHandle(handleArgs) {
2406
2429
  throw projectApiDown(sent);
2407
2430
  return request(opts);
2408
2431
  }, admitToLake = (request) => {
2409
- refuseSession(request), refuseInternalToken(request);
2432
+ if (refuseSession(request), missingKey !== void 0) {
2433
+ const parts = datasetParts(target.id);
2434
+ throw datasetNotFound(request, parts.projectId, parts.dataset);
2435
+ }
2436
+ refuseInternalToken(request);
2410
2437
  const access = activeAccess();
2411
2438
  if (access === void 0) return;
2412
2439
  const refusal = refusedToken(request, access.grants);
@@ -2469,16 +2496,22 @@ function makeHandle(handleArgs) {
2469
2496
  ...accessControl.access !== void 0 ? { access: accessControl.access } : {}
2470
2497
  }
2471
2498
  ] : []
2472
- ], capabilities = capabilitiesOf(target.type), targetLabel = capabilities.label(target.id), targetEntry = ensureEntry(registry, target), { store, history } = targetEntry, publicStore = {
2499
+ ], capabilities = capabilitiesOf(target.type), targetLabel = capabilities.label(target.id), targetEntry = ensureEntry(registry, target), { store, history } = targetEntry, missingKey = (() => {
2500
+ if (target.type !== "dataset") return;
2501
+ const parts = datasetParts(target.id), key = `${parts.projectId}:${parts.dataset}`;
2502
+ return backend.missingDatasets.has(key) ? key : void 0;
2503
+ })(), refuseSeedingMissing = (caller) => {
2504
+ if (missingKey !== void 0) throw seededMissingDataset(caller, missingKey);
2505
+ }, publicStore = {
2473
2506
  ...store,
2474
2507
  seed: (documents) => {
2475
- store.seed(documents), targetEntry.exists = !0;
2508
+ refuseSeedingMissing("seed()"), store.seed(documents), targetEntry.exists = !0;
2476
2509
  },
2477
2510
  add: (documents) => {
2478
- store.add(documents), targetEntry.exists = !0;
2511
+ refuseSeedingMissing("add()"), store.add(documents), targetEntry.exists = !0;
2479
2512
  },
2480
2513
  put: (doc) => {
2481
- store.put(doc), targetEntry.exists = !0;
2514
+ refuseSeedingMissing("put()"), store.put(doc), targetEntry.exists = !0;
2482
2515
  }
2483
2516
  }, concurrentEditKey = (id) => registryKey(target) + " " + id, actorId = () => {
2484
2517
  const access = activeAccess();