@sanity/client 5.2.2 → 5.3.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.
@@ -15,6 +15,7 @@ function _interopDefaultCompat(e) {
15
15
  }
16
16
  var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
17
17
  var envMiddleware = [];
18
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
18
19
  class ClientError extends Error {
19
20
  constructor(res) {
20
21
  const props = extractErrorProps(res);
@@ -44,6 +45,20 @@ function extractErrorProps(res) {
44
45
  props.message = "".concat(body.error, " - ").concat(body.message);
45
46
  return props;
46
47
  }
48
+ if (isMutationError(body)) {
49
+ const allItems = body.error.items || [];
50
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
51
+ var _a;
52
+ return (_a = item.error) == null ? void 0 : _a.description;
53
+ }).filter(Boolean);
54
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
55
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
56
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
57
+ }
58
+ props.message = "".concat(body.error.description).concat(itemsStr);
59
+ props.details = body.error;
60
+ return props;
61
+ }
47
62
  if (body.error && body.error.description) {
48
63
  props.message = body.error.description;
49
64
  props.details = body.error;
@@ -52,6 +67,12 @@ function extractErrorProps(res) {
52
67
  props.message = body.error || body.message || httpErrorMessage(res);
53
68
  return props;
54
69
  }
70
+ function isMutationError(body) {
71
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
72
+ }
73
+ function isPlainObject(obj) {
74
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
75
+ }
55
76
  function httpErrorMessage(res) {
56
77
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
57
78
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -154,7 +175,7 @@ const validateObject = (op, val) => {
154
175
  }
155
176
  };
156
177
  const validateDocumentId = (op, id) => {
157
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
178
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
158
179
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
159
180
  }
160
181
  };
@@ -1124,10 +1145,10 @@ function _listen(query, params) {
1124
1145
  observer.complete();
1125
1146
  }
1126
1147
  function unsubscribe() {
1127
- es.removeEventListener("error", onError, false);
1128
- es.removeEventListener("channelError", onChannelError, false);
1129
- es.removeEventListener("disconnect", onDisconnect, false);
1130
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1148
+ es.removeEventListener("error", onError);
1149
+ es.removeEventListener("channelError", onChannelError);
1150
+ es.removeEventListener("disconnect", onDisconnect);
1151
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1131
1152
  es.close();
1132
1153
  }
1133
1154
  function emitReconnect() {
@@ -1139,10 +1160,10 @@ function _listen(query, params) {
1139
1160
  }
1140
1161
  function getEventSource() {
1141
1162
  const evs = new EventSource(uri, esOptions);
1142
- evs.addEventListener("error", onError, false);
1143
- evs.addEventListener("channelError", onChannelError, false);
1144
- evs.addEventListener("disconnect", onDisconnect, false);
1145
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1163
+ evs.addEventListener("error", onError);
1164
+ evs.addEventListener("channelError", onChannelError);
1165
+ evs.addEventListener("disconnect", onDisconnect);
1166
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1146
1167
  return evs;
1147
1168
  }
1148
1169
  function open() {