@sanity/client 7.22.1 → 7.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
@@ -3191,6 +3191,20 @@ export declare class ObservableSanityClient {
3191
3191
  tag?: string
3192
3192
  },
3193
3193
  ): Observable<(SanityDocument<R> | null)[]>
3194
+ /**
3195
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
3196
+ * Returns a set of the IDs that exist.
3197
+ *
3198
+ * @param ids - Document IDs to check
3199
+ * @param options - Request options
3200
+ */
3201
+ documentsExists(
3202
+ ids: string[],
3203
+ options?: {
3204
+ signal?: AbortSignal
3205
+ tag?: string
3206
+ },
3207
+ ): Observable<Set<string>>
3194
3208
  /**
3195
3209
  * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
3196
3210
  * Returns an observable that resolves to the created document.
@@ -5102,6 +5116,20 @@ export declare class SanityClient {
5102
5116
  tag?: string
5103
5117
  },
5104
5118
  ): Promise<(SanityDocument<R> | null)[]>
5119
+ /**
5120
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
5121
+ * Returns a set of the IDs that exist.
5122
+ *
5123
+ * @param ids - Document IDs to check
5124
+ * @param options - Request options
5125
+ */
5126
+ documentsExists(
5127
+ ids: string[],
5128
+ options?: {
5129
+ signal?: AbortSignal
5130
+ tag?: string
5131
+ },
5132
+ ): Promise<Set<string>>
5105
5133
  /**
5106
5134
  * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
5107
5135
  * Returns a promise that resolves to the created document.
package/dist/index.d.ts CHANGED
@@ -3191,6 +3191,20 @@ export declare class ObservableSanityClient {
3191
3191
  tag?: string
3192
3192
  },
3193
3193
  ): Observable<(SanityDocument<R> | null)[]>
3194
+ /**
3195
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
3196
+ * Returns a set of the IDs that exist.
3197
+ *
3198
+ * @param ids - Document IDs to check
3199
+ * @param options - Request options
3200
+ */
3201
+ documentsExists(
3202
+ ids: string[],
3203
+ options?: {
3204
+ signal?: AbortSignal
3205
+ tag?: string
3206
+ },
3207
+ ): Observable<Set<string>>
3194
3208
  /**
3195
3209
  * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
3196
3210
  * Returns an observable that resolves to the created document.
@@ -5102,6 +5116,20 @@ export declare class SanityClient {
5102
5116
  tag?: string
5103
5117
  },
5104
5118
  ): Promise<(SanityDocument<R> | null)[]>
5119
+ /**
5120
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
5121
+ * Returns a set of the IDs that exist.
5122
+ *
5123
+ * @param ids - Document IDs to check
5124
+ * @param options - Request options
5125
+ */
5126
+ documentsExists(
5127
+ ids: string[],
5128
+ options?: {
5129
+ signal?: AbortSignal
5130
+ tag?: string
5131
+ },
5132
+ ): Promise<Set<string>>
5105
5133
  /**
5106
5134
  * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
5107
5135
  * Returns a promise that resolves to the created document.
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { retry, jsonRequest, jsonResponse, progress, observable, debug, headers,
4
4
  import { Observable, defer, isObservable, of, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, timer, throwError, tap, finalize, share, merge, map as map$1, firstValueFrom } from "rxjs";
5
5
  import { isRecord } from "./_chunks-es/isRecord.js";
6
6
  import { stegaClean } from "./_chunks-es/stegaClean.js";
7
- import { combineLatestWith, map, filter, finalize as finalize$1 } from "rxjs/operators";
7
+ import { combineLatestWith, map, filter, concatMap, reduce, finalize as finalize$1 } from "rxjs/operators";
8
8
  import { getVersionFromId, isDraftId, getVersionId, getDraftId, isVersionId, getPublishedId } from "@sanity/client/csm";
9
9
  import { customAlphabet } from "nanoid";
10
10
  import { validateObject, validateInsert, requireDocumentId, validateDocumentId, requireDocumentType, printCreateVersionWithBaseIdWarning, resourceConfig, hasDataset, requestTag, printPreviewDraftsDeprecationWarning, validateApiPerspective, printCdnPreviewDraftsWarning, validateAssetType, resourceGuard, dataset, validateVersionIdMatch, defaultConfig, initConfig, printNoDefaultExport } from "./_chunks-es/config.js";
@@ -783,6 +783,35 @@ function _getDocuments(client, httpRequest, ids, opts = {}) {
783
783
  })
784
784
  );
785
785
  }
786
+ const DOCUMENTS_EXISTS_BATCH_SIZE = 100;
787
+ function _documentsExists(client, httpRequest, ids, opts = {}) {
788
+ if (ids.length === 0)
789
+ return of(/* @__PURE__ */ new Set());
790
+ const batches = [];
791
+ for (let i = 0; i < ids.length; i += DOCUMENTS_EXISTS_BATCH_SIZE)
792
+ batches.push(ids.slice(i, i + DOCUMENTS_EXISTS_BATCH_SIZE));
793
+ const fetchBatch = (batchIds) => _requestObservable(client, httpRequest, {
794
+ uri: _getDataUrl(client, "doc", batchIds.map(encodeURIComponent).join(",")),
795
+ tag: opts.tag,
796
+ signal: opts.signal,
797
+ query: { excludeContent: !0 }
798
+ }).pipe(
799
+ filter(isResponse),
800
+ map((event) => {
801
+ const missing = /* @__PURE__ */ new Set();
802
+ for (const omitted of event.body.omitted || [])
803
+ omitted.reason === "existence" && missing.add(omitted.id);
804
+ return new Set(batchIds.filter((id) => !missing.has(id)));
805
+ })
806
+ );
807
+ return from(batches).pipe(
808
+ concatMap(fetchBatch),
809
+ reduce((acc, set) => {
810
+ for (const id of set) acc.add(id);
811
+ return acc;
812
+ }, /* @__PURE__ */ new Set())
813
+ );
814
+ }
786
815
  function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
