@sanity/client 0.0.0-dev.3 → 0.0.0-dev.4

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
@@ -738,7 +738,7 @@ An important note on this approach is that you cannot call `commit()` on transac
738
738
  Assets can be uploaded using the `client.assets.upload(...)` method.
739
739
 
740
740
  ```
741
- client.assets.upload(type: 'file' | image', body: File | Blob | Buffer | NodeStream, options = {}): Promise<AssetDocument>
741
+ client.assets.upload(type: 'file' | image', body: File | Blob | Buffer | NodeJS.ReadableStream, options = {}): Promise<AssetDocument>
742
742
  ```
743
743
 
744
744
  👉 Read more about [assets in Sanity](https://sanity.io/docs/assets)
@@ -7,14 +7,8 @@ var getIt = require('get-it');
7
7
  var middleware = require('get-it/middleware');
8
8
  var rxjs = require('rxjs');
9
9
  var operators = require('rxjs/operators');
10
- var polyfilledEventSource = require('@sanity/eventsource');
11
- function _interopDefaultCompat(e) {
12
- return e && typeof e === 'object' && 'default' in e ? e : {
13
- default: e
14
- };
15
- }
16
- var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
17
10
  var envMiddleware = [];
11
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
18
12
  class ClientError extends Error {
19
13
  constructor(res) {
20
14
  const props = extractErrorProps(res);
@@ -44,6 +38,20 @@ function extractErrorProps(res) {
44
38
  props.message = "".concat(body.error, " - ").concat(body.message);
45
39
  return props;
46
40
  }
41
+ if (isMutationError(body)) {
42
+ const allItems = body.error.items || [];
43
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
44
+ var _a;
45
+ return (_a = item.error) == null ? void 0 : _a.description;
46
+ }).filter(Boolean);
47
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
48
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
49
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
50
+ }
51
+ props.message = "".concat(body.error.description).concat(itemsStr);
52
+ props.details = body.error;
53
+ return props;
54
+ }
47
55
  if (body.error && body.error.description) {
48
56
  props.message = body.error.description;
49
57
  props.details = body.error;
@@ -52,6 +60,12 @@ function extractErrorProps(res) {
52
60
  props.message = body.error || body.message || httpErrorMessage(res);
53
61
  return props;
54
62
  }
63
+ function isMutationError(body) {
64
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
65
+ }
66
+ function isPlainObject(obj) {
67
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
68
+ }
55
69
  function httpErrorMessage(res) {
56
70
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
57
71
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -154,7 +168,7 @@ const validateObject = (op, val) => {
154
168
  }
155
169
  };
156
170
  const validateDocumentId = (op, id) => {
157
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
171
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
158
172
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
159
173
  }
160
174
  };
@@ -671,23 +685,9 @@ const indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
671
685
  return indexed;
672
686
  }, /* @__PURE__ */Object.create(null));
673
687
  const getQuerySizeLimit = 11264;
