@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.
@@ -4,6 +4,7 @@ import { Observable, lastValueFrom } from 'rxjs';
4
4
  import { map, filter } from 'rxjs/operators';
5
5
  import polyfilledEventSource from '@sanity/eventsource';
6
6
  var envMiddleware = [];
7
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
7
8
  class ClientError extends Error {
8
9
  constructor(res) {
9
10
  const props = extractErrorProps(res);
@@ -33,6 +34,20 @@ function extractErrorProps(res) {
33
34
  props.message = "".concat(body.error, " - ").concat(body.message);
34
35
  return props;
35
36
  }
37
+ if (isMutationError(body)) {
38
+ const allItems = body.error.items || [];
39
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
40
+ var _a;
41
+ return (_a = item.error) == null ? void 0 : _a.description;
42
+ }).filter(Boolean);
43
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
44
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
45
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
46
+ }
47
+ props.message = "".concat(body.error.description).concat(itemsStr);
48
+ props.details = body.error;
49
+ return props;
50
+ }
36
51
  if (body.error && body.error.description) {
37
52
  props.message = body.error.description;
38
53
  props.details = body.error;
@@ -41,6 +56,12 @@ function extractErrorProps(res) {
41
56
  props.message = body.error || body.message || httpErrorMessage(res);
42
57
  return props;
43
58
  }
59
+ function isMutationError(body) {
60
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
61
+ }
62
+ function isPlainObject(obj) {
63
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
64
+ }
44
65
  function httpErrorMessage(res) {
45
66
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
46
67
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -143,7 +164,7 @@ const validateObject = (op, val) => {
143
164
  }
144
165
  };
145
166
  const validateDocumentId = (op, id) => {
146
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
167
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
147
168
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
148
169
  }
149
170
  };
@@ -1113,10 +1134,10 @@ function _listen(query, params) {
1113
1134
  observer.complete();
1114
1135
  }
1115
1136
  function unsubscribe() {
1116
- es.removeEventListener("error", onError, false);
1117
- es.removeEventListener("channelError", onChannelError, false);
1118
- es.removeEventListener("disconnect", onDisconnect, false);
1119
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1137
+ es.removeEventListener("error", onError);
1138
+ es.removeEventListener("channelError", onChannelError);
1139
+ es.removeEventListener("disconnect", onDisconnect);
1140
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1120
1141
  es.close();
1121
1142
  }
1122
1143
  function emitReconnect() {
@@ -1128,10 +1149,10 @@ function _listen(query, params) {
1128
1149
  }
1129
1150
  function getEventSource() {
1130
1151
  const evs = new EventSource(uri, esOptions);
1131
- evs.addEventListener("error", onError, false);
1132
- evs.addEventListener("channelError", onChannelError, false);
1133
- evs.addEventListener("disconnect", onDisconnect, false);
1134
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1152
+ evs.addEventListener("error", onError);
1153
+ evs.addEventListener("channelError", onChannelError);
1154
+ evs.addEventListener("disconnect", onDisconnect);
1155
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1135
1156
  return evs;
1136
1157
  }
1137
1158
  function open() {