@sanity/client 5.0.0-esm.9 → 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 +141 -65
- package/dist/index.browser.cjs +82 -162
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +82 -162
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +83 -163
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +51 -58
- package/dist/index.js +83 -163
- package/dist/index.js.map +1 -1
- package/package.json +22 -23
- package/src/SanityClient.ts +23 -25
- package/src/assets/AssetsClient.ts +29 -5
- package/src/data/dataMethods.ts +2 -2
- package/src/data/encodeQueryString.ts +15 -14
- package/src/data/listen.ts +4 -4
- package/src/data/patch.ts +1 -13
- package/src/datasets/DatasetsClient.ts +1 -1
- package/src/generateHelpUrl.ts +1 -1
- package/src/http/errors.ts +3 -5
- package/src/http/request.ts +1 -1
- package/src/projects/ProjectsClient.ts +1 -1
- package/src/users/UsersClient.ts +1 -1
- package/src/warnings.ts +1 -1
- package/umd/sanityClient.js +109 -414
- package/umd/sanityClient.min.js +3 -3
- package/src/auth/AuthClient.ts +0 -67
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ Import and create a new client instance, and use its methods to interact with yo
|
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
21
|
// sanity.js
|
|
22
|
-
import {createClient}
|
|
22
|
+
import {createClient} from '@sanity/client'
|
|
23
23
|
// Import using ESM URL imports in environments that supports it:
|
|
24
24
|
// import {createClient} from 'https://esm.sh/@sanity/client'
|
|
25
25
|
|
|
@@ -98,7 +98,7 @@ export async function updateDocumentTitle(_id, title) {
|
|
|
98
98
|
|
|
99
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
100
|
|
|
101
|
-
For legacy ES5 environments
|
|
101
|
+
[For legacy ES5 environments we recommend v4.](https://github.com/sanity-io/client/tree/v4.0.0#sanityclient)
|
|
102
102
|
|
|
103
103
|
## Installation
|
|
104
104
|
|
|
@@ -127,13 +127,13 @@ import {createClient} from '@sanity/client'
|
|
|
127
127
|
|
|
128
128
|
const client = createClient({
|
|
129
129
|
projectId: 'your-project-id',
|
|
130
|
-
dataset: '
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
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
|
|
134
133
|
})
|
|
135
134
|
|
|
136
|
-
|
|
135
|
+
const data = await client.fetch(`count(*)`)
|
|
136
|
+
console.log(`Number of documents: ${data}`)
|
|
137
137
|
```
|
|
138
138
|
|
|
139
139
|
#### [CommonJS]
|
|
@@ -143,13 +143,15 @@ const {createClient} = require('@sanity/client')
|
|
|
143
143
|
|
|
144
144
|
const client = createClient({
|
|
145
145
|
projectId: 'your-project-id',
|
|
146
|
-
dataset: '
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
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
|
|
150
149
|
})
|
|
151
150
|
|
|
152
|
-
|
|
151
|
+
client
|
|
152
|
+
.fetch(`count(*)`)
|
|
153
|
+
.then((data) => console.log(`Number of documents: ${data}`))
|
|
154
|
+
.catch(console.error)
|
|
153
155
|
```
|
|
154
156
|
|
|
155
157
|
#### [TypeScript]
|
|
@@ -157,17 +159,41 @@ module.exports = client
|
|
|
157
159
|
```ts
|
|
158
160
|
import {createClient, type ClientConfig} from '@sanity/client'
|
|
159
161
|
|
|
160
|
-
const
|
|
162
|
+
const config: ClientConfig = {
|
|
161
163
|
projectId: 'your-project-id',
|
|
162
|
-
dataset: '
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
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
|
|
166
167
|
}
|
|
168
|
+
const client = createClient(config)
|
|
167
169
|
|
|
168
|
-
|
|
170
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
171
|
+
// data is typed as `number`
|
|
172
|
+
console.log(`Number of documents: ${data}`)
|
|
169
173
|
```
|
|
170
174
|
|
|
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}`)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Another alternative is [groqd].
|
|
196
|
+
|
|
171
197
|
#### [Bun]
|
|
172
198
|
|
|
173
199
|
```bash
|
|
@@ -182,13 +208,12 @@ import {createClient} from '@sanity/client'
|
|
|
182
208
|
|
|
183
209
|
const client = createClient({
|
|
184
210
|
projectId: 'your-project-id',
|
|
185
|
-
dataset: '
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
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
|
|
189
214
|
})
|
|
190
215
|
|
|
191
|
-
const data = await client.fetch(`count(*
|
|
216
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
192
217
|
|
|
193
218
|
console.write(`Number of documents: ${data}`)
|
|
194
219
|
```
|
|
@@ -211,13 +236,12 @@ import {createClient} from 'https://esm.sh/@sanity/client'
|
|
|
211
236
|
|
|
212
237
|
const client = createClient({
|
|
213
238
|
projectId: 'your-project-id',
|
|
214
|
-
dataset: '
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
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
|
|
218
242
|
})
|
|
219
243
|
|
|
220
|
-
const data = await client.fetch(`count(*
|
|
244
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
221
245
|
|
|
222
246
|
console.log(`Number of documents: ${data}`)
|
|
223
247
|
```
|
|
@@ -245,23 +269,18 @@ export const config = {
|
|
|
245
269
|
export default async function handler(req: NextRequest) {
|
|
246
270
|
const client = createClient({
|
|
247
271
|
projectId: 'your-project-id',
|
|
248
|
-
dataset: '
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
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
|
|
252
275
|
})
|
|
253
276
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
'content-type': 'application/json',
|
|
262
|
-
},
|
|
263
|
-
}
|
|
264
|
-
)
|
|
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
|
+
})
|
|
265
284
|
}
|
|
266
285
|
```
|
|
267
286
|
|
|
@@ -281,13 +300,12 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
|
|
|
281
300
|
|
|
282
301
|
const client = createClient({
|
|
283
302
|
projectId: 'your-project-id',
|
|
284
|
-
dataset: '
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
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
|
|
288
306
|
})
|
|
289
307
|
|
|
290
|
-
const data = await client.fetch(`count(*
|
|
308
|
+
const data = await client.fetch(`count(*)`)
|
|
291
309
|
document.getElementById('results').innerText = `Number of documents: ${data}`
|
|
292
310
|
</script>
|
|
293
311
|
<div id="results"></div>
|
|
@@ -296,18 +314,17 @@ Using [esm.sh] you can either load the client using a `<script type="module">` t
|
|
|
296
314
|
Or from anywhere using a dynamic `import()`:
|
|
297
315
|
|
|
298
316
|
```js
|
|
299
|
-
// You can run this snippet from your
|
|
317
|
+
// You can run this snippet from your browser DevTools console.
|
|
300
318
|
// Super handy when you're quickly testing out queries.
|
|
301
319
|
const {createClient} = await import('https://esm.sh/@sanity/client')
|
|
302
320
|
const client = createClient({
|
|
303
321
|
projectId: 'your-project-id',
|
|
304
|
-
dataset: '
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
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
|
|
308
325
|
})
|
|
309
326
|
|
|
310
|
-
const data = await client.fetch(`count(*
|
|
327
|
+
const data = await client.fetch(`count(*)`)
|
|
311
328
|
console.log(`Number of documents: ${data}`)
|
|
312
329
|
```
|
|
313
330
|
|
|
@@ -324,13 +341,12 @@ Loading the UMD script creates a `SanityClient` global that have the same export
|
|
|
324
341
|
|
|
325
342
|
const client = createClient({
|
|
326
343
|
projectId: 'your-project-id',
|
|
327
|
-
dataset: '
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
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
|
|
331
347
|
})
|
|
332
348
|
|
|
333
|
-
client.fetch(`count(*
|
|
349
|
+
client.fetch(`count(*)`).then((data) => console.log(`Number of documents: ${data}`))
|
|
334
350
|
</script>
|
|
335
351
|
```
|
|
336
352
|
|
|
@@ -346,13 +362,12 @@ The `require-unpkg` library lets you consume `npm` packages from `unpkg.com` sim
|
|
|
346
362
|
|
|
347
363
|
const client = createClient({
|
|
348
364
|
projectId: 'your-project-id',
|
|
349
|
-
dataset: '
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
token: 'sanity-secret-auth-token', // leave blank for unauthenticated usage
|
|
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
|
|
353
368
|
})
|
|
354
369
|
|
|
355
|
-
const data = await client.fetch(`count(*
|
|
370
|
+
const data = await client.fetch(`count(*)`)
|
|
356
371
|
$('#results').text(`Number of documents: ${data}`)
|
|
357
372
|
})()
|
|
358
373
|
</script>
|
|
@@ -1142,6 +1157,65 @@ client.createIfNotExists(doc, options)
|
|
|
1142
1157
|
client.createOrReplace(doc, options)
|
|
1143
1158
|
```
|
|
1144
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
|
+
|
|
1145
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
|
|
1146
1220
|
[Deno]: https://deno.land/
|
|
1147
1221
|
[Edge Runtime]: https://edge-runtime.vercel.sh/
|
|
@@ -1157,4 +1231,6 @@ client.createOrReplace(doc, options)
|
|
|
1157
1231
|
[api-cdn]: https://www.sanity.io/docs/api-cdn
|
|
1158
1232
|
[CommonJS]: https://nodejs.org/api/modules.html#modules-commonjs-modules
|
|
1159
1233
|
[TypeScript]: https://www.typescriptlang.org/
|
|
1160
|
-
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1234
|
+
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1235
|
+
[zod]: https://zod.dev/
|
|
1236
|
+
[groqd]: https://github.com/FormidableLabs/groqd#readme
|