@sanity/client 5.0.0-esm.8 → 5.0.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 +247 -118
- package/dist/index.browser.cjs +84 -162
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +84 -162
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +85 -163
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +168 -169
- package/dist/index.js +85 -163
- package/dist/index.js.map +1 -1
- package/package.json +23 -24
- package/src/SanityClient.ts +97 -98
- package/src/assets/AssetsClient.ts +33 -8
- package/src/config.ts +1 -0
- package/src/data/dataMethods.ts +30 -28
- package/src/data/encodeQueryString.ts +17 -16
- package/src/data/listen.ts +13 -13
- package/src/data/patch.ts +17 -28
- package/src/data/transaction.ts +15 -12
- package/src/datasets/DatasetsClient.ts +1 -1
- package/src/generateHelpUrl.ts +1 -1
- package/src/http/errors.ts +9 -11
- package/src/http/request.ts +5 -5
- package/src/http/requestOptions.ts +4 -2
- package/src/projects/ProjectsClient.ts +1 -1
- package/src/types.ts +36 -30
- package/src/users/UsersClient.ts +1 -1
- package/src/util/defaults.ts +4 -2
- package/src/util/once.ts +5 -3
- package/src/util/pick.ts +4 -2
- package/src/validators.ts +4 -4
- package/src/warnings.ts +3 -2
- package/umd/sanityClient.js +111 -414
- package/umd/sanityClient.min.js +3 -3
- package/src/auth/AuthClient.ts +0 -67
package/README.md
CHANGED
|
@@ -5,65 +5,111 @@
|
|
|
5
5
|
[![gzip size][gzip-badge]][bundlephobia]
|
|
6
6
|
[![size][size-badge]][bundlephobia]
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
JavaScript client for Sanity. Works in modern browsers, as well as runtimes like [Node.js], [Bun], [Deno], and [Edge Runtime]
|
|
9
|
+
|
|
10
|
+
## QuickStart
|
|
11
|
+
|
|
12
|
+
Install the client with a package manager:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @sanity/client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Import and create a new client instance, and use its methods to interact with your project's [Content Lake]. Below are some simple examples in plain JavaScript. Read further for more comprehensive documentation.
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
// sanity.js
|
|
22
|
+
import {createClient} from '@sanity/client'
|
|
23
|
+
// Import using ESM URL imports in environments that supports it:
|
|
24
|
+
// import {createClient} from 'https://esm.sh/@sanity/client'
|
|
25
|
+
|
|
26
|
+
export const client = createClient({
|
|
27
|
+
projectId: 'your-project-id',
|
|
28
|
+
dataset: 'your-dataset-name',
|
|
29
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
30
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
31
|
+
// token: process.env.SANITY_SECRET_TOKEN // Only if you want to update content with the client
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// uses GROQ to query content: https://www.sanity.io/docs/groq
|
|
35
|
+
export async function getPosts() {
|
|
36
|
+
const posts = await client.fetch('*[_type == "post"]')
|
|
37
|
+
return posts
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function createPost(post: Post) {
|
|
41
|
+
const result = client.create(post)
|
|
42
|
+
return result
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function updateDocumentTitle(_id, title) {
|
|
46
|
+
const result = client.patch(_id).set({title})
|
|
47
|
+
return result
|
|
48
|
+
}
|
|
49
|
+
```
|
|
9
50
|
|
|
10
51
|
# Table of contents<!-- omit in toc -->
|
|
11
52
|
|
|
12
|
-
- [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
- [
|
|
55
|
-
- [From `v4`](#from-v4)
|
|
53
|
+
- [QuickStart](#quickstart)
|
|
54
|
+
- [Requirements](#requirements)
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [API](#api)
|
|
57
|
+
- [Creating a client instance](#creating-a-client-instance)
|
|
58
|
+
- [ESM](#esm)
|
|
59
|
+
- [CommonJS](#commonjs)
|
|
60
|
+
- [TypeScript](#typescript)
|
|
61
|
+
- [Bun](#bun)
|
|
62
|
+
- [Deno](#deno)
|
|
63
|
+
- [Edge Runtime](#edge-runtime)
|
|
64
|
+
- [Browser ESM CDN](#browser-esm-cdn)
|
|
65
|
+
- [UMD](#umd)
|
|
66
|
+
- [Specifying API version](#specifying-api-version)
|
|
67
|
+
- [Performing queries](#performing-queries)
|
|
68
|
+
- [Listening to queries](#listening-to-queries)
|
|
69
|
+
- [Fetch a single document](#fetch-a-single-document)
|
|
70
|
+
- [Fetch multiple documents in one go](#fetch-multiple-documents-in-one-go)
|
|
71
|
+
- [Creating documents](#creating-documents)
|
|
72
|
+
- [Creating/replacing documents](#creatingreplacing-documents)
|
|
73
|
+
- [Creating if not already present](#creating-if-not-already-present)
|
|
74
|
+
- [Patch/update a document](#patchupdate-a-document)
|
|
75
|
+
- [Setting a field only if not already present](#setting-a-field-only-if-not-already-present)
|
|
76
|
+
- [Removing/unsetting fields](#removingunsetting-fields)
|
|
77
|
+
- [Incrementing/decrementing numbers](#incrementingdecrementing-numbers)
|
|
78
|
+
- [Patch a document only if revision matches](#patch-a-document-only-if-revision-matches)
|
|
79
|
+
- [Adding elements to an array](#adding-elements-to-an-array)
|
|
80
|
+
- [Appending/prepending elements to an array](#appendingprepending-elements-to-an-array)
|
|
81
|
+
- [Deleting an element from an array](#deleting-an-element-from-an-array)
|
|
82
|
+
- [Delete documents](#delete-documents)
|
|
83
|
+
- [Multiple mutations in a transaction](#multiple-mutations-in-a-transaction)
|
|
84
|
+
- [Clientless patches \& transactions](#clientless-patches--transactions)
|
|
85
|
+
- [Uploading assets](#uploading-assets)
|
|
86
|
+
- [Examples: Uploading assets from Node.js](#examples-uploading-assets-from-nodejs)
|
|
87
|
+
- [Examples: Uploading assets from the Browser](#examples-uploading-assets-from-the-browser)
|
|
88
|
+
- [Examples: Specify image metadata to extract](#examples-specify-image-metadata-to-extract)
|
|
89
|
+
- [Deleting an asset](#deleting-an-asset)
|
|
90
|
+
- [Mutation options](#mutation-options)
|
|
91
|
+
- [Get client configuration](#get-client-configuration)
|
|
92
|
+
- [Set client configuration](#set-client-configuration)
|
|
93
|
+
- [Release new version](#release-new-version)
|
|
94
|
+
- [License](#license)
|
|
95
|
+
- [From `v4`](#from-v4)
|
|
56
96
|
|
|
57
97
|
## Requirements
|
|
58
98
|
|
|
59
|
-
Sanity Client transpiles syntax for [modern browsers]. The JavaScript runtime must support ES6 features such as [class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) and more.
|
|
99
|
+
Sanity Client transpiles syntax for [modern browsers]. The JavaScript runtime must support ES6 features such as [class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) and more. Most modern web frameworks, [browsers][modern browsers], and developer tooling supports ES6 today.
|
|
100
|
+
|
|
101
|
+
[For legacy ES5 environments we recommend v4.](https://github.com/sanity-io/client/tree/v4.0.0#sanityclient)
|
|
60
102
|
|
|
61
103
|
## Installation
|
|
62
104
|
|
|
63
|
-
The client can be installed from npm:
|
|
105
|
+
The client can be installed from [npm]:
|
|
64
106
|
|
|
65
|
-
```
|
|
107
|
+
```sh
|
|
66
108
|
npm install @sanity/client
|
|
109
|
+
|
|
110
|
+
# Alternative package managers
|
|
111
|
+
yarn add @sanity/client
|
|
112
|
+
pnpm install @sanity/client
|
|
67
113
|
```
|
|
68
114
|
|
|
69
115
|
## API
|
|
@@ -72,7 +118,7 @@ npm install @sanity/client
|
|
|
72
118
|
|
|
73
119
|
`const client = createClient(options)`
|
|
74
120
|
|
|
75
|
-
Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. Setting a value for `useCdn` is encouraged.
|
|
121
|
+
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].
|
|
76
122
|
|
|
77
123
|
#### [ESM](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)
|
|
78
124
|
|
|
@@ -81,47 +127,73 @@ import {createClient} from '@sanity/client'
|
|
|
81
127
|
|
|
82
128
|
const client = createClient({
|
|
83
129
|
projectId: 'your-project-id',
|
|
84
|
-
dataset: '
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
130
|
+
dataset: 'your-dataset-name',
|
|
131
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
132
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
88
133
|
})
|
|
89
134
|
|
|
90
|
-
|
|
135
|
+
const data = await client.fetch(`count(*)`)
|
|
136
|
+
console.log(`Number of documents: ${data}`)
|
|
91
137
|
```
|
|
92
138
|
|
|
93
|
-
#### [CommonJS]
|
|
139
|
+
#### [CommonJS]
|
|
94
140
|
|
|
95
141
|
```js
|
|
96
142
|
const {createClient} = require('@sanity/client')
|
|
97
143
|
|
|
98
144
|
const client = createClient({
|
|
99
145
|
projectId: 'your-project-id',
|
|
100
|
-
dataset: '
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
146
|
+
dataset: 'your-dataset-name',
|
|
147
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
148
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
104
149
|
})
|
|
105
150
|
|
|
106
|
-
|
|
151
|
+
client
|
|
152
|
+
.fetch(`count(*)`)
|
|
153
|
+
.then((data) => console.log(`Number of documents: ${data}`))
|
|
154
|
+
.catch(console.error)
|
|
107
155
|
```
|
|
108
156
|
|
|
109
|
-
#### [TypeScript]
|
|
157
|
+
#### [TypeScript]
|
|
110
158
|
|
|
111
159
|
```ts
|
|
112
160
|
import {createClient, type ClientConfig} from '@sanity/client'
|
|
113
161
|
|
|
114
|
-
const
|
|
162
|
+
const config: ClientConfig = {
|
|
115
163
|
projectId: 'your-project-id',
|
|
116
|
-
dataset: '
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
164
|
+
dataset: 'your-dataset-name',
|
|
165
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
166
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
120
167
|
}
|
|
168
|
+
const client = createClient(config)
|
|
169
|
+
|
|
170
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
171
|
+
// data is typed as `number`
|
|
172
|
+
console.log(`Number of documents: ${data}`)
|
|
173
|
+
```
|
|
121
174
|
|
|
122
|
-
|
|
175
|
+
We're currently exploring typed GROQ queries that are runtime safe, and will share more when we've landed on a solution we're satisifed with.
|
|
176
|
+
Until then you can achieve this using [Zod]:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import {createClient} from '@sanity/client'
|
|
180
|
+
import {z} from 'zod'
|
|
181
|
+
|
|
182
|
+
const client = createClient({
|
|
183
|
+
projectId: 'your-project-id',
|
|
184
|
+
dataset: 'your-dataset-name',
|
|
185
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
186
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
const schema = z.number()
|
|
190
|
+
const data = schema.parse(await client.fetch(`count(*)`))
|
|
191
|
+
// data is guaranteed to be `number`, or zod will throw an error
|
|
192
|
+
console.log(`Number of documents: ${data}`)
|
|
123
193
|
```
|
|
124
194
|
|
|
195
|
+
Another alternative is [groqd].
|
|
196
|
+
|
|
125
197
|
#### [Bun]
|
|
126
198
|
|
|
127
199
|
```bash
|
|
@@ -136,13 +208,12 @@ import {createClient} from '@sanity/client'
|
|
|
136
208
|
|
|
137
209
|
const client = createClient({
|
|
138
210
|
projectId: 'your-project-id',
|
|
139
|
-
dataset: '
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
211
|
+
dataset: 'your-dataset-name',
|
|
212
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
213
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
143
214
|
})
|
|
144
215
|
|
|
145
|
-
const data = await client.fetch(`count(*
|
|
216
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
146
217
|
|
|
147
218
|
console.write(`Number of documents: ${data}`)
|
|
148
219
|
```
|
|
@@ -165,13 +236,12 @@ import {createClient} from 'https://esm.sh/@sanity/client'
|
|
|
165
236
|
|
|
166
237
|
const client = createClient({
|
|
167
238
|
projectId: 'your-project-id',
|
|
168
|
-
dataset: '
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
239
|
+
dataset: 'your-dataset-name',
|
|
240
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
241
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
172
242
|
})
|
|
173
243
|
|
|
174
|
-
const data = await client.fetch(`count(*
|
|
244
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
175
245
|
|
|
176
246
|
console.log(`Number of documents: ${data}`)
|
|
177
247
|
```
|
|
@@ -199,23 +269,18 @@ export const config = {
|
|
|
199
269
|
export default async function handler(req: NextRequest) {
|
|
200
270
|
const client = createClient({
|
|
201
271
|
projectId: 'your-project-id',
|
|
202
|
-
dataset: '
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
272
|
+
dataset: 'your-dataset-name',
|
|
273
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
274
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
206
275
|
})
|
|
207
276
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
'content-type': 'application/json',
|
|
216
|
-
},
|
|
217
|
-
}
|
|
218
|
-
)
|
|
277
|
+
const count = await client.fetch<number>(`count(*)`)
|
|
278
|
+
return new Response(JSON.stringify({count}), {
|
|
279
|
+
status: 200,
|
|
280
|
+
headers: {
|
|
281
|
+
'content-type': 'application/json',
|
|
282
|
+
},
|
|
283
|
+
})
|
|
219
284
|
}
|
|
220
285
|
```
|
|
221
286
|
|
|
@@ -235,13 +300,12 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
|
|
|
235
300
|
|
|
236
301
|
const client = createClient({
|
|
237
302
|
projectId: 'your-project-id',
|
|
238
|
-
dataset: '
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
303
|
+
dataset: 'your-dataset-name',
|
|
304
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
305
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
242
306
|
})
|
|
243
307
|
|
|
244
|
-
const data = await client.fetch(`count(*
|
|
308
|
+
const data = await client.fetch(`count(*)`)
|
|
245
309
|
document.getElementById('results').innerText = `Number of documents: ${data}`
|
|
246
310
|
</script>
|
|
247
311
|
<div id="results"></div>
|
|
@@ -250,18 +314,17 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
|
|
|
250
314
|
Or from anywhere using a dynamic `import()`:
|
|
251
315
|
|
|
252
316
|
```js
|
|
253
|
-
// You can run this snippet from your
|
|
317
|
+
// You can run this snippet from your browser DevTools console.
|
|
254
318
|
// Super handy when you're quickly testing out queries.
|
|
255
319
|
const {createClient} = await import('https://esm.sh/@sanity/client')
|
|
256
320
|
const client = createClient({
|
|
257
321
|
projectId: 'your-project-id',
|
|
258
|
-
dataset: '
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
322
|
+
dataset: 'your-dataset-name',
|
|
323
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
324
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
262
325
|
})
|
|
263
326
|
|
|
264
|
-
const data = await client.fetch(`count(*
|
|
327
|
+
const data = await client.fetch(`count(*)`)
|
|
265
328
|
console.log(`Number of documents: ${data}`)
|
|
266
329
|
```
|
|
267
330
|
|
|
@@ -278,13 +341,12 @@ Loading the UMD script creates a `SanityClient` global that have the same export
|
|
|
278
341
|
|
|
279
342
|
const client = createClient({
|
|
280
343
|
projectId: 'your-project-id',
|
|
281
|
-
dataset: '
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
344
|
+
dataset: 'your-dataset-name',
|
|
345
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
346
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
285
347
|
})
|
|
286
348
|
|
|
287
|
-
client.fetch(`count(*
|
|
349
|
+
client.fetch(`count(*)`).then((data) => console.log(`Number of documents: ${data}`))
|
|
288
350
|
</script>
|
|
289
351
|
```
|
|
290
352
|
|
|
@@ -300,13 +362,12 @@ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` sim
|
|
|
300
362
|
|
|
301
363
|
const client = createClient({
|
|
302
364
|
projectId: 'your-project-id',
|
|
303
|
-
dataset: '
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
useCdn: true, // `false` if you want to ensure fresh data
|
|
365
|
+
dataset: 'your-dataset-name',
|
|
366
|
+
useCdn: false, // set to `true` to fetch from edge cache
|
|
367
|
+
apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
|
|
307
368
|
})
|
|
308
369
|
|
|
309
|
-
const data = await client.fetch(`count(*
|
|
370
|
+
const data = await client.fetch(`count(*)`)
|
|
310
371
|
$('#results').text(`Number of documents: ${data}`)
|
|
311
372
|
})()
|
|
312
373
|
</script>
|
|
@@ -314,7 +375,7 @@ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` sim
|
|
|
314
375
|
|
|
315
376
|
### Specifying API version
|
|
316
377
|
|
|
317
|
-
Sanity uses ISO dates (YYYY-MM-DD) in UTC timezone for versioning. The explanation for this can be found [in the documentation]
|
|
378
|
+
Sanity uses ISO dates (YYYY-MM-DD) in UTC timezone for versioning. The explanation for this can be found [in the documentation][api-versioning]
|
|
318
379
|
|
|
319
380
|
In general, unless you know what API version you want to use, you'll want to statically set it to today's UTC date when starting a new project. By doing this, you'll get all the latest bugfixes and features, while locking the API to prevent breaking changes.
|
|
320
381
|
|
|
@@ -410,7 +471,7 @@ client.create(doc).then((res) => {
|
|
|
410
471
|
})
|
|
411
472
|
```
|
|
412
473
|
|
|
413
|
-
`client.create(doc)`
|
|
474
|
+
`client.create(doc)`
|
|
414
475
|
`client.create(doc, mutationOptions)`
|
|
415
476
|
|
|
416
477
|
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.
|
|
@@ -432,7 +493,7 @@ client.createOrReplace(doc).then((res) => {
|
|
|
432
493
|
})
|
|
433
494
|
```
|
|
434
495
|
|
|
435
|
-
`client.createOrReplace(doc)`
|
|
496
|
+
`client.createOrReplace(doc)`
|
|
436
497
|
`client.createOrReplace(doc, mutationOptions)`
|
|
437
498
|
|
|
438
499
|
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.
|
|
@@ -452,7 +513,7 @@ client.createIfNotExists(doc).then((res) => {
|
|
|
452
513
|
})
|
|
453
514
|
```
|
|
454
515
|
|
|
455
|
-
`client.createIfNotExists(doc)`
|
|
516
|
+
`client.createIfNotExists(doc)`
|
|
456
517
|
`client.createIfNotExists(doc, mutationOptions)`
|
|
457
518
|
|
|
458
519
|
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.
|
|
@@ -561,7 +622,7 @@ client.patch('bike-123').unset(reviewsToRemove).commit()
|
|
|
561
622
|
|
|
562
623
|
A single document can be deleted by specifying a document ID:
|
|
563
624
|
|
|
564
|
-
`client.delete(docId)`
|
|
625
|
+
`client.delete(docId)`
|
|
565
626
|
`client.delete(docId, mutationOptions)`
|
|
566
627
|
|
|
567
628
|
```js
|
|
@@ -1096,6 +1157,65 @@ client.createIfNotExists(doc, options)
|
|
|
1096
1157
|
client.createOrReplace(doc, options)
|
|
1097
1158
|
```
|
|
1098
1159
|
|
|
1160
|
+
### `client.patch.replace` is removed, replace with `client.createOrReplace`<!-- omit in toc -->
|
|
1161
|
+
|
|
1162
|
+
Before:
|
|
1163
|
+
|
|
1164
|
+
```ts
|
|
1165
|
+
import createClient from '@sanity/client'
|
|
1166
|
+
const client = createClient()
|
|
1167
|
+
|
|
1168
|
+
client.patch('tropic-hab').replace({name: 'Tropical Habanero', ingredients: []}).commit()
|
|
1169
|
+
```
|
|
1170
|
+
|
|
1171
|
+
After:
|
|
1172
|
+
|
|
1173
|
+
```ts
|
|
1174
|
+
import {createClient} from '@sanity/client'
|
|
1175
|
+
const client = createClient()
|
|
1176
|
+
|
|
1177
|
+
client.createOrReplace({
|
|
1178
|
+
_id: 'tropic-hab',
|
|
1179
|
+
_type: 'hotsauce',
|
|
1180
|
+
name: 'Tropical Habanero',
|
|
1181
|
+
ingredients: [],
|
|
1182
|
+
})
|
|
1183
|
+
```
|
|
1184
|
+
|
|
1185
|
+
### `client.auth` is removed, replace with `client.request`<!-- omit in toc -->
|
|
1186
|
+
|
|
1187
|
+
Before:
|
|
1188
|
+
|
|
1189
|
+
```ts
|
|
1190
|
+
import createClient from '@sanity/client'
|
|
1191
|
+
const client = createClient()
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* Fetch available login providers
|
|
1195
|
+
*/
|
|
1196
|
+
const loginProviders = await client.auth.getLoginProviders()
|
|
1197
|
+
/**
|
|
1198
|
+
* Revoke the configured session/token
|
|
1199
|
+
*/
|
|
1200
|
+
await client.auth.logout()
|
|
1201
|
+
```
|
|
1202
|
+
|
|
1203
|
+
After:
|
|
1204
|
+
|
|
1205
|
+
```ts
|
|
1206
|
+
import {createclient, type AuthProviderResponse} from '@sanity/client'
|
|
1207
|
+
const client = createClient()
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* Fetch available login providers
|
|
1211
|
+
*/
|
|
1212
|
+
const loginProviders = await client.request<AuthProviderResponse>({uri: '/auth/providers'})
|
|
1213
|
+
/**
|
|
1214
|
+
* Revoke the configured session/token
|
|
1215
|
+
*/
|
|
1216
|
+
await client.request<void>({uri: '/auth/logout', method: 'POST'})
|
|
1217
|
+
```
|
|
1218
|
+
|
|
1099
1219
|
[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
|
|
1100
1220
|
[Deno]: https://deno.land/
|
|
1101
1221
|
[Edge Runtime]: https://edge-runtime.vercel.sh/
|
|
@@ -1105,3 +1225,12 @@ client.createOrReplace(doc, options)
|
|
|
1105
1225
|
[unpkg-dist]: https://unpkg.com/@sanity/client/umd/
|
|
1106
1226
|
[bundlephobia]: https://bundlephobia.com/package/@sanity/client
|
|
1107
1227
|
[esm.sh]: https://esm.sh
|
|
1228
|
+
[Node.js]: https://nodejs.org/en/
|
|
1229
|
+
[Content Lake]: https://www.sanity.io/docs/datastore
|
|
1230
|
+
[npm]: https://npmjs.com
|
|
1231
|
+
[api-cdn]: https://www.sanity.io/docs/api-cdn
|
|
1232
|
+
[CommonJS]: https://nodejs.org/api/modules.html#modules-commonjs-modules
|
|
1233
|
+
[TypeScript]: https://www.typescriptlang.org/
|
|
1234
|
+
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1235
|
+
[zod]: https://zod.dev/
|
|
1236
|
+
[groqd]: https://github.com/FormidableLabs/groqd#readme
|