@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.
package/dist/index.cjs CHANGED
@@ -8,14 +8,12 @@ var getIt = require('get-it');
8
8
  var rxjs = require('rxjs');
9
9
  var operators = require('rxjs/operators');
10
10
  var name = "@sanity/client";
11
- var version = "5.4.2-dev.0";
11
+ var version = "5.4.3-dev.0";
12
12
  const middleware = [middleware$1.debug({
13
13
  verbose: true,
14
14
  namespace: "sanity:client"
15
15
  }), middleware$1.headers({
16
16
  "User-Agent": "".concat(name, " ").concat(version)
17
- }), middleware$1.retry({
18
- maxRetries: 3
19
17
  })];
20
18
  const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
21
19
  class ClientError extends Error {
@@ -102,8 +100,18 @@ const printWarnings = {
102
100
  return res;
103
101
  }
104
102
  };
105
- function defineHttpRequest(envMiddleware) {
106
- const request = getIt.getIt([...envMiddleware, printWarnings, middleware$1.jsonRequest(), middleware$1.jsonResponse(), middleware$1.progress(), httpError, middleware$1.observable({
103
+ function defineHttpRequest(envMiddleware, _ref) {
104
+ let {
105
+ maxRetries = 5,
106
+ retryDelay
107
+ } = _ref;
108
+ const request = getIt.getIt([maxRetries > 0 ? middleware$1.retry({
109
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
+ retryDelay,
111
+ // This option is typed incorrectly in get-it.
112
+ maxRetries,
113
+ shouldRetry
114
+ }) : {}, ...envMiddleware, printWarnings, middleware$1.jsonRequest(), middleware$1.jsonResponse(), middleware$1.progress(), httpError, middleware$1.observable({
107
115
  implementation: rxjs.Observable
108
116
  })]);
109
117
  function httpRequest(options) {
@@ -116,6 +124,13 @@ function defineHttpRequest(envMiddleware) {
116
124
  httpRequest.defaultRequester = request;
117
125
  return httpRequest;
118
126
  }
127
+ function shouldRetry(err, attempt, options) {
128
+ const isSafe = options.method === "GET" || options.method === "HEAD";
129
+ const isQuery = options.uri.startsWith("/data/query");
130
+ const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
131
+ if ((isSafe || isQuery) && isRetriableResponse) return true;
132
+ return middleware$1.retry.shouldRetry(err, attempt, options);
133
+ }
119
134
  const projectHeader = "X-Sanity-Project-ID";
120
135
  function requestOptions(config) {
121
136
  let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -212,12 +227,12 @@ const requestTag = tag => {
212
227
  }
213
228
  return tag;
214
229
  };
215
- const encodeQueryString = _ref => {
230
+ const encodeQueryString = _ref2 => {
216
231
  let {
217
232
  query,
218
233
  params = {},
219
234
  options = {}
220
- } = _ref;
235
+ } = _ref2;
221
236
  const searchParams = new URLSearchParams();
222
237
  const {
223
238
  tag,
@@ -739,9 +754,18 @@ function _delete(client, httpRequest, selection, options) {
739
754
  }, options);
740
755
  }
741
756
  function _mutate(client, httpRequest, mutations, options) {
742
- const mut = mutations instanceof Patch || mutations instanceof ObservablePatch || mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mutations.serialize() : mutations;
757
+ let mut;
758
+ if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
759
+ mut = {
760
+ patch: mutations.serialize()
761
+ };
762
+ } else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
763
+ mut = mutations.serialize();
764
+ } else {
765
+ mut = mutations;
766
+ }
743
767
  const muts = Array.isArray(mut) ? mut : [mut];
744
- const transactionId = options && options.transactionId;
768
+ const transactionId = options && options.transactionId || void 0;
745
769
  return _dataRequest(client, httpRequest, "mutate", {
746
770
  mutations: muts,
747
771
  transactionId
@@ -999,10 +1023,10 @@ once(function () {
999
1023
  }
1000
1024
  return console.warn(message.join(" "), ...args);
1001
1025
  });
1002
- 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."]);
1026
+ 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."]);
1003
1027
  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.")]);
1004
1028
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1005
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
1029
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
1006
1030
  const defaultCdnHost = "apicdn.sanity.io";
1007
1031
  const defaultConfig = {
1008
1032
  apiHost: "https://api.sanity.io",
@@ -1053,7 +1077,7 @@ const initConfig = (config, prevConfig) => {
1053
1077
  }
1054
1078
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1055
1079
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1056
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1080
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1057
1081
  validateApiVersion(newConfig.apiVersion);
1058
1082
  const hostParts = newConfig.apiHost.split("://", 2);
1059
1083
  const protocol = hostParts[0];
@@ -1730,21 +1754,24 @@ const _SanityClient = class {
1730
1754
  return new Transaction(operations, this);
1731
1755
  }
1732
1756
  /**
1733
- * DEPRECATED: Perform an HTTP request against the Sanity API
1757
+ * Perform a request against the Sanity API
1758
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1734
1759
  *
1735
- * @deprecated Use your own request library!
1736
1760
  * @param options - Request options
1761
+ * @returns Promise resolving to the response body
1737
1762
  */
1738
1763
  request(options) {
1739
1764
  return rxjs.lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1740
1765
  }
1741
1766
  /**
1742
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1767
+ * Perform an HTTP request a `/data` sub-endpoint
1768
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1743
1769
  *
1744
- * @deprecated Use your own request library!
1770
+ * @deprecated - Use `request()` or your own HTTP library instead
1745
1771
  * @param endpoint - Endpoint to hit (mutate, query etc)
1746
1772
  * @param body - Request body
1747
1773
  * @param options - Request options
1774
+ * @internal
1748
1775
  */
1749
1776
  dataRequest(endpoint, body, options) {
1750
1777
  return rxjs.lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1771,9 +1798,12 @@ const _SanityClient = class {
1771
1798
  let SanityClient = _SanityClient;
1772
1799
  _clientConfig2 = new WeakMap();
1773
1800
  _httpRequest2 = new WeakMap();
1774
- const httpRequest = defineHttpRequest(middleware);
1801
+ const httpRequest = defineHttpRequest(middleware, {});
1775
1802
  const requester = httpRequest.defaultRequester;
1776
- const createClient = config => new SanityClient(httpRequest, config);
1803
+ const createClient = config => new SanityClient(defineHttpRequest(middleware, {
1804
+ maxRetries: config.maxRetries,
1805
+ retryDelay: config.retryDelay
1806
+ }), config);
1777
1807
  function deprecatedCreateClient(config) {
1778
1808
  printNoDefaultExport();
1779
1809
  return new SanityClient(httpRequest, config);