@sanity/client 5.2.2 → 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.
@@ -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
  };