@sanity/client 5.0.0 → 5.2.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.
@@ -738,7 +738,8 @@ function _dataRequest(client, httpRequest, endpoint, body) {
738
738
  headers,
739
739
  token,
740
740
  tag,
741
- canUseCdn: isQuery
741
+ canUseCdn: isQuery,
742
+ signal: options.signal
742
743
  };
743
744
  return _requestObservable(client, httpRequest, reqOptions).pipe(filter(isResponse), map(getBody), map(res => {
744
745
  if (!isMutation) {
@@ -785,9 +786,10 @@ function _requestObservable(client, httpRequest, options) {
785
786
  const reqOptions = getRequestOptions(config, Object.assign({}, options, {
786
787
  url: _getUrl(client, uri, useCdn)
787
788
  }));
788
- return new Observable(subscriber =>
789
+ const request = new Observable(subscriber =>
789
790
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the typings thinks it's optional because it's not required to specify it when calling createClient, but it's always defined in practice since SanityClient provides a default
790
791
  httpRequest(reqOptions, config.requester).subscribe(subscriber));
792
+ return options.signal ? request.pipe(_withAbortSignal(options.signal)) : request;
791
793
  }
792
794
  function _request(client, httpRequest, options) {
793
795
  const observable = _requestObservable(client, httpRequest, options).pipe(filter(event => event.type === "response"), map(event => event.body));
@@ -809,6 +811,33 @@ function _getUrl(client, uri) {
809
811
  const base = canUseCdn ? cdnUrl : url;
810
812
  return "".concat(base, "/").concat(uri.replace(/^\//, ""));
811
813
  }
814
+ function _withAbortSignal(signal) {
815
+ return input => {
816
+ return new Observable(observer => {
817
+ const abort = () => observer.error(_createAbortError(signal));
818
+ if (signal && signal.aborted) {
819
+ abort();
820
+ return;
821
+ }
822
+ const subscription = input.subscribe(observer);
823
+ signal.addEventListener("abort", abort);
824
+ return () => {
825
+ signal.removeEventListener("abort", abort);
826
+ subscription.unsubscribe();
827
+ };
828
+ });
829
+ };
830
+ }
831
+ const isDomExceptionSupported = Boolean(globalThis.DOMException);
832
+ function _createAbortError(signal) {
833
+ var _a, _b;
834
+ if (isDomExceptionSupported) {
835
+ return new DOMException((_a = signal == null ? void 0 : signal.reason) != null ? _a : "The operation was aborted.", "AbortError");
836
+ }
837
+ const error = new Error((_b = signal == null ? void 0 : signal.reason) != null ? _b : "The operation was aborted.");
838
+ error.name = "AbortError";
839
+ return error;
840
+ }
812
841
  var __accessCheck$4 = (obj, member, msg) => {
813
842
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
814
843
  };
@@ -1514,6 +1543,24 @@ const _ObservableSanityClient = class {
1514
1543
  request(options) {
1515
1544
  return _request(this, __privateGet(this, _httpRequest), options);
1516
1545
  }
1546
+ /**
1547
+ * Get a Sanity API URL for the URI provided
1548
+ *
1549
+ * @param uri - URI/path to build URL for
1550
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
1551
+ */
1552
+ getUrl(uri, canUseCdn) {
1553
+ return _getUrl(this, uri, canUseCdn);
1554
+ }
1555
+ /**
1556
+ * Get a Sanity API URL for the data operation and path provided
1557
+ *
1558
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
1559
+ * @param path - Path to append after the operation
1560
+ */
1561
+ getDataUrl(operation, path) {
1562
+ return _getDataUrl(this, operation, path);
1563
+ }
1517
1564
  };
1518
1565
  let ObservableSanityClient = _ObservableSanityClient;
1519
1566
  _clientConfig = new WeakMap();
@@ -1647,12 +1694,33 @@ const _SanityClient = class {
1647
1694
  dataRequest(endpoint, body, options) {
1648
1695
  return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
1649
1696
  }
1697
+ /**
1698
+ * Get a Sanity API URL for the URI provided
1699
+ *
1700
+ * @param uri - URI/path to build URL for
1701
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
1702
+ */
1703
+ getUrl(uri, canUseCdn) {
1704
+ return _getUrl(this, uri, canUseCdn);
1705
+ }
1706
+ /**
1707
+ * Get a Sanity API URL for the data operation and path provided
1708
+ *
1709
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
1710
+ * @param path - Path to append after the operation
1711
+ */
1712
+ getDataUrl(operation, path) {
1713
+ return _getDataUrl(this, operation, path);
1714
+ }
1650
1715
  };
1651
1716
  let SanityClient = _SanityClient;
1652
1717
  _clientConfig2 = new WeakMap();
1653
1718
  _httpRequest2 = new WeakMap();
1719
+ function migrationNotice() {
1720
+ throw new TypeError("The default export of @sanity/client has been deprecated. Use the named export `createClient` instead");
1721
+ }
1654
1722
  const httpRequest = defineHttpRequest(envMiddleware);
1655
1723
  const requester = httpRequest.defaultRequester;
1656
1724
  const createClient = config => new SanityClient(httpRequest, config);
1657
- export { BasePatch, BaseTransaction, ClientError, ObservablePatch, ObservableSanityClient, ObservableTransaction, Patch, SanityClient, ServerError, Transaction, createClient, requester };
1725
+ export { BasePatch, BaseTransaction, ClientError, ObservablePatch, ObservableSanityClient, ObservableTransaction, Patch, SanityClient, ServerError, Transaction, createClient, migrationNotice as default, requester };
1658
1726
  //# sourceMappingURL=index.browser.js.map