@sanity/client 5.2.1 → 5.3.0

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.1";
18
+ var version = "5.3.0";
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
  };