@sanity/client 0.0.0-dev.2 → 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
  };
@@ -794,16 +808,10 @@ function _requestObservable(client, httpRequest, options) {
794
808
  ...options.query
795
809
  };
796
810
  }
797
- if (config.unstable_overlayDrafts) {
798
- if (config.apiVersion !== "X") {
799
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
800
- }
811
+ if (config.encodeStegaSourceMap) {
801
812
  options.query = {
802
- ...options.query,
803
- params: {
804
- ...(options.query.params || {}),
805
- draftMode: true
806
- }
813
+ encodeHackySourceMap: true,
814
+ ...options.query
807
815
  };
808
816
  }
809
817
  const reqOptions = requestOptions(config, Object.assign({}, options, {
@@ -950,7 +958,7 @@ function _upload(client, httpRequest, assetType, body) {
950
958
  });
951
959
  }
952
960
  function optionsFromFile(opts, file) {
953
- if (typeof window === "undefined" || !(file instanceof window.File)) {
961
+ if (typeof File === "undefined" || !(file instanceof File)) {
954
962
  return opts;
955
963
  }
956
964
  return Object.assign({
@@ -1063,7 +1071,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1063
1071
  return selection;
1064
1072
  }, {});
1065
1073
  const MAX_URL_LENGTH = 16e3 - 1200;
1066
- const EventSource = polyfilledEventSource__default.default;
1067
1074
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1068
1075
  const defaultOptions = {
1069
1076
  includeResult: true
@@ -1106,7 +1113,13 @@ function _listen(query, params) {
1106
1113
  };
1107
1114
  }
1108
1115
  return new rxjs.Observable(observer => {
1109
- let es = getEventSource();
1116
+ let es;
1117
+ getEventSource().then(eventSource => {
1118
+ es = eventSource;
1119
+ }).catch(reason => {
1120
+ observer.error(reason);
1121
+ stop();
1122
+ });
1110
1123
  let reconnectTimer;
1111
1124
  let stopped = false;
1112
1125
  function onError() {
@@ -1117,7 +1130,7 @@ function _listen(query, params) {
1117
1130
  if (stopped) {
1118
1131
  return;
1119
1132
  }
1120
- if (es.readyState === EventSource.CLOSED) {
1133
+ if (es.readyState === es.CLOSED) {
1121
1134
  unsubscribe();
1122
1135
  clearTimeout(reconnectTimer);
1123
1136
  reconnectTimer = setTimeout(open, 100);
@@ -1136,10 +1149,11 @@ function _listen(query, params) {
1136
1149
  observer.complete();
1137
1150
  }
1138
1151
  function unsubscribe() {
1139
- es.removeEventListener("error", onError, false);
1140
- es.removeEventListener("channelError", onChannelError, false);
1141
- es.removeEventListener("disconnect", onDisconnect, false);
1142
- 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));
1143
1157
  es.close();
1144
1158
  }
1145
1159
  function emitReconnect() {
@@ -1149,16 +1163,24 @@ function _listen(query, params) {
1149
1163
  });
1150
1164
  }
1151
1165
  }
1152
- function getEventSource() {
1166
+ async function getEventSource() {
1167
+ const {
1168
+ default: EventSource
1169
+ } = await import('@sanity/eventsource');
1153
1170
  const evs = new EventSource(uri, esOptions);
1154
- evs.addEventListener("error", onError, false);
1155
- evs.addEventListener("channelError", onChannelError, false);
1156
- evs.addEventListener("disconnect", onDisconnect, false);
1157
- 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));
1158
1175
  return evs;
1159
1176
  }
1160
1177
  function open() {
1161
- es = getEventSource();
1178
+ getEventSource().then(eventSource => {
1179
+ es = eventSource;
1180
+ }).catch(reason => {
1181
+ observer.error(reason);
1182
+ stop();
1183
+ });
1162
1184
  }
1163
1185
  function stop() {
1164
1186
  stopped = true;
@@ -1747,6 +1769,18 @@ function deprecatedCreateClient(config) {
1747
1769
  printNoDefaultExport();
1748
1770
  return new SanityClient(httpRequest, config);
1749
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
+ });
1750
1784
  exports.BasePatch = BasePatch;
1751
1785
  exports.BaseTransaction = BaseTransaction;
1752
1786
  exports.ClientError = ClientError;