@sanity/client 5.4.2-dev.0 → 5.4.3-dev.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.
@@ -1,6 +1,6 @@
1
1
  import { getIt } from 'get-it';
2
2
  export { adapter as unstable__adapter, environment as unstable__environment } from 'get-it';
3
- import { jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
3
+ import { retry, jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
4
4
  import { Observable, lastValueFrom } from 'rxjs';
5
5
  import { map, filter } from 'rxjs/operators';
6
6
  var envMiddleware = [];
@@ -89,8 +89,18 @@ const printWarnings = {
89
89
  return res;
90
90
  }
91
91
  };
92
- function defineHttpRequest(envMiddleware) {
93
- const request = getIt([...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
92
+ function defineHttpRequest(envMiddleware, _ref) {
93
+ let {
94
+ maxRetries = 5,
95
+ retryDelay
96
+ } = _ref;
97
+ const request = getIt([maxRetries > 0 ? retry({
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ retryDelay,
100
+ // This option is typed incorrectly in get-it.
101
+ maxRetries,
102
+ shouldRetry
103
+ }) : {}, ...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
94
104
  implementation: Observable
95
105
  })]);
96
106
  function httpRequest(options) {
@@ -103,6 +113,13 @@ function defineHttpRequest(envMiddleware) {
103
113
  httpRequest.defaultRequester = request;
104
114
  return httpRequest;
105
115
  }
116
+ function shouldRetry(err, attempt, options) {
117
+ const isSafe = options.method === "GET" || options.method === "HEAD";
118
+ const isQuery = options.uri.startsWith("/data/query");
119
+ const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
120
+ if ((isSafe || isQuery) && isRetriableResponse) return true;
121
+ return retry.shouldRetry(err, attempt, options);
122
+ }
106
123
  const projectHeader = "X-Sanity-Project-ID";
107
124
  function requestOptions(config) {
108
125
  let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -199,12 +216,12 @@ const requestTag = tag => {
199
216
  }
200
217
  return tag;
201
218
  };
202
- const encodeQueryString = _ref => {
219
+ const encodeQueryString = _ref2 => {
203
220
  let {
204
221
  query,
205
222
  params = {},
206
223
  options = {}
207
- } = _ref;
224
+ } = _ref2;
208
225
  const searchParams = new URLSearchParams();
209
226
  const {
210
227
  tag,
@@ -726,9 +743,18 @@ function _delete(client, httpRequest, selection, options) {
726
743
  }, options);
727
744
  }
728
745
  function _mutate(client, httpRequest, mutations, options) {
729
- const mut = mutations instanceof Patch || mutations instanceof ObservablePatch || mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mutations.serialize() : mutations;
746
+ let mut;
747
+ if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
748
+ mut = {
749
+ patch: mutations.serialize()
750
+ };
751
+ } else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
752
+ mut = mutations.serialize();
753
+ } else {
754
+ mut = mutations;
755
+ }
730
756
  const muts = Array.isArray(mut) ? mut : [mut];
731
- const transactionId = options && options.transactionId;
757
+ const transactionId = options && options.transactionId || void 0;
732
758
  return _dataRequest(client, httpRequest, "mutate", {
733
759
  mutations: muts,
734
760
  transactionId
@@ -986,10 +1012,10 @@ once(function () {
986
1012
  }
987
1013
  return console.warn(message.join(" "), ...args);
988
1014
  });
989
- const printCdnWarning = createWarningPrinter(["You are not using the Sanity CDN. That means your data is always fresh, but the CDN is faster and", "cheaper. Think about it! For more info, see ".concat(generateHelpUrl("js-client-cdn-configuration"), " "), "To hide this warning, please set the `useCdn` option to either `true` or `false` when creating", "the client."]);
1015
+ const printCdnWarning = createWarningPrinter(["Since you haven't set a value for `useCdn`, we will deliver content using our", "global, edge-cached API-CDN. If you wish to have content delivered faster, set", "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."]);
990
1016
  const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
991
1017
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
992
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
1018
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
993
1019
  const defaultCdnHost = "apicdn.sanity.io";
994
1020
  const defaultConfig = {
995
1021
  apiHost: "https://api.sanity.io",
@@ -1040,7 +1066,7 @@ const initConfig = (config, prevConfig) => {
1040
1066
  }
1041
1067
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1042
1068
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1043
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1069
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1044
1070
  validateApiVersion(newConfig.apiVersion);
1045
1071
  const hostParts = newConfig.apiHost.split("://", 2);
1046
1072
  const protocol = hostParts[0];
@@ -1717,21 +1743,24 @@ const _SanityClient = class {
1717
1743
  return new Transaction(operations, this);
1718
1744
  }
1719
1745
  /**
1720
- * DEPRECATED: Perform an HTTP request against the Sanity API
1746
+ * Perform a request against the Sanity API
1747
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1721
1748
  *
1722
- * @deprecated Use your own request library!
1723
1749
  * @param options - Request options
1750
+ * @returns Promise resolving to the response body
1724
1751
  */
1725
1752
  request(options) {
1726
1753
  return lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1727
1754
  }
1728
1755
  /**
1729
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1756
+ * Perform an HTTP request a `/data` sub-endpoint
1757
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1730
1758
  *
1731
- * @deprecated Use your own request library!
1759
+ * @deprecated - Use `request()` or your own HTTP library instead
1732
1760
  * @param endpoint - Endpoint to hit (mutate, query etc)
1733
1761
  * @param body - Request body
1734
1762
  * @param options - Request options
1763
+ * @internal
1735
1764
  */
1736
1765
  dataRequest(endpoint, body, options) {
1737
1766
  return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1758,9 +1787,12 @@ const _SanityClient = class {
1758
1787
  let SanityClient = _SanityClient;
1759
1788
  _clientConfig2 = new WeakMap();
1760
1789
  _httpRequest2 = new WeakMap();
1761
- const httpRequest = defineHttpRequest(envMiddleware);
1790
+ const httpRequest = defineHttpRequest(envMiddleware, {});
1762
1791
  const requester = httpRequest.defaultRequester;
1763
- const createClient = config => new SanityClient(httpRequest, config);
1792
+ const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {
1793
+ maxRetries: config.maxRetries,
1794
+ retryDelay: config.retryDelay
1795
+ }), config);
1764
1796
  function deprecatedCreateClient(config) {
1765
1797
  printNoDefaultExport();
1766
1798
  return new SanityClient(httpRequest, config);