@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.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,13 @@ export declare class ClientError extends Error {
309
322
  constructor(res: Any)
310
323
  }
311
324
 
325
+ /** @internal */
326
+ export declare type ContentSourceMapping = {
327
+ mappings: Record<string, Mapping>
328
+ documents: Array<SanityDocument>
329
+ paths: Array<string>
330
+ }
331
+
312
332
  /** @public */
313
333
  export declare const createClient: (config: ClientConfig) => SanityClient
314
334
 
@@ -391,6 +411,16 @@ export declare type DisconnectEvent = {
391
411
  reason: string
392
412
  }
393
413
 
414
+ /**
415
+ * DocumentValueSource is a path to a value within a document
416
+ * @internal
417
+ */
418
+ export declare type DocumentValueSource = {
419
+ type: 'documentValue'
420
+ document: number
421
+ path: number
422
+ }
423
+
394
424
  /** @public */
395
425
  export declare interface ErrorProps {
396
426
  message: string
@@ -520,6 +550,17 @@ export declare interface ListenOptions {
520
550
  tag?: string
521
551
  }
522
552
 
553
+ /**
554
+ * When a value is not from a source, its a literal
555
+ * @internal
556
+ */
557
+ export declare type LiteralSource = {
558
+ type: 'literal'
559
+ }
560
+
561
+ /** @internal */
562
+ export declare type Mapping = ValueMapping
563
+
523
564
  /** @internal */
524
565
  export declare interface MultipleMutationResult {
525
566
  transactionId: string
@@ -1409,6 +1450,7 @@ export declare interface RawQueryResponse<R> {
1409
1450
  q: string
1410
1451
  ms: number
1411
1452
  result: R
1453
+ resultSourceMap: ContentSourceMapping
1412
1454
  }
1413
1455
 
1414
1456
  /** @internal */
@@ -1923,19 +1965,22 @@ export declare class SanityClient {
1923
1965
  operations?: Mutation<R>[]
1924
1966
  ): Transaction
1925
1967
  /**
1926
- * 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!
1927
1970
  *
1928
- * @deprecated Use your own request library!
1929
1971
  * @param options - Request options
1972
+ * @returns Promise resolving to the response body
1930
1973
  */
1931
1974
  request<R = Any>(options: RawRequestOptions): Promise<R>
1932
1975
  /**
1933
- * 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.
1934
1978
  *
1935
- * @deprecated Use your own request library!
1979
+ * @deprecated - Use `request()` or your own HTTP library instead
1936
1980
  * @param endpoint - Endpoint to hit (mutate, query etc)
1937
1981
  * @param body - Request body
1938
1982
  * @param options - Request options
1983
+ * @internal
1939
1984
  */
1940
1985
  dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<Any>
1941
1986
  /**
@@ -2080,6 +2125,9 @@ export declare interface SingleMutationResult {
2080
2125
  }[]
2081
2126
  }
2082
2127
 
2128
+ /** @internal */
2129
+ export declare type Source = DocumentValueSource | LiteralSource | UnknownSource
2130
+
2083
2131
  /** @public */
2084
2132
  export declare class Transaction extends BaseTransaction {
2085
2133
  #private
@@ -2175,6 +2223,14 @@ export declare type UnfilteredResponseQueryOptions = RequestOptions & {
2175
2223
  filterResponse: false
2176
2224
  }
2177
2225
 
2226
+ /**
2227
+ * When a field source is unknown
2228
+ * @internal
2229
+ */
2230
+ export declare type UnknownSource = {
2231
+ type: 'unknown'
2232
+ }
2233
+
2178
2234
  export {unstable__adapter}
2179
2235
 
2180
2236
  export {unstable__environment}
@@ -2255,6 +2311,16 @@ export declare class UsersClient {
2255
2311
  getById<T extends 'me' | string>(id: T): Promise<T extends 'me' ? CurrentSanityUser : SanityUser>
2256
2312
  }
2257
2313
 
2314
+ /**
2315
+ * ValueMapping is a mapping when for value that is from a single source value
2316
+ * It may refer to a field within a document or a literal value
2317
+ * @internal
2318
+ */
2319
+ export declare type ValueMapping = {
2320
+ type: 'value'
2321
+ source: Source
2322
+ }
2323
+
2258
2324
  /** @public */
2259
2325
  export declare type WelcomeEvent = {
2260
2326
  type: 'welcome'
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.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,
@@ -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);