@sanity/client 6.0.0 → 6.1.0-perspective.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
@@ -27,7 +27,7 @@ export const client = createClient({
27
27
  projectId: 'your-project-id',
28
28
  dataset: 'your-dataset-name',
29
29
  useCdn: true, // set to `false` to bypass the edge cache
30
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
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
  })
33
33
 
@@ -65,6 +65,7 @@ export async function updateDocumentTitle(_id, title) {
65
65
  - [UMD](#umd)
66
66
  - [Specifying API version](#specifying-api-version)
67
67
  - [Performing queries](#performing-queries)
68
+ - [Fetching Content Source Maps](#fetching-content-source-maps)
68
69
  - [Listening to queries](#listening-to-queries)
69
70
  - [Fetch a single document](#fetch-a-single-document)
70
71
  - [Fetch multiple documents in one go](#fetch-multiple-documents-in-one-go)
@@ -132,7 +133,7 @@ const client = createClient({
132
133
  projectId: 'your-project-id',
133
134
  dataset: 'your-dataset-name',
134
135
  useCdn: true, // set to `false` to bypass the edge cache
135
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
136
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
136
137
  })
137
138
 
138
139
  const data = await client.fetch(`count(*)`)
@@ -148,7 +149,7 @@ const client = createClient({
148
149
  projectId: 'your-project-id',
149
150
  dataset: 'your-dataset-name',
150
151
  useCdn: true, // set to `false` to bypass the edge cache
151
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
152
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
152
153
  })
153
154
 
154
155
  client
@@ -166,7 +167,7 @@ const config: ClientConfig = {
166
167
  projectId: 'your-project-id',
167
168
  dataset: 'your-dataset-name',
168
169
  useCdn: true, // set to `false` to bypass the edge cache
169
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
170
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
170
171
  }
171
172
  const client = createClient(config)
172
173
 
@@ -186,7 +187,7 @@ const client = createClient({
186
187
  projectId: 'your-project-id',
187
188
  dataset: 'your-dataset-name',
188
189
  useCdn: true, // set to `false` to bypass the edge cache
189
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
190
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
190
191
  })
191
192
 
192
193
  const schema = z.number()
@@ -213,7 +214,7 @@ const client = createClient({
213
214
  projectId: 'your-project-id',
214
215
  dataset: 'your-dataset-name',
215
216
  useCdn: true, // set to `false` to bypass the edge cache
216
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
217
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
217
218
  })
218
219
 
219
220
  const data = await client.fetch<number>(`count(*)`)
@@ -241,7 +242,7 @@ const client = createClient({
241
242
  projectId: 'your-project-id',
242
243
  dataset: 'your-dataset-name',
243
244
  useCdn: true, // set to `false` to bypass the edge cache
244
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
245
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
245
246
  })
246
247
 
247
248
  const data = await client.fetch<number>(`count(*)`)
@@ -274,7 +275,7 @@ export default async function handler(req: NextRequest) {
274
275
  projectId: 'your-project-id',
275
276
  dataset: 'your-dataset-name',
276
277
  useCdn: true, // set to `false` to bypass the edge cache
277
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
278
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
278
279
  })
279
280
 
280
281
  const count = await client.fetch<number>(`count(*)`)
@@ -305,7 +306,7 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
305
306
  projectId: 'your-project-id',
306
307
  dataset: 'your-dataset-name',
307
308
  useCdn: true, // set to `false` to bypass the edge cache
308
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
309
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
309
310
  })
310
311
 
311
312
  const data = await client.fetch(`count(*)`)
@@ -324,7 +325,7 @@ const client = createClient({
324
325
  projectId: 'your-project-id',
325
326
  dataset: 'your-dataset-name',
326
327
  useCdn: true, // set to `false` to bypass the edge cache
327
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
328
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
328
329
  })
329
330
 
330
331
  const data = await client.fetch(`count(*)`)
@@ -346,7 +347,7 @@ Loading the UMD script creates a `SanityClient` global that have the same export
346
347
  projectId: 'your-project-id',
347
348
  dataset: 'your-dataset-name',
348
349
  useCdn: true, // set to `false` to bypass the edge cache
349
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
350
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
350
351
  })
351
352
 
352
353
  client.fetch(`count(*)`).then((data) => console.log(`Number of documents: ${data}`))
@@ -367,7 +368,7 @@ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` sim
367
368
  projectId: 'your-project-id',
368
369
  dataset: 'your-dataset-name',
369
370
  useCdn: true, // set to `false` to bypass the edge cache
370
- apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
371
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
371
372
  })
372
373
 
373
374
  const data = await client.fetch(`count(*)`)
@@ -404,6 +405,60 @@ client.fetch(query, params).then((bikes) => {
404
405
 
405
406
  Perform a query using the given parameters (if any).
406
407
 
408
+ ### Fetching Content Source Maps
409
+
410
+ Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.
411
+
412
+ > **Note**
413
+ >
414
+ > [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]
415
+
416
+ A high level API using Content Source Maps for [Visual editing][visual-editing] is also available in [`@sanity/preview-kit/client`][preview-kit-client]. It offers both a turn-key solution and a flexible API for custom experiences.
417
+
418
+ Read the [Content Source Maps introduction][content-source-maps-intro] before diving in, and keep the [Content Source Maps reference][content-source-maps] handy.
419
+
420
+ Enabling Content Source Maps is a two-step process:
421
+
422
+ 1. Update your client configuration with `resultSourceMap`.
423
+
424
+ ```js
425
+ import {createClient} from '@sanity/client'
426
+
427
+ const client = createClient({
428
+ projectId: 'your-project-id',
429
+ dataset: 'your-dataset-name',
430
+ useCdn: true, // set to `false` to bypass the edge cache
431
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
432
+ resultSourceMap: true, // tells the API to start sending source maps, if available
433
+ })
434
+ ```
435
+
436
+ 2. On `client.fetch` calls add `{filterResponse: false}` to return the full response on queries.
437
+
438
+ ```js
439
+ // Before
440
+ // const result = await client.fetch(query, params)
441
+
442
+ // After adding `filterResponse: false`
443
+ const {result, resultSourceMap} = await client.fetch(query, params, {filterResponse: false})
444
+ // Build something cool with the source map
445
+ console.log(resultSourceMap)
446
+ ```
447
+
448
+ Once enabled, the `resultSourceMap` property will always exist on the response, given your `apiVersion` is recent enough. If there is no source map, it will be an empty object. There's also a TypeScript definition for it:
449
+
450
+ ```ts
451
+ import type {ContentSourceMapping} from '@sanity/client'
452
+
453
+ const {result, resultSourceMap} = await client.fetch(query, params, {filterResponse: false})
454
+
455
+ function useContentSourceMap(resultSourceMap: ContentSourceMapping): unknown {
456
+ // Sky's the limit
457
+ }
458
+
459
+ useContentSourceMap(resultSourceMap)
460
+ ```
461
+
407
462
  ### Listening to queries
408
463
 
409
464
  ```js
@@ -1242,3 +1297,9 @@ await client.request<void>({uri: '/auth/logout', method: 'POST'})
1242
1297
  [groqd]: https://github.com/FormidableLabs/groqd#readme
1243
1298
  [AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
1244
1299
  [AbortController]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
1300
+ [visual-editing]: https://www.sanity.io/docs/vercel-visual-editing?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1301
+ [content-source-maps]: https://www.sanity.io/docs/content-source-maps?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1302
+ [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
1303
+ [preview-kit-client]: https://github.com/sanity-io/preview-kit#sanitypreview-kitclient
1304
+ [sales-cta]: https://www.sanity.io/contact/sales?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1305
+ [enterprise-cta]: https://www.sanity.io/enterprise?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
@@ -834,6 +834,12 @@ function _requestObservable(client, httpRequest, options) {
834
834
  ...options.query
835
835
  };
836
836
  }
837
+ if (config.perspective === "previewDrafts") {
838
+ options.query = {
839
+ perspective: config.perspective,
840
+ ...options.query
841
+ };
842
+ }
837
843
  if (config.resultSourceMap) {
838
844
  options.query = {
839
845
  resultSourceMap: true,