787
816
  return _dataRequest(
788
817
  client,
@@ -919,7 +948,7 @@ const hasDataConfig = (client) => {
919
948
  return config.dataset !== void 0 && config.projectId !== void 0 || config.resource !== void 0;
920
949
  }, 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);
921
950
  function _requestObservable(client, httpRequest, options) {
922
- const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
951
+ const callSiteStack = new Error(), uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
923
952
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
924
953
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
925
954
  if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
@@ -935,7 +964,8 @@ function _requestObservable(client, httpRequest, options) {
935
964
  const reqOptions = requestOptions(
936
965
  config,
937
966
  Object.assign({}, options, {
938
- url: _getUrl(client, uri, useCdn)
967
+ url: _getUrl(client, uri, useCdn),
968
+ callSiteStack
939
969
  })
940
970
  ), request = new Observable(
941
971
  (subscriber) => httpRequest(reqOptions, config.requester).subscribe(subscriber)
@@ -2289,6 +2319,16 @@ class ObservableSanityClient {
2289
2319
  getDocuments(ids, options) {
2290
2320
  return _getDocuments(this, this.#httpRequest, ids, options);
2291
2321
  }
2322
+ /**
2323
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
2324
+ * Returns a set of the IDs that exist.
2325
+ *
2326
+ * @param ids - Document IDs to check
2327
+ * @param options - Request options
2328
+ */
2329
+ documentsExists(ids, options) {
2330
+ return _documentsExists(this, this.#httpRequest, ids, options);
2331
+ }
2292
2332
  create(document, options) {
2293
2333
  return _create(this, this.#httpRequest, document, "create", options);
2294
2334
  }
@@ -2566,6 +2606,16 @@ class SanityClient {
2566
2606
  getDocuments(ids, options) {
2567
2607
  return lastValueFrom(_getDocuments(this, this.#httpRequest, ids, options));
2568
2608
  }
2609
+ /**
2610
+ * Convenient and bandwidth efficient method of checking wether a set of document IDs exists.
2611
+ * Returns a set of the IDs that exist.
2612
+ *
2613
+ * @param ids - Document IDs to check
2614
+ * @param options - Request options
2615
+ */
2616
+ documentsExists(ids, options) {
2617
+ return lastValueFrom(_documentsExists(this, this.#httpRequest, ids, options));
2618
+ }
2569
2619
  create(document, options) {
2570
2620
  return lastValueFrom(
2571
2621
  _create(this, this.#httpRequest, document, "create", options)
@@ -2788,7 +2838,7 @@ function defineDeprecatedCreateClient(createClient2) {
2788
2838
  return printNoDefaultExport(), createClient2(config);
2789
2839
  };
2790
2840
  }
2791
- var name = "@sanity/client", version = "7.22.1";
2841
+ var name = "@sanity/client", version = "7.23.0";
2792
2842
  const middleware = [
2793
2843
  debug({ verbose: !0, namespace: "sanity:client" }),
2794
2844
  headers({ "User-Agent": `${name} ${version}` }),