@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/dist/index.d.cts CHANGED
@@ -196,6 +196,14 @@ export declare interface CreateTestClientOptions {
196
196
  * whole `withConfig` family.
197
197
  */
198
198
  internalTokenKeys?: string[];
199
+ /**
200
+ * Datasets the platform does not have, as `"<projectId>:<dataset>"` keys.
201
+ * Every Content Lake request on one (queries, documents, mutations,
202
+ * actions, the History API) throws the 404 `Dataset not found` the
203
+ * Content Lake answers, after a refused token's 401. A key here must not
204
+ * also be seeded.
205
+ */
206
+ missingDatasets?: string[];
199
207
  /**
200
208
  * Token the root handle is bound to — must name an entry in
201
209
  * `identities` (unknown tokens throw), or be a JWT when
package/dist/index.d.ts CHANGED
@@ -196,6 +196,14 @@ export declare interface CreateTestClientOptions {
196
196
  * whole `withConfig` family.
197
197
  */
198
198
  internalTokenKeys?: string[];
199
+ /**
200
+ * Datasets the platform does not have, as `"<projectId>:<dataset>"` keys.
201
+ * Every Content Lake request on one (queries, documents, mutations,
202
+ * actions, the History API) throws the 404 `Dataset not found` the
203
+ * Content Lake answers, after a refused token's 401. A key here must not
204
+ * also be seeded.
205
+ */
206
+ missingDatasets?: string[];
199
207
  /**
200
208
  * Token the root handle is bound to — must name an entry in
201
209
  * `identities` (unknown tokens throw), or be a JWT when
package/dist/index.js 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();