@sanity/client 6.9.0 → 6.9.2-canary.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.
Files changed (35) hide show
  1. package/README.md +125 -10
  2. package/dist/_chunks/{browserMiddleware-qjpIf6pt.js → browserMiddleware-MafB5TmI.js} +1 -4
  3. package/dist/_chunks/browserMiddleware-MafB5TmI.js.map +1 -0
  4. package/dist/_chunks/{browserMiddleware-QFfeiNj_.cjs → browserMiddleware-yPyURfGu.cjs} +1 -4
  5. package/dist/_chunks/browserMiddleware-yPyURfGu.cjs.map +1 -0
  6. package/dist/_chunks/{nodeMiddleware-1sT0QRFz.cjs → nodeMiddleware-GPc7PMSW.cjs} +2 -5
  7. package/dist/_chunks/nodeMiddleware-GPc7PMSW.cjs.map +1 -0
  8. package/dist/_chunks/{nodeMiddleware-FR4SCkjH.js → nodeMiddleware-q6sQDOEp.js} +2 -5
  9. package/dist/_chunks/nodeMiddleware-q6sQDOEp.js.map +1 -0
  10. package/dist/index.browser.cjs +1 -1
  11. package/dist/index.browser.js +2 -2
  12. package/dist/index.cjs +1 -1
  13. package/dist/index.js +2 -2
  14. package/dist/stega.browser.cjs +7 -28
  15. package/dist/stega.browser.cjs.map +1 -1
  16. package/dist/stega.browser.js +8 -29
  17. package/dist/stega.browser.js.map +1 -1
  18. package/dist/stega.cjs +7 -28
  19. package/dist/stega.cjs.map +1 -1
  20. package/dist/stega.d.ts +1183 -1163
  21. package/dist/stega.js +8 -29
  22. package/dist/stega.js.map +1 -1
  23. package/package.json +14 -15
  24. package/src/data/dataMethods.ts +0 -5
  25. package/src/stega/SanityStegaClient.ts +23 -37
  26. package/src/stega/config.ts +1 -22
  27. package/src/stega/index.browser.ts +1 -0
  28. package/src/stega/index.ts +1 -0
  29. package/src/stega/shared.ts +12 -0
  30. package/umd/sanityClient.js +0 -3
  31. package/umd/sanityClient.min.js +3 -3
  32. package/dist/_chunks/browserMiddleware-QFfeiNj_.cjs.map +0 -1
  33. package/dist/_chunks/browserMiddleware-qjpIf6pt.js.map +0 -1
  34. package/dist/_chunks/nodeMiddleware-1sT0QRFz.cjs.map +0 -1
  35. package/dist/_chunks/nodeMiddleware-FR4SCkjH.js.map +0 -1
package/README.md CHANGED
@@ -606,13 +606,11 @@ Which changes the result to be:
606
606
 
607
607
  Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.
608
608
 
609
- > **Note**
609
+ > [!IMPORTANT]
610
610
  >
611
- > [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]
611
+ > Content Source Maps are supported in the Content Lake API versions `2021-03-25` and later.
612
612
 
613
- 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.
614
-
615
- Read the [Content Source Maps introduction][content-source-maps-intro] before diving in, and keep the [Content Source Maps reference][content-source-maps] handy.
613
+ Before diving in, review the [Content Source Maps introduction][content-source-maps-intro] and keep the [Content Source Maps reference][content-source-maps] within reach for a quick lookup.
616
614
 
617
615
  Enabling Content Source Maps is a two-step process:
618
616
 
@@ -625,7 +623,7 @@ Enabling Content Source Maps is a two-step process:
625
623
  projectId: 'your-project-id',
626
624
  dataset: 'your-dataset-name',
627
625
  useCdn: true, // set to `false` to bypass the edge cache
628
- apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
626
+ apiVersion: '2021-03-25', // use current date (YYYY-MM-DD) to target the latest API version
629
627
  resultSourceMap: true, // tells the API to start sending source maps, if available
630
628
  })
