@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.
package/dist/index.cjs CHANGED
@@ -15,7 +15,7 @@ function _interopDefaultCompat(e) {
15
15
  }
16
16
  var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
17
17
  var name = "@sanity/client";
18
- var version = "5.2.2";
18
+ var version = "5.3.1";
19
19
  const middleware = [middleware$1.debug({
20
20
  verbose: true,
21
21
  namespace: "sanity:client"
@@ -24,6 +24,7 @@ const middleware = [middleware$1.debug({
24
24
  }), middleware$1.retry({
25
25
  maxRetries: 3
26
26
  })];
27
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
27
28
  class ClientError extends Error {
28
29
  constructor(res) {
29
30
  const props = extractErrorProps(res);
@@ -53,6 +54,20 @@ function extractErrorProps(res) {
53
54
  props.message = "".concat(body.error, " - ").concat(body.message);
54
55
  return props;
55
56
  }
57
+ if (isMutationError(body)) {
58
+ const allItems = body.error.items || [];
59
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
60
+ var _a;
61
+ return (_a = item.error) == null ? void 0 : _a.description;
62
+ }).filter(Boolean);
63
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
64
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
65
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
66
+ }
67
+ props.message = "".concat(body.error.description).concat(itemsStr);
68
+ props.details = body.error;
69
+ return props;
70
+ }
56
71
  if (body.error && body.error.description) {
57
72
  props.message = body.error.description;
58
73
  props.details = body.error;
@@ -61,6 +76,12 @@ function extractErrorProps(res) {
61
76
  props.message = body.error || body.message || httpErrorMessage(res);
62
77
  return props;
63
78
  }
79
+ function isMutationError(body) {
80
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
81
+ }
82
+ function isPlainObject(obj) {
83
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
84
+ }
64
85
  function httpErrorMessage(res) {
65
86
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
66
87
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -163,7 +184,7 @@ const validateObject = (op, val) => {
163
184
  }
164
185
  };
165
186
  const validateDocumentId = (op, id) => {
166
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
187
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
167
188
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
168
189
  }
169
190
  };
@@ -1133,10 +1154,10 @@ function _listen(query, params) {
1133
1154
  observer.complete();
1134
1155
  }
1135
1156
  function unsubscribe() {
1136
- es.removeEventListener("error", onError, false);
1137
- es.removeEventListener("channelError", onChannelError, false);
1138
- es.removeEventListener("disconnect", onDisconnect, false);
1139
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1157
+ es.removeEventListener("error", onError);
1158
+ es.removeEventListener("channelError", onChannelError);
1159
+ es.removeEventListener("disconnect", onDisconnect);
1160
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1140
1161
  es.close();
1141
1162
  }
1142
1163
  function emitReconnect() {
@@ -1148,10 +1169,10 @@ function _listen(query, params) {
1148
1169
  }
1149
1170
  function getEventSource() {
1150
1171
  const evs = new EventSource(uri, esOptions);
1151
- evs.addEventListener("error", onError, false);
1152
- evs.addEventListener("channelError", onChannelError, false);
1153
- evs.addEventListener("disconnect", onDisconnect, false);
1154
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1172
+ evs.addEventListener("error", onError);
1173
+ evs.addEventListener("channelError", onChannelError);
1174
+ evs.addEventListener("disconnect", onDisconnect);
1175
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1155
1176
  return evs;
1156
1177
  }
1157
1178
  function open() {