@sanity/client 5.0.0-esm.4 → 5.0.0-esm.6

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
@@ -1,67 +1,58 @@
1
- # @sanity/client<!-- omit in toc -->
1
+ # @sanity/client
2
2
 
3
3
  [![npm stat](https://img.shields.io/npm/dm/@sanity/client.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/client)
4
4
  [![npm version](https://img.shields.io/npm/v/@sanity/client.svg?style=flat-square)](https://www.npmjs.com/package/@sanity/client)
5
- [![gzip size][gzip-badge]][unpkg-dist]
6
- [![size][size-badge]][unpkg-dist]
5
+ [![gzip size][gzip-badge]][bundlephobia]
6
+ [![size][size-badge]][bundlephobia]
7
7
 
8
8
  Javascript client for Sanity. Works in [Node.js](https://nodejs.org/en/), [Bun], [Deno], [Edge Runtime] and [modern browsers].
9
9
 
10
- ## Table of contents<!-- omit in toc -->
11
-
12
- - [Requirements](#requirements)
13
- - [Installation](#installation)
14
- - [API](#api)
15
- - [Creating a client instance](#creating-a-client-instance)
16
- - [ESM](#esm)
17
- - [CommonJS](#commonjs)
18
- - [TypeScript](#typescript)
19
- - [Bun](#bun)
20
- - [Specifying API version](#specifying-api-version)
21
- - [Performing queries](#performing-queries)
22
- - [Listening to queries](#listening-to-queries)
23
- - [Fetch a single document](#fetch-a-single-document)
24
- - [Fetch multiple documents in one go](#fetch-multiple-documents-in-one-go)
25
- - [Creating documents](#creating-documents)
26
- - [Creating/replacing documents](#creatingreplacing-documents)
27
- - [Creating if not already present](#creating-if-not-already-present)
28
- - [Patch/update a document](#patchupdate-a-document)
29
- - [Setting a field only if not already present](#setting-a-field-only-if-not-already-present)
30
- - [Removing/unsetting fields](#removingunsetting-fields)
31
- - [Incrementing/decrementing numbers](#incrementingdecrementing-numbers)
32
- - [Patch a document only if revision matches](#patch-a-document-only-if-revision-matches)
33
- - [Adding elements to an array](#adding-elements-to-an-array)
34
- - [Appending/prepending elements to an array](#appendingprepending-elements-to-an-array)
35
- - [Deleting an element from an array](#deleting-an-element-from-an-array)
36
- - [Delete documents](#delete-documents)
37
- - [Multiple mutations in a transaction](#multiple-mutations-in-a-transaction)
38
- - [Clientless patches \& transactions](#clientless-patches--transactions)
39
- - [Uploading assets](#uploading-assets)
40
- - [Examples: Uploading assets from Node.js](#examples-uploading-assets-from-nodejs)
41
- - [Examples: Uploading assets from the Browser](#examples-uploading-assets-from-the-browser)
42
- - [Examples: Specify image metadata to extract](#examples-specify-image-metadata-to-extract)
43
- - [Deleting an asset](#deleting-an-asset)
44
- - [Mutation options](#mutation-options)
45
- - [Get client configuration](#get-client-configuration)
46
- - [Set client configuration](#set-client-configuration)
10
+ # Table of contents<!-- omit in toc -->
11
+
12
+ - [@sanity/client](#sanityclient)
13
+ - [Requirements](#requirements)
14
+ - [Installation](#installation)
15
+ - [API](#api)
16
+ - [Creating a client instance](#creating-a-client-instance)
17
+ - [ESM](#esm)
18
+ - [CommonJS](#commonjs)
19
+ - [TypeScript](#typescript)
20
+ - [Bun](#bun)
21
+ - [Deno](#deno)
22
+ - [Edge Runtime](#edge-runtime)
23
+ - [Browser ESM CDN](#browser-esm-cdn)
24
+ - [UMD](#umd)
25
+ - [Specifying API version](#specifying-api-version)
26
+ - [Performing queries](#performing-queries)
27
+ - [Listening to queries](#listening-to-queries)
28
+ - [Fetch a single document](#fetch-a-single-document)
29
+ - [Fetch multiple documents in one go](#fetch-multiple-documents-in-one-go)
30
+ - [Creating documents](#creating-documents)
31
+ - [Creating/replacing documents](#creatingreplacing-documents)
32
+ - [Creating if not already present](#creating-if-not-already-present)
33
+ - [Patch/update a document](#patchupdate-a-document)
34
+ - [Setting a field only if not already present](#setting-a-field-only-if-not-already-present)
35
+ - [Removing/unsetting fields](#removingunsetting-fields)
36
+ - [Incrementing/decrementing numbers](#incrementingdecrementing-numbers)
37
+ - [Patch a document only if revision matches](#patch-a-document-only-if-revision-matches)
38
+ - [Adding elements to an array](#adding-elements-to-an-array)
39
+ - [Appending/prepending elements to an array](#appendingprepending-elements-to-an-array)
40
+ - [Deleting an element from an array](#deleting-an-element-from-an-array)
41
+ - [Delete documents](#delete-documents)
42
+ - [Multiple mutations in a transaction](#multiple-mutations-in-a-transaction)
43
+ - [Clientless patches \& transactions](#clientless-patches--transactions)
44
+ - [Uploading assets](#uploading-assets)
45
+ - [Examples: Uploading assets from Node.js](#examples-uploading-assets-from-nodejs)
46
+ - [Examples: Uploading assets from the Browser](#examples-uploading-assets-from-the-browser)
47
+ - [Examples: Specify image metadata to extract](#examples-specify-image-metadata-to-extract)
48
+ - [Deleting an asset](#deleting-an-asset)
49
+ - [Mutation options](#mutation-options)
50
+ - [Get client configuration](#get-client-configuration)
51
+ - [Set client configuration](#set-client-configuration)
52
+ - [Release new version](#release-new-version)
53
+ - [License](#license)
47
54
  - [Migrate](#migrate)
48
55
  - [From `v4`](#from-v4)
49
- - [No longer shipping `ES5`](#no-longer-shipping-es5)
50
- - [Node.js `v12` no longer supported](#nodejs-v12-no-longer-supported)
51
- - [The `default` export is replaced with the named export `createClient`](#the-default-export-is-replaced-with-the-named-export-createclient)
52
- - [`client.assets.delete` is removed](#clientassetsdelete-is-removed)
53
- - [`client.assets.getImageUrl` is removed, replace with `@sanity/image-url`](#clientassetsgetimageurl-is-removed-replace-with-sanityimage-url)
54
- - [`SanityClient` static properties moved to named exports](#sanityclient-static-properties-moved-to-named-exports)
55
- - [`client.clientConfig` is removed, replace with `client.config()`](#clientclientconfig-is-removed-replace-with-clientconfig)
56
- - [`client.getUrl()` is removed](#clientgeturl-is-removed)
57
- - [`client.getDataUrl()` is removed](#clientgetdataurl-is-removed)
58
- - [`client.isPromiseAPI()` is removed, replace with `instanceof` check](#clientispromiseapi-is-removed-replace-with-instanceof-check)
59
- - [`client.observable.isObservableAPI()` is removed, replace with `instanceof` check](#clientobservableisobservableapi-is-removed-replace-with-instanceof-check)
60
- - [`client._requestObservable` is removed, replace with `client.observable.request`](#client_requestobservable-is-removed-replace-with-clientobservablerequest)
61
- - [`client._dataRequest` is removed, replace with `client.dataRequest`](#client_datarequest-is-removed-replace-with-clientdatarequest)
62
- - [`client._create_` is removed, replace with one of `client.create`, `client.createIfNotExists` or `client.createOrReplace`](#client_create_-is-removed-replace-with-one-of-clientcreate-clientcreateifnotexists-or-clientcreateorreplace)
63
- - [Release new version](#release-new-version)
64
- - [License](#license)
65
56
 
66
57
  ## Requirements
67
58
 
@@ -87,6 +78,7 @@ Initializes a new Sanity Client. Required options are `projectId`, `dataset`, an
87
78
 
88
79
  ```js
89
80
  import {createClient} from '@sanity/client'
81
+
90
82
  const client = createClient({
91
83
  projectId: 'your-project-id',
92
84
  dataset: 'bikeshop',
@@ -102,6 +94,7 @@ export default client
102
94
 
103
95
  ```js
104
96
  const {createClient} = require('@sanity/client')
97
+
105
98
  const client = createClient({
106
99
  projectId: 'your-project-id',
107
100
  dataset: 'bikeshop',
@@ -132,21 +125,191 @@ export default createClient(config)
132
125
  #### [Bun]
133
126
 
134
127
  ```bash
135
- bun
128
+ bun init
129
+ bun add @sanity/client
130
+ open index.ts
136
131
  ```
137
132
 
138
133
  ```ts
139
- import {createClient, type ClientConfig} from '@sanity/client'
134
+ // index.ts
135
+ import {createClient} from '@sanity/client'
140
136
 
141
- const client: ClientConfig = {
137
+ const client = createClient({
142
138
  projectId: 'your-project-id',
143
139
  dataset: 'bikeshop',
144
140
  apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
145
141
  token: 'sanity-auth-token', // or leave blank for unauthenticated usage
146
142
  useCdn: true, // `false` if you want to ensure fresh data
143
+ })
144
+
145
+ const data = await client.fetch(`count(*[])`)
146
+
147
+ console.write(`Number of documents: ${data}`)
148
+ ```
149
+
150
+ ```bash
151
+ bun run index.ts
152
+ # Number of documents ${number}
153
+ ```
154
+
155
+ #### [Deno]
156
+
157
+ ```bash
158
+ deno init
159
+ open main.ts
160
+ ```
161
+
162
+ ```ts
163
+ // main.ts
164
+ import {createClient} from 'https://esm.sh/@sanity/client'
165
+
166
+ const client = createClient({
167
+ projectId: 'your-project-id',
168
+ dataset: 'bikeshop',
169
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
170
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
171
+ useCdn: true, // `false` if you want to ensure fresh data
172
+ })
173
+
174
+ const data = await client.fetch(`count(*[])`)
175
+
176
+ console.log(`Number of documents: ${data}`)
177
+ ```
178
+
179
+ ```bash
180
+ deno run --allow-net --allow-env main.ts
181
+ # Number of documents ${number}
182
+ ```
183
+
184
+ #### [Edge Runtime]
185
+
186
+ ```bash
187
+ npm install next
188
+ ```
189
+
190
+ ```ts
191
+ // pages/api/total.ts
192
+ import {createClient} from '@sanity/client'
193
+ import type {NextRequest} from 'next/server'
194
+
195
+ export const config = {
196
+ runtime: 'edge',
147
197
  }
148
198
 
149
- export default createClient(config)
199
+ export default async function handler(req: NextRequest) {
200
+ const client = createClient({
201
+ projectId: 'your-project-id',
202
+ dataset: 'bikeshop',
203
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
204
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
205
+ useCdn: true, // `false` if you want to ensure fresh data
206
+ })
207
+
208
+ return new Response(
209
+ JSON.stringify({
210
+ count: await client.fetch(`count(*[])`),
211
+ }),
212
+ {
213
+ status: 200,
214
+ headers: {
215
+ 'content-type': 'application/json',
216
+ },
217
+ }
218
+ )
219
+ }
220
+ ```
221
+
222
+ ```bash
223
+ npx next dev
224
+ # Open http://localhost:3000/api/total
225
+ # {"count": number}
226
+ ```
227
+
228
+ #### Browser ESM CDN
229
+
230
+ Using [esm.sh] you can either load the client using a `<script type="module">` tag:
231
+
232
+ ```html
233
+ <script type="module">
234
+ import {createClient} from 'https://esm.sh/@sanity/client'
235
+
236
+ const client = createClient({
237
+ projectId: 'your-project-id',
238
+ dataset: 'bikeshop',
239
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
240
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
241
+ useCdn: true, // `false` if you want to ensure fresh data
242
+ })
243
+
244
+ const data = await client.fetch(`count(*[])`)
245
+ document.getElementById('results').innerText = `Number of documents: ${data}`
246
+ </script>
247
+ <div id="results"></div>
248
+ ```
249
+
250
+ Or from anywhere using a dynamic `import()`:
251
+
252
+ ```js
253
+ // You can run this snippet from your browwser DevTools console.
254
+ // Super handy when you're quickly testing out queries.
255
+ const {createClient} = await import('https://esm.sh/@sanity/client')
256
+ const client = createClient({
257
+ projectId: 'your-project-id',
258
+ dataset: 'bikeshop',
259
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
260
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
261
+ useCdn: true, // `false` if you want to ensure fresh data
262
+ })
263
+
264
+ const data = await client.fetch(`count(*[])`)
265
+ console.log(`Number of documents: ${data}`)
266
+ ```
267
+
268
+ #### [UMD][unpkg-dist]
269
+
270
+ Loading the UMD script creates a `SanityClient` global that have the same exports as `import * as SanityClient from '@sanity/client'`:
271
+
272
+ ```html
273
+ <script src="https://unpkg.com/@sanity/client"></script>
274
+ <!-- Unminified build for debugging -->
275
+ <!--<script src="https://unpkg.com/@sanity/client/umd/sanityClient.js"></script>-->
276
+ <script>
277
+ const {createClient} = SanityClient
278
+
279
+ const client = createClient({
280
+ projectId: 'your-project-id',
281
+ dataset: 'bikeshop',
282
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
283
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
284
+ useCdn: true, // `false` if you want to ensure fresh data
285
+ })
286
+
287
+ client.fetch(`count(*[])`).then((data) => console.log(`Number of documents: ${data}`))
288
+ </script>
289
+ ```
290
+
291
+ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` similar to how `esm.sh` lets you `import()` anything:
292
+
293
+ ```html
294
+ <div id="results"></div>
295
+ <script src="https://unpkg.com/require-unpkg"></script>
296
+ <script>
297
+ ;(async () => {
298
+ // const {createClient} = await require('@sanity/client')
299
+ const [$, {createClient}] = await require(['jquery', '@sanity/client'])
300
+
301
+ const client = createClient({
302
+ projectId: 'your-project-id',
303
+ dataset: 'bikeshop',
304
+ apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
305
+ token: 'sanity-auth-token', // or leave blank for unauthenticated usage
306
+ useCdn: true, // `false` if you want to ensure fresh data
307
+ })
308
+
309
+ const data = await client.fetch(`count(*[])`)
310
+ $('#results').text(`Number of documents: ${data}`)
311
+ })()
312
+ </script>
150
313
  ```
151
314
 
152
315
  ### Specifying API version
@@ -643,23 +806,30 @@ client.config({dataset: 'newDataset'})
643
806
 
644
807
  Set client configuration. Required options are `projectId` and `dataset`.
645
808
 
646
- ## Migrate
809
+ ## Release new version
647
810
 
648
- ### From `v4`
811
+ Run ["CI & Release" workflow](https://github.com/sanity-io/client/actions/workflows/ci.yml).
812
+ Make sure to select the main branch and check "Release new version".
649
813
 
650
- #### No longer shipping `ES5`
814
+ Semantic release will only release on configured branches, so it is safe to run release on any branch.
651
815
 
652
- The target is changed to [modern browsers] that supports `ES6` `class`, `{...rest}` syntax and more. You may need to update your bundler to a recent major version. Or you could configure your bundler to transpile `@sanity/client`, and `get-it`, which is the engine that powers `@sanity/client` and uses the same output target.
816
+ ## License
653
817
 
654
- ---
818
+ MIT © [Sanity.io](https://www.sanity.io/)
655
819
 
656
- #### Node.js `v12` no longer supported
820
+ # Migrate
657
821
 
658
- Upgrade to the [LTS release, or one of the Maintenance releases](https://github.com/nodejs/release#release-schedule).
822
+ ## From `v4`
823
+
824
+ ### No longer shipping `ES5`<!-- omit in toc -->
659
825
 
660
- ---
826
+ The target is changed to [modern browsers] that supports `ES6` `class`, `{...rest}` syntax and more. You may need to update your bundler to a recent major version. Or you could configure your bundler to transpile `@sanity/client`, and `get-it`, which is the engine that powers `@sanity/client` and uses the same output target.
661
827
 
662
- #### The `default` export is replaced with the named export `createClient`
828
+ ### Node.js `v12` no longer supported<!-- omit in toc -->
829
+
830
+ Upgrade to the [LTS release, or one of the Maintenance releases](https://github.com/nodejs/release#release-schedule).
831
+
832
+ ### The `default` export is replaced with the named export `createClient`<!-- omit in toc -->
663
833
 
664
834
  Before:
665
835
 
@@ -680,9 +850,7 @@ import {createClient} from '@sanity/client'
680
850
  const client = createClient()
681
851
  ```
682
852
 
683
- ---
684
-
685
- #### `client.assets.delete` is removed
853
+ ### `client.assets.delete` is removed<!-- omit in toc -->
686
854
 
687
855
  Before:
688
856
 
@@ -698,9 +866,7 @@ After:
698
866
  client.delete('image-abc123_foobar-123x123-png')
699
867
  ```
700
868
 
701
- ---
702
-
703
- #### `client.assets.getImageUrl` is removed, replace with [`@sanity/image-url`](https://github.com/sanity-io/image-url)
869
+ ### `client.assets.getImageUrl` is removed, replace with [`@sanity/image-url`](https://github.com/sanity-io/image-url)<!-- omit in toc -->
704
870
 
705
871
  Before:
706
872
 
@@ -731,7 +897,7 @@ urlFor({_ref: 'image-abc123_foobar-123x123-png'}).url()
731
897
  urlFor({_ref: 'image-abc123_foobar-123x123-png'}).auto('format').url()
732
898
  ```
733
899
 
734
- #### `SanityClient` static properties moved to named exports
900
+ ### `SanityClient` static properties moved to named exports<!-- omit in toc -->
735
901
 
736
902
  Before:
737
903
 
@@ -747,9 +913,7 @@ After:
747
913
  import {Patch, Transaction, ClientError, ServerError, requester} from '@sanity/client'
748
914
  ```
749
915
 
750
- ---
751
-
752
- #### `client.clientConfig` is removed, replace with `client.config()`
916
+ ### `client.clientConfig` is removed, replace with `client.config()`<!-- omit in toc -->
753
917
 
754
918
  Before:
755
919
 
@@ -769,9 +933,7 @@ const client = createClient()
769
933
  console.log(client.config().projectId)
770
934
  ```
771
935
 
772
- ---
773
-
774
- #### `client.getUrl()` is removed
936
+ ### `client.getUrl()` is removed<!-- omit in toc -->
775
937
 
776
938
  Before:
777
939
 
@@ -799,9 +961,7 @@ console.log(getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar')
799
961
  console.log(getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar')
800
962
  ```
801
963
 
802
- ---
803
-
804
- #### `client.getDataUrl()` is removed
964
+ ### `client.getDataUrl()` is removed<!-- omit in toc -->
805
965
 
806
966
  Before:
807
967
 
@@ -830,9 +990,7 @@ console.log(getDataUrl('doc') === '/data/doc/bikeshop')
830
990
  console.log(getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123')
831
991
  ```
832
992
 
833
- ---
834
-
835
- #### `client.isPromiseAPI()` is removed, replace with `instanceof` check
993
+ ### `client.isPromiseAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
836
994
 
837
995
  Before:
838
996
 
@@ -854,9 +1012,7 @@ const client = createClient()
854
1012
  console.log(client instanceof SanityClient)
855
1013
  ```
856
1014
 
857
- ---
858
-
859
- #### `client.observable.isObservableAPI()` is removed, replace with `instanceof` check
1015
+ ### `client.observable.isObservableAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
860
1016
 
861
1017
  Before:
862
1018
 
@@ -876,9 +1032,7 @@ const client = createClient()
876
1032
  console.log(client.observable instanceof ObservableSanityClient)
877
1033
  ```
878
1034
 
879
- ---
880
-
881
- #### `client._requestObservable` is removed, replace with `client.observable.request`
1035
+ ### `client._requestObservable` is removed, replace with `client.observable.request`<!-- omit in toc -->
882
1036
 
883
1037
  Before:
884
1038
 
@@ -898,9 +1052,7 @@ const client = createClient()
898
1052
  client.observable.request({uri: '/ping'}).subscribe()
899
1053
  ```
900
1054
 
901
- ---
902
-
903
- #### `client._dataRequest` is removed, replace with `client.dataRequest`
1055
+ ### `client._dataRequest` is removed, replace with `client.dataRequest`<!-- omit in toc -->
904
1056
 
905
1057
  Before:
906
1058
 
@@ -920,9 +1072,7 @@ const client = createClient()
920
1072
  client.dataRequest(endpoint, body, options)
921
1073
  ```
922
1074
 
923
- ---
924
-
925
- #### `client._create_` is removed, replace with one of `client.create`, `client.createIfNotExists` or `client.createOrReplace`
1075
+ ### `client._create_` is removed, replace with one of `client.create`, `client.createIfNotExists` or `client.createOrReplace`<!-- omit in toc -->
926
1076
 
927
1077
  Before:
928
1078
 
@@ -946,19 +1096,6 @@ client.createIfNotExists(doc, options)
946
1096
  client.createOrReplace(doc, options)
947
1097
  ```
948
1098
 
949
- ---
950
-
951
- ## Release new version
952
-
953
- Run ["CI & Release" workflow](https://github.com/sanity-io/client/actions/workflows/ci.yml).
954
- Make sure to select the main branch and check "Release new version".
955
-
956
- Semantic release will only release on configured branches, so it is safe to run release on any branch.
957
-
958
- ## License
959
-
960
- MIT © [Sanity.io](https://www.sanity.io/)
961
-
962
1099
  [modern browsers]: https://browsersl.ist/#q=%3E+0.2%25+and+supports+es6-module+and+supports+es6-module-dynamic-import+and+not+dead+and+not+IE+11
963
1100
  [Deno]: https://deno.land/
964
1101
  [Edge Runtime]: https://edge-runtime.vercel.sh/
@@ -966,3 +1103,5 @@ MIT © [Sanity.io](https://www.sanity.io/)
966
1103
  [gzip-badge]: https://img.shields.io/bundlephobia/minzip/@sanity/client?label=gzip%20size&style=flat-square
967
1104
  [size-badge]: https://img.shields.io/bundlephobia/min/@sanity/client?label=size&style=flat-square
968
1105
  [unpkg-dist]: https://unpkg.com/@sanity/client/umd/
1106
+ [bundlephobia]: https://bundlephobia.com/package/@sanity/client
1107
+ [esm.sh]: https://esm.sh