@sanity/client 5.4.2 → 5.4.3-dev.1

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
@@ -280,6 +280,7 @@ export declare type ChannelErrorEvent = {
280
280
  export declare interface ClientConfig {
281
281
  projectId?: string
282
282
  dataset?: string
283
+ /** @defaultValue true */
283
284
  useCdn?: boolean
284
285
  token?: string
285
286
  apiHost?: string
@@ -290,6 +291,17 @@ export declare interface ClientConfig {
290
291
  withCredentials?: boolean
291
292
  allowReconfigure?: boolean
292
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
293
305
  /**
294
306
  * @deprecated Don't use
295
307
  */
@@ -298,6 +310,7 @@ export declare interface ClientConfig {
298
310
  * @deprecated Don't use
299
311
  */
300
312
  requester?: Requester
313
+ resultSourceMap?: boolean
301
314
  }
302
315
 
303
316
  /** @public */
@@ -309,6 +322,67 @@ export declare class ClientError extends Error {
309
322
  constructor(res: Any)
310
323
  }
311
324
 
325
+ /** @public */
326
+ export declare type ContentSourceMap = {
327
+ mappings: ContentSourceMapMappings
328
+ documents: ContentSourceMapDocuments
329
+ paths: ContentSourceMapPaths
330
+ }
331
+
332
+ /** @public */
333
+ export declare type ContentSourceMapDocuments = SanityDocument[]
334
+
335
+ /**
336
+ * DocumentValueSource is a path to a value within a document
337
+ * @public
338
+ */
339
+ export declare type ContentSourceMapDocumentValueSource = {
340
+ type: 'documentValue'
341
+ document: number
342
+ path: number
343
+ }
344
+
345
+ /**
346
+ * When a value is not from a source, its a literal
347
+ * @public
348
+ */
349
+ export declare type ContentSourceMapLiteralSource = {
350
+ type: 'literal'
351
+ }
352
+
353
+ /** @public */
354
+ export declare type ContentSourceMapMapping = ContentSourceMapValueMapping
355
+
356
+ /** @public */
357
+ export declare type ContentSourceMapMappings = Record<string, ContentSourceMapMapping>
358
+
359
+ /** @public */
360
+ export declare type ContentSourceMapPaths = string[]
361
+
362
+ /** @public */
363
+ export declare type ContentSourceMapSource =
364
+ | ContentSourceMapDocumentValueSource
365
+ | ContentSourceMapLiteralSource
366
+ | ContentSourceMapUnknownSource
367
+
368
+ /**
369
+ * When a field source is unknown
370
+ * @public
371
+ */
372
+ export declare type ContentSourceMapUnknownSource = {
373
+ type: 'unknown'
374
+ }
375
+
376
+ /**
377
+ * ValueMapping is a mapping when for value that is from a single source value
378
+ * It may refer to a field within a document or a literal value
379
+ * @public
380
+ */
381
+ export declare type ContentSourceMapValueMapping = {
382
+ type: 'value'
383
+ source: ContentSourceMapSource
384
+ }
385
+
312
386
  /** @public */
313
387
  export declare const createClient: (config: ClientConfig) => SanityClient
314
388
 
@@ -400,7 +474,7 @@ export declare interface ErrorProps {
400
474
  details: Any
401
475
  }
402
476
 
403
- /** @internal */
477
+ /** @public */
404
478
  export declare type FilteredResponseQueryOptions = RequestOptions & {
405
479
  filterResponse?: true
406
480
  }
@@ -1404,11 +1478,12 @@ export declare type QueryParams = {
1404
1478
  [key: string]: Any
1405
1479
  }
1406
1480
 
1407
- /** @internal */
1481
+ /** @public */
1408
1482
  export declare interface RawQueryResponse<R> {
1409
1483
  q: string
1410
1484
  ms: number
1411
1485
  result: R
1486
+ resultSourceMap: ContentSourceMap
1412
1487
  }
1413
1488
 
1414
1489
  /** @internal */
@@ -1923,19 +1998,22 @@ export declare class SanityClient {
1923
1998
  operations?: Mutation<R>[]
1924
1999
  ): Transaction
1925
2000
  /**
1926
- * DEPRECATED: Perform an HTTP request against the Sanity API
2001
+ * Perform a request against the Sanity API
2002
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1927
2003
  *
1928
- * @deprecated Use your own request library!
1929
2004
  * @param options - Request options
2005
+ * @returns Promise resolving to the response body
1930
2006
  */
1931
2007
  request<R = Any>(options: RawRequestOptions): Promise<R>
1932
2008
  /**
1933
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
2009
+ * Perform an HTTP request a `/data` sub-endpoint
2010
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1934
2011
  *
1935
- * @deprecated Use your own request library!
2012
+ * @deprecated - Use `request()` or your own HTTP library instead
1936
2013
  * @param endpoint - Endpoint to hit (mutate, query etc)
1937
2014
  * @param body - Request body
1938
2015
  * @param options - Request options
2016
+ * @internal
1939
2017
  */
1940
2018
  dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<Any>
1941
2019
  /**
@@ -2170,7 +2248,7 @@ export declare type TransactionMutationOptions =
2170
2248
  | TransactionAllDocumentsMutationOptions
2171
2249
  | TransactionAllDocumentIdsMutationOptions
2172
2250
 
2173
- /** @internal */
2251
+ /** @public */
2174
2252
  export declare type UnfilteredResponseQueryOptions = RequestOptions & {
2175
2253
  filterResponse: false
2176
2254
  }
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";
7
+ var version = "5.4.3-dev.1";
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,
@@ -822,6 +837,12 @@ function _requestObservable(client, httpRequest, options) {
822
837
  ...options.query
823
838
  };
824
839
  }
840
+ if (config.resultSourceMap) {
841
+ options.query = {
842
+ resultSourceMap: true,
843
+ ...options.query
844
+ };
845
+ }
825
846
  const reqOptions = requestOptions(config, Object.assign({}, options, {
826
847
  url: _getUrl(client, uri, useCdn)
827
848
  }));
@@ -998,10 +1019,10 @@ once(function () {
998
1019
  }
999
1020
  return console.warn(message.join(" "), ...args);
1000
1021
  });
1001
- 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."]);
1002
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.")]);
1003
1024
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1004
- 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."]);
1005
1026
  const defaultCdnHost = "apicdn.sanity.io";
1006
1027
  const defaultConfig = {
1007
1028
  apiHost: "https://api.sanity.io",
@@ -1052,7 +1073,7 @@ const initConfig = (config, prevConfig) => {
1052
1073
  }
1053
1074
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1054
1075
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1055
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1076
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1056
1077
  validateApiVersion(newConfig.apiVersion);
1057
1078
  const hostParts = newConfig.apiHost.split("://", 2);
1058
1079
  const protocol = hostParts[0];
@@ -1729,21 +1750,24 @@ const _SanityClient = class {
1729
1750
  return new Transaction(operations, this);
1730
1751
  }
1731
1752
  /**
1732
- * 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!
1733
1755
  *
1734
- * @deprecated Use your own request library!
1735
1756
  * @param options - Request options
1757
+ * @returns Promise resolving to the response body
1736
1758
  */
1737
1759
  request(options) {
1738
1760
  return lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1739
1761
  }
1740
1762
  /**
1741
- * 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.
1742
1765
  *
1743
- * @deprecated Use your own request library!
1766
+ * @deprecated - Use `request()` or your own HTTP library instead
1744
1767
  * @param endpoint - Endpoint to hit (mutate, query etc)
1745
1768
  * @param body - Request body
1746
1769
  * @param options - Request options
1770
+ * @internal
1747
1771
  */
1748
1772
  dataRequest(endpoint, body, options) {
1749
1773
  return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1770,9 +1794,12 @@ const _SanityClient = class {
1770
1794
  let SanityClient = _SanityClient;
1771
1795
  _clientConfig2 = new WeakMap();
1772
1796
  _httpRequest2 = new WeakMap();
1773
- const httpRequest = defineHttpRequest(middleware);
1797
+ const httpRequest = defineHttpRequest(middleware, {});
1774
1798
  const requester = httpRequest.defaultRequester;
1775
- 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);
1776
1803
  function deprecatedCreateClient(config) {
1777
1804
  printNoDefaultExport();
1778
1805
  return new SanityClient(httpRequest, config);