631
629
  ```
@@ -642,7 +640,8 @@ Enabling Content Source Maps is a two-step process:
642
640
  console.log(resultSourceMap)
643
641
  ```
644
642
 
645
- 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:
643
+ If your `apiVersion` is `2021-03-25` or later, the `resultSourceMap` property will always exist in the response after enabling it. If there is no source map, `resultSourceMap` is an empty object.
644
+ This is the corresponding TypeScript definition:
646
645
 
647
646
  ```ts
648
647
  import type {ContentSourceMapping} from '@sanity/client'
@@ -656,6 +655,125 @@ function useContentSourceMap(resultSourceMap: ContentSourceMapping): unknown {
656
655
  useContentSourceMap(resultSourceMap)
657
656
  ```
658
657
 
658
+ #### Using [Visual editing][visual-editing] with steganography
659
+
660
+ A turnkey integration with [Visual editing][visual-editing] is available in [`@sanity/client/stega`]. It creates edit intent links for all the string values in your query result, using [steganography](https://npmjs.com/package/@vercel/stega) under the hood.
661
+
662
+ ```ts
663
+ import {createClient} from '@sanity/client/stega'
664
+
665
+ const client = createClient({
666
+ // ...base config options
667
+ stega: {
668
+ // If you use Vercel Visual Editing, we recommend enabling it for Preview deployments
669
+ enabled: process.env.VERCEL_ENV === 'preview',
670
+ // Required: Set it to the relative or absolute URL of your Sanity Studio instance
671
+ studioUrl: '/studio', // or 'https://your-project-name.sanity.studio'
672
+ // To resolve Cross Dataset References, pass a function returning a URL
673
+ studioUrl: (sourceDocument: ContentSourceMapDocument | ContentSourceMapRemoteDocument) => {
674
+ // If `sourceDocument` has a projectId and a dataset, then it's a Cross Dataset Reference
675
+ if (source._projectId && source._dataset) {
676
+ return 'https://acme-global.sanity.studio'
677
+ }
678
+ return 'https://acme-store.sanity.studio'
679
+ },
680
+ // If your Studio has Workspaces: https://www.sanity.io/docs/workspaces
681
+ // and if your Cross Dataset References are available in a workspace, you can return an object to let the client set up the URL
682
+ studioUrl: (sourceDocument) => {
683
+ // This organization has a single studio with everything organized in workspaces
684
+ const baseUrl = 'https://acme.sanity.studio'
685
+ // If `sourceDocument` has a projectId and a dataset, then it's a Cross Dataset Reference
686
+ if (source._projectId && source._dataset) {
687
+ return {baseUrl, workspace: 'global'}
688
+ }
689
+ return {baseUrl, workspace: 'store'}
690
+ },
691
+
692
+ // Optional, to control which fields have stega payloads
693
+ filter: (props) => {
694
+ const {resultPath, sourcePath, sourceDocument, value} = props
695
+ if (sourcePath[0] === 'externalurl') {
696
+ return false
697
+ }
698
+ // The default behavior is packaged into `filterDefault`, allowing you to enable encoding fields that are skipped by default
699
+ return props.filterDefault(props)
700
+ },
701
+
702
+ // Optional, to log what's encoded and what isn't
703
+ // logger: console,
704
+ },
705
+ })
706
+
707
+ // Disable on demand
708
+ client.config({stega: {enabled: false}})
709
+
710
+ // New client with different stega settings
711
+ const debugClient = client.withConfig({
712
+ stega: {studioUrl: 'https://your-project-name.sanity.studio', logger: console},
713
+ })
714
+ ```
715
+
716
+ Removing stega from part of the result:
717
+
718
+ ```ts
719
+ import {vercelStegaCleanAll} from '@sanity/client/stega'
720
+ const result = await client.fetch('*[_type == "video"][0]')
721
+
722
+ // Remove stega from the payload sent to a third party library
723
+ const videoAsset = vercelStegaCleanAll(result.videoAsset)
724
+ ```
725
+
726
+ #### Creating Studio edit intent links
727
+
728
+ If you want to create an edit link to something that isn't a string, or a field that isn't rendered directly, like a `slug` used in a URL but not rendered on the page, you can use the `resolveEditUrl` function.
729
+
730
+ ```ts
731
+ import {createClient} from '@sanity/client' // or '@sanity/client/stega'
732
+ import {resolveEditUrl} from '@sanity/client/csm'
733
+
734
+ const client = createClient({
735
+ // ... standard client config
736
+ // Required: the new 'withKeyArraySelector' option is used here instead of 'true' so that links to array items and portable text are stable even if the array is reordered
737
+ resultSourceMap: 'withKeyArraySelector',
738
+ })
739
+ const {result, resultSourceMap} = await client.fetch(
740
+ `*[_type == "author" && slug.current == $slug][0]{name, pictures}`,
741
+ {slug: 'john-doe'},
742
+ // Required, otherwise you can't access `resultSourceMap`
743
+ {filterResponse: false},
744
+ )
745
+
746
+ // The `result` looks like this:
747
+ const result = {
748
+ name: 'John Doe',
749
+ pictures: [
750
+ {
751
+ _type: 'image',
752
+ alt: 'A picture of exactly what you think someone named John Doe would look like',
753
+ _key: 'cee5fbb69da2',
754
+ asset: {
755
+ _ref: 'image-a75b03fdd5b5fa36947bf2b776a542e0c940f682-1000x1500-jpg',
756
+ _type: 'reference',
757
+ },
758
+ },
759
+ ],
760
+ }
761
+
762
+ const studioUrl = 'https://your-project-name.sanity.studio'
763
+
764
+ resolveEditUrl({
765
+ // The URL resolver requires the base URL of your Sanity Studio instance
766
+ studioUrl,
767
+ // It also requires a Content Source Map for the query result you want to create an edit intent link for
768
+ resultSourceMap,
769
+ // The path to the field you want to edit. You can pass a string
770
+ resultPath: 'pictures[0].alt',
771
+ // or an array of segments
772
+ resultPath: ['pictures', 0, 'alt'],
773
+ })
774
+ // ^? 'https://your-project-name.sanity.studio/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=author;path=pictures[_key=="cee5fbb69da2"].alt'
775
+ ```
776
+
659
777
  ### Listening to queries
660
778
 
661
779
  ```js
@@ -1490,6 +1608,3 @@ await client.request<void>({uri: '/auth/logout', method: 'POST'})
1490
1608
  [visual-editing]: https://www.sanity.io/docs/vercel-visual-editing?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1491
1609
  [content-source-maps]: https://www.sanity.io/docs/content-source-maps?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1492
1610
  [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
1493
- [preview-kit-client]: https://github.com/sanity-io/preview-kit#sanitypreview-kitclient
1494
- [sales-cta]: https://www.sanity.io/contact/sales?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
1495
- [enterprise-cta]: https://www.sanity.io/enterprise?utm_source=github.com&utm_medium=referral&utm_campaign=may-vercel-launch
@@ -816,9 +816,6 @@ const indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
816
816
  const getQuerySizeLimit = 11264;
817
817
  function _fetch(client, httpRequest, query, params) {
818
818
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
819
- if ("stega" in options && options["stega"] !== void 0 && options["stega"] !== false) {
820
- throw new Error("It looks like you're using options meant for '@sanity/client/stega'. Make sure you're using the right import. Or set 'stega' in 'fetch' to 'false'.");
821
- }
822
819
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
823
820
  const {
824
821
  cache,
@@ -1862,4 +1859,4 @@ function defineCreateClientExports(envMiddleware, ClassConstructor) {
1862
1859
  }
1863
1860
  var envMiddleware = [];
1864
1861
  export { BasePatch, BaseTransaction, ClientError, ObservablePatch, ObservableSanityClient, ObservableTransaction, Patch, SanityClient, ServerError, Transaction, defaultConfig, defineCreateClientExports, envMiddleware, printNoDefaultExport };
1865
- //# sourceMappingURL=browserMiddleware-qjpIf6pt.js.map
1862
+ //# sourceMappingURL=browserMiddleware-MafB5TmI.js.map