@sanity/client 6.1.3-perspective.0 → 6.2.0-fetch.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 CHANGED
@@ -65,9 +65,6 @@ 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)
71
68
  - [Fetching Content Source Maps](#fetching-content-source-maps)
72
69
  - [Listening to queries](#listening-to-queries)
73
70
  - [Fetch a single document](#fetch-a-single-document)
@@ -408,162 +405,6 @@ client.fetch(query, params).then((bikes) => {
408
405
 
409
406
  Perform a query using the given parameters (if any).
410
407
 
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
-
567
408
  ### Fetching Content Source Maps
568
409
 
569
410
  Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.
@@ -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.statusCode = 400;
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.statusCode = 500;
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
- const _Patch = class extends BasePatch {
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
- const _ObservableTransaction = class extends BaseTransaction {
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;
@@ -706,10 +749,22 @@ const getQuerySizeLimit = 11264;
706
749
  function _fetch(client, httpRequest, query, params) {
707
750
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
708
751
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
752
+ const {
753
+ cache,
754
+ next,
755
+ ...opts
756
+ } = options;
757
+ const reqOpts = typeof cache !== "undefined" || typeof next !== "undefined" ? {
758
+ ...opts,
759
+ fetch: {
760
+ cache,
761
+ next
762
+ }
763
+ } : opts;
709
764
  return _dataRequest(client, httpRequest, "query", {
710
765
  query,
711
766
  params
712
- }, options).pipe(operators.map(mapResponse));
767
+ }, reqOpts).pipe(operators.map(mapResponse));
713
768
  }
714
769
  function _getDocument(client, httpRequest, id) {
715
770
  let opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
@@ -791,7 +846,8 @@ function _dataRequest(client, httpRequest, endpoint, body) {
791
846
  token,
792
847
  tag,
793
848
  canUseCdn: isQuery,
794
- signal: options.signal
849
+ signal: options.signal,
850
+ fetch: options.fetch
795
851
  };
796
852
  return _requestObservable(client, httpRequest, reqOptions).pipe(operators.filter(isResponse), operators.map(getBody), operators.map(res => {
797
853
  if (!isMutation) {
@@ -1493,6 +1549,17 @@ class UsersClient {
1493
1549
  }
1494
1550
  _client2 = new WeakMap();
1495
1551
  _httpRequest2$1 = new WeakMap();
1552
+ var __defProp = Object.defineProperty;
1553
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
1554
+ enumerable: true,
1555
+ configurable: true,
1556
+ writable: true,
1557
+ value
1558
+ }) : obj[key] = value;
1559
+ var __publicField = (obj, key, value) => {
1560
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1561
+ return value;
1562
+ };
1496
1563
  var __accessCheck = (obj, member, msg) => {
1497
1564
  if (!member.has(obj)) throw TypeError("Cannot " + msg);
1498
1565
  };
@@ -1510,9 +1577,13 @@ var __privateSet = (obj, member, value, setter) => {
1510
1577
  return value;
1511
1578
  };
1512
1579
  var _clientConfig, _httpRequest, _clientConfig2, _httpRequest2;
1513
- const _ObservableSanityClient = class {
1580
+ const _ObservableSanityClient = class _ObservableSanityClient {
1514
1581
  constructor(httpRequest) {
1515
1582
  let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
1583
+ __publicField(this, "assets");
1584
+ __publicField(this, "datasets");
1585
+ __publicField(this, "projects");
1586
+ __publicField(this, "users");
1516
1587
  /**
1517
1588
  * Private properties
1518
1589
  */
@@ -1521,7 +1592,7 @@ const _ObservableSanityClient = class {
1521
1592
  /**
1522
1593
  * Instance properties
1523
1594
  */
1524
- this.listen = _listen;
1595
+ __publicField(this, "listen", _listen);
1525
1596
  this.config(config);
1526
1597
  __privateSet(this, _httpRequest, httpRequest);
1527
1598
  this.assets = new ObservableAssetsClient(this, __privateGet(this, _httpRequest));
@@ -1643,12 +1714,20 @@ const _ObservableSanityClient = class {
1643
1714
  return _getDataUrl(this, operation, path);
1644
1715
  }
1645
1716
  };
1646
- let ObservableSanityClient = _ObservableSanityClient;
1647
1717
  _clientConfig = new WeakMap();
1648
1718
  _httpRequest = new WeakMap();
1649
- const _SanityClient = class {
1719
+ let ObservableSanityClient = _ObservableSanityClient;
1720
+ const _SanityClient = class _SanityClient {
1650
1721
  constructor(httpRequest) {
1651
1722
  let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultConfig;
1723
+ __publicField(this, "assets");
1724
+ __publicField(this, "datasets");
1725
+ __publicField(this, "projects");
1726
+ __publicField(this, "users");
1727
+ /**
1728
+ * Observable version of the Sanity client, with the same configuration as the promise-based one
1729
+ */
1730
+ __publicField(this, "observable");
1652
1731
  /**
1653
1732
  * Private properties
1654
1733
  */
@@ -1657,7 +1736,7 @@ const _SanityClient = class {
1657
1736
  /**
1658
1737
  * Instance properties
1659
1738
  */
1660
- this.listen = _listen;
1739
+ __publicField(this, "listen", _listen);
1661
1740
  this.config(config);
1662
1741
  __privateSet(this, _httpRequest2, httpRequest);
1663
1742
  this.assets = new AssetsClient(this, __privateGet(this, _httpRequest2));
@@ -1797,9 +1876,9 @@ const _SanityClient = class {
1797
1876
  return _getDataUrl(this, operation, path);
1798
1877
  }
1799
1878
  };
1800
- let SanityClient = _SanityClient;
1801
1879
  _clientConfig2 = new WeakMap();
1802
1880
  _httpRequest2 = new WeakMap();
1881
+ let SanityClient = _SanityClient;
1803
1882
  const httpRequest = defineHttpRequest(envMiddleware, {});
1804
1883
  const requester = httpRequest.defaultRequester;
1805
1884
  const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {