@sanity/client 6.1.2 → 6.1.3-perspective.1
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 +159 -0
- package/dist/index.browser.cjs +83 -17
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +83 -17
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +84 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +84 -18
- package/dist/index.js.map +1 -1
- package/package.json +18 -18
- package/src/data/dataMethods.ts +1 -1
- package/src/types.ts +9 -1
- package/umd/sanityClient.js +151 -85
- package/umd/sanityClient.min.js +3 -3
package/README.md
CHANGED
|
@@ -65,6 +65,9 @@ export async function updateDocumentTitle(_id, title) {
|
|
|
65
65
|
- [UMD](#umd)
|
|
66
66
|
- [Specifying API version](#specifying-api-version)
|
|
67
67
|
- [Performing queries](#performing-queries)
|
|
68
|
+
- [Using perspectives](#using-perspectives)
|
|
69
|
+
- [`published`](#published)
|
|
70
|
+
- [`previewDrafts`](#previewdrafts)
|
|
68
71
|
- [Fetching Content Source Maps](#fetching-content-source-maps)
|
|
69
72
|
- [Listening to queries](#listening-to-queries)
|
|
70
73
|
- [Fetch a single document](#fetch-a-single-document)
|
|
@@ -405,6 +408,162 @@ client.fetch(query, params).then((bikes) => {
|
|
|
405
408
|
|
|
406
409
|
Perform a query using the given parameters (if any).
|
|
407
410
|
|
|
411
|
+
### Using perspectives
|
|
412
|
+
|
|
413
|
+
The `perspective` option can be used to specify special filtering behavior for queries. The default value is `raw`, which means no special filtering is applied, while [`published`](#published) and [`previewDrafts`](#previewdrafts) can be used to optimize for specific use cases.
|
|
414
|
+
|
|
415
|
+
#### `published`
|
|
416
|
+
|
|
417
|
+
Useful for when you want to be sure that draft documents are not returned in production. Pairs well with private datasets.
|
|
418
|
+
|
|
419
|
+
With a dataset that looks like this:
|
|
420
|
+
|
|
421
|
+
```json
|
|
422
|
+
[
|
|
423
|
+
{
|
|
424
|
+
"_type": "author",
|
|
425
|
+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
426
|
+
"name": "George Martin"
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
"_type": "author",
|
|
430
|
+
"_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
431
|
+
"name": "George R.R. Martin"
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
"_type": "author",
|
|
435
|
+
"_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
436
|
+
"name": "Stephen King"
|
|
437
|
+
}
|
|
438
|
+
]
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
And a query like this:
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
import {createClient} from '@sanity/client'
|
|
445
|
+
|
|
446
|
+
const client = createClient({
|
|
447
|
+
...config,
|
|
448
|
+
useCdn: true, // set to `false` to bypass the edge cache
|
|
449
|
+
perspective: 'published',
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
const authors = await client.fetch('*[_type == "author"]')
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
Then `authors` will only contain documents that don't have a `drafts.` prefix in their `_id`, in this case just "George Martin":
|
|
456
|
+
|
|
457
|
+
```json
|
|
458
|
+
[
|
|
459
|
+
{
|
|
460
|
+
"_type": "author",
|
|
461
|
+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
462
|
+
"name": "George Martin"
|
|
463
|
+
}
|
|
464
|
+
]
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
#### `previewDrafts`
|
|
468
|
+
|
|
469
|
+
Designed to help answer the question "What is our app going to look like after all the draft documents are published?".
|
|
470
|
+
|
|
471
|
+
Given a dataset like this:
|
|
472
|
+
|
|
473
|
+
```json
|
|
474
|
+
[
|
|
475
|
+
{
|
|
476
|
+
"_type": "author",
|
|
477
|
+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
478
|
+
"name": "George Martin"
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
"_type": "author",
|
|
482
|
+
"_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
483
|
+
"name": "George R.R. Martin"
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
"_type": "author",
|
|
487
|
+
"_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
488
|
+
"name": "Stephen King"
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
"_type": "author",
|
|
492
|
+
"_id": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
|
|
493
|
+
"name": "Terry Pratchett"
|
|
494
|
+
}
|
|
495
|
+
]
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
And a query like this:
|
|
499
|
+
|
|
500
|
+
```ts
|
|
501
|
+
import {createClient} from '@sanity/client'
|
|
502
|
+
|
|
503
|
+
const client = createClient({
|
|
504
|
+
...config,
|
|
505
|
+
useCdn: false, // the `previewDrafts` perspective requires this to be `false`
|
|
506
|
+
perspective: 'previewDrafts',
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
const authors = await client.fetch('*[_type == "author"]')
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
Then `authors` will look like this. Note that the result dedupes documents with a preference for the draft version:
|
|
513
|
+
|
|
514
|
+
```json
|
|
515
|
+
[
|
|
516
|
+
{
|
|
517
|
+
"_type": "author",
|
|
518
|
+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
519
|
+
"_originalId": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
520
|
+
"name": "George R.R. Martin"
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
"_type": "author",
|
|
524
|
+
"_id": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
525
|
+
"_originalId": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
526
|
+
"name": "Stephen King"
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
"_type": "author",
|
|
530
|
+
"_id": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
|
|
531
|
+
"_originalId": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
|
|
532
|
+
"name": "Terry Pratchett"
|
|
533
|
+
}
|
|
534
|
+
]
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
Since the query simulates what the result will be after publishing the drafts, the `_id` doesn't contain the `drafts.` prefix. If you want to check if a document is a draft or not you can use the `_originalId` field, which is only available when using the `previewDrafts` perspective.
|
|
538
|
+
|
|
539
|
+
```ts
|
|
540
|
+
const authors = await client.fetch(`*[_type == "author"]{..., "status": select(
|
|
541
|
+
_originalId in path("drafts.**") => "draft",
|
|
542
|
+
"published"
|
|
543
|
+
)}`)
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
Which changes the result to be:
|
|
547
|
+
|
|
548
|
+
```json
|
|
549
|
+
[
|
|
550
|
+
{
|
|
551
|
+
"_type": "author",
|
|
552
|
+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
553
|
+
"_originalId": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
|
|
554
|
+
"name": "George R.R. Martin",
|
|
555
|
+
"status": "draft"
|
|
556
|
+
},
|
|
557
|
+
{
|
|
558
|
+
"_type": "author",
|
|
559
|
+
"_id": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
560
|
+
"_originalId": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
|
|
561
|
+
"name": "Stephen King",
|
|
562
|
+
"status": "published"
|
|
563
|
+
}
|
|
564
|
+
]
|
|
565
|
+
```
|
|
566
|
+
|
|
408
567
|
### Fetching Content Source Maps
|
|
409
568
|
|
|
410
569
|
Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.
|
package/dist/index.browser.cjs
CHANGED
|
@@ -8,12 +8,26 @@ var middleware = require('get-it/middleware');
|
|
|
8
8
|
var rxjs = require('rxjs');
|
|
9
9
|
var operators = require('rxjs/operators');
|
|
10
10
|
var envMiddleware = [];
|
|
11
|
+
var __defProp$3 = Object.defineProperty;
|
|
12
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value
|
|
17
|
+
}) : obj[key] = value;
|
|
18
|
+
var __publicField$3 = (obj, key, value) => {
|
|
19
|
+
__defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
20
|
+
return value;
|
|
21
|
+
};
|
|
11
22
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
|
|
12
23
|
class ClientError extends Error {
|
|
13
24
|
constructor(res) {
|
|
14
25
|
const props = extractErrorProps(res);
|
|
15
26
|
super(props.message);
|
|
16
|
-
this
|
|
27
|
+
__publicField$3(this, "response");
|
|
28
|
+
__publicField$3(this, "statusCode", 400);
|
|
29
|
+
__publicField$3(this, "responseBody");
|
|
30
|
+
__publicField$3(this, "details");
|
|
17
31
|
Object.assign(this, props);
|
|
18
32
|
}
|
|
19
33
|
}
|
|
@@ -21,7 +35,10 @@ class ServerError extends Error {
|
|
|
21
35
|
constructor(res) {
|
|
22
36
|
const props = extractErrorProps(res);
|
|
23
37
|
super(props.message);
|
|
24
|
-
this
|
|
38
|
+
__publicField$3(this, "response");
|
|
39
|
+
__publicField$3(this, "statusCode", 500);
|
|
40
|
+
__publicField$3(this, "responseBody");
|
|
41
|
+
__publicField$3(this, "details");
|
|
25
42
|
Object.assign(this, props);
|
|
26
43
|
}
|
|
27
44
|
}
|
|
@@ -242,6 +259,17 @@ const encodeQueryString = _ref2 => {
|
|
|
242
259
|
}
|
|
243
260
|
return "?".concat(searchParams);
|
|
244
261
|
};
|
|
262
|
+
var __defProp$2 = Object.defineProperty;
|
|
263
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
configurable: true,
|
|
266
|
+
writable: true,
|
|
267
|
+
value
|
|
268
|
+
}) : obj[key] = value;
|
|
269
|
+
var __publicField$2 = (obj, key, value) => {
|
|
270
|
+
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
271
|
+
return value;
|
|
272
|
+
};
|
|
245
273
|
var __accessCheck$6 = (obj, member, msg) => {
|
|
246
274
|
if (!member.has(obj)) throw TypeError("Cannot " + msg);
|
|
247
275
|
};
|
|
@@ -262,6 +290,8 @@ var _client$5, _client2$5;
|
|
|
262
290
|
class BasePatch {
|
|
263
291
|
constructor(selection) {
|
|
264
292
|
let operations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
293
|
+
__publicField$2(this, "selection");
|
|
294
|
+
__publicField$2(this, "operations");
|
|
265
295
|
this.selection = selection;
|
|
266
296
|
this.operations = operations;
|
|
267
297
|
}
|
|
@@ -415,7 +445,7 @@ class BasePatch {
|
|
|
415
445
|
return this._assign(op, props, false);
|
|
416
446
|
}
|
|
417
447
|
}
|
|
418
|
-
const _ObservablePatch = class extends BasePatch {
|
|
448
|
+
const _ObservablePatch = class _ObservablePatch extends BasePatch {
|
|
419
449
|
constructor(selection, operations, client) {
|
|
420
450
|
super(selection, operations);
|
|
421
451
|
__privateAdd$6(this, _client$5, void 0);
|
|
@@ -443,9 +473,9 @@ const _ObservablePatch = class extends BasePatch {
|
|
|
443
473
|
}, opts);
|
|
444
474
|
}
|
|
445
475
|
};
|
|
446
|
-
let ObservablePatch = _ObservablePatch;
|
|
447
476
|
_client$5 = new WeakMap();
|
|
448
|
-
|
|
477
|
+
let ObservablePatch = _ObservablePatch;
|
|
478
|
+
const _Patch = class _Patch extends BasePatch {
|
|
449
479
|
constructor(selection, operations, client) {
|
|
450
480
|
super(selection, operations);
|
|
451
481
|
__privateAdd$6(this, _client2$5, void 0);
|
|
@@ -473,8 +503,19 @@ const _Patch = class extends BasePatch {
|
|
|
473
503
|
}, opts);
|
|
474
504
|
}
|
|
475
505
|
};
|
|
476
|
-
let Patch = _Patch;
|
|
477
506
|
_client2$5 = new WeakMap();
|
|
507
|
+
let Patch = _Patch;
|
|
508
|
+
var __defProp$1 = Object.defineProperty;
|
|
509
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, {
|
|
510
|
+
enumerable: true,
|
|
511
|
+
configurable: true,
|
|
512
|
+
writable: true,
|
|
513
|
+
value
|
|
514
|
+
}) : obj[key] = value;
|
|
515
|
+
var __publicField$1 = (obj, key, value) => {
|
|
516
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
517
|
+
return value;
|
|
518
|
+
};
|
|
478
519
|
var __accessCheck$5 = (obj, member, msg) => {
|
|
479
520
|
if (!member.has(obj)) throw TypeError("Cannot " + msg);
|
|
480
521
|
};
|
|
@@ -499,6 +540,8 @@ class BaseTransaction {
|
|
|
499
540
|
constructor() {
|
|
500
541
|
let operations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
501
542
|
let transactionId = arguments.length > 1 ? arguments[1] : undefined;
|
|
543
|
+
__publicField$1(this, "operations");
|
|
544
|
+
__publicField$1(this, "trxId");
|
|
502
545
|
this.operations = operations;
|
|
503
546
|
this.trxId = transactionId;
|
|
504
547
|
}
|
|
@@ -587,7 +630,7 @@ class BaseTransaction {
|
|
|
587
630
|
return this;
|
|
588
631
|
}
|
|
589
632
|
}
|
|
590
|
-
const _Transaction = class extends BaseTransaction {
|
|
633
|
+
const _Transaction = class _Transaction extends BaseTransaction {
|
|
591
634
|
constructor(operations, client, transactionId) {
|
|
592
635
|
super(operations, transactionId);
|
|
593
636
|
__privateAdd$5(this, _client$4, void 0);
|
|
@@ -632,9 +675,9 @@ const _Transaction = class extends BaseTransaction {
|
|
|
632
675
|
});
|
|
633
676
|
}
|
|
634
677
|
};
|
|
635
|
-
let Transaction = _Transaction;
|
|
636
678
|
_client$4 = new WeakMap();
|
|
637
|
-
|
|
679
|
+
let Transaction = _Transaction;
|
|
680
|
+
const _ObservableTransaction = class _ObservableTransaction extends BaseTransaction {
|
|
638
681
|
constructor(operations, client, transactionId) {
|
|
639
682
|
super(operations, transactionId);
|
|
640
683
|
__privateAdd$5(this, _client2$4, void 0);
|
|
@@ -679,8 +722,8 @@ const _ObservableTransaction = class extends BaseTransaction {
|
|
|
679
722
|
});
|
|
680
723
|
}
|
|
681
724
|
};
|
|
682
|
-
let ObservableTransaction = _ObservableTransaction;
|
|
683
725
|
_client2$4 = new WeakMap();
|
|
726
|
+
let ObservableTransaction = _ObservableTransaction;
|
|
684
727
|
const excludeFalsey = (param, defValue) => {
|
|
685
728
|
const value = typeof param === "undefined" ? defValue : param;
|
|
686
729
|
return param === false ? void 0 : value;
|
|
@@ -842,7 +885,7 @@ function _requestObservable(client, httpRequest, options) {
|
|
|
842
885
|
...options.query
|
|
843
886
|
};
|
|
844
887
|
}
|
|
845
|
-
if (typeof config.perspective === "string" && config.perspective !== "
|
|
888
|
+
if (typeof config.perspective === "string" && config.perspective !== "raw") {
|
|
846
889
|
options.query = {
|
|
847
890
|
perspective: config.perspective,
|
|
848
891
|
...options.query
|
|
@@ -1493,6 +1536,17 @@ class UsersClient {
|
|
|
1493
1536
|
}
|
|
1494
1537
|
_client2 = new WeakMap();
|
|
1495
1538
|
_httpRequest2$1 = new WeakMap();
|
|
1539
|
+
var __defProp = Object.defineProperty;
|
|
1540
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
|
|
1541
|
+
enumerable: true,
|
|
1542
|
+
configurable: true,
|
|
1543
|
+
writable: true,
|
|
1544
|
+
value
|
|
1545
|
+
}) : obj[key] = value;
|
|
1546
|
+
var __publicField = (obj, key, value) => {
|
|
1547
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1548
|
+
return value;
|
|
1549
|
+
};
|
|
1496
1550
|
var __accessCheck = (obj, member, msg) => {
|
|
1497
1551
|
if (!member.has(obj)) throw TypeError("Cannot " + msg);
|
|
1498
1552
|
};
|
|
@@ -1510,9 +1564,13 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
1510
1564
|
return value;
|
|
1511
1565
|
};
|
|
1512
1566
|
var _clientConfig, _httpRequest, _clientConfig2, _httpRequest2;
|
|
1513
|
-
const _ObservableSanityClient = class {
|
|
1567
|
+
const _ObservableSanityClient = class _ObservableSanityClient {
|
|
1514
1568
|
constructor(httpRequest) {
|
|
1515
1569
|
let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
|
|
1570
|
+
__publicField(this, "assets");
|
|
1571
|
+
__publicField(this, "datasets");
|
|
1572
|
+
__publicField(this, "projects");
|
|
1573
|
+
__publicField(this, "users");
|
|
1516
1574
|
/**
|
|
1517
1575
|
* Private properties
|
|
1518
1576
|
*/
|
|
@@ -1521,7 +1579,7 @@ const _ObservableSanityClient = class {
|
|
|
1521
1579
|
/**
|
|
1522
1580
|
* Instance properties
|
|
1523
1581
|
*/
|
|
1524
|
-
this
|
|
1582
|
+
__publicField(this, "listen", _listen);
|
|
1525
1583
|
this.config(config);
|
|
1526
1584
|
__privateSet(this, _httpRequest, httpRequest);
|
|
1527
1585
|
this.assets = new ObservableAssetsClient(this, __privateGet(this, _httpRequest));
|
|
@@ -1643,12 +1701,20 @@ const _ObservableSanityClient = class {
|
|
|
1643
1701
|
return _getDataUrl(this, operation, path);
|
|
1644
1702
|
}
|
|
1645
1703
|
};
|
|
1646
|
-
let ObservableSanityClient = _ObservableSanityClient;
|
|
1647
1704
|
_clientConfig = new WeakMap();
|
|
1648
1705
|
_httpRequest = new WeakMap();
|
|
1649
|
-
|
|
1706
|
+
let ObservableSanityClient = _ObservableSanityClient;
|
|
1707
|
+
const _SanityClient = class _SanityClient {
|
|
1650
1708
|
constructor(httpRequest) {
|
|
1651
1709
|
let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
|
|
1710
|
+
__publicField(this, "assets");
|
|
1711
|
+
__publicField(this, "datasets");
|
|
1712
|
+
__publicField(this, "projects");
|
|
1713
|
+
__publicField(this, "users");
|
|
1714
|
+
/**
|
|
1715
|
+
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
1716
|
+
*/
|
|
1717
|
+
__publicField(this, "observable");
|
|
1652
1718
|
/**
|
|
1653
1719
|
* Private properties
|
|
1654
1720
|
*/
|
|
@@ -1657,7 +1723,7 @@ const _SanityClient = class {
|
|
|
1657
1723
|
/**
|
|
1658
1724
|
* Instance properties
|
|
1659
1725
|
*/
|
|
1660
|
-
this
|
|
1726
|
+
__publicField(this, "listen", _listen);
|
|
1661
1727
|
this.config(config);
|
|
1662
1728
|
__privateSet(this, _httpRequest2, httpRequest);
|
|
1663
1729
|
this.assets = new AssetsClient(this, __privateGet(this, _httpRequest2));
|
|
@@ -1797,9 +1863,9 @@ const _SanityClient = class {
|
|
|
1797
1863
|
return _getDataUrl(this, operation, path);
|
|
1798
1864
|
}
|
|
1799
1865
|
};
|
|
1800
|
-
let SanityClient = _SanityClient;
|
|
1801
1866
|
_clientConfig2 = new WeakMap();
|
|
1802
1867
|
_httpRequest2 = new WeakMap();
|
|
1868
|
+
let SanityClient = _SanityClient;
|
|
1803
1869
|
const httpRequest = defineHttpRequest(envMiddleware, {});
|
|
1804
1870
|
const requester = httpRequest.defaultRequester;
|
|
1805
1871
|
const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {
|