@sanity/client 5.4.2 → 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";
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,
@@ -826,6 +841,12 @@ function _requestObservable(client, httpRequest, options) {
826
841
  ...options.query
827
842
  };
828
843
  }
844
+ if (config.resultSourceMap) {
845
+ options.query = {
846
+ resultSourceMap: true,
847
+ ...options.query
848
+ };
849
+ }
829
850
  const reqOptions = requestOptions(config, Object.assign({}, options, {
830
851
  url: _getUrl(client, uri, useCdn)
831
852
  }));
@@ -1002,10 +1023,10 @@ once(function () {
1002
1023
  }
1003
1024
  return console.warn(message.join(" "), ...args);
1004
1025
  });
1005
- 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."]);
1006
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.")]);
1007
1028
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1008
- 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."]);
1009
1030
  const defaultCdnHost = "apicdn.sanity.io";
1010
1031
  const defaultConfig = {
1011
1032
  apiHost: "https://api.sanity.io",
@@ -1056,7 +1077,7 @@ const initConfig = (config, prevConfig) => {
1056
1077
  }
1057
1078
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1058
1079
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1059
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1080
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1060
1081
  validateApiVersion(newConfig.apiVersion);
1061
1082
  const hostParts = newConfig.apiHost.split("://", 2);
1062
1083
  const protocol = hostParts[0];
@@ -1733,21 +1754,24 @@ const _SanityClient = class {
1733
1754
  return new Transaction(operations, this);
1734
1755
  }
1735
1756
  /**
1736
- * 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!
1737
1759
  *
1738
- * @deprecated Use your own request library!
1739
1760
  * @param options - Request options
1761
+ * @returns Promise resolving to the response body
1740
1762
  */
1741
1763
  request(options) {
1742
1764
  return rxjs.lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1743
1765
  }
1744
1766
  /**
1745
- * 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.
1746
1769
  *
1747
- * @deprecated Use your own request library!
1770
+ * @deprecated - Use `request()` or your own HTTP library instead
1748
1771
  * @param endpoint - Endpoint to hit (mutate, query etc)
1749
1772
  * @param body - Request body
1750
1773
  * @param options - Request options
1774
+ * @internal
1751
1775
  */
1752
1776
  dataRequest(endpoint, body, options) {
1753
1777
  return rxjs.lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1774,9 +1798,12 @@ const _SanityClient = class {
1774
1798
  let SanityClient = _SanityClient;
1775
1799
  _clientConfig2 = new WeakMap();
1776
1800
  _httpRequest2 = new WeakMap();
1777
- const httpRequest = defineHttpRequest(middleware);
1801
+ const httpRequest = defineHttpRequest(middleware, {});
1778
1802
  const requester = httpRequest.defaultRequester;
1779
- 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);
1780
1807
  function deprecatedCreateClient(config) {
1781
1808
  printNoDefaultExport();
1782
1809
  return new SanityClient(httpRequest, config);