@sanity/client 3.2.2 → 4.0.0-alpha.esm.2
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 +43 -26
- package/dist/browser/sanityClient.js +956 -0
- package/dist/node/sanityClient.js +969 -0
- package/lib/assets/assetsClient.js +132 -106
- package/lib/auth/authClient.js +38 -19
- package/lib/config.js +25 -13
- package/lib/data/dataMethods.js +35 -30
- package/lib/data/encodeQueryString.js +8 -2
- package/lib/data/listen.js +25 -21
- package/lib/data/patch.js +169 -118
- package/lib/data/transaction.js +134 -88
- package/lib/datasets/datasetsClient.js +63 -32
- package/lib/http/browserMiddleware.js +6 -1
- package/lib/http/errors.js +12 -8
- package/lib/http/nodeMiddleware.js +16 -9
- package/lib/http/queryString.js +9 -2
- package/lib/http/request.js +27 -24
- package/lib/http/requestOptions.js +10 -6
- package/lib/projects/projectsClient.js +37 -18
- package/lib/sanityClient.js +143 -105
- package/lib/users/usersClient.js +28 -11
- package/lib/util/defaults.js +12 -4
- package/lib/util/getSelection.js +7 -2
- package/lib/util/observable.js +24 -13
- package/lib/util/once.js +9 -2
- package/lib/util/pick.js +9 -2
- package/lib/validators.js +38 -15
- package/lib/warnings.js +14 -6
- package/package.json +22 -3
- package/sanityClient.d.ts +3 -13
- package/src/assets/assetsClient.js +132 -0
- package/src/auth/authClient.js +13 -0
- package/src/config.js +93 -0
- package/src/data/dataMethods.js +182 -0
- package/src/data/encodeQueryString.js +18 -0
- package/src/data/listen.js +159 -0
- package/src/data/patch.js +119 -0
- package/src/data/transaction.js +103 -0
- package/src/datasets/datasetsClient.js +28 -0
- package/src/http/browserMiddleware.js +1 -0
- package/src/http/errors.js +53 -0
- package/src/http/nodeMiddleware.js +11 -0
- package/src/http/queryString.js +10 -0
- package/src/http/request.js +50 -0
- package/src/http/requestOptions.js +29 -0
- package/src/projects/projectsClient.js +13 -0
- package/src/sanityClient.js +124 -0
- package/src/users/usersClient.js +9 -0
- package/src/util/defaults.js +9 -0
- package/src/util/getSelection.js +17 -0
- package/src/util/observable.js +6 -0
- package/src/util/once.js +12 -0
- package/src/util/pick.js +9 -0
- package/src/validators.js +76 -0
- package/src/warnings.js +25 -0
- package/test/client.test.js +188 -41
- package/test/encodeQueryString.test.js +3 -1
- package/test/helpers/sseServer.js +2 -1
- package/test/listen.test.js +4 -2
- package/test/warnings.test.disabled.js +8 -4
- package/umd/sanityClient.js +38 -38
- package/umd/sanityClient.min.js +1 -1
package/README.md
CHANGED
|
@@ -19,8 +19,8 @@ npm install -g @sanity/client
|
|
|
19
19
|
## API
|
|
20
20
|
|
|
21
21
|
```js
|
|
22
|
-
|
|
23
|
-
const client =
|
|
22
|
+
import {createClient} from '@sanity/client'
|
|
23
|
+
const client = createClient({
|
|
24
24
|
projectId: 'your-project-id',
|
|
25
25
|
dataset: 'bikeshop',
|
|
26
26
|
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
@@ -29,7 +29,7 @@ const client = sanityClient({
|
|
|
29
29
|
})
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
`const client =
|
|
32
|
+
`const client = createClient(options)`
|
|
33
33
|
|
|
34
34
|
Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. Setting a value for `useCdn` is encouraged.
|
|
35
35
|
|
|
@@ -131,11 +131,12 @@ client.create(doc).then((res) => {
|
|
|
131
131
|
})
|
|
132
132
|
```
|
|
133
133
|
|
|
134
|
-
`client.create(doc)`
|
|
134
|
+
`client.create(doc)`
|
|
135
|
+
`client.create(doc, mutationOptions)`
|
|
135
136
|
|
|
136
137
|
Create a document. Argument is a plain JS object representing the document. It must contain a `_type` attribute. It _may_ contain an `_id`. If an ID is not specified, it will automatically be created.
|
|
137
138
|
|
|
138
|
-
To create a draft document,
|
|
139
|
+
To create a draft document, prefix the document ID with `drafts.` - eg `_id: 'drafts.myDocumentId'`. To auto-generate a draft document ID, set `_id` to `drafts.` (nothing after the `.`).
|
|
139
140
|
|
|
140
141
|
### Creating/replacing documents
|
|
141
142
|
|
|
@@ -152,7 +153,8 @@ client.createOrReplace(doc).then((res) => {
|
|
|
152
153
|
})
|
|
153
154
|
```
|
|
154
155
|
|
|
155
|
-
`client.createOrReplace(doc)`
|
|
156
|
+
`client.createOrReplace(doc)`
|
|
157
|
+
`client.createOrReplace(doc, mutationOptions)`
|
|
156
158
|
|
|
157
159
|
If you are not sure whether or not a document exists but want to overwrite it if it does, you can use the `createOrReplace()` method. When using this method, the document must contain an `_id` attribute.
|
|
158
160
|
|
|
@@ -171,7 +173,8 @@ client.createIfNotExists(doc).then((res) => {
|
|
|
171
173
|
})
|
|
172
174
|
```
|
|
173
175
|
|
|
174
|
-
`client.createIfNotExists(doc)`
|
|
176
|
+
`client.createIfNotExists(doc)`
|
|
177
|
+
`client.createIfNotExists(doc, mutationOptions)`
|
|
175
178
|
|
|
176
179
|
If you want to create a document if it does not already exist, but fall back without error if it does, you can use the `createIfNotExists()` method. When using this method, the document must contain an `_id` attribute.
|
|
177
180
|
|
|
@@ -194,6 +197,12 @@ client
|
|
|
194
197
|
|
|
195
198
|
Modify a document. `patch` takes a document ID. `set` merges the partialDoc with the stored document. `inc` increments the given field with the given numeric value. `commit` executes the given `patch`. Returns the updated object.
|
|
196
199
|
|
|
200
|
+
```
|
|
201
|
+
client.patch()
|
|
202
|
+
[operations]
|
|
203
|
+
.commit(mutationOptions)
|
|
204
|
+
```
|
|
205
|
+
|
|
197
206
|
### Setting a field only if not already present
|
|
198
207
|
|
|
199
208
|
```js
|
|
@@ -233,19 +242,17 @@ client
|
|
|
233
242
|
The patch operation `insert` takes a location (`before`, `after` or `replace`), a path selector and an array of elements to insert.
|
|
234
243
|
|
|
235
244
|
```js
|
|
236
|
-
const {nanoid} = require('nanoid')
|
|
237
|
-
|
|
238
245
|
client
|
|
239
246
|
.patch('bike-123')
|
|
240
247
|
// Ensure that the `reviews` arrays exists before attempting to add items to it
|
|
241
248
|
.setIfMissing({reviews: []})
|
|
242
249
|
// Add the items after the last item in the array (append)
|
|
243
|
-
.insert('after', 'reviews[-1]', [
|
|
244
|
-
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
250
|
+
.insert('after', 'reviews[-1]', [{title: 'Great bike!', stars: 5}])
|
|
251
|
+
.commit({
|
|
252
|
+
// Adds a `_key` attribute to array items, unique within the array, to
|
|
253
|
+
// ensure it can be addressed uniquely in a real-time collaboration context
|
|
254
|
+
autoGenerateArrayKeys: true,
|
|
255
|
+
})
|
|
249
256
|
```
|
|
250
257
|
|
|
251
258
|
### Appending/prepending elements to an array
|
|
@@ -253,7 +260,7 @@ client
|
|
|
253
260
|
The operations of appending and prepending to an array are so common that they have been given their own methods for better readability:
|
|
254
261
|
|
|
255
262
|
```js
|
|
256
|
-
|
|
263
|
+
import {nanoid} from 'nanoid'
|
|
257
264
|
|
|
258
265
|
client
|
|
259
266
|
.patch('bike-123')
|
|
@@ -277,7 +284,8 @@ client.patch('bike-123').unset(reviewsToRemove).commit()
|
|
|
277
284
|
|
|
278
285
|
A single document can be deleted by specifying a document ID:
|
|
279
286
|
|
|
280
|
-
`client.delete(docId)`
|
|
287
|
+
`client.delete(docId)`
|
|
288
|
+
`client.delete(docId, mutationOptions)`
|
|
281
289
|
|
|
282
290
|
```js
|
|
283
291
|
client
|
|
@@ -296,7 +304,6 @@ One or more documents can be deleted by specifying a GROQ query (and optionally,
|
|
|
296
304
|
|
|
297
305
|
```js
|
|
298
306
|
// Without params
|
|
299
|
-
|
|
300
307
|
client
|
|
301
308
|
.delete({query: '*[_type == "bike"][0]'})
|
|
302
309
|
.then(() => {
|
|
@@ -309,7 +316,6 @@ client
|
|
|
309
316
|
|
|
310
317
|
```js
|
|
311
318
|
// With params
|
|
312
|
-
|
|
313
319
|
client
|
|
314
320
|
.delete({query: '*[_type == $type][0..1]', params: {type: 'bike'}})
|
|
315
321
|
.then(() => {
|
|
@@ -366,27 +372,27 @@ A `patch` can be performed inline on a `transaction`.
|
|
|
366
372
|
Transactions and patches can also be built outside the scope of a client:
|
|
367
373
|
|
|
368
374
|
```js
|
|
369
|
-
|
|
370
|
-
const client =
|
|
375
|
+
import {SanityClient} from '@sanity/client'
|
|
376
|
+
const client = new SanityClient({
|
|
371
377
|
projectId: 'your-project-id',
|
|
372
378
|
dataset: 'bikeshop',
|
|
373
379
|
})
|
|
374
380
|
|
|
375
381
|
// Patches:
|
|
376
|
-
const patch = new
|
|
382
|
+
const patch = new SanityClient.Patch('<documentId>')
|
|
377
383
|
client.mutate(patch.inc({count: 1}).unset(['visits']))
|
|
378
384
|
|
|
379
385
|
// Transactions:
|
|
380
|
-
const transaction = new
|
|
386
|
+
const transaction = new SanityClient.Transaction()
|
|
381
387
|
.create({_id: '123', name: 'FooBike'})
|
|
382
388
|
.delete('someDocId')
|
|
383
389
|
|
|
384
390
|
client.mutate(transaction)
|
|
385
391
|
```
|
|
386
392
|
|
|
387
|
-
`const patch = new
|
|
393
|
+
`const patch = new SanityClient.Patch(docId)`
|
|
388
394
|
|
|
389
|
-
`const transaction = new
|
|
395
|
+
`const transaction = new SanityClient.Transaction()`
|
|
390
396
|
|
|
391
397
|
An important note on this approach is that you cannot call `commit()` on transactions or patches instantiated this way, instead you have to pass them to `client.mutate()`
|
|
392
398
|
|
|
@@ -484,7 +490,7 @@ client.assets
|
|
|
484
490
|
Deleting an asset document will also trigger deletion of the actual asset.
|
|
485
491
|
|
|
486
492
|
```
|
|
487
|
-
client.delete(
|
|
493
|
+
client.delete(assetDocumentId: string): Promise
|
|
488
494
|
```
|
|
489
495
|
|
|
490
496
|
```js
|
|
@@ -493,6 +499,17 @@ client.delete('image-abc123_someAssetId-500x500-png').then((result) => {
|
|
|
493
499
|
})
|
|
494
500
|
```
|
|
495
501
|
|
|
502
|
+
### Mutation options
|
|
503
|
+
|
|
504
|
+
The following options are available for mutations, and can be applied either as the second argument to `create()`, `createOrReplace`, `createIfNotExist`, `delete()` and `mutate()` - or as an argument to the `commit()` method on patches and transactions.
|
|
505
|
+
|
|
506
|
+
- `visibility` (`'sync'|'async'|'deferred'`) - default `'sync'`
|
|
507
|
+
- `sync`: request will not return until the requested changes are visible to subsequent queries.
|
|
508
|
+
- `async`: request will return immediately when the changes have been committed, but it might still be a second or more until changes are reflected in a query. Unless you are immediately re-querying for something that includes the mutated data, this is the preferred choice.
|
|
509
|
+
- `deferred`: fastest way to write - bypasses real-time indexing completely, and should be used in cases where you are bulk importing/mutating a large number of documents and don't need to see that data in a query for tens of seconds.
|
|
510
|
+
- `dryRun` (`true|false`) - default `false`. If true, the mutation will be a dry run - the response will be identical to the one returned had this property been omitted or false (including error responses) but no documents will be affected.
|
|
511
|
+
- `autoGenerateArrayKeys` (`true|false`) - default `false`. If true, the mutation API will automatically add `_key` attributes to objects in arrays that is missing them. This makes array operations more robust by having a unique key within the array available for selections, which helps prevent race conditions in real-time, collaborative editing.
|
|
512
|
+
|
|
496
513
|
### Get client configuration
|
|
497
514
|
|
|
498
515
|
```js
|