@sanity/client 5.0.0-esm.3 → 5.0.0-esm.5
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 +580 -16
- package/dist/index.browser.cjs +314 -8
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +314 -8
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +315 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +297 -213
- package/dist/index.js +315 -9
- package/dist/index.js.map +1 -1
- package/package.json +26 -23
- package/src/SanityClient.ts +172 -167
- package/src/assets/AssetsClient.ts +14 -12
- package/src/auth/AuthClient.ts +2 -0
- package/src/config.ts +2 -2
- package/src/data/dataMethods.ts +4 -1
- package/src/data/listen.ts +6 -6
- package/src/data/patch.ts +34 -31
- package/src/data/transaction.ts +5 -0
- package/src/datasets/DatasetsClient.ts +2 -0
- package/src/http/errors.ts +2 -0
- package/src/http/request.ts +1 -3
- package/src/index.browser.ts +3 -3
- package/src/index.ts +3 -42
- package/src/projects/ProjectsClient.ts +2 -0
- package/src/types.ts +60 -0
- package/src/users/UsersClient.ts +2 -2
- package/src/validators.ts +4 -2
- package/umd/sanityClient.js +5753 -5470
- package/umd/sanityClient.min.js +12 -12
package/README.md
CHANGED
|
@@ -1,26 +1,85 @@
|
|
|
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)
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
[![gzip size][gzip-badge]][bundlephobia]
|
|
6
|
+
[![size][size-badge]][bundlephobia]
|
|
7
|
+
|
|
8
|
+
Javascript client for Sanity. Works in [Node.js](https://nodejs.org/en/), [Bun], [Deno], [Edge Runtime] and [modern browsers].
|
|
9
|
+
|
|
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)
|
|
54
|
+
- [Migrate](#migrate)
|
|
55
|
+
- [From `v4`](#from-v4)
|
|
6
56
|
|
|
7
57
|
## Requirements
|
|
8
58
|
|
|
9
|
-
Sanity Client
|
|
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. For 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
60
|
|
|
11
61
|
## Installation
|
|
12
62
|
|
|
13
63
|
The client can be installed from npm:
|
|
14
64
|
|
|
15
65
|
```
|
|
16
|
-
npm install
|
|
66
|
+
npm install @sanity/client
|
|
17
67
|
```
|
|
18
68
|
|
|
19
69
|
## API
|
|
20
70
|
|
|
71
|
+
### Creating a client instance
|
|
72
|
+
|
|
73
|
+
`const client = createClient(options)`
|
|
74
|
+
|
|
75
|
+
Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. Setting a value for `useCdn` is encouraged.
|
|
76
|
+
|
|
77
|
+
#### [ESM](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)
|
|
78
|
+
|
|
21
79
|
```js
|
|
22
|
-
|
|
23
|
-
|
|
80
|
+
import {createClient} from '@sanity/client'
|
|
81
|
+
|
|
82
|
+
const client = createClient({
|
|
24
83
|
projectId: 'your-project-id',
|
|
25
84
|
dataset: 'bikeshop',
|
|
26
85
|
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
@@ -31,9 +90,227 @@ const client = sanityClient({
|
|
|
31
90
|
export default client
|
|
32
91
|
```
|
|
33
92
|
|
|
34
|
-
|
|
93
|
+
#### [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules)
|
|
35
94
|
|
|
36
|
-
|
|
95
|
+
```js
|
|
96
|
+
const {createClient} = require('@sanity/client')
|
|
97
|
+
|
|
98
|
+
const client = createClient({
|
|
99
|
+
projectId: 'your-project-id',
|
|
100
|
+
dataset: 'bikeshop',
|
|
101
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
102
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
103
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
module.exports = client
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### [TypeScript](https://www.typescriptlang.org/)
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import {createClient, type ClientConfig} from '@sanity/client'
|
|
113
|
+
|
|
114
|
+
const client: ClientConfig = {
|
|
115
|
+
projectId: 'your-project-id',
|
|
116
|
+
dataset: 'bikeshop',
|
|
117
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
118
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
119
|
+
useCdn: true, // `false` if you want to ensure fresh data
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export default createClient(config)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### [Bun]
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
bun init
|
|
129
|
+
bun add @sanity/client
|
|
130
|
+
open index.ts
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
// index.ts
|
|
135
|
+
import {createClient} from '@sanity/client'
|
|
136
|
+
|
|
137
|
+
const client = createClient({
|
|
138
|
+
projectId: 'your-project-id',
|
|
139
|
+
dataset: 'bikeshop',
|
|
140
|
+
apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
|
|
141
|
+
token: 'sanity-auth-token', // or leave blank for unauthenticated usage
|
|
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',
|
|
197
|
+
}
|
|
198
|
+
|
|
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>
|
|
313
|
+
```
|
|
37
314
|
|
|
38
315
|
### Specifying API version
|
|
39
316
|
|
|
@@ -372,27 +649,25 @@ A `patch` can be performed inline on a `transaction`.
|
|
|
372
649
|
Transactions and patches can also be built outside the scope of a client:
|
|
373
650
|
|
|
374
651
|
```js
|
|
375
|
-
|
|
376
|
-
const client =
|
|
652
|
+
import {createClient, Patch, Transaction} from '@sanity/client'
|
|
653
|
+
const client = createClient({
|
|
377
654
|
projectId: 'your-project-id',
|
|
378
655
|
dataset: 'bikeshop',
|
|
379
656
|
})
|
|
380
657
|
|
|
381
658
|
// Patches:
|
|
382
|
-
const patch = new
|
|
659
|
+
const patch = new Patch('<documentId>')
|
|
383
660
|
client.mutate(patch.inc({count: 1}).unset(['visits']))
|
|
384
661
|
|
|
385
662
|
// Transactions:
|
|
386
|
-
const transaction = new
|
|
387
|
-
.create({_id: '123', name: 'FooBike'})
|
|
388
|
-
.delete('someDocId')
|
|
663
|
+
const transaction = new Transaction().create({_id: '123', name: 'FooBike'}).delete('someDocId')
|
|
389
664
|
|
|
390
665
|
client.mutate(transaction)
|
|
391
666
|
```
|
|
392
667
|
|
|
393
|
-
`const patch = new
|
|
668
|
+
`const patch = new Patch(docId)`
|
|
394
669
|
|
|
395
|
-
`const transaction = new
|
|
670
|
+
`const transaction = new Transaction()`
|
|
396
671
|
|
|
397
672
|
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
673
|
|
|
@@ -541,3 +816,292 @@ Semantic release will only release on configured branches, so it is safe to run
|
|
|
541
816
|
## License
|
|
542
817
|
|
|
543
818
|
MIT © [Sanity.io](https://www.sanity.io/)
|
|
819
|
+
|
|
820
|
+
# Migrate
|
|
821
|
+
|
|
822
|
+
## From `v4`
|
|
823
|
+
|
|
824
|
+
### No longer shipping `ES5`<!-- omit in toc -->
|
|
825
|
+
|
|
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.
|
|
827
|
+
|
|
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 -->
|
|
833
|
+
|
|
834
|
+
Before:
|
|
835
|
+
|
|
836
|
+
```ts
|
|
837
|
+
import createClient from '@sanity/client'
|
|
838
|
+
const client = createClient()
|
|
839
|
+
```
|
|
840
|
+
|
|
841
|
+
```ts
|
|
842
|
+
import SanityClient from '@sanity/client'
|
|
843
|
+
const client = new SanityClient()
|
|
844
|
+
```
|
|
845
|
+
|
|
846
|
+
After:
|
|
847
|
+
|
|
848
|
+
```ts
|
|
849
|
+
import {createClient} from '@sanity/client'
|
|
850
|
+
const client = createClient()
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
### `client.assets.delete` is removed<!-- omit in toc -->
|
|
854
|
+
|
|
855
|
+
Before:
|
|
856
|
+
|
|
857
|
+
```ts
|
|
858
|
+
client.assets.delete('image', 'abc123_foobar-123x123-png')
|
|
859
|
+
client.assets.delete('image', 'image-abc123_foobar-123x123-png')
|
|
860
|
+
client.assets.delete({_id: 'image-abc123_foobar-123x123-png'})
|
|
861
|
+
```
|
|
862
|
+
|
|
863
|
+
After:
|
|
864
|
+
|
|
865
|
+
```ts
|
|
866
|
+
client.delete('image-abc123_foobar-123x123-png')
|
|
867
|
+
```
|
|
868
|
+
|
|
869
|
+
### `client.assets.getImageUrl` is removed, replace with [`@sanity/image-url`](https://github.com/sanity-io/image-url)<!-- omit in toc -->
|
|
870
|
+
|
|
871
|
+
Before:
|
|
872
|
+
|
|
873
|
+
```ts
|
|
874
|
+
import {createClient} from '@sanity/client'
|
|
875
|
+
const client = createClient({projectId: 'abc123', dataset: 'foo'})
|
|
876
|
+
|
|
877
|
+
client.assets.getImageUrl('image-abc123_foobar-123x123-png')
|
|
878
|
+
client.assets.getImageUrl('image-abc123_foobar-123x123-png', {auto: 'format'})
|
|
879
|
+
client.assets.getImageUrl({_ref: 'image-abc123_foobar-123x123-png'})
|
|
880
|
+
client.assets.getImageUrl({_ref: 'image-abc123_foobar-123x123-png'}, {auto: 'format'})
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
After:
|
|
884
|
+
|
|
885
|
+
```bash
|
|
886
|
+
npm install @sanity/image-url
|
|
887
|
+
```
|
|
888
|
+
|
|
889
|
+
```ts
|
|
890
|
+
import imageUrlBuilder from '@sanity/image-url'
|
|
891
|
+
const builder = imageUrlBuilder({projectId: 'abc123', dataset: 'foo'})
|
|
892
|
+
const urlFor = (source) => builder.image(source)
|
|
893
|
+
|
|
894
|
+
urlFor('image-abc123_foobar-123x123-png').url()
|
|
895
|
+
urlFor('image-abc123_foobar-123x123-png').auto('format').url()
|
|
896
|
+
urlFor({_ref: 'image-abc123_foobar-123x123-png'}).url()
|
|
897
|
+
urlFor({_ref: 'image-abc123_foobar-123x123-png'}).auto('format').url()
|
|
898
|
+
```
|
|
899
|
+
|
|
900
|
+
### `SanityClient` static properties moved to named exports<!-- omit in toc -->
|
|
901
|
+
|
|
902
|
+
Before:
|
|
903
|
+
|
|
904
|
+
```ts
|
|
905
|
+
import SanityClient from '@sanity/client'
|
|
906
|
+
|
|
907
|
+
const {Patch, Transaction, ClientError, ServerError, requester} = SanityClient
|
|
908
|
+
```
|
|
909
|
+
|
|
910
|
+
After:
|
|
911
|
+
|
|
912
|
+
```ts
|
|
913
|
+
import {Patch, Transaction, ClientError, ServerError, requester} from '@sanity/client'
|
|
914
|
+
```
|
|
915
|
+
|
|
916
|
+
### `client.clientConfig` is removed, replace with `client.config()`<!-- omit in toc -->
|
|
917
|
+
|
|
918
|
+
Before:
|
|
919
|
+
|
|
920
|
+
```ts
|
|
921
|
+
import createClient from '@sanity/client'
|
|
922
|
+
const client = createClient()
|
|
923
|
+
|
|
924
|
+
console.log(client.clientConfig.projectId)
|
|
925
|
+
```
|
|
926
|
+
|
|
927
|
+
After:
|
|
928
|
+
|
|
929
|
+
```ts
|
|
930
|
+
import {createClient} from '@sanity/client'
|
|
931
|
+
const client = createClient()
|
|
932
|
+
|
|
933
|
+
console.log(client.config().projectId)
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
### `client.getUrl()` is removed<!-- omit in toc -->
|
|
937
|
+
|
|
938
|
+
Before:
|
|
939
|
+
|
|
940
|
+
```ts
|
|
941
|
+
import createClient from '@sanity/client'
|
|
942
|
+
const client = createClient({projectId: 'abc123'})
|
|
943
|
+
|
|
944
|
+
console.log(client.getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar')
|
|
945
|
+
console.log(client.getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar')
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
After:
|
|
949
|
+
|
|
950
|
+
```ts
|
|
951
|
+
import {createClient} from '@sanity/client'
|
|
952
|
+
const client = createClient({projectId: 'abc123'})
|
|
953
|
+
|
|
954
|
+
const getUrl = (uri: string, useCdn = false) => {
|
|
955
|
+
const config = client.config()
|
|
956
|
+
const base = useCdn ? config.cdnUrl : config.url
|
|
957
|
+
return `${base}/${uri.replace(/^\//, '')}`
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
console.log(getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar')
|
|
961
|
+
console.log(getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar')
|
|
962
|
+
```
|
|
963
|
+
|
|
964
|
+
### `client.getDataUrl()` is removed<!-- omit in toc -->
|
|
965
|
+
|
|
966
|
+
Before:
|
|
967
|
+
|
|
968
|
+
```ts
|
|
969
|
+
import createClient from '@sanity/client'
|
|
970
|
+
const client = createClient({dataset: 'bikeshop'})
|
|
971
|
+
|
|
972
|
+
console.log(client.getDataUrl('doc') === '/data/doc/bikeshop')
|
|
973
|
+
console.log(client.getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123')
|
|
974
|
+
```
|
|
975
|
+
|
|
976
|
+
After:
|
|
977
|
+
|
|
978
|
+
```ts
|
|
979
|
+
import {createClient} from '@sanity/client'
|
|
980
|
+
const client = createClient({dataset: 'bikeshop'})
|
|
981
|
+
|
|
982
|
+
const getDataUrl = (operation: string, path?: string) => {
|
|
983
|
+
const {dataset} = client.config()
|
|
984
|
+
const baseUri = `/${operation}/${dataset}`
|
|
985
|
+
const uri = path ? `${baseUri}/${path}` : baseUri
|
|
986
|
+
return `/data${uri}`.replace(/\/($|\?)/, '$1')
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
console.log(getDataUrl('doc') === '/data/doc/bikeshop')
|
|
990
|
+
console.log(getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123')
|
|
991
|
+
```
|
|
992
|
+
|
|
993
|
+
### `client.isPromiseAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
|
|
994
|
+
|
|
995
|
+
Before:
|
|
996
|
+
|
|
997
|
+
```ts
|
|
998
|
+
import createClient from '@sanity/client'
|
|
999
|
+
const client = createClient()
|
|
1000
|
+
|
|
1001
|
+
console.log(client.isPromiseAPI())
|
|
1002
|
+
console.log(client.clientConfig.isPromiseAPI)
|
|
1003
|
+
console.log(client.config().isPromiseAPI)
|
|
1004
|
+
```
|
|
1005
|
+
|
|
1006
|
+
After:
|
|
1007
|
+
|
|
1008
|
+
```ts
|
|
1009
|
+
import {createClient, SanityClient} from '@sanity/client'
|
|
1010
|
+
const client = createClient()
|
|
1011
|
+
|
|
1012
|
+
console.log(client instanceof SanityClient)
|
|
1013
|
+
```
|
|
1014
|
+
|
|
1015
|
+
### `client.observable.isObservableAPI()` is removed, replace with an `instanceof` check<!-- omit in toc -->
|
|
1016
|
+
|
|
1017
|
+
Before:
|
|
1018
|
+
|
|
1019
|
+
```ts
|
|
1020
|
+
import createClient from '@sanity/client'
|
|
1021
|
+
const client = createClient()
|
|
1022
|
+
|
|
1023
|
+
console.log(client.observable.isObservableAPI())
|
|
1024
|
+
```
|
|
1025
|
+
|
|
1026
|
+
After:
|
|
1027
|
+
|
|
1028
|
+
```ts
|
|
1029
|
+
import {createClient, ObservableSanityClient} from '@sanity/client'
|
|
1030
|
+
const client = createClient()
|
|
1031
|
+
|
|
1032
|
+
console.log(client.observable instanceof ObservableSanityClient)
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
### `client._requestObservable` is removed, replace with `client.observable.request`<!-- omit in toc -->
|
|
1036
|
+
|
|
1037
|
+
Before:
|
|
1038
|
+
|
|
1039
|
+
```ts
|
|
1040
|
+
import createClient from '@sanity/client'
|
|
1041
|
+
const client = createClient()
|
|
1042
|
+
|
|
1043
|
+
client._requestObservable({uri: '/ping'}).subscribe()
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
After:
|
|
1047
|
+
|
|
1048
|
+
```ts
|
|
1049
|
+
import {createClient} from '@sanity/client'
|
|
1050
|
+
const client = createClient()
|
|
1051
|
+
|
|
1052
|
+
client.observable.request({uri: '/ping'}).subscribe()
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
### `client._dataRequest` is removed, replace with `client.dataRequest`<!-- omit in toc -->
|
|
1056
|
+
|
|
1057
|
+
Before:
|
|
1058
|
+
|
|
1059
|
+
```ts
|
|
1060
|
+
import createClient from '@sanity/client'
|
|
1061
|
+
const client = createClient()
|
|
1062
|
+
|
|
1063
|
+
client._dataRequest(endpoint, body, options)
|
|
1064
|
+
```
|
|
1065
|
+
|
|
1066
|
+
After:
|
|
1067
|
+
|
|
1068
|
+
```ts
|
|
1069
|
+
import {createClient} from '@sanity/client'
|
|
1070
|
+
const client = createClient()
|
|
1071
|
+
|
|
1072
|
+
client.dataRequest(endpoint, body, options)
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
### `client._create_` is removed, replace with one of `client.create`, `client.createIfNotExists` or `client.createOrReplace`<!-- omit in toc -->
|
|
1076
|
+
|
|
1077
|
+
Before:
|
|
1078
|
+
|
|
1079
|
+
```ts
|
|
1080
|
+
import createClient from '@sanity/client'
|
|
1081
|
+
const client = createClient()
|
|
1082
|
+
|
|
1083
|
+
client._create(doc, 'create', options)
|
|
1084
|
+
client._create(doc, 'createIfNotExists', options)
|
|
1085
|
+
client._create(doc, 'createOrReplace', options)
|
|
1086
|
+
```
|
|
1087
|
+
|
|
1088
|
+
After:
|
|
1089
|
+
|
|
1090
|
+
```ts
|
|
1091
|
+
import {createClient} from '@sanity/client'
|
|
1092
|
+
const client = createClient()
|
|
1093
|
+
|
|
1094
|
+
client.create(doc, options)
|
|
1095
|
+
client.createIfNotExists(doc, options)
|
|
1096
|
+
client.createOrReplace(doc, options)
|
|
1097
|
+
```
|
|
1098
|
+
|
|
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
|
|
1100
|
+
[Deno]: https://deno.land/
|
|
1101
|
+
[Edge Runtime]: https://edge-runtime.vercel.sh/
|
|
1102
|
+
[Bun]: https://bun.sh/
|
|
1103
|
+
[gzip-badge]: https://img.shields.io/bundlephobia/minzip/@sanity/client?label=gzip%20size&style=flat-square
|
|
1104
|
+
[size-badge]: https://img.shields.io/bundlephobia/min/@sanity/client?label=size&style=flat-square
|
|
1105
|
+
[unpkg-dist]: https://unpkg.com/@sanity/client/umd/
|
|
1106
|
+
[bundlephobia]: https://bundlephobia.com/package/@sanity/client
|
|
1107
|
+
[esm.sh]: https://esm.sh
|