@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/README.md CHANGED
@@ -26,7 +26,7 @@ import {createClient} from '@sanity/client'
26
26
  export const client = createClient({
27
27
  projectId: 'your-project-id',
28
28
  dataset: 'your-dataset-name',
29
- useCdn: false, // set to `true` to fetch from edge cache
29
+ useCdn: true, // set to `false` to bypass the edge cache
30
30
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
31
31
  // token: process.env.SANITY_SECRET_TOKEN // Only if you want to update content with the client
32
32
  })
@@ -94,7 +94,9 @@ export async function updateDocumentTitle(_id, title) {
94
94
  - [Set client configuration](#set-client-configuration)
95
95
  - [Release new version](#release-new-version)
96
96
  - [License](#license)
97
- - [From `v4`](#from-v4)
97
+ - [Migrate](#migrate)
98
+ - [From `v5`](#from-v5)
99
+ - [From `v4`](#from-v4)
98
100
 
99
101
  ## Requirements
100
102
 
@@ -120,7 +122,7 @@ pnpm install @sanity/client
120
122
 
121
123
  `const client = createClient(options)`
122
124
 
123
- Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. Setting a value for `useCdn` is encouraged. Typically you want to have it as `false` in development to always fetch the freshest content and `true` in production environments so that content is fetched from the distributed cache. [You can learn more about the API CDN here][api-cdn].
125
+ Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. [We encourage setting `useCdn` to either `true` or `false`.](https://www.sanity.io/help/js-client-cdn-configuration) The default is `true`. If you're not sure which option to choose we recommend starting with `true` and revise later if you find that you require uncached content. [Our awesome Slack community can help guide you on how to avoid stale data tailored to your tech stack and architecture.](https://slack.sanity.io/)
124
126
 
125
127
  #### [ESM](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)
126
128
 
@@ -130,7 +132,7 @@ import {createClient} from '@sanity/client'
130
132
  const client = createClient({
131
133
  projectId: 'your-project-id',
132
134
  dataset: 'your-dataset-name',
133
- useCdn: false, // set to `true` to fetch from edge cache
135
+ useCdn: true, // set to `false` to bypass the edge cache
134
136
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
135
137
  })
136
138
 
@@ -146,7 +148,7 @@ const {createClient} = require('@sanity/client')
146
148
  const client = createClient({
147
149
  projectId: 'your-project-id',
148
150
  dataset: 'your-dataset-name',
149
- useCdn: false, // set to `true` to fetch from edge cache
151
+ useCdn: true, // set to `false` to bypass the edge cache
150
152
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
151
153
  })
152
154
 
@@ -164,7 +166,7 @@ import {createClient, type ClientConfig} from '@sanity/client'
164
166
  const config: ClientConfig = {
165
167
  projectId: 'your-project-id',
166
168
  dataset: 'your-dataset-name',
167
- useCdn: false, // set to `true` to fetch from edge cache
169
+ useCdn: true, // set to `false` to bypass the edge cache
168
170
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
169
171
  }
170
172
  const client = createClient(config)
@@ -184,7 +186,7 @@ import {z} from 'zod'
184
186
  const client = createClient({
185
187
  projectId: 'your-project-id',
186
188
  dataset: 'your-dataset-name',
187
- useCdn: false, // set to `true` to fetch from edge cache
189
+ useCdn: true, // set to `false` to bypass the edge cache
188
190
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
189
191
  })
190
192
 
@@ -211,7 +213,7 @@ import {createClient} from '@sanity/client'
211
213
  const client = createClient({
212
214
  projectId: 'your-project-id',
213
215
  dataset: 'your-dataset-name',
214
- useCdn: false, // set to `true` to fetch from edge cache
216
+ useCdn: true, // set to `false` to bypass the edge cache
215
217
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
216
218
  })
217
219
 
@@ -239,7 +241,7 @@ import {createClient} from 'https://esm.sh/@sanity/client'
239
241
  const client = createClient({
240
242
  projectId: 'your-project-id',
241
243
  dataset: 'your-dataset-name',
242
- useCdn: false, // set to `true` to fetch from edge cache
244
+ useCdn: true, // set to `false` to bypass the edge cache
243
245
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
244
246
  })
245
247
 
@@ -272,7 +274,7 @@ export default async function handler(req: NextRequest) {
272
274
  const client = createClient({
273
275
  projectId: 'your-project-id',
274
276
  dataset: 'your-dataset-name',
275
- useCdn: false, // set to `true` to fetch from edge cache
277
+ useCdn: true, // set to `false` to bypass the edge cache
276
278
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
277
279
  })
278
280
 
@@ -303,7 +305,7 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
303
305
  const client = createClient({
304
306
  projectId: 'your-project-id',
305
307
  dataset: 'your-dataset-name',
306
- useCdn: false, // set to `true` to fetch from edge cache
308
+ useCdn: true, // set to `false` to bypass the edge cache
307
309
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
308
310
  })
309
311
 
@@ -322,7 +324,7 @@ const {createClient} = await import('https://esm.sh/@sanity/client')
322
324
  const client = createClient({
323
325
  projectId: 'your-project-id',
324
326
  dataset: 'your-dataset-name',
325
- useCdn: false, // set to `true` to fetch from edge cache
327
+ useCdn: true, // set to `false` to bypass the edge cache
326
328
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
327
329
  })
328
330
 
@@ -344,7 +346,7 @@ Loading the UMD script creates a `SanityClient` global that have the same export
344
346
  const client = createClient({
345
347
  projectId: 'your-project-id',
346
348
  dataset: 'your-dataset-name',
347
- useCdn: false, // set to `true` to fetch from edge cache
349
+ useCdn: true, // set to `false` to bypass the edge cache
348
350
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
349
351
  })
350
352
 
@@ -365,7 +367,7 @@ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` sim
365
367
  const client = createClient({
366
368
  projectId: 'your-project-id',
367
369
  dataset: 'your-dataset-name',
368
- useCdn: false, // set to `true` to fetch from edge cache
370
+ useCdn: true, // set to `false` to bypass the edge cache
369
371
  apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
370
372
  })
371
373
 
@@ -407,11 +409,11 @@ Perform a query using the given parameters (if any).
407
409
 
408
410
  > **Note**
409
411
  >
410
- > [Content Source Maps][content-source-maps-intro] are available [as an API][content-source-maps] for select Sanity enterprise customers. [Contact our sales team for more information.][content-source-maps-cta]
412
+ > [Content Source Maps][content-source-maps-intro] are available [as an API][content-source-maps] for select [Sanity enterprise customers][enterprise-cta]. [Contact our sales team for more information.][sales-cta]
411
413
 
412
414
  [Visual editing][visual-editing] is available in [`@sanity/preview-kit/client`][preview-kit-client]. It offers both a turn-key solution and a flexible API for custom experiences.
413
415
 
414
- This guide is for developers who want to build custom use cases beyond, or in addition to, [visual editing][visual-editing]. Read the [Content Source Maps introduction](https://www.sanity.io/docs/content-source-maps) before diving in, and keep the [Content Source Maps reference](https://www.sanity.io/docs/content-source-maps) handy.
416
+ This guide is for developers who want to build custom use cases beyond, or in addition to, [visual editing][visual-editing]. Read the [Content Source Maps introduction][content-source-maps-intro] before diving in, and keep the [Content Source Maps reference][content-source-maps] handy.
415
417
 
416
418
  Enabling Content Source Maps is a two-step process:
417
419
 
@@ -445,6 +447,7 @@ Once enabled, the `resultSourceMap` property will always exist on the response,
445
447
 
446
448
  ```ts
447
449
  import type {ContentSourceMapping} from '@sanity/client'
450
+
448
451
  const {result, resultSourceMap} = await client.fetch(query, params, {filterResponse: false})
449
452
 
450
453
  function useContentSourceMap(resultSourceMap: ContentSourceMapping): unknown {
@@ -976,6 +979,23 @@ MIT © [Sanity.io](https://www.sanity.io/)
976
979
 
977
980
  # Migrate
978
981
 
982
+ ## From `v5`
983
+
984
+ ### The default `useCdn` is changed to `true`
985
+
986
+ It was previously `false`. If you were relying on the default being `false` you can continue using the live API by setting it in the constructor:
987
+
988
+ ```diff
989
+ import {createClient} from '@sanity/client'
990
+
991
+ export const client = createClient({
992
+ projectId: 'your-project-id',
993
+ dataset: 'your-dataset-name',
994
+ apiVersion: '2023-03-12',
995
+ + useCdn: false, // set to `true` to use the edge cache
996
+ })
997
+ ```
998
+
979
999
  ## From `v4`
980
1000
 
981
1001
  ### No longer shipping `ES5`
@@ -1275,8 +1295,9 @@ await client.request<void>({uri: '/auth/logout', method: 'POST'})
1275
1295
  [groqd]: https://github.com/FormidableLabs/groqd#readme
1276
1296
  [AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
1277
1297
  [AbortController]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
1278
- [visual-editing]: https://www.sanity.io/docs/vercel-visual-editing
1279
- [content-source-maps]: https://www.sanity.io/docs/content-source-maps
1280
- [content-source-maps-intro]: https://www.sanity.io/blog/content-source-maps-announce
1281
- [content-source-maps-cta]: https://www.sanity.io/contact/sales
1298
+ [visual-editing]: https://www.sanity.io/docs/vercel-visual-editing?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1299
+ [content-source-maps]: https://www.sanity.io/docs/content-source-maps?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1300
+ [content-source-maps-intro]: https://www.sanity.io/blog/content-source-maps-announce?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1282
1301
  [preview-kit-client]: https://github.com/sanity-io/preview-kit#sanitypreview-kitclient
1302
+ [sales-cta]: https://www.sanity.io/contact/sales?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1303
+ [enterprise-cta]: https://www.sanity.io/enterprise?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
@@ -93,8 +93,18 @@ const printWarnings = {
93
93
  return res;
94
94
  }
95
95
  };
96
- function defineHttpRequest(envMiddleware) {
97
- const request = getIt.getIt([...envMiddleware, printWarnings, middleware.jsonRequest(), middleware.jsonResponse(), middleware.progress(), httpError, middleware.observable({
96
+ function defineHttpRequest(envMiddleware, _ref) {
97
+ let {
98
+ maxRetries = 5,
99
+ retryDelay
100
+ } = _ref;
101
+ const request = getIt.getIt([maxRetries > 0 ? middleware.retry({
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ retryDelay,
104
+ // This option is typed incorrectly in get-it.
105
+ maxRetries,
106
+ shouldRetry
107
+ }) : {}, ...envMiddleware, printWarnings, middleware.jsonRequest(), middleware.jsonResponse(), middleware.progress(), httpError, middleware.observable({
98
108
  implementation: rxjs.Observable
99
109
  })]);
100
110
  function httpRequest(options) {
@@ -107,6 +117,13 @@ function defineHttpRequest(envMiddleware) {
107
117
  httpRequest.defaultRequester = request;
108
118
  return httpRequest;
109
119
  }
120
+ function shouldRetry(err, attempt, options) {
121
+ const isSafe = options.method === "GET" || options.method === "HEAD";
122
+ const isQuery = options.uri.startsWith("/data/query");
123
+ const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
124
+ if ((isSafe || isQuery) && isRetriableResponse) return true;
125
+ return middleware.retry.shouldRetry(err, attempt, options);
126
+ }
110
127
  const projectHeader = "X-Sanity-Project-ID";
111
128
  function requestOptions(config) {
112
129
  let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -203,12 +220,12 @@ const requestTag = tag => {
203
220
  }
204
221
  return tag;
205
222
  };
206
- const encodeQueryString = _ref => {
223
+ const encodeQueryString = _ref2 => {
207
224
  let {
208
225
  query,
209
226
  params = {},
210
227
  options = {}
211
- } = _ref;
228
+ } = _ref2;
212
229
  const searchParams = new URLSearchParams();
213
230
  const {
214
231
  tag,
@@ -730,9 +747,18 @@ function _delete(client, httpRequest, selection, options) {
730
747
  }, options);
731
748
  }
732
749
  function _mutate(client, httpRequest, mutations, options) {
733
- const mut = mutations instanceof Patch || mutations instanceof ObservablePatch || mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mutations.serialize() : mutations;
750
+ let mut;
751
+ if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
752
+ mut = {
753
+ patch: mutations.serialize()
754
+ };
755
+ } else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
756
+ mut = mutations.serialize();
757
+ } else {
758
+ mut = mutations;
759
+ }
734
760
  const muts = Array.isArray(mut) ? mut : [mut];
735
- const transactionId = options && options.transactionId;
761
+ const transactionId = options && options.transactionId || void 0;
736
762
  return _dataRequest(client, httpRequest, "mutate", {
737
763
  mutations: muts,
738
764
  transactionId
@@ -990,10 +1016,10 @@ once(function () {
990
1016
  }
991
1017
  return console.warn(message.join(" "), ...args);
992
1018
  });
993
- 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."]);
1019
+ 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."]);
994
1020
  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.")]);
995
1021
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
996
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
1022
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
997
1023
  const defaultCdnHost = "apicdn.sanity.io";
998
1024
  const defaultConfig = {
999
1025
  apiHost: "https://api.sanity.io",
@@ -1044,7 +1070,7 @@ const initConfig = (config, prevConfig) => {
1044
1070
  }
1045
1071
  newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1046
1072
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1047
- newConfig.useCdn = Boolean(newConfig.useCdn) && !newConfig.withCredentials;
1073
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1048
1074
  validateApiVersion(newConfig.apiVersion);
1049
1075
  const hostParts = newConfig.apiHost.split("://", 2);
1050
1076
  const protocol = hostParts[0];
@@ -1721,21 +1747,24 @@ const _SanityClient = class {
1721
1747
  return new Transaction(operations, this);
1722
1748
  }
1723
1749
  /**
1724
- * DEPRECATED: Perform an HTTP request against the Sanity API
1750
+ * Perform a request against the Sanity API
1751
+ * NOTE: Only use this for Sanity API endpoints, not for your own APIs!
1725
1752
  *
1726
- * @deprecated Use your own request library!
1727
1753
  * @param options - Request options
1754
+ * @returns Promise resolving to the response body
1728
1755
  */
1729
1756
  request(options) {
1730
1757
  return rxjs.lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
1731
1758
  }
1732
1759
  /**
1733
- * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1760
+ * Perform an HTTP request a `/data` sub-endpoint
1761
+ * NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
1734
1762
  *
1735
- * @deprecated Use your own request library!
1763
+ * @deprecated - Use `request()` or your own HTTP library instead
1736
1764
  * @param endpoint - Endpoint to hit (mutate, query etc)
1737
1765
  * @param body - Request body
1738
1766
  * @param options - Request options
1767
+ * @internal
1739
1768
  */
1740
1769
  dataRequest(endpoint, body, options) {
1741
1770
  return rxjs.lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
@@ -1762,9 +1791,12 @@ const _SanityClient = class {
1762
1791
  let SanityClient = _SanityClient;
1763
1792
  _clientConfig2 = new WeakMap();
1764
1793
  _httpRequest2 = new WeakMap();
1765
- const httpRequest = defineHttpRequest(envMiddleware);
1794
+ const httpRequest = defineHttpRequest(envMiddleware, {});
1766
1795
  const requester = httpRequest.defaultRequester;
1767
- const createClient = config => new SanityClient(httpRequest, config);
1796
+ const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {
1797
+ maxRetries: config.maxRetries,
1798
+ retryDelay: config.retryDelay
1799
+ }), config);
1768
1800
  function deprecatedCreateClient(config) {
1769
1801
  printNoDefaultExport();
1770
1802
  return new SanityClient(httpRequest, config);