@sanity/client 7.22.1 → 7.23.1

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.cjs CHANGED
@@ -235,6 +235,17 @@ function shouldRetry(err, attempt, options) {
235
235
  const EXPERIMENTAL_API_WARNING = "This is an experimental API version";
236
236
  class ConnectionFailedError extends Error {
237
237
  name = "ConnectionFailedError";
238
+ /**
239
+ * HTTP status code of the rejected connection attempt, if known.
240
+ * Only set when the EventSource implementation exposes it — the polyfill used in
241
+ * Node.js and when custom headers (eg authorization) are required does, while
242
+ * native EventSource implementations (browser and Node.js) do not.
243
+ */
244
+ status;
245
+ constructor(message, options = {}) {
246
+ const { status, ...errorOptions } = options;
247
+ super(message, errorOptions), this.status = status;
248
+ }
238
249
  }
239
250
  class DisconnectError extends Error {
240
251
  name = "DisconnectError";
@@ -278,6 +289,11 @@ function connectWithESInstance(es, events) {
278
289
  );
279
290
  return;
280
291
  }
292
+ const rawStatus = evt.status, status = typeof rawStatus == "number" ? rawStatus : void 0;
293
+ if (status !== void 0) {
294
+ observer.error(new ConnectionFailedError("EventSource connection failed", { status }));
295
+ return;
296
+ }
281
297
  es.readyState === es.CLOSED ? observer.error(new ConnectionFailedError("EventSource connection failed")) : emitReconnect && observer.next({ type: "reconnect" });
282
298
  }
283
299
  function onOpen() {
@@ -797,6 +813,35 @@ function _getDocuments(client, httpRequest, ids, opts = {}) {
797
813
  })
798
814
  );
799
815
  }
816
+ const DOCUMENTS_EXISTS_BATCH_SIZE = 100;
817
+ function _documentsExists(client, httpRequest, ids, opts = {}) {
818
+ if (ids.length === 0)
819
+ return rxjs.of(/* @__PURE__ */ new Set());
820
+ const batches = [];
821
+ for (let i = 0; i < ids.length; i += DOCUMENTS_EXISTS_BATCH_SIZE)
822
+ batches.push(ids.slice(i, i + DOCUMENTS_EXISTS_BATCH_SIZE));
823
+ const fetchBatch = (batchIds) => _requestObservable(client, httpRequest, {
824
+ uri: _getDataUrl(client, "doc", batchIds.map(encodeURIComponent).join(",")),
825
+ tag: opts.tag,
826
+ signal: opts.signal,
827
+ query: { excludeContent: !0 }
828
+ }).pipe(
829
+ operators.filter(isResponse),
830
+ operators.map((event) => {
831
+ const missing = /* @__PURE__ */ new Set();
832
+ for (const omitted of event.body.omitted || [])
833
+ omitted.reason === "existence" && missing.add(omitted.id);
834
+ return new Set(batchIds.filter((id) => !missing.has(id)));
835
+ })
836
+ );
837
+ return rxjs.from(batches).pipe(
838
+ operators.concatMap(fetchBatch),
839
+ operators.reduce((acc, set) => {
840
+ for (const id of set) acc.add(id);
841
+ return acc;
842
+ }, /* @__PURE__ */ new Set())
843
+ );
844
+ }
800
845
  function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
801
846
  return _dataRequest(
802
847
  client,
@@ -933,7 +978,7 @@ const hasDataConfig = (client) => {
933
978
  return config2.dataset !== void 0 && config2.projectId !== void 0 || config2.resource !== void 0;
934
979
  }, isQuery = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "query")), isMutate = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "mutate")), isDoc = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "doc", "")), isListener = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "listen")), isHistory = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "history", "")), isData = (client, uri) => uri.startsWith("/data/") || isQuery(client, uri) || isMutate(client, uri) || isDoc(client, uri) || isListener(client, uri) || isHistory(client, uri);