674
- function _fetch(client, httpRequest, query, _params) {
688
+ function _fetch(client, httpRequest, query, params) {
675
689
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
676
690
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
677
- let params = _params;
678
- const {
679
- unstable_overlayDrafts,
680
- apiVersion
681
- } = client.config();
682
- if (unstable_overlayDrafts) {
683
- if (apiVersion !== "X") {
684
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
685
- }
686
- params = {
687
- ...params,
688
- draftMode: true
689
- };
690
- }
691
691
  return _dataRequest(client, httpRequest, "query", {
692
692
  query,
693
693
  params
@@ -808,6 +808,12 @@ function _requestObservable(client, httpRequest, options) {
808
808
  ...options.query
809
809
  };
810
810
  }
811
+ if (config.encodeStegaSourceMap) {
812
+ options.query = {
813
+ encodeHackySourceMap: true,
814
+ ...options.query
815
+ };
816
+ }
811
817
  const reqOptions = requestOptions(config, Object.assign({}, options, {
812
818
  url: _getUrl(client, uri, useCdn)
813
819
  }));
@@ -952,7 +958,7 @@ function _upload(client, httpRequest, assetType, body) {
952
958
  });
953
959
  }
954
960
  function optionsFromFile(opts, file) {
955
- if (typeof window === "undefined" || !(file instanceof window.File)) {
961
+ if (typeof File === "undefined" || !(file instanceof File)) {
956
962
  return opts;
957
963
  }
958
964
  return Object.assign({
@@ -1065,7 +1071,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1065
1071
  return selection;
1066
1072
  }, {});
1067
1073
  const MAX_URL_LENGTH = 16e3 - 1200;
1068
- const EventSource = polyfilledEventSource__default.default;
1069
1074
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1070
1075
  const defaultOptions = {
1071
1076
  includeResult: true
@@ -1108,7 +1113,13 @@ function _listen(query, params) {
1108
1113
  };
1109
1114
  }
1110
1115
  return new rxjs.Observable(observer => {
1111
- let es = getEventSource();
1116
+ let es;
1117
+ getEventSource().then(eventSource => {
1118
+ es = eventSource;
1119
+ }).catch(reason => {
1120
+ observer.error(reason);
1121
+ stop();
1122
+ });
1112
1123
  let reconnectTimer;
1113
1124
  let stopped = false;
1114
1125
  function onError() {
@@ -1119,7 +1130,7 @@ function _listen(query, params) {
1119
1130
  if (stopped) {
1120
1131
  return;
1121
1132
  }
1122
- if (es.readyState === EventSource.CLOSED) {
1133
+ if (es.readyState === es.CLOSED) {
1123
1134
  unsubscribe();
1124
1135
  clearTimeout(reconnectTimer);
1125
1136
  reconnectTimer = setTimeout(open, 100);
@@ -1138,10 +1149,11 @@ function _listen(query, params) {
1138
1149
  observer.complete();
1139
1150
  }
1140
1151
  function unsubscribe() {
1141
- es.removeEventListener("error", onError, false);
1142
- es.removeEventListener("channelError", onChannelError, false);
1143
- es.removeEventListener("disconnect", onDisconnect, false);
1144
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1152
+ if (!es) return;
1153
+ es.removeEventListener("error", onError);
1154
+ es.removeEventListener("channelError", onChannelError);
1155
+ es.removeEventListener("disconnect", onDisconnect);
1156
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1145
1157
  es.close();
1146
1158
  }
1147
1159
  function emitReconnect() {
@@ -1151,16 +1163,24 @@ function _listen(query, params) {
1151
1163
  });
1152
1164
  }
1153
1165
  }
1154
- function getEventSource() {
1166
+ async function getEventSource() {
1167
+ const {
1168
+ default: EventSource
1169
+ } = await import('@sanity/eventsource');
1155
1170
  const evs = new EventSource(uri, esOptions);
1156
- evs.addEventListener("error", onError, false);
1157
- evs.addEventListener("channelError", onChannelError, false);
1158
- evs.addEventListener("disconnect", onDisconnect, false);
1159
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1171
+ evs.addEventListener("error", onError);
1172
+ evs.addEventListener("channelError", onChannelError);
1173
+ evs.addEventListener("disconnect", onDisconnect);
1174
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1160
1175
  return evs;
1161
1176
  }
1162
1177
  function open() {
1163
- es = getEventSource();
1178
+ getEventSource().then(eventSource => {
1179
+ es = eventSource;
1180
+ }).catch(reason => {
1181
+ observer.error(reason);
1182
+ stop();
1183
+ });
1164
1184
  }
1165
1185
  function stop() {
1166
1186
  stopped = true;
@@ -1749,6 +1769,18 @@ function deprecatedCreateClient(config) {
1749
1769
  printNoDefaultExport();
1750
1770
  return new SanityClient(httpRequest, config);
1751
1771
  }
1772
+ Object.defineProperty(exports, 'unstable__adapter', {
1773
+ enumerable: true,
1774
+ get: function () {
1775
+ return getIt.adapter;
1776
+ }
1777
+ });
1778
+ Object.defineProperty(exports, 'unstable__environment', {
1779
+ enumerable: true,
1780
+ get: function () {
1781
+ return getIt.environment;
1782
+ }
1783
+ });
1752
1784
  exports.BasePatch = BasePatch;
1753
1785
  exports.BaseTransaction = BaseTransaction;
1754
1786
  exports.ClientError = ClientError;