@sanity/client 5.0.0-esm.0 → 5.0.0-esm.10
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 +639 -22
- package/dist/index.browser.cjs +610 -185
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +611 -185
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +611 -186
- package/dist/index.cjs.js +0 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +570 -501
- package/dist/index.js +612 -186
- package/dist/index.js.map +1 -1
- package/package.json +35 -32
- package/src/SanityClient.ts +295 -341
- package/src/assets/AssetsClient.ts +18 -15
- package/src/auth/AuthClient.ts +47 -23
- package/src/config.ts +3 -2
- package/src/data/dataMethods.ts +60 -30
- package/src/data/encodeQueryString.ts +4 -4
- package/src/data/listen.ts +19 -18
- package/src/data/patch.ts +50 -46
- package/src/data/transaction.ts +20 -12
- package/src/datasets/DatasetsClient.ts +75 -57
- package/src/generateHelpUrl.ts +1 -1
- package/src/http/errors.ts +9 -7
- package/src/http/request.ts +5 -7
- package/src/http/requestOptions.ts +4 -2
- package/src/index.browser.ts +3 -3
- package/src/index.ts +3 -42
- package/src/projects/ProjectsClient.ts +39 -23
- package/src/types.ts +96 -30
- package/src/users/UsersClient.ts +37 -28
- 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 +7 -5
- package/src/warnings.ts +2 -1
- package/umd/sanityClient.js +4816 -5179
- package/umd/sanityClient.min.js +12 -12
package/README.md
CHANGED
|
@@ -1,26 +1,131 @@
|
|
|
1
1
|
# @sanity/client
|
|
2
2
|
|
|
3
|
+
[](https://npm-stat.com/charts.html?package=@sanity/client)
|
|
3
4
|
[](https://www.npmjs.com/package/@sanity/client)
|
|
5
|
+
[![gzip size][gzip-badge]][bundlephobia]
|
|
6
|
+
[![size][size-badge]][bundlephobia]
|
|
4
7
|
|
|
5
|
-
|
|
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
|
+
```
|
|
50
|
+
|
|
51
|
+
# Table of contents<!-- omit in toc -->
|
|
52
|
+
|
|
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)
|
|
6
96
|
|
|
7
97
|
## Requirements
|
|
8
98
|
|
|
9
|
-
Sanity Client
|
|
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 you'll need to transpile `@sanity/client` and its `dependencies` with your own bundler, and have a global ES6-compliant `Promise` available. If your runtime environment doesn't provide a spec compliant `Promise` implementation, we recommend using [native-promise-only](https://www.npmjs.com/package/native-promise-only), [es6-promise](https://www.npmjs.com/package/es6-promise) or another [spec-compliant](https://promisesaplus.com/implementations) implementation. See [this article](https://www.sanity.io/help/js-client-promise-polyfill) for more information.
|
|
10
102
|
|
|
11
103
|
## Installation
|
|
12
104
|
|
|
13
|
-
The client can be installed from npm:
|
|
105
|
+
The client can be installed from [npm]:
|
|
14
106
|
|
|
15
|
-
```
|
|
16
|
-
npm install
|
|
107
|
+
```sh
|
|
108
|
+
npm install @sanity/client
|
|
109
|
+
|
|
110
|
+
# Alternative package managers
|
|
111
|
+
yarn add @sanity/client
|
|
112
|
+
pnpm install @sanity/client
|
|
17
113
|
```
|
|
18
114
|
|
|
19
115
|
## API
|
|
20
116
|
|
|
117
|
+
### Creating a client instance
|
|
118
|
+
|
|
119
|
+
`const client = createClient(options)`
|
|
120
|
+
|
|
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].
|
|
122
|
+
|
|
123
|
+
#### [ESM](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)
|
|
124
|
+
|
|
21
125
|
```js
|
|
22
|
-
|
|
23
|
-
|
|
126
|
+
import {createClient} from '@sanity/client'
|
|
127
|
+
|
|
128
|
+
const client = createClient({
|
|
24
129
|
projectId: 'your-project-id',
|
|
25
130
|
dataset: 'bikeshop',
|
|
26
131
|
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
@@ -31,13 +136,231 @@ const client = sanityClient({
|
|
|
31
136
|
export default client
|
|
32
137
|
```
|
|
33
138
|
|
|
34
|
-
|
|
139
|
+
#### [CommonJS]
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
const {createClient} = require('@sanity/client')
|
|
143
|
+
|
|
144
|
+
const client = createClient({
|
|
145
|
+
projectId: 'your-project-id',
|
|
146
|
+
dataset: 'bikeshop',
|
|
147
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
148
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
149
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
module.exports = client
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### [TypeScript]
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import {createClient, type ClientConfig} from '@sanity/client'
|
|
159
|
+
|
|
160
|
+
const client: ClientConfig = {
|
|
161
|
+
projectId: 'your-project-id',
|
|
162
|
+
dataset: 'bikeshop',
|
|
163
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
164
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
165
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export default createClient(config)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### [Bun]
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
bun init
|
|
175
|
+
bun add @sanity/client
|
|
176
|
+
open index.ts
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// index.ts
|
|
181
|
+
import {createClient} from '@sanity/client'
|
|
182
|
+
|
|
183
|
+
const client = createClient({
|
|
184
|
+
projectId: 'your-project-id',
|
|
185
|
+
dataset: 'bikeshop',
|
|
186
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
187
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
188
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
const data = await client.fetch(`count(*[])`)
|
|
192
|
+
|
|
193
|
+
console.write(`Number of documents: ${data}`)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
bun run index.ts
|
|
198
|
+
# Number of documents ${number}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### [Deno]
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
deno init
|
|
205
|
+
open main.ts
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
// main.ts
|
|
210
|
+
import {createClient} from 'https://esm.sh/@sanity/client'
|
|
211
|
+
|
|
212
|
+
const client = createClient({
|
|
213
|
+
projectId: 'your-project-id',
|
|
214
|
+
dataset: 'bikeshop',
|
|
215
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
216
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
217
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
const data = await client.fetch(`count(*[])`)
|
|
221
|
+
|
|
222
|
+
console.log(`Number of documents: ${data}`)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
deno run --allow-net --allow-env main.ts
|
|
227
|
+
# Number of documents ${number}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### [Edge Runtime]
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm install next
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
// pages/api/total.ts
|
|
238
|
+
import {createClient} from '@sanity/client'
|
|
239
|
+
import type {NextRequest} from 'next/server'
|
|
240
|
+
|
|
241
|
+
export const config = {
|
|
242
|
+
runtime: 'edge',
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export default async function handler(req: NextRequest) {
|
|
246
|
+
const client = createClient({
|
|
247
|
+
projectId: 'your-project-id',
|
|
248
|
+
dataset: 'bikeshop',
|
|
249
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
250
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
251
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
return new Response(
|
|
255
|
+
JSON.stringify({
|
|
256
|
+
count: await client.fetch(`count(*[])`),
|
|
257
|
+
}),
|
|
258
|
+
{
|
|
259
|
+
status: 200,
|
|
260
|
+
headers: {
|
|
261
|
+
'content-type': 'application/json',
|
|
262
|
+
},
|
|
263
|
+
}
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
npx next dev
|
|
270
|
+
# Open http://localhost:3000/api/total
|
|
271
|
+
# {"count": number}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
#### Browser ESM CDN
|
|
275
|
+
|
|
276
|
+
Using [esm.sh] you can either load the client using a `<script type="module">` tag:
|
|
277
|
+
|
|
278
|
+
```html
|
|
279
|
+
<script type="module">
|
|
280
|
+
import {createClient} from 'https://esm.sh/@sanity/client'
|
|
281
|
+
|
|
282
|
+
const client = createClient({
|
|
283
|
+
projectId: 'your-project-id',
|
|
284
|
+
dataset: 'bikeshop',
|
|
285
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
286
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
287
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
const data = await client.fetch(`count(*[])`)
|
|
291
|
+
document.getElementById('results').innerText = `Number of documents: ${data}`
|
|
292
|
+
</script>
|
|
293
|
+
<div id="results"></div>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Or from anywhere using a dynamic `import()`:
|
|
297
|
+
|
|
298
|
+
```js
|
|
299
|
+
// You can run this snippet from your browwser DevTools console.
|
|
300
|
+
// Super handy when you're quickly testing out queries.
|
|
301
|
+
const {createClient} = await import('https://esm.sh/@sanity/client')
|
|
302
|
+
const client = createClient({
|
|
303
|
+
projectId: 'your-project-id',
|
|
304
|
+
dataset: 'bikeshop',
|
|
305
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
306
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
307
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
const data = await client.fetch(`count(*[])`)
|
|
311
|
+
console.log(`Number of documents: ${data}`)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
#### [UMD][unpkg-dist]
|
|
315
|
+
|
|
316
|
+
Loading the UMD script creates a `SanityClient` global that have the same exports as `import * as SanityClient from '@sanity/client'`:
|
|
35
317
|
|
|
36
|
-
|
|
318
|
+
```html
|
|
319
|
+
<script src="https://unpkg.com/@sanity/client"></script>
|
|
320
|
+
<!-- Unminified build for debugging -->
|
|
321
|
+
<!--<script src="https://unpkg.com/@sanity/client/umd/sanityClient.js"></script>-->
|
|
322
|
+
<script>
|
|
323
|
+
const {createClient} = SanityClient
|
|
324
|
+
|
|
325
|
+
const client = createClient({
|
|
326
|
+
projectId: 'your-project-id',
|
|
327
|
+
dataset: 'bikeshop',
|
|
328
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
329
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
330
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
client.fetch(`count(*[])`).then((data) => console.log(`Number of documents: ${data}`))
|
|
334
|
+
</script>
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` similar to how `esm.sh` lets you `import()` anything:
|
|
338
|
+
|
|
339
|
+
```html
|
|
340
|
+
<div id="results"></div>
|
|
341
|
+
<script src="https://unpkg.com/require-unpkg"></script>
|
|
342
|
+
<script>
|
|
343
|
+
;(async () => {
|
|
344
|
+
// const {createClient} = await require('@sanity/client')
|
|
345
|
+
const [$, {createClient}] = await require(['jquery', '@sanity/client'])
|
|
346
|
+
|
|
347
|
+
const client = createClient({
|
|
348
|
+
projectId: 'your-project-id',
|
|
349
|
+
dataset: 'bikeshop',
|
|
350
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
351
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
352
|
+
token: 'sanity-secret-auth-token', // leave blank for unauthenticated usage
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
const data = await client.fetch(`count(*[])`)
|
|
356
|
+
$('#results').text(`Number of documents: ${data}`)
|
|
357
|
+
})()
|
|
358
|
+
</script>
|
|
359
|
+
```
|
|
37
360
|
|
|
38
361
|
### Specifying API version
|
|
39
362
|
|
|
40
|
-
Sanity uses ISO dates (YYYY-MM-DD) in UTC timezone for versioning. The explanation for this can be found [in the documentation]
|
|
363
|
+
Sanity uses ISO dates (YYYY-MM-DD) in UTC timezone for versioning. The explanation for this can be found [in the documentation][api-versioning]
|
|
41
364
|
|
|
42
365
|
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.
|
|
43
366
|
|
|
@@ -133,7 +456,7 @@ client.create(doc).then((res) => {
|
|
|
133
456
|
})
|
|
134
457
|
```
|
|
135
458
|
|
|
136
|
-
`client.create(doc)`
|
|
459
|
+
`client.create(doc)`
|
|
137
460
|
`client.create(doc, mutationOptions)`
|
|
138
461
|
|
|
139
462
|
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.
|
|
@@ -155,7 +478,7 @@ client.createOrReplace(doc).then((res) => {
|
|
|
155
478
|
})
|
|
156
479
|
```
|
|
157
480
|
|
|
158
|
-
`client.createOrReplace(doc)`
|
|
481
|
+
`client.createOrReplace(doc)`
|
|
159
482
|
`client.createOrReplace(doc, mutationOptions)`
|
|
160
483
|
|
|
161
484
|
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.
|
|
@@ -175,7 +498,7 @@ client.createIfNotExists(doc).then((res) => {
|
|
|
175
498
|
})
|
|
176
499
|
```
|
|
177
500
|
|
|
178
|
-
`client.createIfNotExists(doc)`
|
|
501
|
+
`client.createIfNotExists(doc)`
|
|
179
502
|
`client.createIfNotExists(doc, mutationOptions)`
|
|
180
503
|
|
|
181
504
|
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.
|
|
@@ -284,7 +607,7 @@ client.patch('bike-123').unset(reviewsToRemove).commit()
|
|
|
284
607
|
|
|
285
608
|
A single document can be deleted by specifying a document ID:
|
|
286
609
|
|
|
287
|
-
`client.delete(docId)`
|
|
610
|
+
`client.delete(docId)`
|
|
288
611
|
`client.delete(docId, mutationOptions)`
|
|
289
612
|
|
|
290
613
|
```js
|
|
@@ -372,27 +695,25 @@ A `patch` can be performed inline on a `transaction`.
|
|
|
372
695
|
Transactions and patches can also be built outside the scope of a client:
|
|
373
696
|
|
|
374
697
|
```js
|
|
375
|
-
|
|
376
|
-
const client =
|
|
698
|
+
import {createClient, Patch, Transaction} from '@sanity/client'
|
|
699
|
+
const client = createClient({
|
|
377
700
|
projectId: 'your-project-id',
|
|
378
701
|
dataset: 'bikeshop',
|
|
379
702
|
})
|
|
380
703
|
|
|
381
704
|
// Patches:
|
|
382
|
-
const patch = new
|
|
705
|
+
const patch = new Patch('<documentId>')
|
|
383
706
|
client.mutate(patch.inc({count: 1}).unset(['visits']))
|
|
384
707
|
|
|
385
708
|
// Transactions:
|
|
386
|
-
const transaction = new
|
|
387
|
-
.create({_id: '123', name: 'FooBike'})
|
|
388
|
-
.delete('someDocId')
|
|
709
|
+
const transaction = new Transaction().create({_id: '123', name: 'FooBike'}).delete('someDocId')
|
|
389
710
|
|
|
390
711
|
client.mutate(transaction)
|
|
391
712
|
```
|
|
392
713
|
|
|
393
|
-
`const patch = new
|
|
714
|
+
`const patch = new Patch(docId)`
|
|
394
715
|
|
|
395
|
-
`const transaction = new
|
|
716
|
+
`const transaction = new Transaction()`
|
|
396
717
|
|
|
397
718
|
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()`
|
|
398
719
|
|
|
@@ -541,3 +862,299 @@ Semantic release will only release on configured branches, so it is safe to run
|
|
|
541
862
|
## License
|
|
542
863
|
|
|
543
864
|
MIT © [Sanity.io](https://www.sanity.io/)
|
|
865
|
+
|
|
866
|
+
# Migrate
|
|
867
|
+
|
|
868
|
+
## From `v4`
|
|
869
|
+
|
|
870
|
+
### No longer shipping `ES5`<!-- omit in toc -->
|
|
871
|
+
|
|
872
|
+
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.
|
|
873
|
+
|
|
874
|
+
### Node.js `v12` no longer supported<!-- omit in toc -->
|
|
875
|
+
|
|
876
|
+
Upgrade to the [LTS release, or one of the Maintenance releases](https://github.com/nodejs/release#release-schedule).
|
|
877
|
+
|
|
878
|
+
### The `default` export is replaced with the named export `createClient`<!-- omit in toc -->
|
|
879
|
+
|
|
880
|
+
Before:
|
|
881
|
+
|
|
882
|
+
```ts
|
|
883
|
+
import createClient from '@sanity/client'
|
|
884
|
+
const client = createClient()
|
|
885
|
+
```
|
|
886
|
+
|
|
887
|
+
```ts
|
|
888
|
+
import SanityClient from '@sanity/client'
|
|
889
|
+
const client = new SanityClient()
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
After:
|
|
893
|
+
|
|
894
|
+
```ts
|
|
895
|
+
import {createClient} from '@sanity/client'
|
|
896
|
+
const client = createClient()
|
|
897
|
+
```
|
|
898
|
+
|
|
899
|
+
### `client.assets.delete` is removed<!-- omit in toc -->
|
|
900
|
+
|
|
901
|
+
Before:
|
|
902
|
+
|
|
903
|
+
```ts
|
|
904
|
+
client.assets.delete('image', 'abc123_foobar-123x123-png')
|
|
905
|
+
client.assets.delete('image', 'image-abc123_foobar-123x123-png')
|
|
906
|
+
client.assets.delete({_id: 'image-abc123_foobar-123x123-png'})
|
|
907
|
+
```
|
|
908
|
+
|
|
909
|
+
After:
|
|
910
|
+
|
|
911
|
+
```ts
|
|
912
|
+
client.delete('image-abc123_foobar-123x123-png')
|
|
913
|
+
```
|
|
914
|
+
|
|
915
|
+
### `client.assets.getImageUrl` is removed, replace with [`@sanity/image-url`](https://github.com/sanity-io/image-url)<!-- omit in toc -->
|
|
916
|
+
|
|
917
|
+
Before:
|
|
918
|
+
|
|
919
|
+
```ts
|
|
920
|
+
import createClient from '@sanity/client'
|
|
921
|
+
const client = createClient({projectId: 'abc123', dataset: 'foo'})
|
|
922
|
+
|
|
923
|
+
client.assets.getImageUrl('image-abc123_foobar-123x123-png')
|
|
924
|
+
client.assets.getImageUrl('image-abc123_foobar-123x123-png', {auto: 'format'})
|
|
925
|
+
client.assets.getImageUrl({_ref: 'image-abc123_foobar-123x123-png'})
|
|
926
|
+
client.assets.getImageUrl({_ref: 'image-abc123_foobar-123x123-png'}, {auto: 'format'})
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
After:
|
|
930
|
+
|
|
931
|
+
```bash
|
|
932
|
+
npm install @sanity/image-url
|
|
933
|
+
```
|
|
934
|
+
|
|
935
|
+
```ts
|
|
936
|
+
import imageUrlBuilder from '@sanity/image-url'
|
|
937
|
+
const builder = imageUrlBuilder({projectId: 'abc123', dataset: 'foo'})
|
|
938
|
+
const urlFor = (source) => builder.image(source)
|
|
939
|
+
|
|
940
|
+
urlFor('image-abc123_foobar-123x123-png').url()
|
|
941
|
+
urlFor('image-abc123_foobar-123x123-png').auto('format').url()
|
|
942
|
+
urlFor({_ref: 'image-abc123_foobar-123x123-png'}).url()
|
|
943
|
+
urlFor({_ref: 'image-abc123_foobar-123x123-png'}).auto('format').url()
|
|
944
|
+
```
|
|
945
|
+
|
|
946
|
+
### `SanityClient` static properties moved to named exports<!-- omit in toc -->
|
|
947
|
+
|
|
948
|
+
Before:
|
|
949
|
+
|
|
950
|
+
```ts
|
|
951
|
+
import SanityClient from '@sanity/client'
|
|
952
|
+
|
|
953
|
+
const {Patch, Transaction, ClientError, ServerError, requester} = SanityClient
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
After:
|
|
957
|
+
|
|
958
|
+
```ts
|
|
959
|
+
import {Patch, Transaction, ClientError, ServerError, requester} from '@sanity/client'
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
### `client.clientConfig` is removed, replace with `client.config()`<!-- omit in toc -->
|
|
963
|
+
|
|
964
|
+
Before:
|
|
965
|
+
|
|
966
|
+
```ts
|
|
967
|
+
import createClient from '@sanity/client'
|
|
968
|
+
const client = createClient()
|
|
969
|
+
|
|
970
|
+
console.log(client.clientConfig.projectId)
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
After:
|
|
974
|
+
|
|
975
|
+
```ts
|
|
976
|
+
import {createClient} from '@sanity/client'
|
|
977
|
+
const client = createClient()
|
|
978
|
+
|
|
979
|
+
console.log(client.config().projectId)
|
|
980
|
+
```
|
|
981
|
+
|
|
982
|
+
### `client.getUrl()` is removed<!-- omit in toc -->
|
|
983
|
+
|
|
984
|
+
Before:
|
|
985
|
+
|
|
986
|
+
```ts
|
|
987
|
+
import createClient from '@sanity/client'
|
|
988
|
+
const client = createClient({projectId: 'abc123'})
|
|
989
|
+
|
|
990
|
+
console.log(client.getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar')
|
|
991
|
+
console.log(client.getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar')
|
|
992
|
+
```
|
|
993
|
+
|
|
994
|
+
After:
|
|
995
|
+
|
|
996
|
+
```ts
|
|
997
|
+
import {createClient} from '@sanity/client'
|
|
998
|
+
const client = createClient({projectId: 'abc123'})
|
|
999
|
+
|
|
1000
|
+
const getUrl = (uri: string, useCdn = false) => {
|
|
1001
|
+
const config = client.config()
|
|
1002
|
+
const base = useCdn ? config.cdnUrl : config.url
|
|
1003
|
+
return `${base}/${uri.replace(/^\//, '')}`
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
console.log(getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar')
|
|
1007
|
+
console.log(getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar')
|
|
1008
|
+
```
|
|
1009
|
+
|
|
1010
|
+
### `client.getDataUrl()` is removed<!-- omit in toc -->
|
|
1011
|
+
|
|
1012
|
+
Before:
|
|
1013
|
+
|
|
1014
|
+
```ts
|
|
1015
|
+
import createClient from '@sanity/client'
|
|
1016
|
+
const client = createClient({dataset: 'bikeshop'})
|
|
1017
|
+
|
|
1018
|
+
console.log(client.getDataUrl('doc') === '/data/doc/bikeshop')
|
|
1019
|
+
console.log(client.getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123')
|
|
1020
|
+
```
|
|
1021
|
+
|
|
1022
|
+
After:
|
|
1023
|
+
|
|
1024
|
+
```ts
|
|
1025
|
+
import {createClient} from '@sanity/client'
|
|
1026
|
+
const client = createClient({dataset: 'bikeshop'})
|
|
1027
|
+
|
|
1028
|
+
const getDataUrl = (operation: string, path?: string) => {
|
|
1029
|
+
const {dataset} = client.config()
|
|
1030
|
+
const baseUri = `/${operation}/${dataset}`
|
|
1031
|
+
const uri = path ? `${baseUri}/${path}` : baseUri
|
|
1032
|
+
return `/data${uri}`.replace(/\/($|\?)/, '$1')
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
console.log(getDataUrl('doc') === '/data/doc/bikeshop')
|
|
1036
|
+
console.log(getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123')
|
|
1037
|
+
```
|
|
1038
|
+
|
|
1039
|
+
### `client.isPromiseAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
|
|
1040
|
+
|
|
1041
|
+
Before:
|
|
1042
|
+
|
|
1043
|
+
```ts
|
|
1044
|
+
import createClient from '@sanity/client'
|
|
1045
|
+
const client = createClient()
|
|
1046
|
+
|
|
1047
|
+
console.log(client.isPromiseAPI())
|
|
1048
|
+
console.log(client.clientConfig.isPromiseAPI)
|
|
1049
|
+
console.log(client.config().isPromiseAPI)
|
|
1050
|
+
```
|
|
1051
|
+
|
|
1052
|
+
After:
|
|
1053
|
+
|
|
1054
|
+
```ts
|
|
1055
|
+
import {createClient, SanityClient} from '@sanity/client'
|
|
1056
|
+
const client = createClient()
|
|
1057
|
+
|
|
1058
|
+
console.log(client instanceof SanityClient)
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
### `client.observable.isObservableAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
|
|
1062
|
+
|
|
1063
|
+
Before:
|
|
1064
|
+
|
|
1065
|
+
```ts
|
|
1066
|
+
import createClient from '@sanity/client'
|
|
1067
|
+
const client = createClient()
|
|
1068
|
+
|
|
1069
|
+
console.log(client.observable.isObservableAPI())
|
|
1070
|
+
```
|
|
1071
|
+
|
|
1072
|
+
After:
|
|
1073
|
+
|
|
1074
|
+
```ts
|
|
1075
|
+
import {createClient, ObservableSanityClient} from '@sanity/client'
|
|
1076
|
+
const client = createClient()
|
|
1077
|
+
|
|
1078
|
+
console.log(client.observable instanceof ObservableSanityClient)
|
|
1079
|
+
```
|
|
1080
|
+
|
|
1081
|
+
### `client._requestObservable` is removed, replace with `client.observable.request`<!-- omit in toc -->
|
|
1082
|
+
|
|
1083
|
+
Before:
|
|
1084
|
+
|
|
1085
|
+
```ts
|
|
1086
|
+
import createClient from '@sanity/client'
|
|
1087
|
+
const client = createClient()
|
|
1088
|
+
|
|
1089
|
+
client._requestObservable({uri: '/ping'}).subscribe()
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
After:
|
|
1093
|
+
|
|
1094
|
+
```ts
|
|
1095
|
+
import {createClient} from '@sanity/client'
|
|
1096
|
+
const client = createClient()
|
|
1097
|
+
|
|
1098
|
+
client.observable.request({uri: '/ping'}).subscribe()
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
### `client._dataRequest` is removed, replace with `client.dataRequest`<!-- omit in toc -->
|
|
1102
|
+
|
|
1103
|
+
Before:
|
|
1104
|
+
|
|
1105
|
+
```ts
|
|
1106
|
+
import createClient from '@sanity/client'
|
|
1107
|
+
const client = createClient()
|
|
1108
|
+
|
|
1109
|
+
client._dataRequest(endpoint, body, options)
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
After:
|
|
1113
|
+
|
|
1114
|
+
```ts
|
|
1115
|
+
import {createClient} from '@sanity/client'
|
|
1116
|
+
const client = createClient()
|
|
1117
|
+
|
|
1118
|
+
client.dataRequest(endpoint, body, options)
|
|
1119
|
+
```
|
|
1120
|
+
|
|
1121
|
+
### `client._create_` is removed, replace with one of `client.create`, `client.createIfNotExists` or `client.createOrReplace`<!-- omit in toc -->
|
|
1122
|
+
|
|
1123
|
+
Before:
|
|
1124
|
+
|
|
1125
|
+
```ts
|
|
1126
|
+
import createClient from '@sanity/client'
|
|
1127
|
+
const client = createClient()
|
|
1128
|
+
|
|
1129
|
+
client._create(doc, 'create', options)
|
|
1130
|
+
client._create(doc, 'createIfNotExists', options)
|
|
1131
|
+
client._create(doc, 'createOrReplace', options)
|
|
1132
|
+
```
|
|
1133
|
+
|
|
1134
|
+
After:
|
|
1135
|
+
|
|
1136
|
+
```ts
|
|
1137
|
+
import {createClient} from '@sanity/client'
|
|
1138
|
+
const client = createClient()
|
|
1139
|
+
|
|
1140
|
+
client.create(doc, options)
|
|
1141
|
+
client.createIfNotExists(doc, options)
|
|
1142
|
+
client.createOrReplace(doc, options)
|
|
1143
|
+
```
|
|
1144
|
+
|
|
1145
|
+
[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
|
|
1146
|
+
[Deno]: https://deno.land/
|
|
1147
|
+
[Edge Runtime]: https://edge-runtime.vercel.sh/
|
|
1148
|
+
[Bun]: https://bun.sh/
|
|
1149
|
+
[gzip-badge]: https://img.shields.io/bundlephobia/minzip/@sanity/client?label=gzip%20size&style=flat-square
|
|
1150
|
+
[size-badge]: https://img.shields.io/bundlephobia/min/@sanity/client?label=size&style=flat-square
|
|
1151
|
+
[unpkg-dist]: https://unpkg.com/@sanity/client/umd/
|
|
1152
|
+
[bundlephobia]: https://bundlephobia.com/package/@sanity/client
|
|
1153
|
+
[esm.sh]: https://esm.sh
|
|
1154
|
+
[Node.js]: https://nodejs.org/en/
|
|
1155
|
+
[Content Lake]: https://www.sanity.io/docs/datastore
|
|
1156
|
+
[npm]: https://npmjs.com
|
|
1157
|
+
[api-cdn]: https://www.sanity.io/docs/api-cdn
|
|
1158
|
+
[CommonJS]: https://nodejs.org/api/modules.html#modules-commonjs-modules
|
|
1159
|
+
[TypeScript]: https://www.typescriptlang.org/
|
|
1160
|
+
[api-versioning]: http://sanity.io/docs/api-versioning
|