@sanity/client 5.0.0-esm.10 → 5.0.0-esm.11
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 +81 -64
- package/dist/index.browser.cjs +3 -4
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +3 -4
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +4 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
- package/src/http/errors.ts +3 -5
- package/src/warnings.ts +1 -1
- package/umd/sanityClient.js +3 -160
- package/umd/sanityClient.min.js +3 -3
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
|
|
|
@@ -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)
|
|
169
|
+
|
|
170
|
+
const data = await client.fetch<number>(`count(*)`)
|
|
171
|
+
// data is typed as `number`
|
|
172
|
+
console.log(`Number of documents: ${data}`)
|
|
173
|
+
```
|
|
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'
|
|
167
181
|
|
|
168
|
-
|
|
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}`)
|
|
169
193
|
```
|
|
170
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>
|
|
@@ -1157,4 +1172,6 @@ client.createOrReplace(doc, options)
|
|
|
1157
1172
|
[api-cdn]: https://www.sanity.io/docs/api-cdn
|
|
1158
1173
|
[CommonJS]: https://nodejs.org/api/modules.html#modules-commonjs-modules
|
|
1159
1174
|
[TypeScript]: https://www.typescriptlang.org/
|
|
1160
|
-
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1175
|
+
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1176
|
+
[zod]: https://zod.dev/
|
|
1177
|
+
[groqd]: https://github.com/FormidableLabs/groqd#readme
|
package/dist/index.browser.cjs
CHANGED
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, '__esModule', {
|
|
|
6
6
|
var getIt = require('get-it');
|
|
7
7
|
var middleware = require('get-it/middleware');
|
|
8
8
|
var rxjs = require('rxjs');
|
|
9
|
-
var makeError = require('make-error');
|
|
10
9
|
var operators = require('rxjs/operators');
|
|
11
10
|
var polyfilledEventSource = require('@sanity/eventsource');
|
|
12
11
|
function _interopDefaultCompat(e) {
|
|
@@ -16,7 +15,7 @@ function _interopDefaultCompat(e) {
|
|
|
16
15
|
}
|
|
17
16
|
var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
|
|
18
17
|
var envMiddleware = [];
|
|
19
|
-
class ClientError extends
|
|
18
|
+
class ClientError extends Error {
|
|
20
19
|
constructor(res) {
|
|
21
20
|
const props = extractErrorProps(res);
|
|
22
21
|
super(props.message);
|
|
@@ -24,7 +23,7 @@ class ClientError extends makeError.BaseError {
|
|
|
24
23
|
Object.assign(this, props);
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
|
-
class ServerError extends
|
|
26
|
+
class ServerError extends Error {
|
|
28
27
|
constructor(res) {
|
|
29
28
|
const props = extractErrorProps(res);
|
|
30
29
|
super(props.message);
|
|
@@ -1023,7 +1022,7 @@ once(function () {
|
|
|
1023
1022
|
}
|
|
1024
1023
|
return console.warn(message.join(" "), ...args);
|
|
1025
1024
|
});
|
|
1026
|
-
const printCdnWarning = createWarningPrinter(["You are not using the Sanity CDN. That means your data is always fresh, but the CDN is faster and", "cheaper. Think about it! For more info, see ".concat(generateHelpUrl("js-client-cdn-configuration"), "
|
|
1025
|
+
const printCdnWarning = createWarningPrinter(["You are not using the Sanity CDN. That means your data is always fresh, but the CDN is faster and", "cheaper. Think about it! For more info, see ".concat(generateHelpUrl("js-client-cdn-configuration"), " "), "To hide this warning, please set the `useCdn` option to either `true` or `false` when creating", "the client."]);
|
|
1027
1026
|
const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
|
|
1028
1027
|
const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
|
|
1029
1028
|
const defaultCdnHost = "apicdn.sanity.io";
|