@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.d.ts CHANGED
@@ -107,6 +107,7 @@ export declare type BaseMutationOptions = RequestOptions & {
107
107
  dryRun?: boolean
108
108
  autoGenerateArrayKeys?: boolean
109
109
  skipCrossDatasetReferenceValidation?: boolean
110
+ transactionId?: string
110
111
  }
111
112
 
112
113
  /** @internal */
@@ -279,6 +280,7 @@ export declare type ChannelErrorEvent = {
279
280
  export declare interface ClientConfig {
280
281
  projectId?: string
281
282
  dataset?: string
283
+ /** @defaultValue true */
282
284
  useCdn?: boolean
283
285
  token?: string
284
286
  apiHost?: string
@@ -289,6 +291,17 @@ export declare interface ClientConfig {
289
291
  withCredentials?: boolean
290
292
  allowReconfigure?: boolean
291
293
  timeout?: number
294
+ /** Number of retries for requests. Defaults to 5. */
295
+ maxRetries?: number
296
+ /**
297
+ * The amount of time, in milliseconds, to wait before retrying, given an attemptNumber (starting at 0).
298
+ *
299
+ * Defaults to exponential back-off, starting at 100ms, doubling for each attempt, together with random
300
+ * jitter between 0 and 100 milliseconds. More specifically the following algorithm is used:
301
+ *
302
+ * Delay = 100 * 2^attemptNumber + randomNumberBetween0and100
303
+ */
304
+ retryDelay?: (attemptNumber: number) => number
292
305
  /**
293
306
  * @deprecated Don't use
294
307
  */
@@ -1952,19 +1965,22 @@ export declare class SanityClient {
1952
1965
  operations?: Mutation<R>[]
1953
1966
  ): Transaction
1954
1967
  /**
1955
- * DEPRECATED: Perform an HTTP request against the Sanity API
1968
+ * Perform a request against the Sanity API
1969
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1956
1970
  *
1957
- * @deprecated Use your own request library!
1958
1971
  * @param options - Request options
1972
+ * @returns Promise resolving to the response body
1959
1973
  */
1960
1974
  request<R = Any>(options: RawRequestOptions): Promise<R>
1961
1975
  /**
1962
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1976
+ * Perform an HTTP request a `/data` sub-endpoint
1977
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1963
1978
  *
1964
- * @deprecated Use your own request library!
1979
+ * @deprecated - Use `request()` or your own HTTP library instead
1965
1980
  * @param endpoint - Endpoint to hit (mutate, query etc)
1966
1981
  * @param body - Request body
1967
1982
  * @param options - Request options
1983
+ * @internal
1968
1984
  */
1969
1985
  dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<Any>
1970
1986
  /**
package/dist/index.js CHANGED
@@ -4,14 +4,12 @@ export { adapter as unstable__adapter, environment as unstable__environment } fr
4
4
  import { Observable, lastValueFrom } from 'rxjs';
5
5
  import { map, filter } from 'rxjs/operators';
6
6
  var name = "@sanity/client";
7
- var version = "5.4.2-dev.0";
7
+ var version = "5.4.3-dev.0";
8
8
  const middleware = [debug({
9
9
  verbose: true,
10
10
  namespace: "sanity:client"
11
11
  }), headers({
12
12
  "User-Agent": "".concat(name, " ").concat(version)
13
- }), retry({
14
- maxRetries: 3
15
13
  })];
16
14
  const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
17
15
  class ClientError extends Error {
@@ -98,8 +96,18 @@ const printWarnings = {
98
96
  return res;
99
97
  }
100
98
  };
101
- function defineHttpRequest(envMiddleware) {
102
- const request = getIt([...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
99
+ function defineHttpRequest(envMiddleware, _ref) {
100
+ let {
101
+ maxRetries = 5,
102
+ retryDelay
103
+ } = _ref;
104
+ const request = getIt([maxRetries > 0 ? retry({
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ retryDelay,
107
+ // This option is typed incorrectly in get-it.
108
+ maxRetries,
109
+ shouldRetry
110
+ }) : {}, ...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
103
111
  implementation: Observable
104
112
  })]);
105
113
  function httpRequest(options) {
@@ -112,6 +120,13 @@ function defineHttpRequest(envMiddleware) {
112
120
  httpRequest.defaultRequester = request;
113
121
  return httpRequest;
114
122
  }
123
+ function shouldRetry(err, attempt, options) {
124
+ const isSafe = options.method === "GET" || options.method === "HEAD";
125
+ const isQuery = options.uri.startsWith("/data/query");
126
+ const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
127
+ if ((isSafe || isQuery) && isRetriableResponse) return true;
128
+ return retry.shouldRetry(err, attempt, options);
129
+ }
115
130
  const projectHeader = "X-Sanity-Project-ID";
116
131
  function requestOptions(config) {
117
132
  let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -208,12 +223,12 @@ const requestTag = tag => {
208
223
  }
209
224
  return tag;
210
225
  };
211
- const encodeQueryString = _ref => {
226
+ const encodeQueryString = _ref2 => {
212
227
  let {
213
228
  query,
214
229
  params = {},
215
230
  options = {}
216
- } = _ref;
231
+ } = _ref2;
217
232
  const searchParams = new URLSearchParams();
218
233
  const {
219
234
  tag,
@@ -735,9 +750,18 @@ function _delete(client, httpRequest, selection, options) {
735
750
  }, options);
736
751
  }
737
752
  function _mutate(client, httpRequest, mutations, options) {
738
- const mut = mutations instanceof Patch || mutations instanceof ObservablePatch || mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mutations.serialize() : mutations;
753
+ let mut;
754
+ if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
755
+ mut = {
756
+ patch: mutations.serialize()
757
+ };
758
+ } else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
759
+ mut = mutations.serialize();
760
+ } else {
761
+ mut = mutations;
762
+ }
739
763
  const muts = Array.isArray(mut) ? mut : [mut];
740
- const transactionId = options && options.transactionId;
764
+ const transactionId = options && options.transactionId || void 0;
741
765
  return _dataRequest(client, httpRequest, "mutate", {
742
766
  mutations: muts,
743
767
  transactionId
@@ -995,10 +1019,10 @@ once(function () {
995
1019
  }
996
1020
  return console.warn(message.join(" "), ...args);
997
1021
  });
998
- 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."]);
1022
+ 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."]);
999
1023
  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.")]);
1000
1024
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1001
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
1025
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
1002
1026
  const defaultCdnHost = "apicdn.sanity.io";
1003
1027
  const defaultConfig = {
1004
1028
  apiHost: "https://api.sanity.io",
@@ -1049,7 +1073,7 @@ const initConfig = (config, prevConfig) => {
1049
1073
  }
1050
1074
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1051
1075
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1052
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1076
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1053
1077
  validateApiVersion(newConfig.apiVersion);
1054
1078
  const hostParts = newConfig.apiHost.split("://", 2);
1055
1079
  const protocol = hostParts[0];
@@ -1726,21 +1750,24 @@ const _SanityClient = class {
1726
1750
  return new Transaction(operations, this);
1727
1751
  }
1728
1752
  /**
1729
- * DEPRECATED: Perform an HTTP request against the Sanity API
1753
+ * Perform a request against the Sanity API
1754
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1730
1755
  *
1731
- * @deprecated Use your own request library!
1732
1756
  * @param options - Request options
1757
+ * @returns Promise resolving to the response body
1733
1758
  */
1734
1759
  request(options) {
1735
1760
  return lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1736
1761
  }
1737
1762
  /**
1738
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1763
+ * Perform an HTTP request a `/data` sub-endpoint
1764
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1739
1765
  *
1740
- * @deprecated Use your own request library!
1766
+ * @deprecated - Use `request()` or your own HTTP library instead
1741
1767
  * @param endpoint - Endpoint to hit (mutate, query etc)
1742
1768
  * @param body - Request body
1743
1769
  * @param options - Request options
1770
+ * @internal
1744
1771
  */
1745
1772
  dataRequest(endpoint, body, options) {
1746
1773
  return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1767,9 +1794,12 @@ const _SanityClient = class {
1767
1794
  let SanityClient = _SanityClient;
1768
1795
  _clientConfig2 = new WeakMap();
1769
1796
  _httpRequest2 = new WeakMap();
1770
- const httpRequest = defineHttpRequest(middleware);
1797
+ const httpRequest = defineHttpRequest(middleware, {});
1771
1798
  const requester = httpRequest.defaultRequester;
1772
- const createClient = config => new SanityClient(httpRequest, config);
1799
+ const createClient = config => new SanityClient(defineHttpRequest(middleware, {
1800
+ maxRetries: config.maxRetries,
1801
+ retryDelay: config.retryDelay
1802
+ }), config);
1773
1803
  function deprecatedCreateClient(config) {
1774
1804
  printNoDefaultExport();
1775
1805
  return new SanityClient(httpRequest, config);