935
980
  function _requestObservable(client, httpRequest, options) {
936
- const uri = options.url || options.uri, config$1 = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
981
+ const callSiteStack = new Error(), uri = options.url || options.uri, config$1 = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
937
982
  let useCdn = (options.useCdn ?? config$1.useCdn) && canUseCdn;
938
983
  const tag = options.tag && config$1.requestTagPrefix ? [config$1.requestTagPrefix, options.tag].join(".") : options.tag || config$1.requestTagPrefix;
939
984
  if (tag && options.tag !== null && (options.query = { tag: config.requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
@@ -949,7 +994,8 @@ function _requestObservable(client, httpRequest, options) {
949
994
  const reqOptions = requestOptions(
950
995
  config$1,
951
996
  Object.assign({}, options, {
952
- url: _getUrl(client, uri, useCdn)
997
+ url: _getUrl(client, uri, useCdn),
998
+ callSiteStack
953
999
  })
954
1000
  ), request = new rxjs.Observable(
955
1001
  (subscriber) => httpRequest(reqOptions, config$1.requester).subscribe(subscriber)
@@ -1219,11 +1265,11 @@ var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj
1219
1265
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = rxjs.defer(() => import("@sanity/eventsource")).pipe(
1220
1266
  operators.map(({ default: EventSource2 }) => EventSource2),
1221
1267
  rxjs.shareReplay(1)
1222
- );
1268
+ ), RETRYABLE_STATUSES = /* @__PURE__ */ new Set([408, 429]);
1223
1269
  function reconnectOnConnectionFailure() {
1224
1270
  return function(source) {
1225
1271
  return source.pipe(
1226
- rxjs.catchError((err, caught) => err instanceof ConnectionFailedError ? rxjs.concat(rxjs.of({ type: "reconnect" }), rxjs.timer(1e3).pipe(rxjs.mergeMap(() => caught))) : rxjs.throwError(() => err))
1272
+ rxjs.catchError((err, caught) => err instanceof ConnectionFailedError && (typeof err.status != "number" || err.status < 400 || err.status >= 500 || RETRYABLE_STATUSES.has(err.status)) ? rxjs.concat(rxjs.of({ type: "reconnect" }), rxjs.timer(1e3).pipe(rxjs.mergeMap(() => caught))) : rxjs.throwError(() => err))
1227
1273
  );
1228
1274
  };
1229
1275
  }
@@ -2303,6 +2349,16 @@ class ObservableSanityClient {
2303
2349
  getDocuments(ids, options) {
2304
2350
  return _getDocuments(this, this.#httpRequest, ids, options);
2305
2351
  }
2352
+ /**
2353
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
2354
+ * Returns a set of the IDs that exist.
2355
+ *
2356
+ * @param ids - Document IDs to check
2357
+ * @param options - Request options
2358
+ */
2359
+ documentsExists(ids, options) {
2360
+ return _documentsExists(this, this.#httpRequest, ids, options);
2361
+ }
2306
2362
  create(document, options) {
2307
2363
  return _create(this, this.#httpRequest, document, "create", options);
2308
2364
  }
@@ -2580,6 +2636,16 @@ class SanityClient {
2580
2636
  getDocuments(ids, options) {
2581
2637
  return rxjs.lastValueFrom(_getDocuments(this, this.#httpRequest, ids, options));
2582
2638
  }
2639
+ /**
2640
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
2641
+ * Returns a set of the IDs that exist.
2642
+ *
2643
+ * @param ids - Document IDs to check
2644
+ * @param options - Request options
2645
+ */
2646
+ documentsExists(ids, options) {
2647
+ return rxjs.lastValueFrom(_documentsExists(this, this.#httpRequest, ids, options));
2648
+ }
2583
2649
  create(document, options) {
2584
2650
  return rxjs.lastValueFrom(
2585
2651
  _create(this, this.#httpRequest, document, "create", options)
@@ -2802,7 +2868,7 @@ function defineDeprecatedCreateClient(createClient2) {
2802
2868
  return config.printNoDefaultExport(), createClient2(config$1);
2803
2869
  };
2804
2870
  }
2805
- var name = "@sanity/client", version = "7.22.1";
2871
+ var name = "@sanity/client", version = "7.23.1";
2806
2872
  const middleware = [
2807
2873
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
2808
2874
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),